blob: 864b623c5c70f8d51cc0443e12b959438ecda401 [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
27static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010028static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010030static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031
32#ifdef FEAT_FLOAT
33static void f_abs(typval_T *argvars, typval_T *rettv);
34static void f_acos(typval_T *argvars, typval_T *rettv);
35#endif
36static void f_add(typval_T *argvars, typval_T *rettv);
37static void f_and(typval_T *argvars, typval_T *rettv);
38static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020039static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_argc(typval_T *argvars, typval_T *rettv);
41static void f_argidx(typval_T *argvars, typval_T *rettv);
42static void f_arglistid(typval_T *argvars, typval_T *rettv);
43static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044#ifdef FEAT_FLOAT
45static void f_asin(typval_T *argvars, typval_T *rettv);
46static void f_atan(typval_T *argvars, typval_T *rettv);
47static void f_atan2(typval_T *argvars, typval_T *rettv);
48#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010049#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020050static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010051static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010052# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010053static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010054# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010055#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_browse(typval_T *argvars, typval_T *rettv);
57static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020058static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_bufexists(typval_T *argvars, typval_T *rettv);
60static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020061static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_bufloaded(typval_T *argvars, typval_T *rettv);
63static void f_bufname(typval_T *argvars, typval_T *rettv);
64static void f_bufnr(typval_T *argvars, typval_T *rettv);
65static void f_bufwinid(typval_T *argvars, typval_T *rettv);
66static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
67static void f_byte2line(typval_T *argvars, typval_T *rettv);
68static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
69static void f_byteidx(typval_T *argvars, typval_T *rettv);
70static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
71static void f_call(typval_T *argvars, typval_T *rettv);
72#ifdef FEAT_FLOAT
73static void f_ceil(typval_T *argvars, typval_T *rettv);
74#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_changenr(typval_T *argvars, typval_T *rettv);
76static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020077static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_cindent(typval_T *argvars, typval_T *rettv);
79static void f_clearmatches(typval_T *argvars, typval_T *rettv);
80static void f_col(typval_T *argvars, typval_T *rettv);
81#if defined(FEAT_INS_EXPAND)
82static void f_complete(typval_T *argvars, typval_T *rettv);
83static void f_complete_add(typval_T *argvars, typval_T *rettv);
84static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010085static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020086#endif
87static void f_confirm(typval_T *argvars, typval_T *rettv);
88static void f_copy(typval_T *argvars, typval_T *rettv);
89#ifdef FEAT_FLOAT
90static void f_cos(typval_T *argvars, typval_T *rettv);
91static void f_cosh(typval_T *argvars, typval_T *rettv);
92#endif
93static void f_count(typval_T *argvars, typval_T *rettv);
94static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
95static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010096#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020097static void f_debugbreak(typval_T *argvars, typval_T *rettv);
98#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_deepcopy(typval_T *argvars, typval_T *rettv);
100static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200101static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_did_filetype(typval_T *argvars, typval_T *rettv);
103static void f_diff_filler(typval_T *argvars, typval_T *rettv);
104static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
105static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200106static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_escape(typval_T *argvars, typval_T *rettv);
108static void f_eval(typval_T *argvars, typval_T *rettv);
109static void f_eventhandler(typval_T *argvars, typval_T *rettv);
110static void f_executable(typval_T *argvars, typval_T *rettv);
111static void f_execute(typval_T *argvars, typval_T *rettv);
112static void f_exepath(typval_T *argvars, typval_T *rettv);
113static void f_exists(typval_T *argvars, typval_T *rettv);
114#ifdef FEAT_FLOAT
115static void f_exp(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200118static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_extend(typval_T *argvars, typval_T *rettv);
120static void f_feedkeys(typval_T *argvars, typval_T *rettv);
121static void f_filereadable(typval_T *argvars, typval_T *rettv);
122static void f_filewritable(typval_T *argvars, typval_T *rettv);
123static void f_filter(typval_T *argvars, typval_T *rettv);
124static void f_finddir(typval_T *argvars, typval_T *rettv);
125static void f_findfile(typval_T *argvars, typval_T *rettv);
126#ifdef FEAT_FLOAT
127static void f_float2nr(typval_T *argvars, typval_T *rettv);
128static void f_floor(typval_T *argvars, typval_T *rettv);
129static void f_fmod(typval_T *argvars, typval_T *rettv);
130#endif
131static void f_fnameescape(typval_T *argvars, typval_T *rettv);
132static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
133static void f_foldclosed(typval_T *argvars, typval_T *rettv);
134static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
135static void f_foldlevel(typval_T *argvars, typval_T *rettv);
136static void f_foldtext(typval_T *argvars, typval_T *rettv);
137static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
138static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200139static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200140static void f_function(typval_T *argvars, typval_T *rettv);
141static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
142static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200143static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200144static void f_getbufline(typval_T *argvars, typval_T *rettv);
145static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100146static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147static void f_getchar(typval_T *argvars, typval_T *rettv);
148static void f_getcharmod(typval_T *argvars, typval_T *rettv);
149static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
150static void f_getcmdline(typval_T *argvars, typval_T *rettv);
151#if defined(FEAT_CMDL_COMPL)
152static void f_getcompletion(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
155static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
156static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
157static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200158static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_getfontname(typval_T *argvars, typval_T *rettv);
160static void f_getfperm(typval_T *argvars, typval_T *rettv);
161static void f_getfsize(typval_T *argvars, typval_T *rettv);
162static void f_getftime(typval_T *argvars, typval_T *rettv);
163static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100164static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200166static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getmatches(typval_T *argvars, typval_T *rettv);
168static void f_getpid(typval_T *argvars, typval_T *rettv);
169static void f_getcurpos(typval_T *argvars, typval_T *rettv);
170static void f_getpos(typval_T *argvars, typval_T *rettv);
171static void f_getqflist(typval_T *argvars, typval_T *rettv);
172static void f_getreg(typval_T *argvars, typval_T *rettv);
173static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200174static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_gettabvar(typval_T *argvars, typval_T *rettv);
176static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100177static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200178static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100179static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_getwinposx(typval_T *argvars, typval_T *rettv);
181static void f_getwinposy(typval_T *argvars, typval_T *rettv);
182static void f_getwinvar(typval_T *argvars, typval_T *rettv);
183static void f_glob(typval_T *argvars, typval_T *rettv);
184static void f_globpath(typval_T *argvars, typval_T *rettv);
185static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
186static void f_has(typval_T *argvars, typval_T *rettv);
187static void f_has_key(typval_T *argvars, typval_T *rettv);
188static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
189static void f_hasmapto(typval_T *argvars, typval_T *rettv);
190static void f_histadd(typval_T *argvars, typval_T *rettv);
191static void f_histdel(typval_T *argvars, typval_T *rettv);
192static void f_histget(typval_T *argvars, typval_T *rettv);
193static void f_histnr(typval_T *argvars, typval_T *rettv);
194static void f_hlID(typval_T *argvars, typval_T *rettv);
195static void f_hlexists(typval_T *argvars, typval_T *rettv);
196static void f_hostname(typval_T *argvars, typval_T *rettv);
197static void f_iconv(typval_T *argvars, typval_T *rettv);
198static void f_indent(typval_T *argvars, typval_T *rettv);
199static void f_index(typval_T *argvars, typval_T *rettv);
200static void f_input(typval_T *argvars, typval_T *rettv);
201static void f_inputdialog(typval_T *argvars, typval_T *rettv);
202static void f_inputlist(typval_T *argvars, typval_T *rettv);
203static void f_inputrestore(typval_T *argvars, typval_T *rettv);
204static void f_inputsave(typval_T *argvars, typval_T *rettv);
205static void f_inputsecret(typval_T *argvars, typval_T *rettv);
206static void f_insert(typval_T *argvars, typval_T *rettv);
207static void f_invert(typval_T *argvars, typval_T *rettv);
208static void f_isdirectory(typval_T *argvars, typval_T *rettv);
209static void f_islocked(typval_T *argvars, typval_T *rettv);
210#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200211static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200212static void f_isnan(typval_T *argvars, typval_T *rettv);
213#endif
214static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_join(typval_T *argvars, typval_T *rettv);
216static void f_js_decode(typval_T *argvars, typval_T *rettv);
217static void f_js_encode(typval_T *argvars, typval_T *rettv);
218static void f_json_decode(typval_T *argvars, typval_T *rettv);
219static void f_json_encode(typval_T *argvars, typval_T *rettv);
220static void f_keys(typval_T *argvars, typval_T *rettv);
221static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
222static void f_len(typval_T *argvars, typval_T *rettv);
223static void f_libcall(typval_T *argvars, typval_T *rettv);
224static void f_libcallnr(typval_T *argvars, typval_T *rettv);
225static void f_line(typval_T *argvars, typval_T *rettv);
226static void f_line2byte(typval_T *argvars, typval_T *rettv);
227static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200228static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_localtime(typval_T *argvars, typval_T *rettv);
230#ifdef FEAT_FLOAT
231static void f_log(typval_T *argvars, typval_T *rettv);
232static void f_log10(typval_T *argvars, typval_T *rettv);
233#endif
234#ifdef FEAT_LUA
235static void f_luaeval(typval_T *argvars, typval_T *rettv);
236#endif
237static void f_map(typval_T *argvars, typval_T *rettv);
238static void f_maparg(typval_T *argvars, typval_T *rettv);
239static void f_mapcheck(typval_T *argvars, typval_T *rettv);
240static void f_match(typval_T *argvars, typval_T *rettv);
241static void f_matchadd(typval_T *argvars, typval_T *rettv);
242static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
243static void f_matcharg(typval_T *argvars, typval_T *rettv);
244static void f_matchdelete(typval_T *argvars, typval_T *rettv);
245static void f_matchend(typval_T *argvars, typval_T *rettv);
246static void f_matchlist(typval_T *argvars, typval_T *rettv);
247static void f_matchstr(typval_T *argvars, typval_T *rettv);
248static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
249static void f_max(typval_T *argvars, typval_T *rettv);
250static void f_min(typval_T *argvars, typval_T *rettv);
251#ifdef vim_mkdir
252static void f_mkdir(typval_T *argvars, typval_T *rettv);
253#endif
254static void f_mode(typval_T *argvars, typval_T *rettv);
255#ifdef FEAT_MZSCHEME
256static void f_mzeval(typval_T *argvars, typval_T *rettv);
257#endif
258static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
259static void f_nr2char(typval_T *argvars, typval_T *rettv);
260static void f_or(typval_T *argvars, typval_T *rettv);
261static void f_pathshorten(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_PERL
263static void f_perleval(typval_T *argvars, typval_T *rettv);
264#endif
265#ifdef FEAT_FLOAT
266static void f_pow(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
269static void f_printf(typval_T *argvars, typval_T *rettv);
270static void f_pumvisible(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_PYTHON3
272static void f_py3eval(typval_T *argvars, typval_T *rettv);
273#endif
274#ifdef FEAT_PYTHON
275static void f_pyeval(typval_T *argvars, typval_T *rettv);
276#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100277#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
278static void f_pyxeval(typval_T *argvars, typval_T *rettv);
279#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200280static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200281static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200282static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200283static void f_reg_executing(typval_T *argvars, typval_T *rettv);
284static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200285static void f_reltime(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_FLOAT
287static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
288#endif
289static void f_reltimestr(typval_T *argvars, typval_T *rettv);
290static void f_remote_expr(typval_T *argvars, typval_T *rettv);
291static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
292static void f_remote_peek(typval_T *argvars, typval_T *rettv);
293static void f_remote_read(typval_T *argvars, typval_T *rettv);
294static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100295static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_remove(typval_T *argvars, typval_T *rettv);
297static void f_rename(typval_T *argvars, typval_T *rettv);
298static void f_repeat(typval_T *argvars, typval_T *rettv);
299static void f_resolve(typval_T *argvars, typval_T *rettv);
300static void f_reverse(typval_T *argvars, typval_T *rettv);
301#ifdef FEAT_FLOAT
302static void f_round(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100304#ifdef FEAT_RUBY
305static void f_rubyeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_screenattr(typval_T *argvars, typval_T *rettv);
308static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100309static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200310static void f_screencol(typval_T *argvars, typval_T *rettv);
311static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100312static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200313static void f_search(typval_T *argvars, typval_T *rettv);
314static void f_searchdecl(typval_T *argvars, typval_T *rettv);
315static void f_searchpair(typval_T *argvars, typval_T *rettv);
316static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
317static void f_searchpos(typval_T *argvars, typval_T *rettv);
318static void f_server2client(typval_T *argvars, typval_T *rettv);
319static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200320static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_setbufvar(typval_T *argvars, typval_T *rettv);
322static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
323static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200324static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_setfperm(typval_T *argvars, typval_T *rettv);
326static void f_setline(typval_T *argvars, typval_T *rettv);
327static void f_setloclist(typval_T *argvars, typval_T *rettv);
328static void f_setmatches(typval_T *argvars, typval_T *rettv);
329static void f_setpos(typval_T *argvars, typval_T *rettv);
330static void f_setqflist(typval_T *argvars, typval_T *rettv);
331static void f_setreg(typval_T *argvars, typval_T *rettv);
332static void f_settabvar(typval_T *argvars, typval_T *rettv);
333static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100334static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_setwinvar(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_CRYPT
337static void f_sha256(typval_T *argvars, typval_T *rettv);
338#endif /* FEAT_CRYPT */
339static void f_shellescape(typval_T *argvars, typval_T *rettv);
340static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
341static void f_simplify(typval_T *argvars, typval_T *rettv);
342#ifdef FEAT_FLOAT
343static void f_sin(typval_T *argvars, typval_T *rettv);
344static void f_sinh(typval_T *argvars, typval_T *rettv);
345#endif
346static void f_sort(typval_T *argvars, typval_T *rettv);
347static void f_soundfold(typval_T *argvars, typval_T *rettv);
348static void f_spellbadword(typval_T *argvars, typval_T *rettv);
349static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
350static void f_split(typval_T *argvars, typval_T *rettv);
351#ifdef FEAT_FLOAT
352static void f_sqrt(typval_T *argvars, typval_T *rettv);
353static void f_str2float(typval_T *argvars, typval_T *rettv);
354#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200355static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_str2nr(typval_T *argvars, typval_T *rettv);
357static void f_strchars(typval_T *argvars, typval_T *rettv);
358#ifdef HAVE_STRFTIME
359static void f_strftime(typval_T *argvars, typval_T *rettv);
360#endif
361static void f_strgetchar(typval_T *argvars, typval_T *rettv);
362static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200363static void f_strlen(typval_T *argvars, typval_T *rettv);
364static void f_strcharpart(typval_T *argvars, typval_T *rettv);
365static void f_strpart(typval_T *argvars, typval_T *rettv);
366static void f_strridx(typval_T *argvars, typval_T *rettv);
367static void f_strtrans(typval_T *argvars, typval_T *rettv);
368static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
369static void f_strwidth(typval_T *argvars, typval_T *rettv);
370static void f_submatch(typval_T *argvars, typval_T *rettv);
371static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200372static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200373static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200374static void f_synID(typval_T *argvars, typval_T *rettv);
375static void f_synIDattr(typval_T *argvars, typval_T *rettv);
376static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
377static void f_synstack(typval_T *argvars, typval_T *rettv);
378static void f_synconcealed(typval_T *argvars, typval_T *rettv);
379static void f_system(typval_T *argvars, typval_T *rettv);
380static void f_systemlist(typval_T *argvars, typval_T *rettv);
381static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
382static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
383static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
384static void f_taglist(typval_T *argvars, typval_T *rettv);
385static void f_tagfiles(typval_T *argvars, typval_T *rettv);
386static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387#ifdef FEAT_FLOAT
388static void f_tan(typval_T *argvars, typval_T *rettv);
389static void f_tanh(typval_T *argvars, typval_T *rettv);
390#endif
391#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200392static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200393static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200394static void f_timer_start(typval_T *argvars, typval_T *rettv);
395static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200396static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397#endif
398static void f_tolower(typval_T *argvars, typval_T *rettv);
399static void f_toupper(typval_T *argvars, typval_T *rettv);
400static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100401static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402#ifdef FEAT_FLOAT
403static void f_trunc(typval_T *argvars, typval_T *rettv);
404#endif
405static void f_type(typval_T *argvars, typval_T *rettv);
406static void f_undofile(typval_T *argvars, typval_T *rettv);
407static void f_undotree(typval_T *argvars, typval_T *rettv);
408static void f_uniq(typval_T *argvars, typval_T *rettv);
409static void f_values(typval_T *argvars, typval_T *rettv);
410static void f_virtcol(typval_T *argvars, typval_T *rettv);
411static void f_visualmode(typval_T *argvars, typval_T *rettv);
412static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200413static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200414static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
415static void f_win_getid(typval_T *argvars, typval_T *rettv);
416static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
417static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
418static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100419static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420static void f_winbufnr(typval_T *argvars, typval_T *rettv);
421static void f_wincol(typval_T *argvars, typval_T *rettv);
422static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200423static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424static void f_winline(typval_T *argvars, typval_T *rettv);
425static void f_winnr(typval_T *argvars, typval_T *rettv);
426static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
427static void f_winrestview(typval_T *argvars, typval_T *rettv);
428static void f_winsaveview(typval_T *argvars, typval_T *rettv);
429static void f_winwidth(typval_T *argvars, typval_T *rettv);
430static void f_writefile(typval_T *argvars, typval_T *rettv);
431static void f_wordcount(typval_T *argvars, typval_T *rettv);
432static void f_xor(typval_T *argvars, typval_T *rettv);
433
434/*
435 * Array with names and number of arguments of all internal functions
436 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
437 */
438static struct fst
439{
440 char *f_name; /* function name */
441 char f_min_argc; /* minimal number of arguments */
442 char f_max_argc; /* maximal number of arguments */
443 void (*f_func)(typval_T *args, typval_T *rvar);
444 /* implementation of function */
445} functions[] =
446{
447#ifdef FEAT_FLOAT
448 {"abs", 1, 1, f_abs},
449 {"acos", 1, 1, f_acos}, /* WJMc */
450#endif
451 {"add", 2, 2, f_add},
452 {"and", 2, 2, f_and},
453 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200454 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200455 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200456 {"argidx", 0, 0, f_argidx},
457 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200458 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459#ifdef FEAT_FLOAT
460 {"asin", 1, 1, f_asin}, /* WJMc */
461#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100462 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100464 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200465 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200466 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200467 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100468 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469 {"assert_match", 2, 3, f_assert_match},
470 {"assert_notequal", 2, 3, f_assert_notequal},
471 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100472 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473 {"assert_true", 1, 2, f_assert_true},
474#ifdef FEAT_FLOAT
475 {"atan", 1, 1, f_atan},
476 {"atan2", 2, 2, f_atan2},
477#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100478#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200479 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100480 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100481# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100482 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100483# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100484#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485 {"browse", 4, 4, f_browse},
486 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200487 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488 {"bufexists", 1, 1, f_bufexists},
489 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
490 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
491 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
492 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200493 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"bufloaded", 1, 1, f_bufloaded},
495 {"bufname", 1, 1, f_bufname},
496 {"bufnr", 1, 2, f_bufnr},
497 {"bufwinid", 1, 1, f_bufwinid},
498 {"bufwinnr", 1, 1, f_bufwinnr},
499 {"byte2line", 1, 1, f_byte2line},
500 {"byteidx", 2, 2, f_byteidx},
501 {"byteidxcomp", 2, 2, f_byteidxcomp},
502 {"call", 2, 3, f_call},
503#ifdef FEAT_FLOAT
504 {"ceil", 1, 1, f_ceil},
505#endif
506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100507 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200509 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
511 {"ch_evalraw", 2, 3, f_ch_evalraw},
512 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
513 {"ch_getjob", 1, 1, f_ch_getjob},
514 {"ch_info", 1, 1, f_ch_info},
515 {"ch_log", 1, 2, f_ch_log},
516 {"ch_logfile", 1, 2, f_ch_logfile},
517 {"ch_open", 1, 2, f_ch_open},
518 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100519 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200520 {"ch_readraw", 1, 2, f_ch_readraw},
521 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
522 {"ch_sendraw", 2, 3, f_ch_sendraw},
523 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200524 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525#endif
526 {"changenr", 0, 0, f_changenr},
527 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200528 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100530 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200531 {"col", 1, 1, f_col},
532#if defined(FEAT_INS_EXPAND)
533 {"complete", 2, 2, f_complete},
534 {"complete_add", 1, 1, f_complete_add},
535 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100536 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537#endif
538 {"confirm", 1, 4, f_confirm},
539 {"copy", 1, 1, f_copy},
540#ifdef FEAT_FLOAT
541 {"cos", 1, 1, f_cos},
542 {"cosh", 1, 1, f_cosh},
543#endif
544 {"count", 2, 4, f_count},
545 {"cscope_connection",0,3, f_cscope_connection},
546 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100547#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200548 {"debugbreak", 1, 1, f_debugbreak},
549#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"deepcopy", 1, 2, f_deepcopy},
551 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200552 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200553 {"did_filetype", 0, 0, f_did_filetype},
554 {"diff_filler", 1, 1, f_diff_filler},
555 {"diff_hlID", 2, 2, f_diff_hlID},
556 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200557 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200558 {"escape", 2, 2, f_escape},
559 {"eval", 1, 1, f_eval},
560 {"eventhandler", 0, 0, f_eventhandler},
561 {"executable", 1, 1, f_executable},
562 {"execute", 1, 2, f_execute},
563 {"exepath", 1, 1, f_exepath},
564 {"exists", 1, 1, f_exists},
565#ifdef FEAT_FLOAT
566 {"exp", 1, 1, f_exp},
567#endif
568 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200569 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200570 {"extend", 2, 3, f_extend},
571 {"feedkeys", 1, 2, f_feedkeys},
572 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
573 {"filereadable", 1, 1, f_filereadable},
574 {"filewritable", 1, 1, f_filewritable},
575 {"filter", 2, 2, f_filter},
576 {"finddir", 1, 3, f_finddir},
577 {"findfile", 1, 3, f_findfile},
578#ifdef FEAT_FLOAT
579 {"float2nr", 1, 1, f_float2nr},
580 {"floor", 1, 1, f_floor},
581 {"fmod", 2, 2, f_fmod},
582#endif
583 {"fnameescape", 1, 1, f_fnameescape},
584 {"fnamemodify", 2, 2, f_fnamemodify},
585 {"foldclosed", 1, 1, f_foldclosed},
586 {"foldclosedend", 1, 1, f_foldclosedend},
587 {"foldlevel", 1, 1, f_foldlevel},
588 {"foldtext", 0, 0, f_foldtext},
589 {"foldtextresult", 1, 1, f_foldtextresult},
590 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200591 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"function", 1, 3, f_function},
593 {"garbagecollect", 0, 1, f_garbagecollect},
594 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200595 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"getbufline", 2, 3, f_getbufline},
597 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100598 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"getchar", 0, 1, f_getchar},
600 {"getcharmod", 0, 0, f_getcharmod},
601 {"getcharsearch", 0, 0, f_getcharsearch},
602 {"getcmdline", 0, 0, f_getcmdline},
603 {"getcmdpos", 0, 0, f_getcmdpos},
604 {"getcmdtype", 0, 0, f_getcmdtype},
605 {"getcmdwintype", 0, 0, f_getcmdwintype},
606#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200607 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608#endif
609 {"getcurpos", 0, 0, f_getcurpos},
610 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200611 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getfontname", 0, 1, f_getfontname},
613 {"getfperm", 1, 1, f_getfperm},
614 {"getfsize", 1, 1, f_getfsize},
615 {"getftime", 1, 1, f_getftime},
616 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100617 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200619 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100620 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"getpid", 0, 0, f_getpid},
622 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200623 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"getreg", 0, 3, f_getreg},
625 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200626 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627 {"gettabvar", 2, 3, f_gettabvar},
628 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100629 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200630 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100631 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getwinposx", 0, 0, f_getwinposx},
633 {"getwinposy", 0, 0, f_getwinposy},
634 {"getwinvar", 2, 3, f_getwinvar},
635 {"glob", 1, 4, f_glob},
636 {"glob2regpat", 1, 1, f_glob2regpat},
637 {"globpath", 2, 5, f_globpath},
638 {"has", 1, 1, f_has},
639 {"has_key", 2, 2, f_has_key},
640 {"haslocaldir", 0, 2, f_haslocaldir},
641 {"hasmapto", 1, 3, f_hasmapto},
642 {"highlightID", 1, 1, f_hlID}, /* obsolete */
643 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
644 {"histadd", 2, 2, f_histadd},
645 {"histdel", 1, 2, f_histdel},
646 {"histget", 1, 2, f_histget},
647 {"histnr", 1, 1, f_histnr},
648 {"hlID", 1, 1, f_hlID},
649 {"hlexists", 1, 1, f_hlexists},
650 {"hostname", 0, 0, f_hostname},
651 {"iconv", 3, 3, f_iconv},
652 {"indent", 1, 1, f_indent},
653 {"index", 2, 4, f_index},
654 {"input", 1, 3, f_input},
655 {"inputdialog", 1, 3, f_inputdialog},
656 {"inputlist", 1, 1, f_inputlist},
657 {"inputrestore", 0, 0, f_inputrestore},
658 {"inputsave", 0, 0, f_inputsave},
659 {"inputsecret", 1, 2, f_inputsecret},
660 {"insert", 2, 3, f_insert},
661 {"invert", 1, 1, f_invert},
662 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200663#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
664 {"isinf", 1, 1, f_isinf},
665#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200666 {"islocked", 1, 1, f_islocked},
667#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
668 {"isnan", 1, 1, f_isnan},
669#endif
670 {"items", 1, 1, f_items},
671#ifdef FEAT_JOB_CHANNEL
672 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200673 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"job_setoptions", 2, 2, f_job_setoptions},
675 {"job_start", 1, 2, f_job_start},
676 {"job_status", 1, 1, f_job_status},
677 {"job_stop", 1, 2, f_job_stop},
678#endif
679 {"join", 1, 2, f_join},
680 {"js_decode", 1, 1, f_js_decode},
681 {"js_encode", 1, 1, f_js_encode},
682 {"json_decode", 1, 1, f_json_decode},
683 {"json_encode", 1, 1, f_json_encode},
684 {"keys", 1, 1, f_keys},
685 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
686 {"len", 1, 1, f_len},
687 {"libcall", 3, 3, f_libcall},
688 {"libcallnr", 3, 3, f_libcallnr},
689 {"line", 1, 1, f_line},
690 {"line2byte", 1, 1, f_line2byte},
691 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200692 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200693 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200694 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200695 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"localtime", 0, 0, f_localtime},
697#ifdef FEAT_FLOAT
698 {"log", 1, 1, f_log},
699 {"log10", 1, 1, f_log10},
700#endif
701#ifdef FEAT_LUA
702 {"luaeval", 1, 2, f_luaeval},
703#endif
704 {"map", 2, 2, f_map},
705 {"maparg", 1, 4, f_maparg},
706 {"mapcheck", 1, 3, f_mapcheck},
707 {"match", 2, 4, f_match},
708 {"matchadd", 2, 5, f_matchadd},
709 {"matchaddpos", 2, 5, f_matchaddpos},
710 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100711 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200712 {"matchend", 2, 4, f_matchend},
713 {"matchlist", 2, 4, f_matchlist},
714 {"matchstr", 2, 4, f_matchstr},
715 {"matchstrpos", 2, 4, f_matchstrpos},
716 {"max", 1, 1, f_max},
717 {"min", 1, 1, f_min},
718#ifdef vim_mkdir
719 {"mkdir", 1, 3, f_mkdir},
720#endif
721 {"mode", 0, 1, f_mode},
722#ifdef FEAT_MZSCHEME
723 {"mzeval", 1, 1, f_mzeval},
724#endif
725 {"nextnonblank", 1, 1, f_nextnonblank},
726 {"nr2char", 1, 2, f_nr2char},
727 {"or", 2, 2, f_or},
728 {"pathshorten", 1, 1, f_pathshorten},
729#ifdef FEAT_PERL
730 {"perleval", 1, 1, f_perleval},
731#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200732#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200733 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200734 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200735 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200736 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200737 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200738 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200739 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200740 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200741 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200742 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200743 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200744 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200745 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200746 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200747 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200748 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200749 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200750 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752#ifdef FEAT_FLOAT
753 {"pow", 2, 2, f_pow},
754#endif
755 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100756 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200757#ifdef FEAT_JOB_CHANNEL
758 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200759 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200760 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
761#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100762#ifdef FEAT_TEXT_PROP
763 {"prop_add", 3, 3, f_prop_add},
764 {"prop_clear", 1, 3, f_prop_clear},
765 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100766 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100767 {"prop_type_add", 2, 2, f_prop_type_add},
768 {"prop_type_change", 2, 2, f_prop_type_change},
769 {"prop_type_delete", 1, 2, f_prop_type_delete},
770 {"prop_type_get", 1, 2, f_prop_type_get},
771 {"prop_type_list", 0, 1, f_prop_type_list},
772#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"pumvisible", 0, 0, f_pumvisible},
774#ifdef FEAT_PYTHON3
775 {"py3eval", 1, 1, f_py3eval},
776#endif
777#ifdef FEAT_PYTHON
778 {"pyeval", 1, 1, f_pyeval},
779#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100780#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
781 {"pyxeval", 1, 1, f_pyxeval},
782#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200784 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200786 {"reg_executing", 0, 0, f_reg_executing},
787 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"reltime", 0, 2, f_reltime},
789#ifdef FEAT_FLOAT
790 {"reltimefloat", 1, 1, f_reltimefloat},
791#endif
792 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100793 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"remote_foreground", 1, 1, f_remote_foreground},
795 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100796 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100798 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200799 {"remove", 2, 3, f_remove},
800 {"rename", 2, 2, f_rename},
801 {"repeat", 2, 2, f_repeat},
802 {"resolve", 1, 1, f_resolve},
803 {"reverse", 1, 1, f_reverse},
804#ifdef FEAT_FLOAT
805 {"round", 1, 1, f_round},
806#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100807#ifdef FEAT_RUBY
808 {"rubyeval", 1, 1, f_rubyeval},
809#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200810 {"screenattr", 2, 2, f_screenattr},
811 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100812 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200814 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100816 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200817 {"search", 1, 4, f_search},
818 {"searchdecl", 1, 3, f_searchdecl},
819 {"searchpair", 3, 7, f_searchpair},
820 {"searchpairpos", 3, 7, f_searchpairpos},
821 {"searchpos", 1, 4, f_searchpos},
822 {"server2client", 2, 2, f_server2client},
823 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200824 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"setbufvar", 3, 3, f_setbufvar},
826 {"setcharsearch", 1, 1, f_setcharsearch},
827 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200828 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829 {"setfperm", 2, 2, f_setfperm},
830 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200831 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100832 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200834 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"setreg", 2, 3, f_setreg},
836 {"settabvar", 3, 3, f_settabvar},
837 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100838 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"setwinvar", 3, 3, f_setwinvar},
840#ifdef FEAT_CRYPT
841 {"sha256", 1, 1, f_sha256},
842#endif
843 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100844 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100845#ifdef FEAT_SIGNS
846 {"sign_define", 1, 2, f_sign_define},
847 {"sign_getdefined", 0, 1, f_sign_getdefined},
848 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100849 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100850 {"sign_place", 4, 5, f_sign_place},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200851 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100852 {"sign_undefine", 0, 1, f_sign_undefine},
853 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200854 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100855#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"simplify", 1, 1, f_simplify},
857#ifdef FEAT_FLOAT
858 {"sin", 1, 1, f_sin},
859 {"sinh", 1, 1, f_sinh},
860#endif
861 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200862#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200864 {"sound_playevent", 1, 2, f_sound_playevent},
865 {"sound_playfile", 1, 2, f_sound_playfile},
866 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"soundfold", 1, 1, f_soundfold},
869 {"spellbadword", 0, 1, f_spellbadword},
870 {"spellsuggest", 1, 3, f_spellsuggest},
871 {"split", 1, 3, f_split},
872#ifdef FEAT_FLOAT
873 {"sqrt", 1, 1, f_sqrt},
874 {"str2float", 1, 1, f_str2float},
875#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200876 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877 {"str2nr", 1, 2, f_str2nr},
878 {"strcharpart", 2, 3, f_strcharpart},
879 {"strchars", 1, 2, f_strchars},
880 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
881#ifdef HAVE_STRFTIME
882 {"strftime", 1, 2, f_strftime},
883#endif
884 {"strgetchar", 2, 2, f_strgetchar},
885 {"stridx", 2, 3, f_stridx},
886 {"string", 1, 1, f_string},
887 {"strlen", 1, 1, f_strlen},
888 {"strpart", 2, 3, f_strpart},
889 {"strridx", 2, 3, f_strridx},
890 {"strtrans", 1, 1, f_strtrans},
891 {"strwidth", 1, 1, f_strwidth},
892 {"submatch", 1, 2, f_submatch},
893 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200894 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200895 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"synID", 3, 3, f_synID},
897 {"synIDattr", 2, 3, f_synIDattr},
898 {"synIDtrans", 1, 1, f_synIDtrans},
899 {"synconcealed", 2, 2, f_synconcealed},
900 {"synstack", 2, 2, f_synstack},
901 {"system", 1, 2, f_system},
902 {"systemlist", 1, 2, f_systemlist},
903 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
904 {"tabpagenr", 0, 1, f_tabpagenr},
905 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
906 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100907 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908#ifdef FEAT_FLOAT
909 {"tan", 1, 1, f_tan},
910 {"tanh", 1, 1, f_tanh},
911#endif
912 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100914 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
915 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100916 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200917 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200918# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
919 {"term_getansicolors", 1, 1, f_term_getansicolors},
920# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200921 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200922 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200924 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200925 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200926 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200927 {"term_getstatus", 1, 1, f_term_getstatus},
928 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200929 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200930 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200931 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200932 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200933# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
934 {"term_setansicolors", 2, 2, f_term_setansicolors},
935# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100936 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100937 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200938 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200939 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200940 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200942 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
943 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200944 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200946 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200947 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100948 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100949 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950#ifdef FEAT_JOB_CHANNEL
951 {"test_null_channel", 0, 0, f_test_null_channel},
952#endif
953 {"test_null_dict", 0, 0, f_test_null_dict},
954#ifdef FEAT_JOB_CHANNEL
955 {"test_null_job", 0, 0, f_test_null_job},
956#endif
957 {"test_null_list", 0, 0, f_test_null_list},
958 {"test_null_partial", 0, 0, f_test_null_partial},
959 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200960 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100961 {"test_override", 2, 2, f_test_override},
962 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200963#ifdef FEAT_GUI
964 {"test_scrollbar", 3, 3, f_test_scrollbar},
965#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200966#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200967 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200968#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969 {"test_settime", 1, 1, f_test_settime},
970#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200971 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200972 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200973 {"timer_start", 2, 3, f_timer_start},
974 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200975 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#endif
977 {"tolower", 1, 1, f_tolower},
978 {"toupper", 1, 1, f_toupper},
979 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100980 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200981#ifdef FEAT_FLOAT
982 {"trunc", 1, 1, f_trunc},
983#endif
984 {"type", 1, 1, f_type},
985 {"undofile", 1, 1, f_undofile},
986 {"undotree", 0, 0, f_undotree},
987 {"uniq", 1, 3, f_uniq},
988 {"values", 1, 1, f_values},
989 {"virtcol", 1, 1, f_virtcol},
990 {"visualmode", 0, 1, f_visualmode},
991 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200992 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200993 {"win_findbuf", 1, 1, f_win_findbuf},
994 {"win_getid", 0, 2, f_win_getid},
995 {"win_gotoid", 1, 1, f_win_gotoid},
996 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
997 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100998 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200999 {"winbufnr", 1, 1, f_winbufnr},
1000 {"wincol", 0, 0, f_wincol},
1001 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001002 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001003 {"winline", 0, 0, f_winline},
1004 {"winnr", 0, 1, f_winnr},
1005 {"winrestcmd", 0, 0, f_winrestcmd},
1006 {"winrestview", 1, 1, f_winrestview},
1007 {"winsaveview", 0, 0, f_winsaveview},
1008 {"winwidth", 1, 1, f_winwidth},
1009 {"wordcount", 0, 0, f_wordcount},
1010 {"writefile", 2, 3, f_writefile},
1011 {"xor", 2, 2, f_xor},
1012};
1013
1014#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1015
1016/*
1017 * Function given to ExpandGeneric() to obtain the list of internal
1018 * or user defined function names.
1019 */
1020 char_u *
1021get_function_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_user_func_name(xp, idx);
1031 if (name != NULL)
1032 return name;
1033 }
1034 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1035 {
1036 STRCPY(IObuff, functions[intidx].f_name);
1037 STRCAT(IObuff, "(");
1038 if (functions[intidx].f_max_argc == 0)
1039 STRCAT(IObuff, ")");
1040 return IObuff;
1041 }
1042
1043 return NULL;
1044}
1045
1046/*
1047 * Function given to ExpandGeneric() to obtain the list of internal or
1048 * user defined variable or function names.
1049 */
1050 char_u *
1051get_expr_name(expand_T *xp, int idx)
1052{
1053 static int intidx = -1;
1054 char_u *name;
1055
1056 if (idx == 0)
1057 intidx = -1;
1058 if (intidx < 0)
1059 {
1060 name = get_function_name(xp, idx);
1061 if (name != NULL)
1062 return name;
1063 }
1064 return get_user_var_name(xp, ++intidx);
1065}
1066
1067#endif /* FEAT_CMDL_COMPL */
1068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001069/*
1070 * Find internal function in table above.
1071 * Return index, or -1 if not found
1072 */
1073 int
1074find_internal_func(
1075 char_u *name) /* name of the function */
1076{
1077 int first = 0;
1078 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1079 int cmp;
1080 int x;
1081
1082 /*
1083 * Find the function name in the table. Binary search.
1084 */
1085 while (first <= last)
1086 {
1087 x = first + ((unsigned)(last - first) >> 1);
1088 cmp = STRCMP(name, functions[x].f_name);
1089 if (cmp < 0)
1090 last = x - 1;
1091 else if (cmp > 0)
1092 first = x + 1;
1093 else
1094 return x;
1095 }
1096 return -1;
1097}
1098
1099 int
1100call_internal_func(
1101 char_u *name,
1102 int argcount,
1103 typval_T *argvars,
1104 typval_T *rettv)
1105{
1106 int i;
1107
1108 i = find_internal_func(name);
1109 if (i < 0)
1110 return ERROR_UNKNOWN;
1111 if (argcount < functions[i].f_min_argc)
1112 return ERROR_TOOFEW;
1113 if (argcount > functions[i].f_max_argc)
1114 return ERROR_TOOMANY;
1115 argvars[argcount].v_type = VAR_UNKNOWN;
1116 functions[i].f_func(argvars, rettv);
1117 return ERROR_NONE;
1118}
1119
1120/*
1121 * Return TRUE for a non-zero Number and a non-empty String.
1122 */
1123 static int
1124non_zero_arg(typval_T *argvars)
1125{
1126 return ((argvars[0].v_type == VAR_NUMBER
1127 && argvars[0].vval.v_number != 0)
1128 || (argvars[0].v_type == VAR_SPECIAL
1129 && argvars[0].vval.v_number == VVAL_TRUE)
1130 || (argvars[0].v_type == VAR_STRING
1131 && argvars[0].vval.v_string != NULL
1132 && *argvars[0].vval.v_string != NUL));
1133}
1134
1135/*
1136 * Get the lnum from the first argument.
1137 * Also accepts ".", "$", etc., but that only works for the current buffer.
1138 * Returns -1 on error.
1139 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001140 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001141tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001142{
1143 typval_T rettv;
1144 linenr_T lnum;
1145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001147 if (lnum == 0) /* no valid number, try using line() */
1148 {
1149 rettv.v_type = VAR_NUMBER;
1150 f_line(argvars, &rettv);
1151 lnum = (linenr_T)rettv.vval.v_number;
1152 clear_tv(&rettv);
1153 }
1154 return lnum;
1155}
1156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001157/*
1158 * Get the lnum from the first argument.
1159 * Also accepts "$", then "buf" is used.
1160 * Returns 0 on error.
1161 */
1162 static linenr_T
1163tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1164{
1165 if (argvars[0].v_type == VAR_STRING
1166 && argvars[0].vval.v_string != NULL
1167 && argvars[0].vval.v_string[0] == '$'
1168 && buf != NULL)
1169 return buf->b_ml.ml_line_count;
1170 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1171}
1172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001173#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174/*
1175 * Get the float value of "argvars[0]" into "f".
1176 * Returns FAIL when the argument is not a Number or Float.
1177 */
1178 static int
1179get_float_arg(typval_T *argvars, float_T *f)
1180{
1181 if (argvars[0].v_type == VAR_FLOAT)
1182 {
1183 *f = argvars[0].vval.v_float;
1184 return OK;
1185 }
1186 if (argvars[0].v_type == VAR_NUMBER)
1187 {
1188 *f = (float_T)argvars[0].vval.v_number;
1189 return OK;
1190 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001192 return FAIL;
1193}
1194
1195/*
1196 * "abs(expr)" function
1197 */
1198 static void
1199f_abs(typval_T *argvars, typval_T *rettv)
1200{
1201 if (argvars[0].v_type == VAR_FLOAT)
1202 {
1203 rettv->v_type = VAR_FLOAT;
1204 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1205 }
1206 else
1207 {
1208 varnumber_T n;
1209 int error = FALSE;
1210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001212 if (error)
1213 rettv->vval.v_number = -1;
1214 else if (n > 0)
1215 rettv->vval.v_number = n;
1216 else
1217 rettv->vval.v_number = -n;
1218 }
1219}
1220
1221/*
1222 * "acos()" function
1223 */
1224 static void
1225f_acos(typval_T *argvars, typval_T *rettv)
1226{
1227 float_T f = 0.0;
1228
1229 rettv->v_type = VAR_FLOAT;
1230 if (get_float_arg(argvars, &f) == OK)
1231 rettv->vval.v_float = acos(f);
1232 else
1233 rettv->vval.v_float = 0.0;
1234}
1235#endif
1236
1237/*
1238 * "add(list, item)" function
1239 */
1240 static void
1241f_add(typval_T *argvars, typval_T *rettv)
1242{
1243 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001244 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245
1246 rettv->vval.v_number = 1; /* Default: Failed */
1247 if (argvars[0].v_type == VAR_LIST)
1248 {
1249 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001250 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001251 (char_u *)N_("add() argument"), TRUE)
1252 && list_append_tv(l, &argvars[1]) == OK)
1253 copy_tv(&argvars[0], rettv);
1254 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001255 else if (argvars[0].v_type == VAR_BLOB)
1256 {
1257 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001258 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001259 (char_u *)N_("add() argument"), TRUE))
1260 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001261 int error = FALSE;
1262 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1263
1264 if (!error)
1265 {
1266 ga_append(&b->bv_ga, (int)n);
1267 copy_tv(&argvars[0], rettv);
1268 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001269 }
1270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001272 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273}
1274
1275/*
1276 * "and(expr, expr)" function
1277 */
1278 static void
1279f_and(typval_T *argvars, typval_T *rettv)
1280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001281 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1282 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001283}
1284
1285/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001286 * If there is a window for "curbuf", make it the current window.
1287 */
1288 static void
1289find_win_for_curbuf(void)
1290{
1291 wininfo_T *wip;
1292
1293 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1294 {
1295 if (wip->wi_win != NULL)
1296 {
1297 curwin = wip->wi_win;
1298 break;
1299 }
1300 }
1301}
1302
1303/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001304 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 */
1306 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001307set_buffer_lines(
1308 buf_T *buf,
1309 linenr_T lnum_arg,
1310 int append,
1311 typval_T *lines,
1312 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313{
Bram Moolenaarca851592018-06-06 21:04:07 +02001314 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1315 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001316 list_T *l = NULL;
1317 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001319 linenr_T append_lnum;
1320 buf_T *curbuf_save = NULL;
1321 win_T *curwin_save = NULL;
1322 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323
Bram Moolenaarca851592018-06-06 21:04:07 +02001324 /* When using the current buffer ml_mfp will be set if needed. Useful when
1325 * setline() is used on startup. For other buffers the buffer must be
1326 * loaded. */
1327 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001329 rettv->vval.v_number = 1; /* FAIL */
1330 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 }
1332
Bram Moolenaarca851592018-06-06 21:04:07 +02001333 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001334 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001335 curbuf_save = curbuf;
1336 curwin_save = curwin;
1337 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001338 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 }
1340
1341 if (append)
1342 // appendbufline() uses the line number below which we insert
1343 append_lnum = lnum - 1;
1344 else
1345 // setbufline() uses the line number above which we insert, we only
1346 // append if it's below the last line
1347 append_lnum = curbuf->b_ml.ml_line_count;
1348
1349 if (lines->v_type == VAR_LIST)
1350 {
1351 l = lines->vval.v_list;
1352 li = l->lv_first;
1353 }
1354 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001355 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001356
1357 /* default result is zero == OK */
1358 for (;;)
1359 {
1360 if (l != NULL)
1361 {
1362 /* list argument, get next string */
1363 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001365 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 li = li->li_next;
1367 }
1368
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 rettv->vval.v_number = 1; /* FAIL */
1370 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1371 break;
1372
1373 /* When coming here from Insert mode, sync undo, so that this can be
1374 * undone separately from what was previously inserted. */
1375 if (u_sync_once == 2)
1376 {
1377 u_sync_once = 1; /* notify that u_sync() was called */
1378 u_sync(TRUE);
1379 }
1380
1381 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1382 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001383 // Existing line, replace it.
1384 // Removes any existing text properties.
1385 if (u_savesub(lnum) == OK && ml_replace_len(
1386 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 {
1388 changed_bytes(lnum, 0);
1389 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1390 check_cursor_col();
1391 rettv->vval.v_number = 0; /* OK */
1392 }
1393 }
1394 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1395 {
1396 /* append the line */
1397 ++added;
1398 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1399 rettv->vval.v_number = 0; /* OK */
1400 }
1401
1402 if (l == NULL) /* only one string argument */
1403 break;
1404 ++lnum;
1405 }
1406
1407 if (added > 0)
1408 {
1409 win_T *wp;
1410 tabpage_T *tp;
1411
1412 appended_lines_mark(append_lnum, added);
1413 FOR_ALL_TAB_WINDOWS(tp, wp)
1414 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1415 wp->w_cursor.lnum += added;
1416 check_cursor_col();
1417
Bram Moolenaarf2732452018-06-03 14:47:35 +02001418#ifdef FEAT_JOB_CHANNEL
1419 if (bt_prompt(curbuf) && (State & INSERT))
1420 // show the line with the prompt
1421 update_topline();
1422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001424
1425 if (!is_curbuf)
1426 {
1427 curbuf = curbuf_save;
1428 curwin = curwin_save;
1429 }
1430}
1431
1432/*
1433 * "append(lnum, string/list)" function
1434 */
1435 static void
1436f_append(typval_T *argvars, typval_T *rettv)
1437{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001438 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001439
1440 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1441}
1442
1443/*
1444 * "appendbufline(buf, lnum, string/list)" function
1445 */
1446 static void
1447f_appendbufline(typval_T *argvars, typval_T *rettv)
1448{
1449 linenr_T lnum;
1450 buf_T *buf;
1451
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001452 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001453 if (buf == NULL)
1454 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001455 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001456 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001457 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001458 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460}
1461
1462/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001463 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001464 */
1465 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001466f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001467{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001468 win_T *wp;
1469
1470 if (argvars[0].v_type == VAR_UNKNOWN)
1471 // use the current window
1472 rettv->vval.v_number = ARGCOUNT;
1473 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001474 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001475 // use the global argument list
1476 rettv->vval.v_number = GARGCOUNT;
1477 else
1478 {
1479 // use the argument list of the specified window
1480 wp = find_win_by_nr_or_id(&argvars[0]);
1481 if (wp != NULL)
1482 rettv->vval.v_number = WARGCOUNT(wp);
1483 else
1484 rettv->vval.v_number = -1;
1485 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
1489 * "argidx()" function
1490 */
1491 static void
1492f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1493{
1494 rettv->vval.v_number = curwin->w_arg_idx;
1495}
1496
1497/*
1498 * "arglistid()" function
1499 */
1500 static void
1501f_arglistid(typval_T *argvars, typval_T *rettv)
1502{
1503 win_T *wp;
1504
1505 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001506 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507 if (wp != NULL)
1508 rettv->vval.v_number = wp->w_alist->id;
1509}
1510
1511/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 * Get the argument list for a given window
1513 */
1514 static void
1515get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1516{
1517 int idx;
1518
1519 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1520 for (idx = 0; idx < argcount; ++idx)
1521 list_append_string(rettv->vval.v_list,
1522 alist_name(&arglist[idx]), -1);
1523}
1524
1525/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526 * "argv(nr)" function
1527 */
1528 static void
1529f_argv(typval_T *argvars, typval_T *rettv)
1530{
1531 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001532 aentry_T *arglist = NULL;
1533 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534
1535 if (argvars[0].v_type != VAR_UNKNOWN)
1536 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 if (argvars[1].v_type == VAR_UNKNOWN)
1538 {
1539 arglist = ARGLIST;
1540 argcount = ARGCOUNT;
1541 }
1542 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001543 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001544 {
1545 arglist = GARGLIST;
1546 argcount = GARGCOUNT;
1547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001548 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001549 {
1550 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1551
1552 if (wp != NULL)
1553 {
1554 /* Use the argument list of the specified window */
1555 arglist = WARGLIST(wp);
1556 argcount = WARGCOUNT(wp);
1557 }
1558 }
1559
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001561 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001562 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001563 if (arglist != NULL && idx >= 0 && idx < argcount)
1564 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1565 else if (idx == -1)
1566 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001568 else
1569 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570}
1571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001572#ifdef FEAT_FLOAT
1573/*
1574 * "asin()" function
1575 */
1576 static void
1577f_asin(typval_T *argvars, typval_T *rettv)
1578{
1579 float_T f = 0.0;
1580
1581 rettv->v_type = VAR_FLOAT;
1582 if (get_float_arg(argvars, &f) == OK)
1583 rettv->vval.v_float = asin(f);
1584 else
1585 rettv->vval.v_float = 0.0;
1586}
1587
1588/*
1589 * "atan()" function
1590 */
1591 static void
1592f_atan(typval_T *argvars, typval_T *rettv)
1593{
1594 float_T f = 0.0;
1595
1596 rettv->v_type = VAR_FLOAT;
1597 if (get_float_arg(argvars, &f) == OK)
1598 rettv->vval.v_float = atan(f);
1599 else
1600 rettv->vval.v_float = 0.0;
1601}
1602
1603/*
1604 * "atan2()" function
1605 */
1606 static void
1607f_atan2(typval_T *argvars, typval_T *rettv)
1608{
1609 float_T fx = 0.0, fy = 0.0;
1610
1611 rettv->v_type = VAR_FLOAT;
1612 if (get_float_arg(argvars, &fx) == OK
1613 && get_float_arg(&argvars[1], &fy) == OK)
1614 rettv->vval.v_float = atan2(fx, fy);
1615 else
1616 rettv->vval.v_float = 0.0;
1617}
1618#endif
1619
1620/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001621 * "balloon_show()" function
1622 */
1623#ifdef FEAT_BEVAL
1624 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001625f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1626{
1627 rettv->v_type = VAR_STRING;
1628 if (balloonEval != NULL)
1629 {
1630 if (balloonEval->msg == NULL)
1631 rettv->vval.v_string = NULL;
1632 else
1633 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1634 }
1635}
1636
1637 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001638f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1639{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001640 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001641 {
1642 if (argvars[0].v_type == VAR_LIST
1643# ifdef FEAT_GUI
1644 && !gui.in_use
1645# endif
1646 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001647 {
1648 list_T *l = argvars[0].vval.v_list;
1649
1650 // empty list removes the balloon
1651 post_balloon(balloonEval, NULL,
1652 l == NULL || l->lv_len == 0 ? NULL : l);
1653 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001654 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001655 {
1656 char_u *mesg = tv_get_string_chk(&argvars[0]);
1657
1658 if (mesg != NULL)
1659 // empty string removes the balloon
1660 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1661 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001662 }
1663}
1664
Bram Moolenaar669a8282017-11-19 20:13:05 +01001665# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001666 static void
1667f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1668{
1669 if (rettv_list_alloc(rettv) == OK)
1670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001671 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001672
1673 if (msg != NULL)
1674 {
1675 pumitem_T *array;
1676 int size = split_message(msg, &array);
1677 int i;
1678
1679 /* Skip the first and last item, they are always empty. */
1680 for (i = 1; i < size - 1; ++i)
1681 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001682 while (size > 0)
1683 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001684 vim_free(array);
1685 }
1686 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001687}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001688# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001689#endif
1690
1691/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692 * "browse(save, title, initdir, default)" function
1693 */
1694 static void
1695f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1696{
1697#ifdef FEAT_BROWSE
1698 int save;
1699 char_u *title;
1700 char_u *initdir;
1701 char_u *defname;
1702 char_u buf[NUMBUFLEN];
1703 char_u buf2[NUMBUFLEN];
1704 int error = FALSE;
1705
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001706 save = (int)tv_get_number_chk(&argvars[0], &error);
1707 title = tv_get_string_chk(&argvars[1]);
1708 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1709 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710
1711 if (error || title == NULL || initdir == NULL || defname == NULL)
1712 rettv->vval.v_string = NULL;
1713 else
1714 rettv->vval.v_string =
1715 do_browse(save ? BROWSE_SAVE : 0,
1716 title, defname, NULL, initdir, NULL, curbuf);
1717#else
1718 rettv->vval.v_string = NULL;
1719#endif
1720 rettv->v_type = VAR_STRING;
1721}
1722
1723/*
1724 * "browsedir(title, initdir)" function
1725 */
1726 static void
1727f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1728{
1729#ifdef FEAT_BROWSE
1730 char_u *title;
1731 char_u *initdir;
1732 char_u buf[NUMBUFLEN];
1733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001734 title = tv_get_string_chk(&argvars[0]);
1735 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001736
1737 if (title == NULL || initdir == NULL)
1738 rettv->vval.v_string = NULL;
1739 else
1740 rettv->vval.v_string = do_browse(BROWSE_DIR,
1741 title, NULL, NULL, initdir, NULL, curbuf);
1742#else
1743 rettv->vval.v_string = NULL;
1744#endif
1745 rettv->v_type = VAR_STRING;
1746}
1747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001748/*
1749 * Find a buffer by number or exact name.
1750 */
1751 static buf_T *
1752find_buffer(typval_T *avar)
1753{
1754 buf_T *buf = NULL;
1755
1756 if (avar->v_type == VAR_NUMBER)
1757 buf = buflist_findnr((int)avar->vval.v_number);
1758 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1759 {
1760 buf = buflist_findname_exp(avar->vval.v_string);
1761 if (buf == NULL)
1762 {
1763 /* No full path name match, try a match with a URL or a "nofile"
1764 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001765 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001766 if (buf->b_fname != NULL
1767 && (path_with_url(buf->b_fname)
1768#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001769 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001770#endif
1771 )
1772 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1773 break;
1774 }
1775 }
1776 return buf;
1777}
1778
1779/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001780 * "bufadd(expr)" function
1781 */
1782 static void
1783f_bufadd(typval_T *argvars, typval_T *rettv)
1784{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001785 char_u *name = tv_get_string(&argvars[0]);
1786
1787 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001788}
1789
1790/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001791 * "bufexists(expr)" function
1792 */
1793 static void
1794f_bufexists(typval_T *argvars, typval_T *rettv)
1795{
1796 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1797}
1798
1799/*
1800 * "buflisted(expr)" function
1801 */
1802 static void
1803f_buflisted(typval_T *argvars, typval_T *rettv)
1804{
1805 buf_T *buf;
1806
1807 buf = find_buffer(&argvars[0]);
1808 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1809}
1810
1811/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001812 * "bufload(expr)" function
1813 */
1814 static void
1815f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1816{
1817 buf_T *buf = get_buf_arg(&argvars[0]);
1818
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001819 if (buf != NULL)
1820 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001821}
1822
1823/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001824 * "bufloaded(expr)" function
1825 */
1826 static void
1827f_bufloaded(typval_T *argvars, typval_T *rettv)
1828{
1829 buf_T *buf;
1830
1831 buf = find_buffer(&argvars[0]);
1832 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1833}
1834
1835 buf_T *
1836buflist_find_by_name(char_u *name, int curtab_only)
1837{
1838 int save_magic;
1839 char_u *save_cpo;
1840 buf_T *buf;
1841
1842 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1843 save_magic = p_magic;
1844 p_magic = TRUE;
1845 save_cpo = p_cpo;
1846 p_cpo = (char_u *)"";
1847
1848 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1849 TRUE, FALSE, curtab_only));
1850
1851 p_magic = save_magic;
1852 p_cpo = save_cpo;
1853 return buf;
1854}
1855
1856/*
1857 * Get buffer by number or pattern.
1858 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001859 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001860tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001861{
1862 char_u *name = tv->vval.v_string;
1863 buf_T *buf;
1864
1865 if (tv->v_type == VAR_NUMBER)
1866 return buflist_findnr((int)tv->vval.v_number);
1867 if (tv->v_type != VAR_STRING)
1868 return NULL;
1869 if (name == NULL || *name == NUL)
1870 return curbuf;
1871 if (name[0] == '$' && name[1] == NUL)
1872 return lastbuf;
1873
1874 buf = buflist_find_by_name(name, curtab_only);
1875
1876 /* If not found, try expanding the name, like done for bufexists(). */
1877 if (buf == NULL)
1878 buf = find_buffer(tv);
1879
1880 return buf;
1881}
1882
1883/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001884 * Get the buffer from "arg" and give an error and return NULL if it is not
1885 * valid.
1886 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001887 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001888get_buf_arg(typval_T *arg)
1889{
1890 buf_T *buf;
1891
1892 ++emsg_off;
1893 buf = tv_get_buf(arg, FALSE);
1894 --emsg_off;
1895 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001896 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001897 return buf;
1898}
1899
1900/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 * "bufname(expr)" function
1902 */
1903 static void
1904f_bufname(typval_T *argvars, typval_T *rettv)
1905{
1906 buf_T *buf;
1907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001908 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001910 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 rettv->v_type = VAR_STRING;
1912 if (buf != NULL && buf->b_fname != NULL)
1913 rettv->vval.v_string = vim_strsave(buf->b_fname);
1914 else
1915 rettv->vval.v_string = NULL;
1916 --emsg_off;
1917}
1918
1919/*
1920 * "bufnr(expr)" function
1921 */
1922 static void
1923f_bufnr(typval_T *argvars, typval_T *rettv)
1924{
1925 buf_T *buf;
1926 int error = FALSE;
1927 char_u *name;
1928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001929 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001931 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 --emsg_off;
1933
1934 /* If the buffer isn't found and the second argument is not zero create a
1935 * new buffer. */
1936 if (buf == NULL
1937 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001938 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001940 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941 && !error)
1942 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1943
1944 if (buf != NULL)
1945 rettv->vval.v_number = buf->b_fnum;
1946 else
1947 rettv->vval.v_number = -1;
1948}
1949
1950 static void
1951buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1952{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 win_T *wp;
1954 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955 buf_T *buf;
1956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001957 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001958 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001959 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001960 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001961 {
1962 ++winnr;
1963 if (wp->w_buffer == buf)
1964 break;
1965 }
1966 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967 --emsg_off;
1968}
1969
1970/*
1971 * "bufwinid(nr)" function
1972 */
1973 static void
1974f_bufwinid(typval_T *argvars, typval_T *rettv)
1975{
1976 buf_win_common(argvars, rettv, FALSE);
1977}
1978
1979/*
1980 * "bufwinnr(nr)" function
1981 */
1982 static void
1983f_bufwinnr(typval_T *argvars, typval_T *rettv)
1984{
1985 buf_win_common(argvars, rettv, TRUE);
1986}
1987
1988/*
1989 * "byte2line(byte)" function
1990 */
1991 static void
1992f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1993{
1994#ifndef FEAT_BYTEOFF
1995 rettv->vval.v_number = -1;
1996#else
1997 long boff = 0;
1998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001999 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000 if (boff < 0)
2001 rettv->vval.v_number = -1;
2002 else
2003 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2004 (linenr_T)0, &boff);
2005#endif
2006}
2007
2008 static void
2009byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2010{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002011 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002012 char_u *str;
2013 varnumber_T idx;
2014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002015 str = tv_get_string_chk(&argvars[0]);
2016 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002017 rettv->vval.v_number = -1;
2018 if (str == NULL || idx < 0)
2019 return;
2020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021 t = str;
2022 for ( ; idx > 0; idx--)
2023 {
2024 if (*t == NUL) /* EOL reached */
2025 return;
2026 if (enc_utf8 && comp)
2027 t += utf_ptr2len(t);
2028 else
2029 t += (*mb_ptr2len)(t);
2030 }
2031 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002032}
2033
2034/*
2035 * "byteidx()" function
2036 */
2037 static void
2038f_byteidx(typval_T *argvars, typval_T *rettv)
2039{
2040 byteidx(argvars, rettv, FALSE);
2041}
2042
2043/*
2044 * "byteidxcomp()" function
2045 */
2046 static void
2047f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2048{
2049 byteidx(argvars, rettv, TRUE);
2050}
2051
2052/*
2053 * "call(func, arglist [, dict])" function
2054 */
2055 static void
2056f_call(typval_T *argvars, typval_T *rettv)
2057{
2058 char_u *func;
2059 partial_T *partial = NULL;
2060 dict_T *selfdict = NULL;
2061
2062 if (argvars[1].v_type != VAR_LIST)
2063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002064 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 return;
2066 }
2067 if (argvars[1].vval.v_list == NULL)
2068 return;
2069
2070 if (argvars[0].v_type == VAR_FUNC)
2071 func = argvars[0].vval.v_string;
2072 else if (argvars[0].v_type == VAR_PARTIAL)
2073 {
2074 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002075 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002076 }
2077 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079 if (*func == NUL)
2080 return; /* type error or empty name */
2081
2082 if (argvars[2].v_type != VAR_UNKNOWN)
2083 {
2084 if (argvars[2].v_type != VAR_DICT)
2085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002086 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 return;
2088 }
2089 selfdict = argvars[2].vval.v_dict;
2090 }
2091
2092 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2093}
2094
2095#ifdef FEAT_FLOAT
2096/*
2097 * "ceil({float})" function
2098 */
2099 static void
2100f_ceil(typval_T *argvars, typval_T *rettv)
2101{
2102 float_T f = 0.0;
2103
2104 rettv->v_type = VAR_FLOAT;
2105 if (get_float_arg(argvars, &f) == OK)
2106 rettv->vval.v_float = ceil(f);
2107 else
2108 rettv->vval.v_float = 0.0;
2109}
2110#endif
2111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112/*
2113 * "changenr()" function
2114 */
2115 static void
2116f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2117{
2118 rettv->vval.v_number = curbuf->b_u_seq_cur;
2119}
2120
2121/*
2122 * "char2nr(string)" function
2123 */
2124 static void
2125f_char2nr(typval_T *argvars, typval_T *rettv)
2126{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002127 if (has_mbyte)
2128 {
2129 int utf8 = 0;
2130
2131 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002132 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002133
2134 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002135 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002137 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 }
2139 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002140 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141}
2142
2143/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002144 * "chdir(dir)" function
2145 */
2146 static void
2147f_chdir(typval_T *argvars, typval_T *rettv)
2148{
2149 char_u *cwd;
2150 cdscope_T scope = CDSCOPE_GLOBAL;
2151
2152 rettv->v_type = VAR_STRING;
2153 rettv->vval.v_string = NULL;
2154
2155 if (argvars[0].v_type != VAR_STRING)
2156 return;
2157
2158 // Return the current directory
2159 cwd = alloc(MAXPATHL);
2160 if (cwd != NULL)
2161 {
2162 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2163 {
2164#ifdef BACKSLASH_IN_FILENAME
2165 slash_adjust(cwd);
2166#endif
2167 rettv->vval.v_string = vim_strsave(cwd);
2168 }
2169 vim_free(cwd);
2170 }
2171
2172 if (curwin->w_localdir != NULL)
2173 scope = CDSCOPE_WINDOW;
2174 else if (curtab->tp_localdir != NULL)
2175 scope = CDSCOPE_TABPAGE;
2176
2177 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2178 // Directory change failed
2179 VIM_CLEAR(rettv->vval.v_string);
2180}
2181
2182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002183 * "cindent(lnum)" function
2184 */
2185 static void
2186f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2187{
2188#ifdef FEAT_CINDENT
2189 pos_T pos;
2190 linenr_T lnum;
2191
2192 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002193 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002194 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2195 {
2196 curwin->w_cursor.lnum = lnum;
2197 rettv->vval.v_number = get_c_indent();
2198 curwin->w_cursor = pos;
2199 }
2200 else
2201#endif
2202 rettv->vval.v_number = -1;
2203}
2204
Bram Moolenaaraff74912019-03-30 18:11:49 +01002205 static win_T *
2206get_optional_window(typval_T *argvars, int idx)
2207{
2208 win_T *win = curwin;
2209
2210 if (argvars[idx].v_type != VAR_UNKNOWN)
2211 {
2212 win = find_win_by_nr_or_id(&argvars[idx]);
2213 if (win == NULL)
2214 {
2215 emsg(_(e_invalwindow));
2216 return NULL;
2217 }
2218 }
2219 return win;
2220}
2221
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222/*
2223 * "clearmatches()" function
2224 */
2225 static void
2226f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2227{
2228#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002229 win_T *win = get_optional_window(argvars, 0);
2230
2231 if (win != NULL)
2232 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233#endif
2234}
2235
2236/*
2237 * "col(string)" function
2238 */
2239 static void
2240f_col(typval_T *argvars, typval_T *rettv)
2241{
2242 colnr_T col = 0;
2243 pos_T *fp;
2244 int fnum = curbuf->b_fnum;
2245
2246 fp = var2fpos(&argvars[0], FALSE, &fnum);
2247 if (fp != NULL && fnum == curbuf->b_fnum)
2248 {
2249 if (fp->col == MAXCOL)
2250 {
2251 /* '> can be MAXCOL, get the length of the line then */
2252 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2253 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2254 else
2255 col = MAXCOL;
2256 }
2257 else
2258 {
2259 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002260 /* col(".") when the cursor is on the NUL at the end of the line
2261 * because of "coladd" can be seen as an extra column. */
2262 if (virtual_active() && fp == &curwin->w_cursor)
2263 {
2264 char_u *p = ml_get_cursor();
2265
2266 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2267 curwin->w_virtcol - curwin->w_cursor.coladd))
2268 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002269 int l;
2270
2271 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2272 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 }
2274 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275 }
2276 }
2277 rettv->vval.v_number = col;
2278}
2279
2280#if defined(FEAT_INS_EXPAND)
2281/*
2282 * "complete()" function
2283 */
2284 static void
2285f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2286{
2287 int startcol;
2288
2289 if ((State & INSERT) == 0)
2290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002291 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002292 return;
2293 }
2294
2295 /* Check for undo allowed here, because if something was already inserted
2296 * the line was already saved for undo and this check isn't done. */
2297 if (!undo_allowed())
2298 return;
2299
2300 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002302 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002303 return;
2304 }
2305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002306 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 if (startcol <= 0)
2308 return;
2309
2310 set_completion(startcol - 1, argvars[1].vval.v_list);
2311}
2312
2313/*
2314 * "complete_add()" function
2315 */
2316 static void
2317f_complete_add(typval_T *argvars, typval_T *rettv)
2318{
2319 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2320}
2321
2322/*
2323 * "complete_check()" function
2324 */
2325 static void
2326f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2327{
2328 int saved = RedrawingDisabled;
2329
2330 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002331 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002332 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002333 RedrawingDisabled = saved;
2334}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002335
2336/*
2337 * "complete_info()" function
2338 */
2339 static void
2340f_complete_info(typval_T *argvars, typval_T *rettv)
2341{
2342 list_T *what_list = NULL;
2343
2344 if (rettv_dict_alloc(rettv) != OK)
2345 return;
2346
2347 if (argvars[0].v_type != VAR_UNKNOWN)
2348 {
2349 if (argvars[0].v_type != VAR_LIST)
2350 {
2351 emsg(_(e_listreq));
2352 return;
2353 }
2354 what_list = argvars[0].vval.v_list;
2355 }
2356 get_complete_info(what_list, rettv->vval.v_dict);
2357}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358#endif
2359
2360/*
2361 * "confirm(message, buttons[, default [, type]])" function
2362 */
2363 static void
2364f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2365{
2366#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2367 char_u *message;
2368 char_u *buttons = NULL;
2369 char_u buf[NUMBUFLEN];
2370 char_u buf2[NUMBUFLEN];
2371 int def = 1;
2372 int type = VIM_GENERIC;
2373 char_u *typestr;
2374 int error = FALSE;
2375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002376 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 if (message == NULL)
2378 error = TRUE;
2379 if (argvars[1].v_type != VAR_UNKNOWN)
2380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002381 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 if (buttons == NULL)
2383 error = TRUE;
2384 if (argvars[2].v_type != VAR_UNKNOWN)
2385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002386 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 if (argvars[3].v_type != VAR_UNKNOWN)
2388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002389 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 if (typestr == NULL)
2391 error = TRUE;
2392 else
2393 {
2394 switch (TOUPPER_ASC(*typestr))
2395 {
2396 case 'E': type = VIM_ERROR; break;
2397 case 'Q': type = VIM_QUESTION; break;
2398 case 'I': type = VIM_INFO; break;
2399 case 'W': type = VIM_WARNING; break;
2400 case 'G': type = VIM_GENERIC; break;
2401 }
2402 }
2403 }
2404 }
2405 }
2406
2407 if (buttons == NULL || *buttons == NUL)
2408 buttons = (char_u *)_("&Ok");
2409
2410 if (!error)
2411 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2412 def, NULL, FALSE);
2413#endif
2414}
2415
2416/*
2417 * "copy()" function
2418 */
2419 static void
2420f_copy(typval_T *argvars, typval_T *rettv)
2421{
2422 item_copy(&argvars[0], rettv, FALSE, 0);
2423}
2424
2425#ifdef FEAT_FLOAT
2426/*
2427 * "cos()" function
2428 */
2429 static void
2430f_cos(typval_T *argvars, typval_T *rettv)
2431{
2432 float_T f = 0.0;
2433
2434 rettv->v_type = VAR_FLOAT;
2435 if (get_float_arg(argvars, &f) == OK)
2436 rettv->vval.v_float = cos(f);
2437 else
2438 rettv->vval.v_float = 0.0;
2439}
2440
2441/*
2442 * "cosh()" function
2443 */
2444 static void
2445f_cosh(typval_T *argvars, typval_T *rettv)
2446{
2447 float_T f = 0.0;
2448
2449 rettv->v_type = VAR_FLOAT;
2450 if (get_float_arg(argvars, &f) == OK)
2451 rettv->vval.v_float = cosh(f);
2452 else
2453 rettv->vval.v_float = 0.0;
2454}
2455#endif
2456
2457/*
2458 * "count()" function
2459 */
2460 static void
2461f_count(typval_T *argvars, typval_T *rettv)
2462{
2463 long n = 0;
2464 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002465 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466
Bram Moolenaar9966b212017-07-28 16:46:57 +02002467 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002468 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002469
2470 if (argvars[0].v_type == VAR_STRING)
2471 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002472 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002473 char_u *p = argvars[0].vval.v_string;
2474 char_u *next;
2475
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002476 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002477 {
2478 if (ic)
2479 {
2480 size_t len = STRLEN(expr);
2481
2482 while (*p != NUL)
2483 {
2484 if (MB_STRNICMP(p, expr, len) == 0)
2485 {
2486 ++n;
2487 p += len;
2488 }
2489 else
2490 MB_PTR_ADV(p);
2491 }
2492 }
2493 else
2494 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2495 != NULL)
2496 {
2497 ++n;
2498 p = next + STRLEN(expr);
2499 }
2500 }
2501
2502 }
2503 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002504 {
2505 listitem_T *li;
2506 list_T *l;
2507 long idx;
2508
2509 if ((l = argvars[0].vval.v_list) != NULL)
2510 {
2511 li = l->lv_first;
2512 if (argvars[2].v_type != VAR_UNKNOWN)
2513 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (argvars[3].v_type != VAR_UNKNOWN)
2515 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002516 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517 if (!error)
2518 {
2519 li = list_find(l, idx);
2520 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 }
2523 }
2524 if (error)
2525 li = NULL;
2526 }
2527
2528 for ( ; li != NULL; li = li->li_next)
2529 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2530 ++n;
2531 }
2532 }
2533 else if (argvars[0].v_type == VAR_DICT)
2534 {
2535 int todo;
2536 dict_T *d;
2537 hashitem_T *hi;
2538
2539 if ((d = argvars[0].vval.v_dict) != NULL)
2540 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (argvars[2].v_type != VAR_UNKNOWN)
2542 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002544 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 }
2546
2547 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2548 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2549 {
2550 if (!HASHITEM_EMPTY(hi))
2551 {
2552 --todo;
2553 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2554 ++n;
2555 }
2556 }
2557 }
2558 }
2559 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002561 rettv->vval.v_number = n;
2562}
2563
2564/*
2565 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2566 *
2567 * Checks the existence of a cscope connection.
2568 */
2569 static void
2570f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2571{
2572#ifdef FEAT_CSCOPE
2573 int num = 0;
2574 char_u *dbpath = NULL;
2575 char_u *prepend = NULL;
2576 char_u buf[NUMBUFLEN];
2577
2578 if (argvars[0].v_type != VAR_UNKNOWN
2579 && argvars[1].v_type != VAR_UNKNOWN)
2580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002581 num = (int)tv_get_number(&argvars[0]);
2582 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002584 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002585 }
2586
2587 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2588#endif
2589}
2590
2591/*
2592 * "cursor(lnum, col)" function, or
2593 * "cursor(list)"
2594 *
2595 * Moves the cursor to the specified line and column.
2596 * Returns 0 when the position could be set, -1 otherwise.
2597 */
2598 static void
2599f_cursor(typval_T *argvars, typval_T *rettv)
2600{
2601 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 int set_curswant = TRUE;
2604
2605 rettv->vval.v_number = -1;
2606 if (argvars[1].v_type == VAR_UNKNOWN)
2607 {
2608 pos_T pos;
2609 colnr_T curswant = -1;
2610
2611 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 return;
2615 }
2616 line = pos.lnum;
2617 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (curswant >= 0)
2620 {
2621 curwin->w_curswant = curswant - 1;
2622 set_curswant = FALSE;
2623 }
2624 }
2625 else
2626 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002627 line = tv_get_lnum(argvars);
2628 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002630 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002632 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002633 return; /* type error; errmsg already given */
2634 if (line > 0)
2635 curwin->w_cursor.lnum = line;
2636 if (col > 0)
2637 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639
2640 /* Make sure the cursor is in a valid position. */
2641 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 /* Correct cursor for multi-byte character. */
2643 if (has_mbyte)
2644 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002645
2646 curwin->w_set_curswant = set_curswant;
2647 rettv->vval.v_number = 0;
2648}
2649
Bram Moolenaar4f974752019-02-17 17:44:42 +01002650#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002651/*
2652 * "debugbreak()" function
2653 */
2654 static void
2655f_debugbreak(typval_T *argvars, typval_T *rettv)
2656{
2657 int pid;
2658
2659 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002661 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002662 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002663 else
2664 {
2665 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2666
2667 if (hProcess != NULL)
2668 {
2669 DebugBreakProcess(hProcess);
2670 CloseHandle(hProcess);
2671 rettv->vval.v_number = OK;
2672 }
2673 }
2674}
2675#endif
2676
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677/*
2678 * "deepcopy()" function
2679 */
2680 static void
2681f_deepcopy(typval_T *argvars, typval_T *rettv)
2682{
2683 int noref = 0;
2684 int copyID;
2685
2686 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002689 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 else
2691 {
2692 copyID = get_copyID();
2693 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2694 }
2695}
2696
2697/*
2698 * "delete()" function
2699 */
2700 static void
2701f_delete(typval_T *argvars, typval_T *rettv)
2702{
2703 char_u nbuf[NUMBUFLEN];
2704 char_u *name;
2705 char_u *flags;
2706
2707 rettv->vval.v_number = -1;
2708 if (check_restricted() || check_secure())
2709 return;
2710
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002711 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 if (name == NULL || *name == NUL)
2713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002714 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 return;
2716 }
2717
2718 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002719 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002720 else
2721 flags = (char_u *)"";
2722
2723 if (*flags == NUL)
2724 /* delete a file */
2725 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2726 else if (STRCMP(flags, "d") == 0)
2727 /* delete an empty directory */
2728 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2729 else if (STRCMP(flags, "rf") == 0)
2730 /* delete a directory recursively */
2731 rettv->vval.v_number = delete_recursive(name);
2732 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002733 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002734}
2735
2736/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002737 * "deletebufline()" function
2738 */
2739 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002740f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002741{
2742 buf_T *buf;
2743 linenr_T first, last;
2744 linenr_T lnum;
2745 long count;
2746 int is_curbuf;
2747 buf_T *curbuf_save = NULL;
2748 win_T *curwin_save = NULL;
2749 tabpage_T *tp;
2750 win_T *wp;
2751
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002752 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002753 if (buf == NULL)
2754 {
2755 rettv->vval.v_number = 1; /* FAIL */
2756 return;
2757 }
2758 is_curbuf = buf == curbuf;
2759
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002760 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002761 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002762 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002763 else
2764 last = first;
2765
2766 if (buf->b_ml.ml_mfp == NULL || first < 1
2767 || first > buf->b_ml.ml_line_count || last < first)
2768 {
2769 rettv->vval.v_number = 1; /* FAIL */
2770 return;
2771 }
2772
2773 if (!is_curbuf)
2774 {
2775 curbuf_save = curbuf;
2776 curwin_save = curwin;
2777 curbuf = buf;
2778 find_win_for_curbuf();
2779 }
2780 if (last > curbuf->b_ml.ml_line_count)
2781 last = curbuf->b_ml.ml_line_count;
2782 count = last - first + 1;
2783
2784 // When coming here from Insert mode, sync undo, so that this can be
2785 // undone separately from what was previously inserted.
2786 if (u_sync_once == 2)
2787 {
2788 u_sync_once = 1; // notify that u_sync() was called
2789 u_sync(TRUE);
2790 }
2791
2792 if (u_save(first - 1, last + 1) == FAIL)
2793 {
2794 rettv->vval.v_number = 1; /* FAIL */
2795 return;
2796 }
2797
2798 for (lnum = first; lnum <= last; ++lnum)
2799 ml_delete(first, TRUE);
2800
2801 FOR_ALL_TAB_WINDOWS(tp, wp)
2802 if (wp->w_buffer == buf)
2803 {
2804 if (wp->w_cursor.lnum > last)
2805 wp->w_cursor.lnum -= count;
2806 else if (wp->w_cursor.lnum> first)
2807 wp->w_cursor.lnum = first;
2808 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2809 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2810 }
2811 check_cursor_col();
2812 deleted_lines_mark(first, count);
2813
2814 if (!is_curbuf)
2815 {
2816 curbuf = curbuf_save;
2817 curwin = curwin_save;
2818 }
2819}
2820
2821/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 * "did_filetype()" function
2823 */
2824 static void
2825f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2826{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002828}
2829
2830/*
2831 * "diff_filler()" function
2832 */
2833 static void
2834f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2835{
2836#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002837 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838#endif
2839}
2840
2841/*
2842 * "diff_hlID()" function
2843 */
2844 static void
2845f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2846{
2847#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002848 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002849 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002850 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 static int fnum = 0;
2852 static int change_start = 0;
2853 static int change_end = 0;
2854 static hlf_T hlID = (hlf_T)0;
2855 int filler_lines;
2856 int col;
2857
2858 if (lnum < 0) /* ignore type error in {lnum} arg */
2859 lnum = 0;
2860 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002861 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002862 || fnum != curbuf->b_fnum)
2863 {
2864 /* New line, buffer, change: need to get the values. */
2865 filler_lines = diff_check(curwin, lnum);
2866 if (filler_lines < 0)
2867 {
2868 if (filler_lines == -1)
2869 {
2870 change_start = MAXCOL;
2871 change_end = -1;
2872 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2873 hlID = HLF_ADD; /* added line */
2874 else
2875 hlID = HLF_CHD; /* changed line */
2876 }
2877 else
2878 hlID = HLF_ADD; /* added line */
2879 }
2880 else
2881 hlID = (hlf_T)0;
2882 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002883 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002884 fnum = curbuf->b_fnum;
2885 }
2886
2887 if (hlID == HLF_CHD || hlID == HLF_TXD)
2888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002889 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 if (col >= change_start && col <= change_end)
2891 hlID = HLF_TXD; /* changed text */
2892 else
2893 hlID = HLF_CHD; /* changed line */
2894 }
2895 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2896#endif
2897}
2898
2899/*
2900 * "empty({expr})" function
2901 */
2902 static void
2903f_empty(typval_T *argvars, typval_T *rettv)
2904{
2905 int n = FALSE;
2906
2907 switch (argvars[0].v_type)
2908 {
2909 case VAR_STRING:
2910 case VAR_FUNC:
2911 n = argvars[0].vval.v_string == NULL
2912 || *argvars[0].vval.v_string == NUL;
2913 break;
2914 case VAR_PARTIAL:
2915 n = FALSE;
2916 break;
2917 case VAR_NUMBER:
2918 n = argvars[0].vval.v_number == 0;
2919 break;
2920 case VAR_FLOAT:
2921#ifdef FEAT_FLOAT
2922 n = argvars[0].vval.v_float == 0.0;
2923 break;
2924#endif
2925 case VAR_LIST:
2926 n = argvars[0].vval.v_list == NULL
2927 || argvars[0].vval.v_list->lv_first == NULL;
2928 break;
2929 case VAR_DICT:
2930 n = argvars[0].vval.v_dict == NULL
2931 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2932 break;
2933 case VAR_SPECIAL:
2934 n = argvars[0].vval.v_number != VVAL_TRUE;
2935 break;
2936
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002937 case VAR_BLOB:
2938 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002939 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2940 break;
2941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 case VAR_JOB:
2943#ifdef FEAT_JOB_CHANNEL
2944 n = argvars[0].vval.v_job == NULL
2945 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2946 break;
2947#endif
2948 case VAR_CHANNEL:
2949#ifdef FEAT_JOB_CHANNEL
2950 n = argvars[0].vval.v_channel == NULL
2951 || !channel_is_open(argvars[0].vval.v_channel);
2952 break;
2953#endif
2954 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002955 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 n = TRUE;
2957 break;
2958 }
2959
2960 rettv->vval.v_number = n;
2961}
2962
2963/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002964 * "environ()" function
2965 */
2966 static void
2967f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2968{
2969#if !defined(AMIGA)
2970 int i = 0;
2971 char_u *entry, *value;
2972# ifdef MSWIN
2973 extern wchar_t **_wenviron;
2974# else
2975 extern char **environ;
2976# endif
2977
2978 if (rettv_dict_alloc(rettv) != OK)
2979 return;
2980
2981# ifdef MSWIN
2982 if (*_wenviron == NULL)
2983 return;
2984# else
2985 if (*environ == NULL)
2986 return;
2987# endif
2988
2989 for (i = 0; ; ++i)
2990 {
2991# ifdef MSWIN
2992 short_u *p;
2993
2994 if ((p = (short_u *)_wenviron[i]) == NULL)
2995 return;
2996 entry = utf16_to_enc(p, NULL);
2997# else
2998 if ((entry = (char_u *)environ[i]) == NULL)
2999 return;
3000 entry = vim_strsave(entry);
3001# endif
3002 if (entry == NULL) // out of memory
3003 return;
3004 if ((value = vim_strchr(entry, '=')) == NULL)
3005 {
3006 vim_free(entry);
3007 continue;
3008 }
3009 *value++ = NUL;
3010 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3011 vim_free(entry);
3012 }
3013#endif
3014}
3015
3016/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 * "escape({string}, {chars})" function
3018 */
3019 static void
3020f_escape(typval_T *argvars, typval_T *rettv)
3021{
3022 char_u buf[NUMBUFLEN];
3023
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003024 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3025 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 rettv->v_type = VAR_STRING;
3027}
3028
3029/*
3030 * "eval()" function
3031 */
3032 static void
3033f_eval(typval_T *argvars, typval_T *rettv)
3034{
3035 char_u *s, *p;
3036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 if (s != NULL)
3039 s = skipwhite(s);
3040
3041 p = s;
3042 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3043 {
3044 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003045 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 need_clr_eos = FALSE;
3047 rettv->v_type = VAR_NUMBER;
3048 rettv->vval.v_number = 0;
3049 }
3050 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052}
3053
3054/*
3055 * "eventhandler()" function
3056 */
3057 static void
3058f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3059{
3060 rettv->vval.v_number = vgetc_busy;
3061}
3062
3063/*
3064 * "executable()" function
3065 */
3066 static void
3067f_executable(typval_T *argvars, typval_T *rettv)
3068{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003069 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003070
3071 /* Check in $PATH and also check directly if there is a directory name. */
3072 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3073 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3074}
3075
3076static garray_T redir_execute_ga;
3077
3078/*
3079 * Append "value[value_len]" to the execute() output.
3080 */
3081 void
3082execute_redir_str(char_u *value, int value_len)
3083{
3084 int len;
3085
3086 if (value_len == -1)
3087 len = (int)STRLEN(value); /* Append the entire string */
3088 else
3089 len = value_len; /* Append only "value_len" characters */
3090 if (ga_grow(&redir_execute_ga, len) == OK)
3091 {
3092 mch_memmove((char *)redir_execute_ga.ga_data
3093 + redir_execute_ga.ga_len, value, len);
3094 redir_execute_ga.ga_len += len;
3095 }
3096}
3097
3098/*
3099 * Get next line from a list.
3100 * Called by do_cmdline() to get the next line.
3101 * Returns allocated string, or NULL for end of function.
3102 */
3103
3104 static char_u *
3105get_list_line(
3106 int c UNUSED,
3107 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003108 int indent UNUSED,
3109 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110{
3111 listitem_T **p = (listitem_T **)cookie;
3112 listitem_T *item = *p;
3113 char_u buf[NUMBUFLEN];
3114 char_u *s;
3115
3116 if (item == NULL)
3117 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 *p = item->li_next;
3120 return s == NULL ? NULL : vim_strsave(s);
3121}
3122
3123/*
3124 * "execute()" function
3125 */
3126 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003127execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128{
3129 char_u *cmd = NULL;
3130 list_T *list = NULL;
3131 int save_msg_silent = msg_silent;
3132 int save_emsg_silent = emsg_silent;
3133 int save_emsg_noredir = emsg_noredir;
3134 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003135 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003137 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003138 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139
3140 rettv->vval.v_string = NULL;
3141 rettv->v_type = VAR_STRING;
3142
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 if (list == NULL || list->lv_first == NULL)
3147 /* empty list, no commands, empty output */
3148 return;
3149 ++list->lv_refcount;
3150 }
3151 else
3152 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003153 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003154 if (cmd == NULL)
3155 return;
3156 }
3157
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003158 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003159 {
3160 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003161 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162
3163 if (s == NULL)
3164 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003165 if (*s == NUL)
3166 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 if (STRNCMP(s, "silent", 6) == 0)
3168 ++msg_silent;
3169 if (STRCMP(s, "silent!") == 0)
3170 {
3171 emsg_silent = TRUE;
3172 emsg_noredir = TRUE;
3173 }
3174 }
3175 else
3176 ++msg_silent;
3177
3178 if (redir_execute)
3179 save_ga = redir_execute_ga;
3180 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3181 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003182 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003183 if (!echo_output)
3184 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185
3186 if (cmd != NULL)
3187 do_cmdline_cmd(cmd);
3188 else
3189 {
3190 listitem_T *item = list->lv_first;
3191
3192 do_cmdline(NULL, get_list_line, (void *)&item,
3193 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3194 --list->lv_refcount;
3195 }
3196
Bram Moolenaard297f352017-01-29 20:31:21 +01003197 /* Need to append a NUL to the result. */
3198 if (ga_grow(&redir_execute_ga, 1) == OK)
3199 {
3200 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3201 rettv->vval.v_string = redir_execute_ga.ga_data;
3202 }
3203 else
3204 {
3205 ga_clear(&redir_execute_ga);
3206 rettv->vval.v_string = NULL;
3207 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003208 msg_silent = save_msg_silent;
3209 emsg_silent = save_emsg_silent;
3210 emsg_noredir = save_emsg_noredir;
3211
3212 redir_execute = save_redir_execute;
3213 if (redir_execute)
3214 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003215 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003217 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003218 if (echo_output)
3219 // When not working silently: put it in column zero. A following
3220 // "echon" will overwrite the message, unavoidably.
3221 msg_col = 0;
3222 else
3223 // When working silently: Put it back where it was, since nothing
3224 // should have been written.
3225 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226}
3227
3228/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003229 * "execute()" function
3230 */
3231 static void
3232f_execute(typval_T *argvars, typval_T *rettv)
3233{
3234 execute_common(argvars, rettv, 0);
3235}
3236
3237/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003238 * "exepath()" function
3239 */
3240 static void
3241f_exepath(typval_T *argvars, typval_T *rettv)
3242{
3243 char_u *p = NULL;
3244
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003245 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 rettv->v_type = VAR_STRING;
3247 rettv->vval.v_string = p;
3248}
3249
3250/*
3251 * "exists()" function
3252 */
3253 static void
3254f_exists(typval_T *argvars, typval_T *rettv)
3255{
3256 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003259 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 if (*p == '$') /* environment variable */
3261 {
3262 /* first try "normal" environment variables (fast) */
3263 if (mch_getenv(p + 1) != NULL)
3264 n = TRUE;
3265 else
3266 {
3267 /* try expanding things like $VIM and ${HOME} */
3268 p = expand_env_save(p);
3269 if (p != NULL && *p != '$')
3270 n = TRUE;
3271 vim_free(p);
3272 }
3273 }
3274 else if (*p == '&' || *p == '+') /* option */
3275 {
3276 n = (get_option_tv(&p, NULL, TRUE) == OK);
3277 if (*skipwhite(p) != NUL)
3278 n = FALSE; /* trailing garbage */
3279 }
3280 else if (*p == '*') /* internal or user defined function */
3281 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003282 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 }
3284 else if (*p == ':')
3285 {
3286 n = cmd_exists(p + 1);
3287 }
3288 else if (*p == '#')
3289 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 if (p[1] == '#')
3291 n = autocmd_supported(p + 2);
3292 else
3293 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 }
3295 else /* internal variable */
3296 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003297 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 }
3299
3300 rettv->vval.v_number = n;
3301}
3302
3303#ifdef FEAT_FLOAT
3304/*
3305 * "exp()" function
3306 */
3307 static void
3308f_exp(typval_T *argvars, typval_T *rettv)
3309{
3310 float_T f = 0.0;
3311
3312 rettv->v_type = VAR_FLOAT;
3313 if (get_float_arg(argvars, &f) == OK)
3314 rettv->vval.v_float = exp(f);
3315 else
3316 rettv->vval.v_float = 0.0;
3317}
3318#endif
3319
3320/*
3321 * "expand()" function
3322 */
3323 static void
3324f_expand(typval_T *argvars, typval_T *rettv)
3325{
3326 char_u *s;
3327 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003328 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3330 expand_T xpc;
3331 int error = FALSE;
3332 char_u *result;
3333
3334 rettv->v_type = VAR_STRING;
3335 if (argvars[1].v_type != VAR_UNKNOWN
3336 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003339 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003341 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 if (*s == '%' || *s == '#' || *s == '<')
3343 {
3344 ++emsg_off;
3345 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3346 --emsg_off;
3347 if (rettv->v_type == VAR_LIST)
3348 {
3349 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3350 list_append_string(rettv->vval.v_list, result, -1);
3351 else
3352 vim_free(result);
3353 }
3354 else
3355 rettv->vval.v_string = result;
3356 }
3357 else
3358 {
3359 /* When the optional second argument is non-zero, don't remove matches
3360 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3361 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003362 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 options |= WILD_KEEP_ALL;
3364 if (!error)
3365 {
3366 ExpandInit(&xpc);
3367 xpc.xp_context = EXPAND_FILES;
3368 if (p_wic)
3369 options += WILD_ICASE;
3370 if (rettv->v_type == VAR_STRING)
3371 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3372 options, WILD_ALL);
3373 else if (rettv_list_alloc(rettv) != FAIL)
3374 {
3375 int i;
3376
3377 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3378 for (i = 0; i < xpc.xp_numfiles; i++)
3379 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3380 ExpandCleanup(&xpc);
3381 }
3382 }
3383 else
3384 rettv->vval.v_string = NULL;
3385 }
3386}
3387
3388/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003389 * "expandcmd()" function
3390 * Expand all the special characters in a command string.
3391 */
3392 static void
3393f_expandcmd(typval_T *argvars, typval_T *rettv)
3394{
3395 exarg_T eap;
3396 char_u *cmdstr;
3397 char *errormsg = NULL;
3398
3399 rettv->v_type = VAR_STRING;
3400 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3401
3402 memset(&eap, 0, sizeof(eap));
3403 eap.cmd = cmdstr;
3404 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003405 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003406 eap.usefilter = FALSE;
3407 eap.nextcmd = NULL;
3408 eap.cmdidx = CMD_USER;
3409
3410 expand_filename(&eap, &cmdstr, &errormsg);
3411 if (errormsg != NULL && *errormsg != NUL)
3412 emsg(errormsg);
3413
3414 rettv->vval.v_string = cmdstr;
3415}
3416
3417/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 * "extend(list, list [, idx])" function
3419 * "extend(dict, dict [, action])" function
3420 */
3421 static void
3422f_extend(typval_T *argvars, typval_T *rettv)
3423{
3424 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3425
3426 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3427 {
3428 list_T *l1, *l2;
3429 listitem_T *item;
3430 long before;
3431 int error = FALSE;
3432
3433 l1 = argvars[0].vval.v_list;
3434 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003435 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 && l2 != NULL)
3437 {
3438 if (argvars[2].v_type != VAR_UNKNOWN)
3439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003440 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 if (error)
3442 return; /* type error; errmsg already given */
3443
3444 if (before == l1->lv_len)
3445 item = NULL;
3446 else
3447 {
3448 item = list_find(l1, before);
3449 if (item == NULL)
3450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003451 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452 return;
3453 }
3454 }
3455 }
3456 else
3457 item = NULL;
3458 list_extend(l1, l2, item);
3459
3460 copy_tv(&argvars[0], rettv);
3461 }
3462 }
3463 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3464 {
3465 dict_T *d1, *d2;
3466 char_u *action;
3467 int i;
3468
3469 d1 = argvars[0].vval.v_dict;
3470 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003471 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 && d2 != NULL)
3473 {
3474 /* Check the third argument. */
3475 if (argvars[2].v_type != VAR_UNKNOWN)
3476 {
3477 static char *(av[]) = {"keep", "force", "error"};
3478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003479 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003480 if (action == NULL)
3481 return; /* type error; errmsg already given */
3482 for (i = 0; i < 3; ++i)
3483 if (STRCMP(action, av[i]) == 0)
3484 break;
3485 if (i == 3)
3486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 return;
3489 }
3490 }
3491 else
3492 action = (char_u *)"force";
3493
3494 dict_extend(d1, d2, action);
3495
3496 copy_tv(&argvars[0], rettv);
3497 }
3498 }
3499 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003500 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501}
3502
3503/*
3504 * "feedkeys()" function
3505 */
3506 static void
3507f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3508{
3509 int remap = TRUE;
3510 int insert = FALSE;
3511 char_u *keys, *flags;
3512 char_u nbuf[NUMBUFLEN];
3513 int typed = FALSE;
3514 int execute = FALSE;
3515 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003516 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003517 char_u *keys_esc;
3518
3519 /* This is not allowed in the sandbox. If the commands would still be
3520 * executed in the sandbox it would be OK, but it probably happens later,
3521 * when "sandbox" is no longer set. */
3522 if (check_secure())
3523 return;
3524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526
3527 if (argvars[1].v_type != VAR_UNKNOWN)
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 for ( ; *flags != NUL; ++flags)
3531 {
3532 switch (*flags)
3533 {
3534 case 'n': remap = FALSE; break;
3535 case 'm': remap = TRUE; break;
3536 case 't': typed = TRUE; break;
3537 case 'i': insert = TRUE; break;
3538 case 'x': execute = TRUE; break;
3539 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003540 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 }
3542 }
3543 }
3544
3545 if (*keys != NUL || execute)
3546 {
3547 /* Need to escape K_SPECIAL and CSI before putting the string in the
3548 * typeahead buffer. */
3549 keys_esc = vim_strsave_escape_csi(keys);
3550 if (keys_esc != NULL)
3551 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003552 if (lowlevel)
3553 {
3554#ifdef USE_INPUT_BUF
3555 add_to_input_buf(keys, (int)STRLEN(keys));
3556#else
3557 emsg(_("E980: lowlevel input not supported"));
3558#endif
3559 }
3560 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003561 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003562 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003563 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003564 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003565#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003566 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003567#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003568 )
3569 typebuf_was_filled = TRUE;
3570 }
3571 vim_free(keys_esc);
3572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573 if (execute)
3574 {
3575 int save_msg_scroll = msg_scroll;
3576
3577 /* Avoid a 1 second delay when the keys start Insert mode. */
3578 msg_scroll = FALSE;
3579
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003580 if (!dangerous)
3581 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003582 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003583 if (!dangerous)
3584 --ex_normal_busy;
3585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 msg_scroll |= save_msg_scroll;
3587 }
3588 }
3589 }
3590}
3591
3592/*
3593 * "filereadable()" function
3594 */
3595 static void
3596f_filereadable(typval_T *argvars, typval_T *rettv)
3597{
3598 int fd;
3599 char_u *p;
3600 int n;
3601
3602#ifndef O_NONBLOCK
3603# define O_NONBLOCK 0
3604#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003605 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3607 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3608 {
3609 n = TRUE;
3610 close(fd);
3611 }
3612 else
3613 n = FALSE;
3614
3615 rettv->vval.v_number = n;
3616}
3617
3618/*
3619 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3620 * rights to write into.
3621 */
3622 static void
3623f_filewritable(typval_T *argvars, typval_T *rettv)
3624{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003625 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626}
3627
3628 static void
3629findfilendir(
3630 typval_T *argvars UNUSED,
3631 typval_T *rettv,
3632 int find_what UNUSED)
3633{
3634#ifdef FEAT_SEARCHPATH
3635 char_u *fname;
3636 char_u *fresult = NULL;
3637 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3638 char_u *p;
3639 char_u pathbuf[NUMBUFLEN];
3640 int count = 1;
3641 int first = TRUE;
3642 int error = FALSE;
3643#endif
3644
3645 rettv->vval.v_string = NULL;
3646 rettv->v_type = VAR_STRING;
3647
3648#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003649 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650
3651 if (argvars[1].v_type != VAR_UNKNOWN)
3652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 if (p == NULL)
3655 error = TRUE;
3656 else
3657 {
3658 if (*p != NUL)
3659 path = p;
3660
3661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003662 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663 }
3664 }
3665
3666 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3667 error = TRUE;
3668
3669 if (*fname != NUL && !error)
3670 {
3671 do
3672 {
3673 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3674 vim_free(fresult);
3675 fresult = find_file_in_path_option(first ? fname : NULL,
3676 first ? (int)STRLEN(fname) : 0,
3677 0, first, path,
3678 find_what,
3679 curbuf->b_ffname,
3680 find_what == FINDFILE_DIR
3681 ? (char_u *)"" : curbuf->b_p_sua);
3682 first = FALSE;
3683
3684 if (fresult != NULL && rettv->v_type == VAR_LIST)
3685 list_append_string(rettv->vval.v_list, fresult, -1);
3686
3687 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3688 }
3689
3690 if (rettv->v_type == VAR_STRING)
3691 rettv->vval.v_string = fresult;
3692#endif
3693}
3694
3695/*
3696 * "filter()" function
3697 */
3698 static void
3699f_filter(typval_T *argvars, typval_T *rettv)
3700{
3701 filter_map(argvars, rettv, FALSE);
3702}
3703
3704/*
3705 * "finddir({fname}[, {path}[, {count}]])" function
3706 */
3707 static void
3708f_finddir(typval_T *argvars, typval_T *rettv)
3709{
3710 findfilendir(argvars, rettv, FINDFILE_DIR);
3711}
3712
3713/*
3714 * "findfile({fname}[, {path}[, {count}]])" function
3715 */
3716 static void
3717f_findfile(typval_T *argvars, typval_T *rettv)
3718{
3719 findfilendir(argvars, rettv, FINDFILE_FILE);
3720}
3721
3722#ifdef FEAT_FLOAT
3723/*
3724 * "float2nr({float})" function
3725 */
3726 static void
3727f_float2nr(typval_T *argvars, typval_T *rettv)
3728{
3729 float_T f = 0.0;
3730
3731 if (get_float_arg(argvars, &f) == OK)
3732 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003733 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003734 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003735 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003736 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 else
3738 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 }
3740}
3741
3742/*
3743 * "floor({float})" function
3744 */
3745 static void
3746f_floor(typval_T *argvars, typval_T *rettv)
3747{
3748 float_T f = 0.0;
3749
3750 rettv->v_type = VAR_FLOAT;
3751 if (get_float_arg(argvars, &f) == OK)
3752 rettv->vval.v_float = floor(f);
3753 else
3754 rettv->vval.v_float = 0.0;
3755}
3756
3757/*
3758 * "fmod()" function
3759 */
3760 static void
3761f_fmod(typval_T *argvars, typval_T *rettv)
3762{
3763 float_T fx = 0.0, fy = 0.0;
3764
3765 rettv->v_type = VAR_FLOAT;
3766 if (get_float_arg(argvars, &fx) == OK
3767 && get_float_arg(&argvars[1], &fy) == OK)
3768 rettv->vval.v_float = fmod(fx, fy);
3769 else
3770 rettv->vval.v_float = 0.0;
3771}
3772#endif
3773
3774/*
3775 * "fnameescape({string})" function
3776 */
3777 static void
3778f_fnameescape(typval_T *argvars, typval_T *rettv)
3779{
3780 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003781 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782 rettv->v_type = VAR_STRING;
3783}
3784
3785/*
3786 * "fnamemodify({fname}, {mods})" function
3787 */
3788 static void
3789f_fnamemodify(typval_T *argvars, typval_T *rettv)
3790{
3791 char_u *fname;
3792 char_u *mods;
3793 int usedlen = 0;
3794 int len;
3795 char_u *fbuf = NULL;
3796 char_u buf[NUMBUFLEN];
3797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003798 fname = tv_get_string_chk(&argvars[0]);
3799 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003800 if (fname == NULL || mods == NULL)
3801 fname = NULL;
3802 else
3803 {
3804 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003805 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 }
3807
3808 rettv->v_type = VAR_STRING;
3809 if (fname == NULL)
3810 rettv->vval.v_string = NULL;
3811 else
3812 rettv->vval.v_string = vim_strnsave(fname, len);
3813 vim_free(fbuf);
3814}
3815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003816/*
3817 * "foldclosed()" function
3818 */
3819 static void
3820foldclosed_both(
3821 typval_T *argvars UNUSED,
3822 typval_T *rettv,
3823 int end UNUSED)
3824{
3825#ifdef FEAT_FOLDING
3826 linenr_T lnum;
3827 linenr_T first, last;
3828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003829 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3831 {
3832 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3833 {
3834 if (end)
3835 rettv->vval.v_number = (varnumber_T)last;
3836 else
3837 rettv->vval.v_number = (varnumber_T)first;
3838 return;
3839 }
3840 }
3841#endif
3842 rettv->vval.v_number = -1;
3843}
3844
3845/*
3846 * "foldclosed()" function
3847 */
3848 static void
3849f_foldclosed(typval_T *argvars, typval_T *rettv)
3850{
3851 foldclosed_both(argvars, rettv, FALSE);
3852}
3853
3854/*
3855 * "foldclosedend()" function
3856 */
3857 static void
3858f_foldclosedend(typval_T *argvars, typval_T *rettv)
3859{
3860 foldclosed_both(argvars, rettv, TRUE);
3861}
3862
3863/*
3864 * "foldlevel()" function
3865 */
3866 static void
3867f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3868{
3869#ifdef FEAT_FOLDING
3870 linenr_T lnum;
3871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003872 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3874 rettv->vval.v_number = foldLevel(lnum);
3875#endif
3876}
3877
3878/*
3879 * "foldtext()" function
3880 */
3881 static void
3882f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3883{
3884#ifdef FEAT_FOLDING
3885 linenr_T foldstart;
3886 linenr_T foldend;
3887 char_u *dashes;
3888 linenr_T lnum;
3889 char_u *s;
3890 char_u *r;
3891 int len;
3892 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003893 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894#endif
3895
3896 rettv->v_type = VAR_STRING;
3897 rettv->vval.v_string = NULL;
3898#ifdef FEAT_FOLDING
3899 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3900 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3901 dashes = get_vim_var_str(VV_FOLDDASHES);
3902 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3903 && dashes != NULL)
3904 {
3905 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003906 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003907 if (!linewhite(lnum))
3908 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909
3910 /* Find interesting text in this line. */
3911 s = skipwhite(ml_get(lnum));
3912 /* skip C comment-start */
3913 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3914 {
3915 s = skipwhite(s + 2);
3916 if (*skipwhite(s) == NUL
3917 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3918 {
3919 s = skipwhite(ml_get(lnum + 1));
3920 if (*s == '*')
3921 s = skipwhite(s + 1);
3922 }
3923 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003924 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003925 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003926 r = alloc(STRLEN(txt)
3927 + STRLEN(dashes) // for %s
3928 + 20 // for %3ld
3929 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 if (r != NULL)
3931 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003932 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 len = (int)STRLEN(r);
3934 STRCAT(r, s);
3935 /* remove 'foldmarker' and 'commentstring' */
3936 foldtext_cleanup(r + len);
3937 rettv->vval.v_string = r;
3938 }
3939 }
3940#endif
3941}
3942
3943/*
3944 * "foldtextresult(lnum)" function
3945 */
3946 static void
3947f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3948{
3949#ifdef FEAT_FOLDING
3950 linenr_T lnum;
3951 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003952 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 foldinfo_T foldinfo;
3954 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003955 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956#endif
3957
3958 rettv->v_type = VAR_STRING;
3959 rettv->vval.v_string = NULL;
3960#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003961 if (entered)
3962 return; /* reject recursive use */
3963 entered = TRUE;
3964
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003965 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966 /* treat illegal types and illegal string values for {lnum} the same */
3967 if (lnum < 0)
3968 lnum = 0;
3969 fold_count = foldedCount(curwin, lnum, &foldinfo);
3970 if (fold_count > 0)
3971 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003972 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3973 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974 if (text == buf)
3975 text = vim_strsave(text);
3976 rettv->vval.v_string = text;
3977 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003978
3979 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003980#endif
3981}
3982
3983/*
3984 * "foreground()" function
3985 */
3986 static void
3987f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3988{
3989#ifdef FEAT_GUI
3990 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003991 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003992 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003993 return;
3994 }
3995#endif
3996#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003997 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998#endif
3999}
4000
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004002common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004003{
4004 char_u *s;
4005 char_u *name;
4006 int use_string = FALSE;
4007 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004008 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009
4010 if (argvars[0].v_type == VAR_FUNC)
4011 {
4012 /* function(MyFunc, [arg], dict) */
4013 s = argvars[0].vval.v_string;
4014 }
4015 else if (argvars[0].v_type == VAR_PARTIAL
4016 && argvars[0].vval.v_partial != NULL)
4017 {
4018 /* function(dict.MyFunc, [arg]) */
4019 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004020 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 }
4022 else
4023 {
4024 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004025 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004026 use_string = TRUE;
4027 }
4028
Bram Moolenaar843b8842016-08-21 14:36:15 +02004029 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004030 {
4031 name = s;
4032 trans_name = trans_function_name(&name, FALSE,
4033 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4034 if (*name != NUL)
4035 s = NULL;
4036 }
4037
Bram Moolenaar843b8842016-08-21 14:36:15 +02004038 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4039 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004042 else if (trans_name != NULL && (is_funcref
4043 ? find_func(trans_name) == NULL
4044 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046 else
4047 {
4048 int dict_idx = 0;
4049 int arg_idx = 0;
4050 list_T *list = NULL;
4051
4052 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4053 {
4054 char sid_buf[25];
4055 int off = *s == 's' ? 2 : 5;
4056
4057 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4058 * also be called from another script. Using trans_function_name()
4059 * would also work, but some plugins depend on the name being
4060 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004061 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004062 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (name != NULL)
4064 {
4065 STRCPY(name, sid_buf);
4066 STRCAT(name, s + off);
4067 }
4068 }
4069 else
4070 name = vim_strsave(s);
4071
4072 if (argvars[1].v_type != VAR_UNKNOWN)
4073 {
4074 if (argvars[2].v_type != VAR_UNKNOWN)
4075 {
4076 /* function(name, [args], dict) */
4077 arg_idx = 1;
4078 dict_idx = 2;
4079 }
4080 else if (argvars[1].v_type == VAR_DICT)
4081 /* function(name, dict) */
4082 dict_idx = 1;
4083 else
4084 /* function(name, [args]) */
4085 arg_idx = 1;
4086 if (dict_idx > 0)
4087 {
4088 if (argvars[dict_idx].v_type != VAR_DICT)
4089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004090 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004092 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 }
4094 if (argvars[dict_idx].vval.v_dict == NULL)
4095 dict_idx = 0;
4096 }
4097 if (arg_idx > 0)
4098 {
4099 if (argvars[arg_idx].v_type != VAR_LIST)
4100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004101 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004102 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004103 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004104 }
4105 list = argvars[arg_idx].vval.v_list;
4106 if (list == NULL || list->lv_len == 0)
4107 arg_idx = 0;
4108 }
4109 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004112 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113
4114 /* result is a VAR_PARTIAL */
4115 if (pt == NULL)
4116 vim_free(name);
4117 else
4118 {
4119 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4120 {
4121 listitem_T *li;
4122 int i = 0;
4123 int arg_len = 0;
4124 int lv_len = 0;
4125
4126 if (arg_pt != NULL)
4127 arg_len = arg_pt->pt_argc;
4128 if (list != NULL)
4129 lv_len = list->lv_len;
4130 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004131 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004132 if (pt->pt_argv == NULL)
4133 {
4134 vim_free(pt);
4135 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 for (i = 0; i < arg_len; i++)
4139 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4140 if (lv_len > 0)
4141 for (li = list->lv_first; li != NULL;
4142 li = li->li_next)
4143 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 }
4145
4146 /* For "function(dict.func, [], dict)" and "func" is a partial
4147 * use "dict". That is backwards compatible. */
4148 if (dict_idx > 0)
4149 {
4150 /* The dict is bound explicitly, pt_auto is FALSE. */
4151 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4152 ++pt->pt_dict->dv_refcount;
4153 }
4154 else if (arg_pt != NULL)
4155 {
4156 /* If the dict was bound automatically the result is also
4157 * bound automatically. */
4158 pt->pt_dict = arg_pt->pt_dict;
4159 pt->pt_auto = arg_pt->pt_auto;
4160 if (pt->pt_dict != NULL)
4161 ++pt->pt_dict->dv_refcount;
4162 }
4163
4164 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4166 {
4167 pt->pt_func = arg_pt->pt_func;
4168 func_ptr_ref(pt->pt_func);
4169 vim_free(name);
4170 }
4171 else if (is_funcref)
4172 {
4173 pt->pt_func = find_func(trans_name);
4174 func_ptr_ref(pt->pt_func);
4175 vim_free(name);
4176 }
4177 else
4178 {
4179 pt->pt_name = name;
4180 func_ref(name);
4181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 }
4183 rettv->v_type = VAR_PARTIAL;
4184 rettv->vval.v_partial = pt;
4185 }
4186 else
4187 {
4188 /* result is a VAR_FUNC */
4189 rettv->v_type = VAR_FUNC;
4190 rettv->vval.v_string = name;
4191 func_ref(name);
4192 }
4193 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194theend:
4195 vim_free(trans_name);
4196}
4197
4198/*
4199 * "funcref()" function
4200 */
4201 static void
4202f_funcref(typval_T *argvars, typval_T *rettv)
4203{
4204 common_function(argvars, rettv, TRUE);
4205}
4206
4207/*
4208 * "function()" function
4209 */
4210 static void
4211f_function(typval_T *argvars, typval_T *rettv)
4212{
4213 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214}
4215
4216/*
4217 * "garbagecollect()" function
4218 */
4219 static void
4220f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4221{
4222 /* This is postponed until we are back at the toplevel, because we may be
4223 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4224 want_garbage_collect = TRUE;
4225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004226 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004227 garbage_collect_at_exit = TRUE;
4228}
4229
4230/*
4231 * "get()" function
4232 */
4233 static void
4234f_get(typval_T *argvars, typval_T *rettv)
4235{
4236 listitem_T *li;
4237 list_T *l;
4238 dictitem_T *di;
4239 dict_T *d;
4240 typval_T *tv = NULL;
4241
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004242 if (argvars[0].v_type == VAR_BLOB)
4243 {
4244 int error = FALSE;
4245 int idx = tv_get_number_chk(&argvars[1], &error);
4246
4247 if (!error)
4248 {
4249 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004250 if (idx < 0)
4251 idx = blob_len(argvars[0].vval.v_blob) + idx;
4252 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4253 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004254 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004255 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004256 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004257 tv = rettv;
4258 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004259 }
4260 }
4261 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 {
4263 if ((l = argvars[0].vval.v_list) != NULL)
4264 {
4265 int error = FALSE;
4266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004267 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004268 if (!error && li != NULL)
4269 tv = &li->li_tv;
4270 }
4271 }
4272 else if (argvars[0].v_type == VAR_DICT)
4273 {
4274 if ((d = argvars[0].vval.v_dict) != NULL)
4275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004276 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 if (di != NULL)
4278 tv = &di->di_tv;
4279 }
4280 }
4281 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4282 {
4283 partial_T *pt;
4284 partial_T fref_pt;
4285
4286 if (argvars[0].v_type == VAR_PARTIAL)
4287 pt = argvars[0].vval.v_partial;
4288 else
4289 {
4290 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4291 fref_pt.pt_name = argvars[0].vval.v_string;
4292 pt = &fref_pt;
4293 }
4294
4295 if (pt != NULL)
4296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004297 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004298 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299
4300 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4301 {
4302 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303 n = partial_name(pt);
4304 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305 rettv->vval.v_string = NULL;
4306 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004307 {
4308 rettv->vval.v_string = vim_strsave(n);
4309 if (rettv->v_type == VAR_FUNC)
4310 func_ref(rettv->vval.v_string);
4311 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312 }
4313 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004314 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 else if (STRCMP(what, "args") == 0)
4316 {
4317 rettv->v_type = VAR_LIST;
4318 if (rettv_list_alloc(rettv) == OK)
4319 {
4320 int i;
4321
4322 for (i = 0; i < pt->pt_argc; ++i)
4323 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4324 }
4325 }
4326 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004327 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328 return;
4329 }
4330 }
4331 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004332 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004333
4334 if (tv == NULL)
4335 {
4336 if (argvars[2].v_type != VAR_UNKNOWN)
4337 copy_tv(&argvars[2], rettv);
4338 }
4339 else
4340 copy_tv(tv, rettv);
4341}
4342
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004343/*
4344 * Returns buffer options, variables and other attributes in a dictionary.
4345 */
4346 static dict_T *
4347get_buffer_info(buf_T *buf)
4348{
4349 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004350 tabpage_T *tp;
4351 win_T *wp;
4352 list_T *windows;
4353
4354 dict = dict_alloc();
4355 if (dict == NULL)
4356 return NULL;
4357
Bram Moolenaare0be1672018-07-08 16:50:37 +02004358 dict_add_number(dict, "bufnr", buf->b_fnum);
4359 dict_add_string(dict, "name", buf->b_ffname);
4360 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4361 : buflist_findlnum(buf));
4362 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4363 dict_add_number(dict, "listed", buf->b_p_bl);
4364 dict_add_number(dict, "changed", bufIsChanged(buf));
4365 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4366 dict_add_number(dict, "hidden",
4367 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004368
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004369 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004370 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004371
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004372 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004373 windows = list_alloc();
4374 if (windows != NULL)
4375 {
4376 FOR_ALL_TAB_WINDOWS(tp, wp)
4377 if (wp->w_buffer == buf)
4378 list_append_number(windows, (varnumber_T)wp->w_id);
4379 dict_add_list(dict, "windows", windows);
4380 }
4381
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004382#ifdef FEAT_TEXT_PROP
4383 // List of popup windows displaying this buffer
4384 windows = list_alloc();
4385 if (windows != NULL)
4386 {
4387 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4388 if (wp->w_buffer == buf)
4389 list_append_number(windows, (varnumber_T)wp->w_id);
4390 FOR_ALL_TABPAGES(tp)
4391 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4392 if (wp->w_buffer == buf)
4393 list_append_number(windows, (varnumber_T)wp->w_id);
4394
4395 dict_add_list(dict, "popups", windows);
4396 }
4397#endif
4398
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004399#ifdef FEAT_SIGNS
4400 if (buf->b_signlist != NULL)
4401 {
4402 /* List of signs placed in this buffer */
4403 list_T *signs = list_alloc();
4404 if (signs != NULL)
4405 {
4406 get_buffer_signs(buf, signs);
4407 dict_add_list(dict, "signs", signs);
4408 }
4409 }
4410#endif
4411
4412 return dict;
4413}
4414
4415/*
4416 * "getbufinfo()" function
4417 */
4418 static void
4419f_getbufinfo(typval_T *argvars, typval_T *rettv)
4420{
4421 buf_T *buf = NULL;
4422 buf_T *argbuf = NULL;
4423 dict_T *d;
4424 int filtered = FALSE;
4425 int sel_buflisted = FALSE;
4426 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004427 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004428
4429 if (rettv_list_alloc(rettv) != OK)
4430 return;
4431
4432 /* List of all the buffers or selected buffers */
4433 if (argvars[0].v_type == VAR_DICT)
4434 {
4435 dict_T *sel_d = argvars[0].vval.v_dict;
4436
4437 if (sel_d != NULL)
4438 {
4439 dictitem_T *di;
4440
4441 filtered = TRUE;
4442
4443 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004444 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004445 sel_buflisted = TRUE;
4446
4447 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004448 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004449 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004450
4451 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004452 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004453 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004454 }
4455 }
4456 else if (argvars[0].v_type != VAR_UNKNOWN)
4457 {
4458 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004459 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004460 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004461 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004462 --emsg_off;
4463 if (argbuf == NULL)
4464 return;
4465 }
4466
4467 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004468 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004469 {
4470 if (argbuf != NULL && argbuf != buf)
4471 continue;
4472 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004473 || (sel_buflisted && !buf->b_p_bl)
4474 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004475 continue;
4476
4477 d = get_buffer_info(buf);
4478 if (d != NULL)
4479 list_append_dict(rettv->vval.v_list, d);
4480 if (argbuf != NULL)
4481 return;
4482 }
4483}
4484
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485/*
4486 * Get line or list of lines from buffer "buf" into "rettv".
4487 * Return a range (from start to end) of lines in rettv from the specified
4488 * buffer.
4489 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4490 */
4491 static void
4492get_buffer_lines(
4493 buf_T *buf,
4494 linenr_T start,
4495 linenr_T end,
4496 int retlist,
4497 typval_T *rettv)
4498{
4499 char_u *p;
4500
4501 rettv->v_type = VAR_STRING;
4502 rettv->vval.v_string = NULL;
4503 if (retlist && rettv_list_alloc(rettv) == FAIL)
4504 return;
4505
4506 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4507 return;
4508
4509 if (!retlist)
4510 {
4511 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4512 p = ml_get_buf(buf, start, FALSE);
4513 else
4514 p = (char_u *)"";
4515 rettv->vval.v_string = vim_strsave(p);
4516 }
4517 else
4518 {
4519 if (end < start)
4520 return;
4521
4522 if (start < 1)
4523 start = 1;
4524 if (end > buf->b_ml.ml_line_count)
4525 end = buf->b_ml.ml_line_count;
4526 while (start <= end)
4527 if (list_append_string(rettv->vval.v_list,
4528 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4529 break;
4530 }
4531}
4532
4533/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534 * "getbufline()" function
4535 */
4536 static void
4537f_getbufline(typval_T *argvars, typval_T *rettv)
4538{
4539 linenr_T lnum;
4540 linenr_T end;
4541 buf_T *buf;
4542
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004543 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004545 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 --emsg_off;
4547
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004548 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 if (argvars[2].v_type == VAR_UNKNOWN)
4550 end = lnum;
4551 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004552 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553
4554 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4555}
4556
4557/*
4558 * "getbufvar()" function
4559 */
4560 static void
4561f_getbufvar(typval_T *argvars, typval_T *rettv)
4562{
4563 buf_T *buf;
4564 buf_T *save_curbuf;
4565 char_u *varname;
4566 dictitem_T *v;
4567 int done = FALSE;
4568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004569 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4570 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004572 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573
4574 rettv->v_type = VAR_STRING;
4575 rettv->vval.v_string = NULL;
4576
4577 if (buf != NULL && varname != NULL)
4578 {
4579 /* set curbuf to be our buf, temporarily */
4580 save_curbuf = curbuf;
4581 curbuf = buf;
4582
Bram Moolenaar30567352016-08-27 21:25:44 +02004583 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004585 if (varname[1] == NUL)
4586 {
4587 /* get all buffer-local options in a dict */
4588 dict_T *opts = get_winbuf_options(TRUE);
4589
4590 if (opts != NULL)
4591 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004592 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004593 done = TRUE;
4594 }
4595 }
4596 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4597 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004598 done = TRUE;
4599 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004600 else
4601 {
4602 /* Look up the variable. */
4603 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4604 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4605 'b', varname, FALSE);
4606 if (v != NULL)
4607 {
4608 copy_tv(&v->di_tv, rettv);
4609 done = TRUE;
4610 }
4611 }
4612
4613 /* restore previous notion of curbuf */
4614 curbuf = save_curbuf;
4615 }
4616
4617 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4618 /* use the default value */
4619 copy_tv(&argvars[2], rettv);
4620
4621 --emsg_off;
4622}
4623
4624/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004625 * "getchangelist()" function
4626 */
4627 static void
4628f_getchangelist(typval_T *argvars, typval_T *rettv)
4629{
4630#ifdef FEAT_JUMPLIST
4631 buf_T *buf;
4632 int i;
4633 list_T *l;
4634 dict_T *d;
4635#endif
4636
4637 if (rettv_list_alloc(rettv) != OK)
4638 return;
4639
4640#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004641 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004642 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004643 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004644 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004645 if (buf == NULL)
4646 return;
4647
4648 l = list_alloc();
4649 if (l == NULL)
4650 return;
4651
4652 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4653 return;
4654 /*
4655 * The current window change list index tracks only the position in the
4656 * current buffer change list. For other buffers, use the change list
4657 * length as the current index.
4658 */
4659 list_append_number(rettv->vval.v_list,
4660 (varnumber_T)((buf == curwin->w_buffer)
4661 ? curwin->w_changelistidx : buf->b_changelistlen));
4662
4663 for (i = 0; i < buf->b_changelistlen; ++i)
4664 {
4665 if (buf->b_changelist[i].lnum == 0)
4666 continue;
4667 if ((d = dict_alloc()) == NULL)
4668 return;
4669 if (list_append_dict(l, d) == FAIL)
4670 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004671 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4672 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004673 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004674 }
4675#endif
4676}
4677/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 * "getchar()" function
4679 */
4680 static void
4681f_getchar(typval_T *argvars, typval_T *rettv)
4682{
4683 varnumber_T n;
4684 int error = FALSE;
4685
Bram Moolenaar84d93902018-09-11 20:10:20 +02004686#ifdef MESSAGE_QUEUE
4687 // vpeekc() used to check for messages, but that caused problems, invoking
4688 // a callback where it was not expected. Some plugins use getchar(1) in a
4689 // loop to await a message, therefore make sure we check for messages here.
4690 parse_queued_messages();
4691#endif
4692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 /* Position the cursor. Needed after a message that ends in a space. */
4694 windgoto(msg_row, msg_col);
4695
4696 ++no_mapping;
4697 ++allow_keys;
4698 for (;;)
4699 {
4700 if (argvars[0].v_type == VAR_UNKNOWN)
4701 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004702 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004703 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004704 /* getchar(1): only check if char avail */
4705 n = vpeekc_any();
4706 else if (error || vpeekc_any() == NUL)
4707 /* illegal argument or getchar(0) and no char avail: return zero */
4708 n = 0;
4709 else
4710 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004711 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004712
4713 if (n == K_IGNORE)
4714 continue;
4715 break;
4716 }
4717 --no_mapping;
4718 --allow_keys;
4719
4720 set_vim_var_nr(VV_MOUSE_WIN, 0);
4721 set_vim_var_nr(VV_MOUSE_WINID, 0);
4722 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4723 set_vim_var_nr(VV_MOUSE_COL, 0);
4724
4725 rettv->vval.v_number = n;
4726 if (IS_SPECIAL(n) || mod_mask != 0)
4727 {
4728 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4729 int i = 0;
4730
4731 /* Turn a special key into three bytes, plus modifier. */
4732 if (mod_mask != 0)
4733 {
4734 temp[i++] = K_SPECIAL;
4735 temp[i++] = KS_MODIFIER;
4736 temp[i++] = mod_mask;
4737 }
4738 if (IS_SPECIAL(n))
4739 {
4740 temp[i++] = K_SPECIAL;
4741 temp[i++] = K_SECOND(n);
4742 temp[i++] = K_THIRD(n);
4743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 else if (has_mbyte)
4745 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004746 else
4747 temp[i++] = n;
4748 temp[i++] = NUL;
4749 rettv->v_type = VAR_STRING;
4750 rettv->vval.v_string = vim_strsave(temp);
4751
4752#ifdef FEAT_MOUSE
4753 if (is_mouse_key(n))
4754 {
4755 int row = mouse_row;
4756 int col = mouse_col;
4757 win_T *win;
4758 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 int winnr = 1;
4761
4762 if (row >= 0 && col >= 0)
4763 {
4764 /* Find the window at the mouse coordinates and compute the
4765 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004766 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004767 if (win == NULL)
4768 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004769 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004770# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004771 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004772 winnr = 0;
4773 else
4774# endif
4775 for (wp = firstwin; wp != win && wp != NULL;
4776 wp = wp->w_next)
4777 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004778 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4779 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4780 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4781 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4782 }
4783 }
4784#endif
4785 }
4786}
4787
4788/*
4789 * "getcharmod()" function
4790 */
4791 static void
4792f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4793{
4794 rettv->vval.v_number = mod_mask;
4795}
4796
4797/*
4798 * "getcharsearch()" function
4799 */
4800 static void
4801f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4802{
4803 if (rettv_dict_alloc(rettv) != FAIL)
4804 {
4805 dict_T *dict = rettv->vval.v_dict;
4806
Bram Moolenaare0be1672018-07-08 16:50:37 +02004807 dict_add_string(dict, "char", last_csearch());
4808 dict_add_number(dict, "forward", last_csearch_forward());
4809 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004810 }
4811}
4812
4813/*
4814 * "getcmdline()" function
4815 */
4816 static void
4817f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4818{
4819 rettv->v_type = VAR_STRING;
4820 rettv->vval.v_string = get_cmdline_str();
4821}
4822
4823/*
4824 * "getcmdpos()" function
4825 */
4826 static void
4827f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4828{
4829 rettv->vval.v_number = get_cmdline_pos() + 1;
4830}
4831
4832/*
4833 * "getcmdtype()" function
4834 */
4835 static void
4836f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4837{
4838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = alloc(2);
4840 if (rettv->vval.v_string != NULL)
4841 {
4842 rettv->vval.v_string[0] = get_cmdline_type();
4843 rettv->vval.v_string[1] = NUL;
4844 }
4845}
4846
4847/*
4848 * "getcmdwintype()" function
4849 */
4850 static void
4851f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4852{
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = NULL;
4855#ifdef FEAT_CMDWIN
4856 rettv->vval.v_string = alloc(2);
4857 if (rettv->vval.v_string != NULL)
4858 {
4859 rettv->vval.v_string[0] = cmdwin_type;
4860 rettv->vval.v_string[1] = NUL;
4861 }
4862#endif
4863}
4864
4865#if defined(FEAT_CMDL_COMPL)
4866/*
4867 * "getcompletion()" function
4868 */
4869 static void
4870f_getcompletion(typval_T *argvars, typval_T *rettv)
4871{
4872 char_u *pat;
4873 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004874 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004875 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4876 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004879 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 if (p_wic)
4882 options |= WILD_ICASE;
4883
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004884 /* For filtered results, 'wildignore' is used */
4885 if (!filtered)
4886 options |= WILD_KEEP_ALL;
4887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004889 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004891 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 if (xpc.xp_context == EXPAND_NOTHING)
4893 {
4894 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004895 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004897 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004898 return;
4899 }
4900
4901# if defined(FEAT_MENU)
4902 if (xpc.xp_context == EXPAND_MENUS)
4903 {
4904 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4905 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4906 }
4907# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004908#ifdef FEAT_CSCOPE
4909 if (xpc.xp_context == EXPAND_CSCOPE)
4910 {
4911 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4912 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4913 }
4914#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004915#ifdef FEAT_SIGNS
4916 if (xpc.xp_context == EXPAND_SIGN)
4917 {
4918 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4919 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4920 }
4921#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922
4923 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4924 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4925 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004926 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927
4928 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4929
4930 for (i = 0; i < xpc.xp_numfiles; i++)
4931 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4932 }
4933 vim_free(pat);
4934 ExpandCleanup(&xpc);
4935}
4936#endif
4937
4938/*
4939 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004940 *
4941 * Return the current working directory of a window in a tab page.
4942 * First optional argument 'winnr' is the window number or -1 and the second
4943 * optional argument 'tabnr' is the tab page number.
4944 *
4945 * If no arguments are supplied, then return the directory of the current
4946 * window.
4947 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4948 * the specified window.
4949 * If 'winnr' is 0 then return the directory of the current window.
4950 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4951 * directory of the specified tab page. Otherwise return the directory of the
4952 * specified window in the specified tab page.
4953 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 */
4955 static void
4956f_getcwd(typval_T *argvars, typval_T *rettv)
4957{
4958 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004959 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004961 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962
4963 rettv->v_type = VAR_STRING;
4964 rettv->vval.v_string = NULL;
4965
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004966 if (argvars[0].v_type == VAR_NUMBER
4967 && argvars[0].vval.v_number == -1
4968 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004969 global = TRUE;
4970 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004971 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004972
4973 if (wp != NULL && wp->w_localdir != NULL)
4974 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004975 else if (tp != NULL && tp->tp_localdir != NULL)
4976 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4977 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004979 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980 rettv->vval.v_string = vim_strsave(globaldir);
4981 else
4982 {
4983 cwd = alloc(MAXPATHL);
4984 if (cwd != NULL)
4985 {
4986 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4987 rettv->vval.v_string = vim_strsave(cwd);
4988 vim_free(cwd);
4989 }
4990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004992#ifdef BACKSLASH_IN_FILENAME
4993 if (rettv->vval.v_string != NULL)
4994 slash_adjust(rettv->vval.v_string);
4995#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004996}
4997
4998/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004999 * "getenv()" function
5000 */
5001 static void
5002f_getenv(typval_T *argvars, typval_T *rettv)
5003{
5004 int mustfree = FALSE;
5005 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5006
5007 if (p == NULL)
5008 {
5009 rettv->v_type = VAR_SPECIAL;
5010 rettv->vval.v_number = VVAL_NULL;
5011 return;
5012 }
5013 if (!mustfree)
5014 p = vim_strsave(p);
5015 rettv->vval.v_string = p;
5016 rettv->v_type = VAR_STRING;
5017}
5018
5019/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020 * "getfontname()" function
5021 */
5022 static void
5023f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5024{
5025 rettv->v_type = VAR_STRING;
5026 rettv->vval.v_string = NULL;
5027#ifdef FEAT_GUI
5028 if (gui.in_use)
5029 {
5030 GuiFont font;
5031 char_u *name = NULL;
5032
5033 if (argvars[0].v_type == VAR_UNKNOWN)
5034 {
5035 /* Get the "Normal" font. Either the name saved by
5036 * hl_set_font_name() or from the font ID. */
5037 font = gui.norm_font;
5038 name = hl_get_font_name();
5039 }
5040 else
5041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005042 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5044 return;
5045 font = gui_mch_get_font(name, FALSE);
5046 if (font == NOFONT)
5047 return; /* Invalid font name, return empty string. */
5048 }
5049 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5050 if (argvars[0].v_type != VAR_UNKNOWN)
5051 gui_mch_free_font(font);
5052 }
5053#endif
5054}
5055
5056/*
5057 * "getfperm({fname})" function
5058 */
5059 static void
5060f_getfperm(typval_T *argvars, typval_T *rettv)
5061{
5062 char_u *fname;
5063 stat_T st;
5064 char_u *perm = NULL;
5065 char_u flags[] = "rwx";
5066 int i;
5067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005068 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069
5070 rettv->v_type = VAR_STRING;
5071 if (mch_stat((char *)fname, &st) >= 0)
5072 {
5073 perm = vim_strsave((char_u *)"---------");
5074 if (perm != NULL)
5075 {
5076 for (i = 0; i < 9; i++)
5077 {
5078 if (st.st_mode & (1 << (8 - i)))
5079 perm[i] = flags[i % 3];
5080 }
5081 }
5082 }
5083 rettv->vval.v_string = perm;
5084}
5085
5086/*
5087 * "getfsize({fname})" function
5088 */
5089 static void
5090f_getfsize(typval_T *argvars, typval_T *rettv)
5091{
5092 char_u *fname;
5093 stat_T st;
5094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096
5097 rettv->v_type = VAR_NUMBER;
5098
5099 if (mch_stat((char *)fname, &st) >= 0)
5100 {
5101 if (mch_isdir(fname))
5102 rettv->vval.v_number = 0;
5103 else
5104 {
5105 rettv->vval.v_number = (varnumber_T)st.st_size;
5106
5107 /* non-perfect check for overflow */
5108 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5109 rettv->vval.v_number = -2;
5110 }
5111 }
5112 else
5113 rettv->vval.v_number = -1;
5114}
5115
5116/*
5117 * "getftime({fname})" function
5118 */
5119 static void
5120f_getftime(typval_T *argvars, typval_T *rettv)
5121{
5122 char_u *fname;
5123 stat_T st;
5124
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005125 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126
5127 if (mch_stat((char *)fname, &st) >= 0)
5128 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5129 else
5130 rettv->vval.v_number = -1;
5131}
5132
5133/*
5134 * "getftype({fname})" function
5135 */
5136 static void
5137f_getftype(typval_T *argvars, typval_T *rettv)
5138{
5139 char_u *fname;
5140 stat_T st;
5141 char_u *type = NULL;
5142 char *t;
5143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005144 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145
5146 rettv->v_type = VAR_STRING;
5147 if (mch_lstat((char *)fname, &st) >= 0)
5148 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 if (S_ISREG(st.st_mode))
5150 t = "file";
5151 else if (S_ISDIR(st.st_mode))
5152 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153 else if (S_ISLNK(st.st_mode))
5154 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155 else if (S_ISBLK(st.st_mode))
5156 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 else if (S_ISCHR(st.st_mode))
5158 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159 else if (S_ISFIFO(st.st_mode))
5160 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005162 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 else
5164 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 type = vim_strsave((char_u *)t);
5166 }
5167 rettv->vval.v_string = type;
5168}
5169
5170/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005171 * "getjumplist()" function
5172 */
5173 static void
5174f_getjumplist(typval_T *argvars, typval_T *rettv)
5175{
5176#ifdef FEAT_JUMPLIST
5177 win_T *wp;
5178 int i;
5179 list_T *l;
5180 dict_T *d;
5181#endif
5182
5183 if (rettv_list_alloc(rettv) != OK)
5184 return;
5185
5186#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005187 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005188 if (wp == NULL)
5189 return;
5190
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005191 cleanup_jumplist(wp, TRUE);
5192
Bram Moolenaar4f505882018-02-10 21:06:32 +01005193 l = list_alloc();
5194 if (l == NULL)
5195 return;
5196
5197 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5198 return;
5199 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5200
5201 for (i = 0; i < wp->w_jumplistlen; ++i)
5202 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005203 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5204 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005205 if ((d = dict_alloc()) == NULL)
5206 return;
5207 if (list_append_dict(l, d) == FAIL)
5208 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005209 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5210 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005211 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005212 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005213 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005214 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005215 }
5216#endif
5217}
5218
5219/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005220 * "getline(lnum, [end])" function
5221 */
5222 static void
5223f_getline(typval_T *argvars, typval_T *rettv)
5224{
5225 linenr_T lnum;
5226 linenr_T end;
5227 int retlist;
5228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005229 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 if (argvars[1].v_type == VAR_UNKNOWN)
5231 {
5232 end = 0;
5233 retlist = FALSE;
5234 }
5235 else
5236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005237 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005238 retlist = TRUE;
5239 }
5240
5241 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5242}
5243
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005244#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005245 static void
5246get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5247{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005248 if (what_arg->v_type == VAR_UNKNOWN)
5249 {
5250 if (rettv_list_alloc(rettv) == OK)
5251 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005252 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005253 }
5254 else
5255 {
5256 if (rettv_dict_alloc(rettv) == OK)
5257 if (is_qf || (wp != NULL))
5258 {
5259 if (what_arg->v_type == VAR_DICT)
5260 {
5261 dict_T *d = what_arg->vval.v_dict;
5262
5263 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005264 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005265 }
5266 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005267 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005268 }
5269 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005270}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005271#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005272
5273/*
5274 * "getloclist()" function
5275 */
5276 static void
5277f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5278{
5279#ifdef FEAT_QUICKFIX
5280 win_T *wp;
5281
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005282 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005283 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5284#endif
5285}
5286
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287/*
5288 * "getmatches()" function
5289 */
5290 static void
5291f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5292{
5293#ifdef FEAT_SEARCH_EXTRA
5294 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005295 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005297 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298
Bram Moolenaaraff74912019-03-30 18:11:49 +01005299 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5300 return;
5301
5302 cur = win->w_match_head;
5303 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005305 dict = dict_alloc();
5306 if (dict == NULL)
5307 return;
5308 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005310 /* match added with matchaddpos() */
5311 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005313 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005314 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005315 list_T *l;
5316
5317 llpos = &cur->pos.pos[i];
5318 if (llpos->lnum == 0)
5319 break;
5320 l = list_alloc();
5321 if (l == NULL)
5322 break;
5323 list_append_number(l, (varnumber_T)llpos->lnum);
5324 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005326 list_append_number(l, (varnumber_T)llpos->col);
5327 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005329 sprintf(buf, "pos%d", i + 1);
5330 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005333 else
5334 {
5335 dict_add_string(dict, "pattern", cur->pattern);
5336 }
5337 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5338 dict_add_number(dict, "priority", (long)cur->priority);
5339 dict_add_number(dict, "id", (long)cur->id);
5340# if defined(FEAT_CONCEAL)
5341 if (cur->conceal_char)
5342 {
5343 char_u buf[MB_MAXBYTES + 1];
5344
5345 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5346 dict_add_string(dict, "conceal", (char_u *)&buf);
5347 }
5348# endif
5349 list_append_dict(rettv->vval.v_list, dict);
5350 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351 }
5352#endif
5353}
5354
5355/*
5356 * "getpid()" function
5357 */
5358 static void
5359f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5360{
5361 rettv->vval.v_number = mch_get_pid();
5362}
5363
5364 static void
5365getpos_both(
5366 typval_T *argvars,
5367 typval_T *rettv,
5368 int getcurpos)
5369{
5370 pos_T *fp;
5371 list_T *l;
5372 int fnum = -1;
5373
5374 if (rettv_list_alloc(rettv) == OK)
5375 {
5376 l = rettv->vval.v_list;
5377 if (getcurpos)
5378 fp = &curwin->w_cursor;
5379 else
5380 fp = var2fpos(&argvars[0], TRUE, &fnum);
5381 if (fnum != -1)
5382 list_append_number(l, (varnumber_T)fnum);
5383 else
5384 list_append_number(l, (varnumber_T)0);
5385 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5386 : (varnumber_T)0);
5387 list_append_number(l, (fp != NULL)
5388 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5389 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005390 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005391 (varnumber_T)0);
5392 if (getcurpos)
5393 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005394 int save_set_curswant = curwin->w_set_curswant;
5395 colnr_T save_curswant = curwin->w_curswant;
5396 colnr_T save_virtcol = curwin->w_virtcol;
5397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005398 update_curswant();
5399 list_append_number(l, curwin->w_curswant == MAXCOL ?
5400 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005401
5402 // Do not change "curswant", as it is unexpected that a get
5403 // function has a side effect.
5404 if (save_set_curswant)
5405 {
5406 curwin->w_set_curswant = save_set_curswant;
5407 curwin->w_curswant = save_curswant;
5408 curwin->w_virtcol = save_virtcol;
5409 curwin->w_valid &= ~VALID_VIRTCOL;
5410 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005411 }
5412 }
5413 else
5414 rettv->vval.v_number = FALSE;
5415}
5416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417/*
5418 * "getcurpos()" function
5419 */
5420 static void
5421f_getcurpos(typval_T *argvars, typval_T *rettv)
5422{
5423 getpos_both(argvars, rettv, TRUE);
5424}
5425
5426/*
5427 * "getpos(string)" function
5428 */
5429 static void
5430f_getpos(typval_T *argvars, typval_T *rettv)
5431{
5432 getpos_both(argvars, rettv, FALSE);
5433}
5434
5435/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005436 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 */
5438 static void
5439f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5440{
5441#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005442 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443#endif
5444}
5445
5446/*
5447 * "getreg()" function
5448 */
5449 static void
5450f_getreg(typval_T *argvars, typval_T *rettv)
5451{
5452 char_u *strregname;
5453 int regname;
5454 int arg2 = FALSE;
5455 int return_list = FALSE;
5456 int error = FALSE;
5457
5458 if (argvars[0].v_type != VAR_UNKNOWN)
5459 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005460 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 error = strregname == NULL;
5462 if (argvars[1].v_type != VAR_UNKNOWN)
5463 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005464 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005465 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005466 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 }
5468 }
5469 else
5470 strregname = get_vim_var_str(VV_REG);
5471
5472 if (error)
5473 return;
5474
5475 regname = (strregname == NULL ? '"' : *strregname);
5476 if (regname == 0)
5477 regname = '"';
5478
5479 if (return_list)
5480 {
5481 rettv->v_type = VAR_LIST;
5482 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5483 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5484 if (rettv->vval.v_list == NULL)
5485 (void)rettv_list_alloc(rettv);
5486 else
5487 ++rettv->vval.v_list->lv_refcount;
5488 }
5489 else
5490 {
5491 rettv->v_type = VAR_STRING;
5492 rettv->vval.v_string = get_reg_contents(regname,
5493 arg2 ? GREG_EXPR_SRC : 0);
5494 }
5495}
5496
5497/*
5498 * "getregtype()" function
5499 */
5500 static void
5501f_getregtype(typval_T *argvars, typval_T *rettv)
5502{
5503 char_u *strregname;
5504 int regname;
5505 char_u buf[NUMBUFLEN + 2];
5506 long reglen = 0;
5507
5508 if (argvars[0].v_type != VAR_UNKNOWN)
5509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005510 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 if (strregname == NULL) /* type error; errmsg already given */
5512 {
5513 rettv->v_type = VAR_STRING;
5514 rettv->vval.v_string = NULL;
5515 return;
5516 }
5517 }
5518 else
5519 /* Default to v:register */
5520 strregname = get_vim_var_str(VV_REG);
5521
5522 regname = (strregname == NULL ? '"' : *strregname);
5523 if (regname == 0)
5524 regname = '"';
5525
5526 buf[0] = NUL;
5527 buf[1] = NUL;
5528 switch (get_reg_type(regname, &reglen))
5529 {
5530 case MLINE: buf[0] = 'V'; break;
5531 case MCHAR: buf[0] = 'v'; break;
5532 case MBLOCK:
5533 buf[0] = Ctrl_V;
5534 sprintf((char *)buf + 1, "%ld", reglen + 1);
5535 break;
5536 }
5537 rettv->v_type = VAR_STRING;
5538 rettv->vval.v_string = vim_strsave(buf);
5539}
5540
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005541/*
5542 * Returns information (variables, options, etc.) about a tab page
5543 * as a dictionary.
5544 */
5545 static dict_T *
5546get_tabpage_info(tabpage_T *tp, int tp_idx)
5547{
5548 win_T *wp;
5549 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005550 list_T *l;
5551
5552 dict = dict_alloc();
5553 if (dict == NULL)
5554 return NULL;
5555
Bram Moolenaare0be1672018-07-08 16:50:37 +02005556 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557
5558 l = list_alloc();
5559 if (l != NULL)
5560 {
5561 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005562 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005563 list_append_number(l, (varnumber_T)wp->w_id);
5564 dict_add_list(dict, "windows", l);
5565 }
5566
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005567 /* Make a reference to tabpage variables */
5568 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005569
5570 return dict;
5571}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005572
5573/*
5574 * "gettabinfo()" function
5575 */
5576 static void
5577f_gettabinfo(typval_T *argvars, typval_T *rettv)
5578{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005579 tabpage_T *tp, *tparg = NULL;
5580 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005581 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582
5583 if (rettv_list_alloc(rettv) != OK)
5584 return;
5585
5586 if (argvars[0].v_type != VAR_UNKNOWN)
5587 {
5588 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005589 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005590 if (tparg == NULL)
5591 return;
5592 }
5593
5594 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005595 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005596 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005597 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005598 if (tparg != NULL && tp != tparg)
5599 continue;
5600 d = get_tabpage_info(tp, tpnr);
5601 if (d != NULL)
5602 list_append_dict(rettv->vval.v_list, d);
5603 if (tparg != NULL)
5604 return;
5605 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005606}
5607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608/*
5609 * "gettabvar()" function
5610 */
5611 static void
5612f_gettabvar(typval_T *argvars, typval_T *rettv)
5613{
5614 win_T *oldcurwin;
5615 tabpage_T *tp, *oldtabpage;
5616 dictitem_T *v;
5617 char_u *varname;
5618 int done = FALSE;
5619
5620 rettv->v_type = VAR_STRING;
5621 rettv->vval.v_string = NULL;
5622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005623 varname = tv_get_string_chk(&argvars[1]);
5624 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625 if (tp != NULL && varname != NULL)
5626 {
5627 /* Set tp to be our tabpage, temporarily. Also set the window to the
5628 * first window in the tabpage, otherwise the window is not valid. */
5629 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005630 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5631 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632 {
5633 /* look up the variable */
5634 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5635 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5636 if (v != NULL)
5637 {
5638 copy_tv(&v->di_tv, rettv);
5639 done = TRUE;
5640 }
5641 }
5642
5643 /* restore previous notion of curwin */
5644 restore_win(oldcurwin, oldtabpage, TRUE);
5645 }
5646
5647 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5648 copy_tv(&argvars[2], rettv);
5649}
5650
5651/*
5652 * "gettabwinvar()" function
5653 */
5654 static void
5655f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5656{
5657 getwinvar(argvars, rettv, 1);
5658}
5659
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005661 * "gettagstack()" function
5662 */
5663 static void
5664f_gettagstack(typval_T *argvars, typval_T *rettv)
5665{
5666 win_T *wp = curwin; // default is current window
5667
5668 if (rettv_dict_alloc(rettv) != OK)
5669 return;
5670
5671 if (argvars[0].v_type != VAR_UNKNOWN)
5672 {
5673 wp = find_win_by_nr_or_id(&argvars[0]);
5674 if (wp == NULL)
5675 return;
5676 }
5677
5678 get_tagstack(wp, rettv->vval.v_dict);
5679}
5680
5681/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682 * Returns information about a window as a dictionary.
5683 */
5684 static dict_T *
5685get_win_info(win_T *wp, short tpnr, short winnr)
5686{
5687 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005688
5689 dict = dict_alloc();
5690 if (dict == NULL)
5691 return NULL;
5692
Bram Moolenaare0be1672018-07-08 16:50:37 +02005693 dict_add_number(dict, "tabnr", tpnr);
5694 dict_add_number(dict, "winnr", winnr);
5695 dict_add_number(dict, "winid", wp->w_id);
5696 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005697 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005698 dict_add_number(dict, "topline", wp->w_topline);
5699 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005700#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005701 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005702#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005703 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005704 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005705 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706
Bram Moolenaar69905d12017-08-13 18:14:47 +02005707#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005708 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005709#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005710#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005711 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5712 dict_add_number(dict, "loclist",
5713 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005714#endif
5715
Bram Moolenaar30567352016-08-27 21:25:44 +02005716 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005717 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005719 return dict;
5720}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005721
5722/*
5723 * "getwininfo()" function
5724 */
5725 static void
5726f_getwininfo(typval_T *argvars, typval_T *rettv)
5727{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728 tabpage_T *tp;
5729 win_T *wp = NULL, *wparg = NULL;
5730 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005731 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005732
5733 if (rettv_list_alloc(rettv) != OK)
5734 return;
5735
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005736 if (argvars[0].v_type != VAR_UNKNOWN)
5737 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005738 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739 if (wparg == NULL)
5740 return;
5741 }
5742
5743 /* Collect information about either all the windows across all the tab
5744 * pages or one particular window.
5745 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005746 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005747 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005748 tabnr++;
5749 winnr = 0;
5750 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005752 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005753 if (wparg != NULL && wp != wparg)
5754 continue;
5755 d = get_win_info(wp, tabnr, winnr);
5756 if (d != NULL)
5757 list_append_dict(rettv->vval.v_list, d);
5758 if (wparg != NULL)
5759 /* found information about a specific window */
5760 return;
5761 }
5762 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005763}
5764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005765/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005766 * "win_execute()" function
5767 */
5768 static void
5769f_win_execute(typval_T *argvars, typval_T *rettv)
5770{
5771 int id = (int)tv_get_number(argvars);
5772 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005773 win_T *save_curwin;
5774 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005775
5776 if (wp != NULL)
5777 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005778 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5779 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005780 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005781 check_cursor();
5782 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005783 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005784 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005785 }
5786}
5787
5788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005789 * "win_findbuf()" function
5790 */
5791 static void
5792f_win_findbuf(typval_T *argvars, typval_T *rettv)
5793{
5794 if (rettv_list_alloc(rettv) != FAIL)
5795 win_findbuf(argvars, rettv->vval.v_list);
5796}
5797
5798/*
5799 * "win_getid()" function
5800 */
5801 static void
5802f_win_getid(typval_T *argvars, typval_T *rettv)
5803{
5804 rettv->vval.v_number = win_getid(argvars);
5805}
5806
5807/*
5808 * "win_gotoid()" function
5809 */
5810 static void
5811f_win_gotoid(typval_T *argvars, typval_T *rettv)
5812{
5813 rettv->vval.v_number = win_gotoid(argvars);
5814}
5815
5816/*
5817 * "win_id2tabwin()" function
5818 */
5819 static void
5820f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5821{
5822 if (rettv_list_alloc(rettv) != FAIL)
5823 win_id2tabwin(argvars, rettv->vval.v_list);
5824}
5825
5826/*
5827 * "win_id2win()" function
5828 */
5829 static void
5830f_win_id2win(typval_T *argvars, typval_T *rettv)
5831{
5832 rettv->vval.v_number = win_id2win(argvars);
5833}
5834
5835/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005836 * "win_screenpos()" function
5837 */
5838 static void
5839f_win_screenpos(typval_T *argvars, typval_T *rettv)
5840{
5841 win_T *wp;
5842
5843 if (rettv_list_alloc(rettv) == FAIL)
5844 return;
5845
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005846 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005847 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5848 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5849}
5850
5851/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005852 * "getwinpos({timeout})" function
5853 */
5854 static void
5855f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5856{
5857 int x = -1;
5858 int y = -1;
5859
5860 if (rettv_list_alloc(rettv) == FAIL)
5861 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005862#if defined(FEAT_GUI) \
5863 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5864 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005865 {
5866 varnumber_T timeout = 100;
5867
5868 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005869 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005870
5871 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005872 }
5873#endif
5874 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5875 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5876}
5877
5878
5879/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005880 * "getwinposx()" function
5881 */
5882 static void
5883f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5884{
5885 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005886#if defined(FEAT_GUI) \
5887 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5888 || defined(MSWIN)
5889
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005890 {
5891 int x, y;
5892
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005893 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005894 rettv->vval.v_number = x;
5895 }
5896#endif
5897}
5898
5899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 * "getwinposy()" function
5901 */
5902 static void
5903f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5904{
5905 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005906#if defined(FEAT_GUI) \
5907 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5908 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 {
5910 int x, y;
5911
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005912 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005913 rettv->vval.v_number = y;
5914 }
5915#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005916}
5917
5918/*
5919 * "getwinvar()" function
5920 */
5921 static void
5922f_getwinvar(typval_T *argvars, typval_T *rettv)
5923{
5924 getwinvar(argvars, rettv, 0);
5925}
5926
5927/*
5928 * "glob()" function
5929 */
5930 static void
5931f_glob(typval_T *argvars, typval_T *rettv)
5932{
5933 int options = WILD_SILENT|WILD_USE_NL;
5934 expand_T xpc;
5935 int error = FALSE;
5936
5937 /* When the optional second argument is non-zero, don't remove matches
5938 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5939 rettv->v_type = VAR_STRING;
5940 if (argvars[1].v_type != VAR_UNKNOWN)
5941 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005942 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 options |= WILD_KEEP_ALL;
5944 if (argvars[2].v_type != VAR_UNKNOWN)
5945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005946 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005947 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005949 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950 options |= WILD_ALLLINKS;
5951 }
5952 }
5953 if (!error)
5954 {
5955 ExpandInit(&xpc);
5956 xpc.xp_context = EXPAND_FILES;
5957 if (p_wic)
5958 options += WILD_ICASE;
5959 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005960 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961 NULL, options, WILD_ALL);
5962 else if (rettv_list_alloc(rettv) != FAIL)
5963 {
5964 int i;
5965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005966 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 NULL, options, WILD_ALL_KEEP);
5968 for (i = 0; i < xpc.xp_numfiles; i++)
5969 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5970
5971 ExpandCleanup(&xpc);
5972 }
5973 }
5974 else
5975 rettv->vval.v_string = NULL;
5976}
5977
5978/*
5979 * "globpath()" function
5980 */
5981 static void
5982f_globpath(typval_T *argvars, typval_T *rettv)
5983{
5984 int flags = 0;
5985 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005986 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987 int error = FALSE;
5988 garray_T ga;
5989 int i;
5990
5991 /* When the optional second argument is non-zero, don't remove matches
5992 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5993 rettv->v_type = VAR_STRING;
5994 if (argvars[2].v_type != VAR_UNKNOWN)
5995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005996 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005997 flags |= WILD_KEEP_ALL;
5998 if (argvars[3].v_type != VAR_UNKNOWN)
5999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006000 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006001 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006003 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006004 flags |= WILD_ALLLINKS;
6005 }
6006 }
6007 if (file != NULL && !error)
6008 {
6009 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006010 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 if (rettv->v_type == VAR_STRING)
6012 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6013 else if (rettv_list_alloc(rettv) != FAIL)
6014 for (i = 0; i < ga.ga_len; ++i)
6015 list_append_string(rettv->vval.v_list,
6016 ((char_u **)(ga.ga_data))[i], -1);
6017 ga_clear_strings(&ga);
6018 }
6019 else
6020 rettv->vval.v_string = NULL;
6021}
6022
6023/*
6024 * "glob2regpat()" function
6025 */
6026 static void
6027f_glob2regpat(typval_T *argvars, typval_T *rettv)
6028{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006029 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030
6031 rettv->v_type = VAR_STRING;
6032 rettv->vval.v_string = (pat == NULL)
6033 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6034}
6035
6036/* for VIM_VERSION_ defines */
6037#include "version.h"
6038
6039/*
6040 * "has()" function
6041 */
6042 static void
6043f_has(typval_T *argvars, typval_T *rettv)
6044{
6045 int i;
6046 char_u *name;
6047 int n = FALSE;
6048 static char *(has_list[]) =
6049 {
6050#ifdef AMIGA
6051 "amiga",
6052# ifdef FEAT_ARP
6053 "arp",
6054# endif
6055#endif
6056#ifdef __BEOS__
6057 "beos",
6058#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006059#if defined(BSD) && !defined(MACOS_X)
6060 "bsd",
6061#endif
6062#ifdef hpux
6063 "hpux",
6064#endif
6065#ifdef __linux__
6066 "linux",
6067#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006068#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006069 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6070 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006071# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006072 "macunix", /* Mac OS X, with the darwin feature */
6073 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006074# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075#endif
6076#ifdef __QNX__
6077 "qnx",
6078#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006079#ifdef SUN_SYSTEM
6080 "sun",
6081#else
6082 "moon",
6083#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084#ifdef UNIX
6085 "unix",
6086#endif
6087#ifdef VMS
6088 "vms",
6089#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006090#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 "win32",
6092#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006093#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 "win32unix",
6095#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006096#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097 "win64",
6098#endif
6099#ifdef EBCDIC
6100 "ebcdic",
6101#endif
6102#ifndef CASE_INSENSITIVE_FILENAME
6103 "fname_case",
6104#endif
6105#ifdef HAVE_ACL
6106 "acl",
6107#endif
6108#ifdef FEAT_ARABIC
6109 "arabic",
6110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006112#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006113 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006114#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006115#ifdef FEAT_AUTOSERVERNAME
6116 "autoservername",
6117#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006118#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006120# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121 "balloon_multiline",
6122# endif
6123#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006124#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006125 "balloon_eval_term",
6126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6128 "builtin_terms",
6129# ifdef ALL_BUILTIN_TCAPS
6130 "all_builtin_terms",
6131# endif
6132#endif
6133#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006134 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 || defined(FEAT_GUI_MOTIF))
6136 "browsefilter",
6137#endif
6138#ifdef FEAT_BYTEOFF
6139 "byte_offset",
6140#endif
6141#ifdef FEAT_JOB_CHANNEL
6142 "channel",
6143#endif
6144#ifdef FEAT_CINDENT
6145 "cindent",
6146#endif
6147#ifdef FEAT_CLIENTSERVER
6148 "clientserver",
6149#endif
6150#ifdef FEAT_CLIPBOARD
6151 "clipboard",
6152#endif
6153#ifdef FEAT_CMDL_COMPL
6154 "cmdline_compl",
6155#endif
6156#ifdef FEAT_CMDHIST
6157 "cmdline_hist",
6158#endif
6159#ifdef FEAT_COMMENTS
6160 "comments",
6161#endif
6162#ifdef FEAT_CONCEAL
6163 "conceal",
6164#endif
6165#ifdef FEAT_CRYPT
6166 "cryptv",
6167 "crypt-blowfish",
6168 "crypt-blowfish2",
6169#endif
6170#ifdef FEAT_CSCOPE
6171 "cscope",
6172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174#ifdef CURSOR_SHAPE
6175 "cursorshape",
6176#endif
6177#ifdef DEBUG
6178 "debug",
6179#endif
6180#ifdef FEAT_CON_DIALOG
6181 "dialog_con",
6182#endif
6183#ifdef FEAT_GUI_DIALOG
6184 "dialog_gui",
6185#endif
6186#ifdef FEAT_DIFF
6187 "diff",
6188#endif
6189#ifdef FEAT_DIGRAPHS
6190 "digraphs",
6191#endif
6192#ifdef FEAT_DIRECTX
6193 "directx",
6194#endif
6195#ifdef FEAT_DND
6196 "dnd",
6197#endif
6198#ifdef FEAT_EMACS_TAGS
6199 "emacs_tags",
6200#endif
6201 "eval", /* always present, of course! */
6202 "ex_extra", /* graduated feature */
6203#ifdef FEAT_SEARCH_EXTRA
6204 "extra_search",
6205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206#ifdef FEAT_SEARCHPATH
6207 "file_in_path",
6208#endif
6209#ifdef FEAT_FILTERPIPE
6210 "filterpipe",
6211#endif
6212#ifdef FEAT_FIND_ID
6213 "find_in_path",
6214#endif
6215#ifdef FEAT_FLOAT
6216 "float",
6217#endif
6218#ifdef FEAT_FOLDING
6219 "folding",
6220#endif
6221#ifdef FEAT_FOOTER
6222 "footer",
6223#endif
6224#if !defined(USE_SYSTEM) && defined(UNIX)
6225 "fork",
6226#endif
6227#ifdef FEAT_GETTEXT
6228 "gettext",
6229#endif
6230#ifdef FEAT_GUI
6231 "gui",
6232#endif
6233#ifdef FEAT_GUI_ATHENA
6234# ifdef FEAT_GUI_NEXTAW
6235 "gui_neXtaw",
6236# else
6237 "gui_athena",
6238# endif
6239#endif
6240#ifdef FEAT_GUI_GTK
6241 "gui_gtk",
6242# ifdef USE_GTK3
6243 "gui_gtk3",
6244# else
6245 "gui_gtk2",
6246# endif
6247#endif
6248#ifdef FEAT_GUI_GNOME
6249 "gui_gnome",
6250#endif
6251#ifdef FEAT_GUI_MAC
6252 "gui_mac",
6253#endif
6254#ifdef FEAT_GUI_MOTIF
6255 "gui_motif",
6256#endif
6257#ifdef FEAT_GUI_PHOTON
6258 "gui_photon",
6259#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006260#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 "gui_win32",
6262#endif
6263#ifdef FEAT_HANGULIN
6264 "hangul_input",
6265#endif
6266#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6267 "iconv",
6268#endif
6269#ifdef FEAT_INS_EXPAND
6270 "insert_expand",
6271#endif
6272#ifdef FEAT_JOB_CHANNEL
6273 "job",
6274#endif
6275#ifdef FEAT_JUMPLIST
6276 "jumplist",
6277#endif
6278#ifdef FEAT_KEYMAP
6279 "keymap",
6280#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006281 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282#ifdef FEAT_LANGMAP
6283 "langmap",
6284#endif
6285#ifdef FEAT_LIBCALL
6286 "libcall",
6287#endif
6288#ifdef FEAT_LINEBREAK
6289 "linebreak",
6290#endif
6291#ifdef FEAT_LISP
6292 "lispindent",
6293#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295#ifdef FEAT_LOCALMAP
6296 "localmap",
6297#endif
6298#ifdef FEAT_LUA
6299# ifndef DYNAMIC_LUA
6300 "lua",
6301# endif
6302#endif
6303#ifdef FEAT_MENU
6304 "menu",
6305#endif
6306#ifdef FEAT_SESSION
6307 "mksession",
6308#endif
6309#ifdef FEAT_MODIFY_FNAME
6310 "modify_fname",
6311#endif
6312#ifdef FEAT_MOUSE
6313 "mouse",
6314#endif
6315#ifdef FEAT_MOUSESHAPE
6316 "mouseshape",
6317#endif
6318#if defined(UNIX) || defined(VMS)
6319# ifdef FEAT_MOUSE_DEC
6320 "mouse_dec",
6321# endif
6322# ifdef FEAT_MOUSE_GPM
6323 "mouse_gpm",
6324# endif
6325# ifdef FEAT_MOUSE_JSB
6326 "mouse_jsbterm",
6327# endif
6328# ifdef FEAT_MOUSE_NET
6329 "mouse_netterm",
6330# endif
6331# ifdef FEAT_MOUSE_PTERM
6332 "mouse_pterm",
6333# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006334# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006335 "mouse_sgr",
6336# endif
6337# ifdef FEAT_SYSMOUSE
6338 "mouse_sysmouse",
6339# endif
6340# ifdef FEAT_MOUSE_URXVT
6341 "mouse_urxvt",
6342# endif
6343# ifdef FEAT_MOUSE_XTERM
6344 "mouse_xterm",
6345# endif
6346#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348#ifdef FEAT_MBYTE_IME
6349 "multi_byte_ime",
6350#endif
6351#ifdef FEAT_MULTI_LANG
6352 "multi_lang",
6353#endif
6354#ifdef FEAT_MZSCHEME
6355#ifndef DYNAMIC_MZSCHEME
6356 "mzscheme",
6357#endif
6358#endif
6359#ifdef FEAT_NUM64
6360 "num64",
6361#endif
6362#ifdef FEAT_OLE
6363 "ole",
6364#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006365#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006367#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368#ifdef FEAT_PATH_EXTRA
6369 "path_extra",
6370#endif
6371#ifdef FEAT_PERL
6372#ifndef DYNAMIC_PERL
6373 "perl",
6374#endif
6375#endif
6376#ifdef FEAT_PERSISTENT_UNDO
6377 "persistent_undo",
6378#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006379#if defined(FEAT_PYTHON)
6380 "python_compiled",
6381# if defined(DYNAMIC_PYTHON)
6382 "python_dynamic",
6383# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006385 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006386# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006388#if defined(FEAT_PYTHON3)
6389 "python3_compiled",
6390# if defined(DYNAMIC_PYTHON3)
6391 "python3_dynamic",
6392# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006394 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006395# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396#endif
6397#ifdef FEAT_POSTSCRIPT
6398 "postscript",
6399#endif
6400#ifdef FEAT_PRINTER
6401 "printer",
6402#endif
6403#ifdef FEAT_PROFILE
6404 "profile",
6405#endif
6406#ifdef FEAT_RELTIME
6407 "reltime",
6408#endif
6409#ifdef FEAT_QUICKFIX
6410 "quickfix",
6411#endif
6412#ifdef FEAT_RIGHTLEFT
6413 "rightleft",
6414#endif
6415#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6416 "ruby",
6417#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006419#ifdef FEAT_CMDL_INFO
6420 "showcmd",
6421 "cmdline_info",
6422#endif
6423#ifdef FEAT_SIGNS
6424 "signs",
6425#endif
6426#ifdef FEAT_SMARTINDENT
6427 "smartindent",
6428#endif
6429#ifdef STARTUPTIME
6430 "startuptime",
6431#endif
6432#ifdef FEAT_STL_OPT
6433 "statusline",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435#ifdef FEAT_NETBEANS_INTG
6436 "netbeans_intg",
6437#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006438#ifdef FEAT_SOUND
6439 "sound",
6440#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441#ifdef FEAT_SPELL
6442 "spell",
6443#endif
6444#ifdef FEAT_SYN_HL
6445 "syntax",
6446#endif
6447#if defined(USE_SYSTEM) || !defined(UNIX)
6448 "system",
6449#endif
6450#ifdef FEAT_TAG_BINS
6451 "tag_binary",
6452#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006453#ifdef FEAT_TCL
6454# ifndef DYNAMIC_TCL
6455 "tcl",
6456# endif
6457#endif
6458#ifdef FEAT_TERMGUICOLORS
6459 "termguicolors",
6460#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006461#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006462 "terminal",
6463#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464#ifdef TERMINFO
6465 "terminfo",
6466#endif
6467#ifdef FEAT_TERMRESPONSE
6468 "termresponse",
6469#endif
6470#ifdef FEAT_TEXTOBJ
6471 "textobjects",
6472#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006473#ifdef FEAT_TEXT_PROP
6474 "textprop",
6475#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#ifdef HAVE_TGETENT
6477 "tgetent",
6478#endif
6479#ifdef FEAT_TIMERS
6480 "timers",
6481#endif
6482#ifdef FEAT_TITLE
6483 "title",
6484#endif
6485#ifdef FEAT_TOOLBAR
6486 "toolbar",
6487#endif
6488#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6489 "unnamedplus",
6490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 "user-commands", /* was accidentally included in 5.4 */
6492 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006493#ifdef FEAT_VARTABS
6494 "vartabs",
6495#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006496 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497#ifdef FEAT_VIMINFO
6498 "viminfo",
6499#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006500 "vimscript-1",
6501 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006502 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006507#ifdef FEAT_VTP
6508 "vtp",
6509#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510#ifdef FEAT_WILDIGN
6511 "wildignore",
6512#endif
6513#ifdef FEAT_WILDMENU
6514 "wildmenu",
6515#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef FEAT_WAK
6518 "winaltkeys",
6519#endif
6520#ifdef FEAT_WRITEBACKUP
6521 "writebackup",
6522#endif
6523#ifdef FEAT_XIM
6524 "xim",
6525#endif
6526#ifdef FEAT_XFONTSET
6527 "xfontset",
6528#endif
6529#ifdef FEAT_XPM_W32
6530 "xpm",
6531 "xpm_w32", /* for backward compatibility */
6532#else
6533# if defined(HAVE_XPM)
6534 "xpm",
6535# endif
6536#endif
6537#ifdef USE_XSMP
6538 "xsmp",
6539#endif
6540#ifdef USE_XSMP_INTERACT
6541 "xsmp_interact",
6542#endif
6543#ifdef FEAT_XCLIPBOARD
6544 "xterm_clipboard",
6545#endif
6546#ifdef FEAT_XTERM_SAVE
6547 "xterm_save",
6548#endif
6549#if defined(UNIX) && defined(FEAT_X11)
6550 "X11",
6551#endif
6552 NULL
6553 };
6554
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006555 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 for (i = 0; has_list[i] != NULL; ++i)
6557 if (STRICMP(name, has_list[i]) == 0)
6558 {
6559 n = TRUE;
6560 break;
6561 }
6562
6563 if (n == FALSE)
6564 {
6565 if (STRNICMP(name, "patch", 5) == 0)
6566 {
6567 if (name[5] == '-'
6568 && STRLEN(name) >= 11
6569 && vim_isdigit(name[6])
6570 && vim_isdigit(name[8])
6571 && vim_isdigit(name[10]))
6572 {
6573 int major = atoi((char *)name + 6);
6574 int minor = atoi((char *)name + 8);
6575
6576 /* Expect "patch-9.9.01234". */
6577 n = (major < VIM_VERSION_MAJOR
6578 || (major == VIM_VERSION_MAJOR
6579 && (minor < VIM_VERSION_MINOR
6580 || (minor == VIM_VERSION_MINOR
6581 && has_patch(atoi((char *)name + 10))))));
6582 }
6583 else
6584 n = has_patch(atoi((char *)name + 5));
6585 }
6586 else if (STRICMP(name, "vim_starting") == 0)
6587 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006588 else if (STRICMP(name, "ttyin") == 0)
6589 n = mch_input_isatty();
6590 else if (STRICMP(name, "ttyout") == 0)
6591 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 else if (STRICMP(name, "multi_byte_encoding") == 0)
6593 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006594#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006595 else if (STRICMP(name, "balloon_multiline") == 0)
6596 n = multiline_balloon_available();
6597#endif
6598#ifdef DYNAMIC_TCL
6599 else if (STRICMP(name, "tcl") == 0)
6600 n = tcl_enabled(FALSE);
6601#endif
6602#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6603 else if (STRICMP(name, "iconv") == 0)
6604 n = iconv_enabled(FALSE);
6605#endif
6606#ifdef DYNAMIC_LUA
6607 else if (STRICMP(name, "lua") == 0)
6608 n = lua_enabled(FALSE);
6609#endif
6610#ifdef DYNAMIC_MZSCHEME
6611 else if (STRICMP(name, "mzscheme") == 0)
6612 n = mzscheme_enabled(FALSE);
6613#endif
6614#ifdef DYNAMIC_RUBY
6615 else if (STRICMP(name, "ruby") == 0)
6616 n = ruby_enabled(FALSE);
6617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618#ifdef DYNAMIC_PYTHON
6619 else if (STRICMP(name, "python") == 0)
6620 n = python_enabled(FALSE);
6621#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef DYNAMIC_PYTHON3
6623 else if (STRICMP(name, "python3") == 0)
6624 n = python3_enabled(FALSE);
6625#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006626#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6627 else if (STRICMP(name, "pythonx") == 0)
6628 {
6629# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6630 if (p_pyx == 0)
6631 n = python3_enabled(FALSE) || python_enabled(FALSE);
6632 else if (p_pyx == 3)
6633 n = python3_enabled(FALSE);
6634 else if (p_pyx == 2)
6635 n = python_enabled(FALSE);
6636# elif defined(DYNAMIC_PYTHON)
6637 n = python_enabled(FALSE);
6638# elif defined(DYNAMIC_PYTHON3)
6639 n = python3_enabled(FALSE);
6640# endif
6641 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#endif
6643#ifdef DYNAMIC_PERL
6644 else if (STRICMP(name, "perl") == 0)
6645 n = perl_enabled(FALSE);
6646#endif
6647#ifdef FEAT_GUI
6648 else if (STRICMP(name, "gui_running") == 0)
6649 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650# ifdef FEAT_BROWSE
6651 else if (STRICMP(name, "browse") == 0)
6652 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6653# endif
6654#endif
6655#ifdef FEAT_SYN_HL
6656 else if (STRICMP(name, "syntax_items") == 0)
6657 n = syntax_present(curwin);
6658#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659#ifdef FEAT_VTP
6660 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006661 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662#endif
6663#ifdef FEAT_NETBEANS_INTG
6664 else if (STRICMP(name, "netbeans_enabled") == 0)
6665 n = netbeans_active();
6666#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006667#ifdef FEAT_MOUSE_GPM
6668 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6669 n = gpm_enabled();
6670#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006671#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006672 else if (STRICMP(name, "terminal") == 0)
6673 n = terminal_enabled();
6674#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006675#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006676 else if (STRICMP(name, "conpty") == 0)
6677 n = use_conpty();
6678#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 }
6680
6681 rettv->vval.v_number = n;
6682}
6683
6684/*
6685 * "has_key()" function
6686 */
6687 static void
6688f_has_key(typval_T *argvars, typval_T *rettv)
6689{
6690 if (argvars[0].v_type != VAR_DICT)
6691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006692 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693 return;
6694 }
6695 if (argvars[0].vval.v_dict == NULL)
6696 return;
6697
6698 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006699 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700}
6701
6702/*
6703 * "haslocaldir()" function
6704 */
6705 static void
6706f_haslocaldir(typval_T *argvars, typval_T *rettv)
6707{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006708 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709 win_T *wp = NULL;
6710
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006711 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6712
6713 // Check for window-local and tab-local directories
6714 if (wp != NULL && wp->w_localdir != NULL)
6715 rettv->vval.v_number = 1;
6716 else if (tp != NULL && tp->tp_localdir != NULL)
6717 rettv->vval.v_number = 2;
6718 else
6719 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720}
6721
6722/*
6723 * "hasmapto()" function
6724 */
6725 static void
6726f_hasmapto(typval_T *argvars, typval_T *rettv)
6727{
6728 char_u *name;
6729 char_u *mode;
6730 char_u buf[NUMBUFLEN];
6731 int abbr = FALSE;
6732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006733 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 if (argvars[1].v_type == VAR_UNKNOWN)
6735 mode = (char_u *)"nvo";
6736 else
6737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006738 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006740 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 }
6742
6743 if (map_to_exists(name, mode, abbr))
6744 rettv->vval.v_number = TRUE;
6745 else
6746 rettv->vval.v_number = FALSE;
6747}
6748
6749/*
6750 * "histadd()" function
6751 */
6752 static void
6753f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6754{
6755#ifdef FEAT_CMDHIST
6756 int histype;
6757 char_u *str;
6758 char_u buf[NUMBUFLEN];
6759#endif
6760
6761 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006762 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763 return;
6764#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006765 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 histype = str != NULL ? get_histtype(str) : -1;
6767 if (histype >= 0)
6768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006769 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 if (*str != NUL)
6771 {
6772 init_history();
6773 add_to_history(histype, str, FALSE, NUL);
6774 rettv->vval.v_number = TRUE;
6775 return;
6776 }
6777 }
6778#endif
6779}
6780
6781/*
6782 * "histdel()" function
6783 */
6784 static void
6785f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6786{
6787#ifdef FEAT_CMDHIST
6788 int n;
6789 char_u buf[NUMBUFLEN];
6790 char_u *str;
6791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006792 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793 if (str == NULL)
6794 n = 0;
6795 else if (argvars[1].v_type == VAR_UNKNOWN)
6796 /* only one argument: clear entire history */
6797 n = clr_history(get_histtype(str));
6798 else if (argvars[1].v_type == VAR_NUMBER)
6799 /* index given: remove that entry */
6800 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006801 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 else
6803 /* string given: remove all matching entries */
6804 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006805 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 rettv->vval.v_number = n;
6807#endif
6808}
6809
6810/*
6811 * "histget()" function
6812 */
6813 static void
6814f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6815{
6816#ifdef FEAT_CMDHIST
6817 int type;
6818 int idx;
6819 char_u *str;
6820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006821 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 if (str == NULL)
6823 rettv->vval.v_string = NULL;
6824 else
6825 {
6826 type = get_histtype(str);
6827 if (argvars[1].v_type == VAR_UNKNOWN)
6828 idx = get_history_idx(type);
6829 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006830 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006831 /* -1 on type error */
6832 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6833 }
6834#else
6835 rettv->vval.v_string = NULL;
6836#endif
6837 rettv->v_type = VAR_STRING;
6838}
6839
6840/*
6841 * "histnr()" function
6842 */
6843 static void
6844f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6845{
6846 int i;
6847
6848#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006849 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850
6851 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6852 if (i >= HIST_CMD && i < HIST_COUNT)
6853 i = get_history_idx(i);
6854 else
6855#endif
6856 i = -1;
6857 rettv->vval.v_number = i;
6858}
6859
6860/*
6861 * "highlightID(name)" function
6862 */
6863 static void
6864f_hlID(typval_T *argvars, typval_T *rettv)
6865{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006866 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867}
6868
6869/*
6870 * "highlight_exists()" function
6871 */
6872 static void
6873f_hlexists(typval_T *argvars, typval_T *rettv)
6874{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876}
6877
6878/*
6879 * "hostname()" function
6880 */
6881 static void
6882f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6883{
6884 char_u hostname[256];
6885
6886 mch_get_host_name(hostname, 256);
6887 rettv->v_type = VAR_STRING;
6888 rettv->vval.v_string = vim_strsave(hostname);
6889}
6890
6891/*
6892 * iconv() function
6893 */
6894 static void
6895f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6896{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 char_u buf1[NUMBUFLEN];
6898 char_u buf2[NUMBUFLEN];
6899 char_u *from, *to, *str;
6900 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901
6902 rettv->v_type = VAR_STRING;
6903 rettv->vval.v_string = NULL;
6904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 str = tv_get_string(&argvars[0]);
6906 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6907 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 vimconv.vc_type = CONV_NONE;
6909 convert_setup(&vimconv, from, to);
6910
6911 /* If the encodings are equal, no conversion needed. */
6912 if (vimconv.vc_type == CONV_NONE)
6913 rettv->vval.v_string = vim_strsave(str);
6914 else
6915 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6916
6917 convert_setup(&vimconv, NULL, NULL);
6918 vim_free(from);
6919 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920}
6921
6922/*
6923 * "indent()" function
6924 */
6925 static void
6926f_indent(typval_T *argvars, typval_T *rettv)
6927{
6928 linenr_T lnum;
6929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006930 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6932 rettv->vval.v_number = get_indent_lnum(lnum);
6933 else
6934 rettv->vval.v_number = -1;
6935}
6936
6937/*
6938 * "index()" function
6939 */
6940 static void
6941f_index(typval_T *argvars, typval_T *rettv)
6942{
6943 list_T *l;
6944 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006945 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 long idx = 0;
6947 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006948 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949
6950 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006951 if (argvars[0].v_type == VAR_BLOB)
6952 {
6953 typval_T tv;
6954 int start = 0;
6955
6956 if (argvars[2].v_type != VAR_UNKNOWN)
6957 {
6958 start = tv_get_number_chk(&argvars[2], &error);
6959 if (error)
6960 return;
6961 }
6962 b = argvars[0].vval.v_blob;
6963 if (b == NULL)
6964 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006965 if (start < 0)
6966 {
6967 start = blob_len(b) + start;
6968 if (start < 0)
6969 start = 0;
6970 }
6971
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006972 for (idx = start; idx < blob_len(b); ++idx)
6973 {
6974 tv.v_type = VAR_NUMBER;
6975 tv.vval.v_number = blob_get(b, idx);
6976 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6977 {
6978 rettv->vval.v_number = idx;
6979 return;
6980 }
6981 }
6982 return;
6983 }
6984 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006986 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 return;
6988 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990 l = argvars[0].vval.v_list;
6991 if (l != NULL)
6992 {
6993 item = l->lv_first;
6994 if (argvars[2].v_type != VAR_UNKNOWN)
6995 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996 /* Start at specified item. Use the cached index that list_find()
6997 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006998 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999 idx = l->lv_idx;
7000 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007001 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002 if (error)
7003 item = NULL;
7004 }
7005
7006 for ( ; item != NULL; item = item->li_next, ++idx)
7007 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7008 {
7009 rettv->vval.v_number = idx;
7010 break;
7011 }
7012 }
7013}
7014
7015static int inputsecret_flag = 0;
7016
7017/*
7018 * "input()" function
7019 * Also handles inputsecret() when inputsecret is set.
7020 */
7021 static void
7022f_input(typval_T *argvars, typval_T *rettv)
7023{
7024 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7025}
7026
7027/*
7028 * "inputdialog()" function
7029 */
7030 static void
7031f_inputdialog(typval_T *argvars, typval_T *rettv)
7032{
7033#if defined(FEAT_GUI_TEXTDIALOG)
7034 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7035 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7036 {
7037 char_u *message;
7038 char_u buf[NUMBUFLEN];
7039 char_u *defstr = (char_u *)"";
7040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7045 else
7046 IObuff[0] = NUL;
7047 if (message != NULL && defstr != NULL
7048 && do_dialog(VIM_QUESTION, NULL, message,
7049 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7050 rettv->vval.v_string = vim_strsave(IObuff);
7051 else
7052 {
7053 if (message != NULL && defstr != NULL
7054 && argvars[1].v_type != VAR_UNKNOWN
7055 && argvars[2].v_type != VAR_UNKNOWN)
7056 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007057 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 else
7059 rettv->vval.v_string = NULL;
7060 }
7061 rettv->v_type = VAR_STRING;
7062 }
7063 else
7064#endif
7065 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7066}
7067
7068/*
7069 * "inputlist()" function
7070 */
7071 static void
7072f_inputlist(typval_T *argvars, typval_T *rettv)
7073{
7074 listitem_T *li;
7075 int selected;
7076 int mouse_used;
7077
7078#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007079 /* While starting up, there is no place to enter text. When running tests
7080 * with --not-a-term we assume feedkeys() will be used. */
7081 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007082 return;
7083#endif
7084 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007087 return;
7088 }
7089
7090 msg_start();
7091 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7092 lines_left = Rows; /* avoid more prompt */
7093 msg_scroll = TRUE;
7094 msg_clr_eos();
7095
7096 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7097 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007098 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 msg_putchar('\n');
7100 }
7101
7102 /* Ask for choice. */
7103 selected = prompt_for_number(&mouse_used);
7104 if (mouse_used)
7105 selected -= lines_left;
7106
7107 rettv->vval.v_number = selected;
7108}
7109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7111
7112/*
7113 * "inputrestore()" function
7114 */
7115 static void
7116f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7117{
7118 if (ga_userinput.ga_len > 0)
7119 {
7120 --ga_userinput.ga_len;
7121 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7122 + ga_userinput.ga_len);
7123 /* default return is zero == OK */
7124 }
7125 else if (p_verbose > 1)
7126 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007127 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 rettv->vval.v_number = 1; /* Failed */
7129 }
7130}
7131
7132/*
7133 * "inputsave()" function
7134 */
7135 static void
7136f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7137{
7138 /* Add an entry to the stack of typeahead storage. */
7139 if (ga_grow(&ga_userinput, 1) == OK)
7140 {
7141 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7142 + ga_userinput.ga_len);
7143 ++ga_userinput.ga_len;
7144 /* default return is zero == OK */
7145 }
7146 else
7147 rettv->vval.v_number = 1; /* Failed */
7148}
7149
7150/*
7151 * "inputsecret()" function
7152 */
7153 static void
7154f_inputsecret(typval_T *argvars, typval_T *rettv)
7155{
7156 ++cmdline_star;
7157 ++inputsecret_flag;
7158 f_input(argvars, rettv);
7159 --cmdline_star;
7160 --inputsecret_flag;
7161}
7162
7163/*
7164 * "insert()" function
7165 */
7166 static void
7167f_insert(typval_T *argvars, typval_T *rettv)
7168{
7169 long before = 0;
7170 listitem_T *item;
7171 list_T *l;
7172 int error = FALSE;
7173
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007174 if (argvars[0].v_type == VAR_BLOB)
7175 {
7176 int val, len;
7177 char_u *p;
7178
7179 len = blob_len(argvars[0].vval.v_blob);
7180 if (argvars[2].v_type != VAR_UNKNOWN)
7181 {
7182 before = (long)tv_get_number_chk(&argvars[2], &error);
7183 if (error)
7184 return; // type error; errmsg already given
7185 if (before < 0 || before > len)
7186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007187 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007188 return;
7189 }
7190 }
7191 val = tv_get_number_chk(&argvars[1], &error);
7192 if (error)
7193 return;
7194 if (val < 0 || val > 255)
7195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007196 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007197 return;
7198 }
7199
7200 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7201 return;
7202 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7203 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7204 *(p + before) = val;
7205 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7206
7207 copy_tv(&argvars[0], rettv);
7208 }
7209 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007210 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007211 else if ((l = argvars[0].vval.v_list) != NULL
7212 && !var_check_lock(l->lv_lock,
7213 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 {
7215 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007216 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 if (error)
7218 return; /* type error; errmsg already given */
7219
7220 if (before == l->lv_len)
7221 item = NULL;
7222 else
7223 {
7224 item = list_find(l, before);
7225 if (item == NULL)
7226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007227 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 l = NULL;
7229 }
7230 }
7231 if (l != NULL)
7232 {
7233 list_insert_tv(l, &argvars[1], item);
7234 copy_tv(&argvars[0], rettv);
7235 }
7236 }
7237}
7238
7239/*
7240 * "invert(expr)" function
7241 */
7242 static void
7243f_invert(typval_T *argvars, typval_T *rettv)
7244{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007245 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246}
7247
7248/*
7249 * "isdirectory()" function
7250 */
7251 static void
7252f_isdirectory(typval_T *argvars, typval_T *rettv)
7253{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007254 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255}
7256
7257/*
7258 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7259 * or it refers to a List or Dictionary that is locked.
7260 */
7261 static int
7262tv_islocked(typval_T *tv)
7263{
7264 return (tv->v_lock & VAR_LOCKED)
7265 || (tv->v_type == VAR_LIST
7266 && tv->vval.v_list != NULL
7267 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7268 || (tv->v_type == VAR_DICT
7269 && tv->vval.v_dict != NULL
7270 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7271}
7272
7273/*
7274 * "islocked()" function
7275 */
7276 static void
7277f_islocked(typval_T *argvars, typval_T *rettv)
7278{
7279 lval_T lv;
7280 char_u *end;
7281 dictitem_T *di;
7282
7283 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007284 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007285 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 if (end != NULL && lv.ll_name != NULL)
7287 {
7288 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007289 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 else
7291 {
7292 if (lv.ll_tv == NULL)
7293 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007294 di = find_var(lv.ll_name, NULL, TRUE);
7295 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007297 /* Consider a variable locked when:
7298 * 1. the variable itself is locked
7299 * 2. the value of the variable is locked.
7300 * 3. the List or Dict value is locked.
7301 */
7302 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7303 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 }
7305 }
7306 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007307 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007308 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 else if (lv.ll_list != NULL)
7311 /* List item. */
7312 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7313 else
7314 /* Dictionary item. */
7315 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7316 }
7317 }
7318
7319 clear_lval(&lv);
7320}
7321
7322#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7323/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007324 * "isinf()" function
7325 */
7326 static void
7327f_isinf(typval_T *argvars, typval_T *rettv)
7328{
7329 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7330 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7331}
7332
7333/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 * "isnan()" function
7335 */
7336 static void
7337f_isnan(typval_T *argvars, typval_T *rettv)
7338{
7339 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7340 && isnan(argvars[0].vval.v_float);
7341}
7342#endif
7343
7344/*
7345 * "items(dict)" function
7346 */
7347 static void
7348f_items(typval_T *argvars, typval_T *rettv)
7349{
7350 dict_list(argvars, rettv, 2);
7351}
7352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353/*
7354 * "join()" function
7355 */
7356 static void
7357f_join(typval_T *argvars, typval_T *rettv)
7358{
7359 garray_T ga;
7360 char_u *sep;
7361
7362 if (argvars[0].v_type != VAR_LIST)
7363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 return;
7366 }
7367 if (argvars[0].vval.v_list == NULL)
7368 return;
7369 if (argvars[1].v_type == VAR_UNKNOWN)
7370 sep = (char_u *)" ";
7371 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007372 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373
7374 rettv->v_type = VAR_STRING;
7375
7376 if (sep != NULL)
7377 {
7378 ga_init2(&ga, (int)sizeof(char), 80);
7379 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7380 ga_append(&ga, NUL);
7381 rettv->vval.v_string = (char_u *)ga.ga_data;
7382 }
7383 else
7384 rettv->vval.v_string = NULL;
7385}
7386
7387/*
7388 * "js_decode()" function
7389 */
7390 static void
7391f_js_decode(typval_T *argvars, typval_T *rettv)
7392{
7393 js_read_T reader;
7394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396 reader.js_fill = NULL;
7397 reader.js_used = 0;
7398 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007399 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400}
7401
7402/*
7403 * "js_encode()" function
7404 */
7405 static void
7406f_js_encode(typval_T *argvars, typval_T *rettv)
7407{
7408 rettv->v_type = VAR_STRING;
7409 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7410}
7411
7412/*
7413 * "json_decode()" function
7414 */
7415 static void
7416f_json_decode(typval_T *argvars, typval_T *rettv)
7417{
7418 js_read_T reader;
7419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 reader.js_fill = NULL;
7422 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007423 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007424}
7425
7426/*
7427 * "json_encode()" function
7428 */
7429 static void
7430f_json_encode(typval_T *argvars, typval_T *rettv)
7431{
7432 rettv->v_type = VAR_STRING;
7433 rettv->vval.v_string = json_encode(&argvars[0], 0);
7434}
7435
7436/*
7437 * "keys()" function
7438 */
7439 static void
7440f_keys(typval_T *argvars, typval_T *rettv)
7441{
7442 dict_list(argvars, rettv, 0);
7443}
7444
7445/*
7446 * "last_buffer_nr()" function.
7447 */
7448 static void
7449f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7450{
7451 int n = 0;
7452 buf_T *buf;
7453
Bram Moolenaar29323592016-07-24 22:04:11 +02007454 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 if (n < buf->b_fnum)
7456 n = buf->b_fnum;
7457
7458 rettv->vval.v_number = n;
7459}
7460
7461/*
7462 * "len()" function
7463 */
7464 static void
7465f_len(typval_T *argvars, typval_T *rettv)
7466{
7467 switch (argvars[0].v_type)
7468 {
7469 case VAR_STRING:
7470 case VAR_NUMBER:
7471 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007472 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007474 case VAR_BLOB:
7475 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7476 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 case VAR_LIST:
7478 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7479 break;
7480 case VAR_DICT:
7481 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7482 break;
7483 case VAR_UNKNOWN:
7484 case VAR_SPECIAL:
7485 case VAR_FLOAT:
7486 case VAR_FUNC:
7487 case VAR_PARTIAL:
7488 case VAR_JOB:
7489 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 break;
7492 }
7493}
7494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007496libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497{
7498#ifdef FEAT_LIBCALL
7499 char_u *string_in;
7500 char_u **string_result;
7501 int nr_result;
7502#endif
7503
7504 rettv->v_type = type;
7505 if (type != VAR_NUMBER)
7506 rettv->vval.v_string = NULL;
7507
7508 if (check_restricted() || check_secure())
7509 return;
7510
7511#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007512 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7514 {
7515 string_in = NULL;
7516 if (argvars[2].v_type == VAR_STRING)
7517 string_in = argvars[2].vval.v_string;
7518 if (type == VAR_NUMBER)
7519 string_result = NULL;
7520 else
7521 string_result = &rettv->vval.v_string;
7522 if (mch_libcall(argvars[0].vval.v_string,
7523 argvars[1].vval.v_string,
7524 string_in,
7525 argvars[2].vval.v_number,
7526 string_result,
7527 &nr_result) == OK
7528 && type == VAR_NUMBER)
7529 rettv->vval.v_number = nr_result;
7530 }
7531#endif
7532}
7533
7534/*
7535 * "libcall()" function
7536 */
7537 static void
7538f_libcall(typval_T *argvars, typval_T *rettv)
7539{
7540 libcall_common(argvars, rettv, VAR_STRING);
7541}
7542
7543/*
7544 * "libcallnr()" function
7545 */
7546 static void
7547f_libcallnr(typval_T *argvars, typval_T *rettv)
7548{
7549 libcall_common(argvars, rettv, VAR_NUMBER);
7550}
7551
7552/*
7553 * "line(string)" function
7554 */
7555 static void
7556f_line(typval_T *argvars, typval_T *rettv)
7557{
7558 linenr_T lnum = 0;
7559 pos_T *fp;
7560 int fnum;
7561
7562 fp = var2fpos(&argvars[0], TRUE, &fnum);
7563 if (fp != NULL)
7564 lnum = fp->lnum;
7565 rettv->vval.v_number = lnum;
7566}
7567
7568/*
7569 * "line2byte(lnum)" function
7570 */
7571 static void
7572f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7573{
7574#ifndef FEAT_BYTEOFF
7575 rettv->vval.v_number = -1;
7576#else
7577 linenr_T lnum;
7578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007579 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7581 rettv->vval.v_number = -1;
7582 else
7583 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7584 if (rettv->vval.v_number >= 0)
7585 ++rettv->vval.v_number;
7586#endif
7587}
7588
7589/*
7590 * "lispindent(lnum)" function
7591 */
7592 static void
7593f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7594{
7595#ifdef FEAT_LISP
7596 pos_T pos;
7597 linenr_T lnum;
7598
7599 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007600 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7602 {
7603 curwin->w_cursor.lnum = lnum;
7604 rettv->vval.v_number = get_lisp_indent();
7605 curwin->w_cursor = pos;
7606 }
7607 else
7608#endif
7609 rettv->vval.v_number = -1;
7610}
7611
7612/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007613 * "list2str()" function
7614 */
7615 static void
7616f_list2str(typval_T *argvars, typval_T *rettv)
7617{
7618 list_T *l;
7619 listitem_T *li;
7620 garray_T ga;
7621 int utf8 = FALSE;
7622
7623 rettv->v_type = VAR_STRING;
7624 rettv->vval.v_string = NULL;
7625 if (argvars[0].v_type != VAR_LIST)
7626 {
7627 emsg(_(e_invarg));
7628 return;
7629 }
7630
7631 l = argvars[0].vval.v_list;
7632 if (l == NULL)
7633 return; // empty list results in empty string
7634
7635 if (argvars[1].v_type != VAR_UNKNOWN)
7636 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7637
7638 ga_init2(&ga, 1, 80);
7639 if (has_mbyte || utf8)
7640 {
7641 char_u buf[MB_MAXBYTES + 1];
7642 int (*char2bytes)(int, char_u *);
7643
7644 if (utf8 || enc_utf8)
7645 char2bytes = utf_char2bytes;
7646 else
7647 char2bytes = mb_char2bytes;
7648
7649 for (li = l->lv_first; li != NULL; li = li->li_next)
7650 {
7651 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7652 ga_concat(&ga, buf);
7653 }
7654 ga_append(&ga, NUL);
7655 }
7656 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7657 {
7658 for (li = l->lv_first; li != NULL; li = li->li_next)
7659 ga_append(&ga, tv_get_number(&li->li_tv));
7660 ga_append(&ga, NUL);
7661 }
7662
7663 rettv->v_type = VAR_STRING;
7664 rettv->vval.v_string = ga.ga_data;
7665}
7666
7667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 * "localtime()" function
7669 */
7670 static void
7671f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7672{
7673 rettv->vval.v_number = (varnumber_T)time(NULL);
7674}
7675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676 static void
7677get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7678{
7679 char_u *keys;
7680 char_u *which;
7681 char_u buf[NUMBUFLEN];
7682 char_u *keys_buf = NULL;
7683 char_u *rhs;
7684 int mode;
7685 int abbr = FALSE;
7686 int get_dict = FALSE;
7687 mapblock_T *mp;
7688 int buffer_local;
7689
7690 /* return empty string for failure */
7691 rettv->v_type = VAR_STRING;
7692 rettv->vval.v_string = NULL;
7693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007694 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 if (*keys == NUL)
7696 return;
7697
7698 if (argvars[1].v_type != VAR_UNKNOWN)
7699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 if (argvars[2].v_type != VAR_UNKNOWN)
7702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007703 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007705 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 }
7707 }
7708 else
7709 which = (char_u *)"";
7710 if (which == NULL)
7711 return;
7712
7713 mode = get_map_mode(&which, 0);
7714
7715 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7716 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7717 vim_free(keys_buf);
7718
7719 if (!get_dict)
7720 {
7721 /* Return a string. */
7722 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007723 {
7724 if (*rhs == NUL)
7725 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7726 else
7727 rettv->vval.v_string = str2special_save(rhs, FALSE);
7728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729
7730 }
7731 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7732 {
7733 /* Return a dictionary. */
7734 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7735 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7736 dict_T *dict = rettv->vval.v_dict;
7737
Bram Moolenaare0be1672018-07-08 16:50:37 +02007738 dict_add_string(dict, "lhs", lhs);
7739 dict_add_string(dict, "rhs", mp->m_orig_str);
7740 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7741 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7742 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007743 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7744 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007745 dict_add_number(dict, "buffer", (long)buffer_local);
7746 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7747 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748
7749 vim_free(lhs);
7750 vim_free(mapmode);
7751 }
7752}
7753
7754#ifdef FEAT_FLOAT
7755/*
7756 * "log()" function
7757 */
7758 static void
7759f_log(typval_T *argvars, typval_T *rettv)
7760{
7761 float_T f = 0.0;
7762
7763 rettv->v_type = VAR_FLOAT;
7764 if (get_float_arg(argvars, &f) == OK)
7765 rettv->vval.v_float = log(f);
7766 else
7767 rettv->vval.v_float = 0.0;
7768}
7769
7770/*
7771 * "log10()" function
7772 */
7773 static void
7774f_log10(typval_T *argvars, typval_T *rettv)
7775{
7776 float_T f = 0.0;
7777
7778 rettv->v_type = VAR_FLOAT;
7779 if (get_float_arg(argvars, &f) == OK)
7780 rettv->vval.v_float = log10(f);
7781 else
7782 rettv->vval.v_float = 0.0;
7783}
7784#endif
7785
7786#ifdef FEAT_LUA
7787/*
7788 * "luaeval()" function
7789 */
7790 static void
7791f_luaeval(typval_T *argvars, typval_T *rettv)
7792{
7793 char_u *str;
7794 char_u buf[NUMBUFLEN];
7795
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007796 if (check_restricted() || check_secure())
7797 return;
7798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007799 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 do_luaeval(str, argvars + 1, rettv);
7801}
7802#endif
7803
7804/*
7805 * "map()" function
7806 */
7807 static void
7808f_map(typval_T *argvars, typval_T *rettv)
7809{
7810 filter_map(argvars, rettv, TRUE);
7811}
7812
7813/*
7814 * "maparg()" function
7815 */
7816 static void
7817f_maparg(typval_T *argvars, typval_T *rettv)
7818{
7819 get_maparg(argvars, rettv, TRUE);
7820}
7821
7822/*
7823 * "mapcheck()" function
7824 */
7825 static void
7826f_mapcheck(typval_T *argvars, typval_T *rettv)
7827{
7828 get_maparg(argvars, rettv, FALSE);
7829}
7830
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007831typedef enum
7832{
7833 MATCH_END, /* matchend() */
7834 MATCH_MATCH, /* match() */
7835 MATCH_STR, /* matchstr() */
7836 MATCH_LIST, /* matchlist() */
7837 MATCH_POS /* matchstrpos() */
7838} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839
7840 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007841find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842{
7843 char_u *str = NULL;
7844 long len = 0;
7845 char_u *expr = NULL;
7846 char_u *pat;
7847 regmatch_T regmatch;
7848 char_u patbuf[NUMBUFLEN];
7849 char_u strbuf[NUMBUFLEN];
7850 char_u *save_cpo;
7851 long start = 0;
7852 long nth = 1;
7853 colnr_T startcol = 0;
7854 int match = 0;
7855 list_T *l = NULL;
7856 listitem_T *li = NULL;
7857 long idx = 0;
7858 char_u *tofree = NULL;
7859
7860 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7861 save_cpo = p_cpo;
7862 p_cpo = (char_u *)"";
7863
7864 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007865 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007867 /* type MATCH_LIST: return empty list when there are no matches.
7868 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 if (rettv_list_alloc(rettv) == FAIL)
7870 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007871 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 && (list_append_string(rettv->vval.v_list,
7873 (char_u *)"", 0) == FAIL
7874 || list_append_number(rettv->vval.v_list,
7875 (varnumber_T)-1) == FAIL
7876 || list_append_number(rettv->vval.v_list,
7877 (varnumber_T)-1) == FAIL
7878 || list_append_number(rettv->vval.v_list,
7879 (varnumber_T)-1) == FAIL))
7880 {
7881 list_free(rettv->vval.v_list);
7882 rettv->vval.v_list = NULL;
7883 goto theend;
7884 }
7885 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007886 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 {
7888 rettv->v_type = VAR_STRING;
7889 rettv->vval.v_string = NULL;
7890 }
7891
7892 if (argvars[0].v_type == VAR_LIST)
7893 {
7894 if ((l = argvars[0].vval.v_list) == NULL)
7895 goto theend;
7896 li = l->lv_first;
7897 }
7898 else
7899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007900 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 len = (long)STRLEN(str);
7902 }
7903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 if (pat == NULL)
7906 goto theend;
7907
7908 if (argvars[2].v_type != VAR_UNKNOWN)
7909 {
7910 int error = FALSE;
7911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007912 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 if (error)
7914 goto theend;
7915 if (l != NULL)
7916 {
7917 li = list_find(l, start);
7918 if (li == NULL)
7919 goto theend;
7920 idx = l->lv_idx; /* use the cached index */
7921 }
7922 else
7923 {
7924 if (start < 0)
7925 start = 0;
7926 if (start > len)
7927 goto theend;
7928 /* When "count" argument is there ignore matches before "start",
7929 * otherwise skip part of the string. Differs when pattern is "^"
7930 * or "\<". */
7931 if (argvars[3].v_type != VAR_UNKNOWN)
7932 startcol = start;
7933 else
7934 {
7935 str += start;
7936 len -= start;
7937 }
7938 }
7939
7940 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007941 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 if (error)
7943 goto theend;
7944 }
7945
7946 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7947 if (regmatch.regprog != NULL)
7948 {
7949 regmatch.rm_ic = p_ic;
7950
7951 for (;;)
7952 {
7953 if (l != NULL)
7954 {
7955 if (li == NULL)
7956 {
7957 match = FALSE;
7958 break;
7959 }
7960 vim_free(tofree);
7961 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7962 if (str == NULL)
7963 break;
7964 }
7965
7966 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7967
7968 if (match && --nth <= 0)
7969 break;
7970 if (l == NULL && !match)
7971 break;
7972
7973 /* Advance to just after the match. */
7974 if (l != NULL)
7975 {
7976 li = li->li_next;
7977 ++idx;
7978 }
7979 else
7980 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 startcol = (colnr_T)(regmatch.startp[0]
7982 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 if (startcol > (colnr_T)len
7984 || str + startcol <= regmatch.startp[0])
7985 {
7986 match = FALSE;
7987 break;
7988 }
7989 }
7990 }
7991
7992 if (match)
7993 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007994 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 {
7996 listitem_T *li1 = rettv->vval.v_list->lv_first;
7997 listitem_T *li2 = li1->li_next;
7998 listitem_T *li3 = li2->li_next;
7999 listitem_T *li4 = li3->li_next;
8000
8001 vim_free(li1->li_tv.vval.v_string);
8002 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8003 (int)(regmatch.endp[0] - regmatch.startp[0]));
8004 li3->li_tv.vval.v_number =
8005 (varnumber_T)(regmatch.startp[0] - expr);
8006 li4->li_tv.vval.v_number =
8007 (varnumber_T)(regmatch.endp[0] - expr);
8008 if (l != NULL)
8009 li2->li_tv.vval.v_number = (varnumber_T)idx;
8010 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008011 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 {
8013 int i;
8014
8015 /* return list with matched string and submatches */
8016 for (i = 0; i < NSUBEXP; ++i)
8017 {
8018 if (regmatch.endp[i] == NULL)
8019 {
8020 if (list_append_string(rettv->vval.v_list,
8021 (char_u *)"", 0) == FAIL)
8022 break;
8023 }
8024 else if (list_append_string(rettv->vval.v_list,
8025 regmatch.startp[i],
8026 (int)(regmatch.endp[i] - regmatch.startp[i]))
8027 == FAIL)
8028 break;
8029 }
8030 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008031 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 {
8033 /* return matched string */
8034 if (l != NULL)
8035 copy_tv(&li->li_tv, rettv);
8036 else
8037 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8038 (int)(regmatch.endp[0] - regmatch.startp[0]));
8039 }
8040 else if (l != NULL)
8041 rettv->vval.v_number = idx;
8042 else
8043 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008044 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 rettv->vval.v_number =
8046 (varnumber_T)(regmatch.startp[0] - str);
8047 else
8048 rettv->vval.v_number =
8049 (varnumber_T)(regmatch.endp[0] - str);
8050 rettv->vval.v_number += (varnumber_T)(str - expr);
8051 }
8052 }
8053 vim_regfree(regmatch.regprog);
8054 }
8055
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008056theend:
8057 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 /* matchstrpos() without a list: drop the second item. */
8059 listitem_remove(rettv->vval.v_list,
8060 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 vim_free(tofree);
8062 p_cpo = save_cpo;
8063}
8064
8065/*
8066 * "match()" function
8067 */
8068 static void
8069f_match(typval_T *argvars, typval_T *rettv)
8070{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008071 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072}
8073
Bram Moolenaar95e51472018-07-28 16:55:56 +02008074#ifdef FEAT_SEARCH_EXTRA
8075 static int
8076matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8077{
8078 dictitem_T *di;
8079
8080 if (tv->v_type != VAR_DICT)
8081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008082 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008083 return FAIL;
8084 }
8085
8086 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008087 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008088 (char_u *)"conceal", FALSE);
8089
8090 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8091 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008092 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008093 if (*win == NULL)
8094 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008095 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008096 return FAIL;
8097 }
8098 }
8099
8100 return OK;
8101}
8102#endif
8103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104/*
8105 * "matchadd()" function
8106 */
8107 static void
8108f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8109{
8110#ifdef FEAT_SEARCH_EXTRA
8111 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008112 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8113 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 int prio = 10; /* default priority */
8115 int id = -1;
8116 int error = FALSE;
8117 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008118 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119
8120 rettv->vval.v_number = -1;
8121
8122 if (grp == NULL || pat == NULL)
8123 return;
8124 if (argvars[2].v_type != VAR_UNKNOWN)
8125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 if (argvars[3].v_type != VAR_UNKNOWN)
8128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008129 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008130 if (argvars[4].v_type != VAR_UNKNOWN
8131 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8132 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 }
8134 }
8135 if (error == TRUE)
8136 return;
8137 if (id >= 1 && id <= 3)
8138 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008139 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 return;
8141 }
8142
Bram Moolenaar95e51472018-07-28 16:55:56 +02008143 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 conceal_char);
8145#endif
8146}
8147
8148/*
8149 * "matchaddpos()" function
8150 */
8151 static void
8152f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8153{
8154#ifdef FEAT_SEARCH_EXTRA
8155 char_u buf[NUMBUFLEN];
8156 char_u *group;
8157 int prio = 10;
8158 int id = -1;
8159 int error = FALSE;
8160 list_T *l;
8161 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008162 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163
8164 rettv->vval.v_number = -1;
8165
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008166 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 if (group == NULL)
8168 return;
8169
8170 if (argvars[1].v_type != VAR_LIST)
8171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008172 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 return;
8174 }
8175 l = argvars[1].vval.v_list;
8176 if (l == NULL)
8177 return;
8178
8179 if (argvars[2].v_type != VAR_UNKNOWN)
8180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008181 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182 if (argvars[3].v_type != VAR_UNKNOWN)
8183 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008184 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008185
8186 if (argvars[4].v_type != VAR_UNKNOWN
8187 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8188 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 }
8190 }
8191 if (error == TRUE)
8192 return;
8193
8194 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8195 if (id == 1 || id == 2)
8196 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008197 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 return;
8199 }
8200
Bram Moolenaar95e51472018-07-28 16:55:56 +02008201 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 conceal_char);
8203#endif
8204}
8205
8206/*
8207 * "matcharg()" function
8208 */
8209 static void
8210f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8211{
8212 if (rettv_list_alloc(rettv) == OK)
8213 {
8214#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008215 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 matchitem_T *m;
8217
8218 if (id >= 1 && id <= 3)
8219 {
8220 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8221 {
8222 list_append_string(rettv->vval.v_list,
8223 syn_id2name(m->hlg_id), -1);
8224 list_append_string(rettv->vval.v_list, m->pattern, -1);
8225 }
8226 else
8227 {
8228 list_append_string(rettv->vval.v_list, NULL, -1);
8229 list_append_string(rettv->vval.v_list, NULL, -1);
8230 }
8231 }
8232#endif
8233 }
8234}
8235
8236/*
8237 * "matchdelete()" function
8238 */
8239 static void
8240f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8241{
8242#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008243 win_T *win = get_optional_window(argvars, 1);
8244
8245 if (win == NULL)
8246 rettv->vval.v_number = -1;
8247 else
8248 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250#endif
8251}
8252
8253/*
8254 * "matchend()" function
8255 */
8256 static void
8257f_matchend(typval_T *argvars, typval_T *rettv)
8258{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008259 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260}
8261
8262/*
8263 * "matchlist()" function
8264 */
8265 static void
8266f_matchlist(typval_T *argvars, typval_T *rettv)
8267{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008268 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269}
8270
8271/*
8272 * "matchstr()" function
8273 */
8274 static void
8275f_matchstr(typval_T *argvars, typval_T *rettv)
8276{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008277 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278}
8279
8280/*
8281 * "matchstrpos()" function
8282 */
8283 static void
8284f_matchstrpos(typval_T *argvars, typval_T *rettv)
8285{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008286 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287}
8288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 static void
8290max_min(typval_T *argvars, typval_T *rettv, int domax)
8291{
8292 varnumber_T n = 0;
8293 varnumber_T i;
8294 int error = FALSE;
8295
8296 if (argvars[0].v_type == VAR_LIST)
8297 {
8298 list_T *l;
8299 listitem_T *li;
8300
8301 l = argvars[0].vval.v_list;
8302 if (l != NULL)
8303 {
8304 li = l->lv_first;
8305 if (li != NULL)
8306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 for (;;)
8309 {
8310 li = li->li_next;
8311 if (li == NULL)
8312 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008313 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 if (domax ? i > n : i < n)
8315 n = i;
8316 }
8317 }
8318 }
8319 }
8320 else if (argvars[0].v_type == VAR_DICT)
8321 {
8322 dict_T *d;
8323 int first = TRUE;
8324 hashitem_T *hi;
8325 int todo;
8326
8327 d = argvars[0].vval.v_dict;
8328 if (d != NULL)
8329 {
8330 todo = (int)d->dv_hashtab.ht_used;
8331 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8332 {
8333 if (!HASHITEM_EMPTY(hi))
8334 {
8335 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008336 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 if (first)
8338 {
8339 n = i;
8340 first = FALSE;
8341 }
8342 else if (domax ? i > n : i < n)
8343 n = i;
8344 }
8345 }
8346 }
8347 }
8348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008349 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 rettv->vval.v_number = error ? 0 : n;
8351}
8352
8353/*
8354 * "max()" function
8355 */
8356 static void
8357f_max(typval_T *argvars, typval_T *rettv)
8358{
8359 max_min(argvars, rettv, TRUE);
8360}
8361
8362/*
8363 * "min()" function
8364 */
8365 static void
8366f_min(typval_T *argvars, typval_T *rettv)
8367{
8368 max_min(argvars, rettv, FALSE);
8369}
8370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371/*
8372 * Create the directory in which "dir" is located, and higher levels when
8373 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008374 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 */
8376 static int
8377mkdir_recurse(char_u *dir, int prot)
8378{
8379 char_u *p;
8380 char_u *updir;
8381 int r = FAIL;
8382
8383 /* Get end of directory name in "dir".
8384 * We're done when it's "/" or "c:/". */
8385 p = gettail_sep(dir);
8386 if (p <= get_past_head(dir))
8387 return OK;
8388
8389 /* If the directory exists we're done. Otherwise: create it.*/
8390 updir = vim_strnsave(dir, (int)(p - dir));
8391 if (updir == NULL)
8392 return FAIL;
8393 if (mch_isdir(updir))
8394 r = OK;
8395 else if (mkdir_recurse(updir, prot) == OK)
8396 r = vim_mkdir_emsg(updir, prot);
8397 vim_free(updir);
8398 return r;
8399}
8400
8401#ifdef vim_mkdir
8402/*
8403 * "mkdir()" function
8404 */
8405 static void
8406f_mkdir(typval_T *argvars, typval_T *rettv)
8407{
8408 char_u *dir;
8409 char_u buf[NUMBUFLEN];
8410 int prot = 0755;
8411
8412 rettv->vval.v_number = FAIL;
8413 if (check_restricted() || check_secure())
8414 return;
8415
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008418 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008420 if (*gettail(dir) == NUL)
8421 /* remove trailing slashes */
8422 *gettail_sep(dir) = NUL;
8423
8424 if (argvars[1].v_type != VAR_UNKNOWN)
8425 {
8426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008429 if (prot == -1)
8430 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008432 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008433 {
8434 if (mch_isdir(dir))
8435 {
8436 /* With the "p" flag it's OK if the dir already exists. */
8437 rettv->vval.v_number = OK;
8438 return;
8439 }
8440 mkdir_recurse(dir, prot);
8441 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008443 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444}
8445#endif
8446
8447/*
8448 * "mode()" function
8449 */
8450 static void
8451f_mode(typval_T *argvars, typval_T *rettv)
8452{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008453 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454
Bram Moolenaar612cc382018-07-29 15:34:26 +02008455 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456
8457 if (time_for_testing == 93784)
8458 {
8459 /* Testing the two-character code. */
8460 buf[0] = 'x';
8461 buf[1] = '!';
8462 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008463#ifdef FEAT_TERMINAL
8464 else if (term_use_loop())
8465 buf[0] = 't';
8466#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 else if (VIsual_active)
8468 {
8469 if (VIsual_select)
8470 buf[0] = VIsual_mode + 's' - 'v';
8471 else
8472 buf[0] = VIsual_mode;
8473 }
8474 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8475 || State == CONFIRM)
8476 {
8477 buf[0] = 'r';
8478 if (State == ASKMORE)
8479 buf[1] = 'm';
8480 else if (State == CONFIRM)
8481 buf[1] = '?';
8482 }
8483 else if (State == EXTERNCMD)
8484 buf[0] = '!';
8485 else if (State & INSERT)
8486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487 if (State & VREPLACE_FLAG)
8488 {
8489 buf[0] = 'R';
8490 buf[1] = 'v';
8491 }
8492 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008493 {
8494 if (State & REPLACE_FLAG)
8495 buf[0] = 'R';
8496 else
8497 buf[0] = 'i';
8498#ifdef FEAT_INS_EXPAND
8499 if (ins_compl_active())
8500 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008501 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008502 buf[1] = 'x';
8503#endif
8504 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008506 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 {
8508 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008509 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008511 else if (exmode_active == EXMODE_NORMAL)
8512 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 }
8514 else
8515 {
8516 buf[0] = 'n';
8517 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008518 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008520 // to be able to detect force-linewise/blockwise/characterwise operations
8521 buf[2] = motion_force;
8522 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008523 else if (restart_edit == 'I' || restart_edit == 'R'
8524 || restart_edit == 'V')
8525 {
8526 buf[1] = 'i';
8527 buf[2] = restart_edit;
8528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 }
8530
8531 /* Clear out the minor mode when the argument is not a non-zero number or
8532 * non-empty string. */
8533 if (!non_zero_arg(&argvars[0]))
8534 buf[1] = NUL;
8535
8536 rettv->vval.v_string = vim_strsave(buf);
8537 rettv->v_type = VAR_STRING;
8538}
8539
8540#if defined(FEAT_MZSCHEME) || defined(PROTO)
8541/*
8542 * "mzeval()" function
8543 */
8544 static void
8545f_mzeval(typval_T *argvars, typval_T *rettv)
8546{
8547 char_u *str;
8548 char_u buf[NUMBUFLEN];
8549
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008550 if (check_restricted() || check_secure())
8551 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008552 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 do_mzeval(str, rettv);
8554}
8555
8556 void
8557mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8558{
8559 typval_T argvars[3];
8560
8561 argvars[0].v_type = VAR_STRING;
8562 argvars[0].vval.v_string = name;
8563 copy_tv(args, &argvars[1]);
8564 argvars[2].v_type = VAR_UNKNOWN;
8565 f_call(argvars, rettv);
8566 clear_tv(&argvars[1]);
8567}
8568#endif
8569
8570/*
8571 * "nextnonblank()" function
8572 */
8573 static void
8574f_nextnonblank(typval_T *argvars, typval_T *rettv)
8575{
8576 linenr_T lnum;
8577
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 {
8580 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8581 {
8582 lnum = 0;
8583 break;
8584 }
8585 if (*skipwhite(ml_get(lnum)) != NUL)
8586 break;
8587 }
8588 rettv->vval.v_number = lnum;
8589}
8590
8591/*
8592 * "nr2char()" function
8593 */
8594 static void
8595f_nr2char(typval_T *argvars, typval_T *rettv)
8596{
8597 char_u buf[NUMBUFLEN];
8598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 if (has_mbyte)
8600 {
8601 int utf8 = 0;
8602
8603 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008604 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008606 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008608 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 }
8610 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008612 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 buf[1] = NUL;
8614 }
8615 rettv->v_type = VAR_STRING;
8616 rettv->vval.v_string = vim_strsave(buf);
8617}
8618
8619/*
8620 * "or(expr, expr)" function
8621 */
8622 static void
8623f_or(typval_T *argvars, typval_T *rettv)
8624{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008625 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8626 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627}
8628
8629/*
8630 * "pathshorten()" function
8631 */
8632 static void
8633f_pathshorten(typval_T *argvars, typval_T *rettv)
8634{
8635 char_u *p;
8636
8637 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008638 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 if (p == NULL)
8640 rettv->vval.v_string = NULL;
8641 else
8642 {
8643 p = vim_strsave(p);
8644 rettv->vval.v_string = p;
8645 if (p != NULL)
8646 shorten_dir(p);
8647 }
8648}
8649
8650#ifdef FEAT_PERL
8651/*
8652 * "perleval()" function
8653 */
8654 static void
8655f_perleval(typval_T *argvars, typval_T *rettv)
8656{
8657 char_u *str;
8658 char_u buf[NUMBUFLEN];
8659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008660 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 do_perleval(str, rettv);
8662}
8663#endif
8664
8665#ifdef FEAT_FLOAT
8666/*
8667 * "pow()" function
8668 */
8669 static void
8670f_pow(typval_T *argvars, typval_T *rettv)
8671{
8672 float_T fx = 0.0, fy = 0.0;
8673
8674 rettv->v_type = VAR_FLOAT;
8675 if (get_float_arg(argvars, &fx) == OK
8676 && get_float_arg(&argvars[1], &fy) == OK)
8677 rettv->vval.v_float = pow(fx, fy);
8678 else
8679 rettv->vval.v_float = 0.0;
8680}
8681#endif
8682
8683/*
8684 * "prevnonblank()" function
8685 */
8686 static void
8687f_prevnonblank(typval_T *argvars, typval_T *rettv)
8688{
8689 linenr_T lnum;
8690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8693 lnum = 0;
8694 else
8695 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8696 --lnum;
8697 rettv->vval.v_number = lnum;
8698}
8699
8700/* This dummy va_list is here because:
8701 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8702 * - locally in the function results in a "used before set" warning
8703 * - using va_start() to initialize it gives "function with fixed args" error */
8704static va_list ap;
8705
8706/*
8707 * "printf()" function
8708 */
8709 static void
8710f_printf(typval_T *argvars, typval_T *rettv)
8711{
8712 char_u buf[NUMBUFLEN];
8713 int len;
8714 char_u *s;
8715 int saved_did_emsg = did_emsg;
8716 char *fmt;
8717
8718 rettv->v_type = VAR_STRING;
8719 rettv->vval.v_string = NULL;
8720
8721 /* Get the required length, allocate the buffer and do it for real. */
8722 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008724 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 if (!did_emsg)
8726 {
8727 s = alloc(len + 1);
8728 if (s != NULL)
8729 {
8730 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008731 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8732 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 }
8734 }
8735 did_emsg |= saved_did_emsg;
8736}
8737
8738/*
8739 * "pumvisible()" function
8740 */
8741 static void
8742f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8743{
8744#ifdef FEAT_INS_EXPAND
8745 if (pum_visible())
8746 rettv->vval.v_number = 1;
8747#endif
8748}
8749
8750#ifdef FEAT_PYTHON3
8751/*
8752 * "py3eval()" function
8753 */
8754 static void
8755f_py3eval(typval_T *argvars, typval_T *rettv)
8756{
8757 char_u *str;
8758 char_u buf[NUMBUFLEN];
8759
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008760 if (check_restricted() || check_secure())
8761 return;
8762
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008763 if (p_pyx == 0)
8764 p_pyx = 3;
8765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008766 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 do_py3eval(str, rettv);
8768}
8769#endif
8770
8771#ifdef FEAT_PYTHON
8772/*
8773 * "pyeval()" function
8774 */
8775 static void
8776f_pyeval(typval_T *argvars, typval_T *rettv)
8777{
8778 char_u *str;
8779 char_u buf[NUMBUFLEN];
8780
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008781 if (check_restricted() || check_secure())
8782 return;
8783
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008784 if (p_pyx == 0)
8785 p_pyx = 2;
8786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008787 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788 do_pyeval(str, rettv);
8789}
8790#endif
8791
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008792#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8793/*
8794 * "pyxeval()" function
8795 */
8796 static void
8797f_pyxeval(typval_T *argvars, typval_T *rettv)
8798{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008799 if (check_restricted() || check_secure())
8800 return;
8801
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008802# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8803 init_pyxversion();
8804 if (p_pyx == 2)
8805 f_pyeval(argvars, rettv);
8806 else
8807 f_py3eval(argvars, rettv);
8808# elif defined(FEAT_PYTHON)
8809 f_pyeval(argvars, rettv);
8810# elif defined(FEAT_PYTHON3)
8811 f_py3eval(argvars, rettv);
8812# endif
8813}
8814#endif
8815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816/*
8817 * "range()" function
8818 */
8819 static void
8820f_range(typval_T *argvars, typval_T *rettv)
8821{
8822 varnumber_T start;
8823 varnumber_T end;
8824 varnumber_T stride = 1;
8825 varnumber_T i;
8826 int error = FALSE;
8827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008828 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 if (argvars[1].v_type == VAR_UNKNOWN)
8830 {
8831 end = start - 1;
8832 start = 0;
8833 }
8834 else
8835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 }
8840
8841 if (error)
8842 return; /* type error; errmsg already given */
8843 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008844 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008846 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 else
8848 {
8849 if (rettv_list_alloc(rettv) == OK)
8850 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8851 if (list_append_number(rettv->vval.v_list,
8852 (varnumber_T)i) == FAIL)
8853 break;
8854 }
8855}
8856
8857/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008858 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008859 */
8860 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008861readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008862{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008863 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008864 typval_T save_val;
8865 typval_T rettv;
8866 typval_T argv[2];
8867 int retval = 0;
8868 int error = FALSE;
8869
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008870 if (expr->v_type == VAR_UNKNOWN)
8871 return 1;
8872
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008873 prepare_vimvar(VV_VAL, &save_val);
8874 set_vim_var_string(VV_VAL, name, -1);
8875 argv[0].v_type = VAR_STRING;
8876 argv[0].vval.v_string = name;
8877
8878 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8879 goto theend;
8880
8881 retval = tv_get_number_chk(&rettv, &error);
8882 if (error)
8883 retval = -1;
8884 clear_tv(&rettv);
8885
8886theend:
8887 set_vim_var_string(VV_VAL, NULL, 0);
8888 restore_vimvar(VV_VAL, &save_val);
8889 return retval;
8890}
8891
8892/*
8893 * "readdir()" function
8894 */
8895 static void
8896f_readdir(typval_T *argvars, typval_T *rettv)
8897{
8898 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008899 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008900 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008901 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008902 garray_T ga;
8903 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008904
8905 if (rettv_list_alloc(rettv) == FAIL)
8906 return;
8907 path = tv_get_string(&argvars[0]);
8908 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008909
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008910 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8911 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008912 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008913 for (i = 0; i < ga.ga_len; i++)
8914 {
8915 p = ((char_u **)ga.ga_data)[i];
8916 list_append_string(rettv->vval.v_list, p, -1);
8917 }
8918 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008919 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008920}
8921
8922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 * "readfile()" function
8924 */
8925 static void
8926f_readfile(typval_T *argvars, typval_T *rettv)
8927{
8928 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008929 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 int failed = FALSE;
8931 char_u *fname;
8932 FILE *fd;
8933 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8934 int io_size = sizeof(buf);
8935 int readlen; /* size of last fread() */
8936 char_u *prev = NULL; /* previously read bytes, if any */
8937 long prevlen = 0; /* length of data in prev */
8938 long prevsize = 0; /* size of prev buffer */
8939 long maxline = MAXLNUM;
8940 long cnt = 0;
8941 char_u *p; /* position in buf */
8942 char_u *start; /* start of current line */
8943
8944 if (argvars[1].v_type != VAR_UNKNOWN)
8945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008946 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008948 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8949 blob = TRUE;
8950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008952 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953 }
8954
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008955 if (blob)
8956 {
8957 if (rettv_blob_alloc(rettv) == FAIL)
8958 return;
8959 }
8960 else
8961 {
8962 if (rettv_list_alloc(rettv) == FAIL)
8963 return;
8964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965
8966 /* Always open the file in binary mode, library functions have a mind of
8967 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008968 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008971 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 return;
8973 }
8974
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008975 if (blob)
8976 {
8977 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008979 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008980 blob_free(rettv->vval.v_blob);
8981 }
8982 fclose(fd);
8983 return;
8984 }
8985
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 while (cnt < maxline || maxline < 0)
8987 {
8988 readlen = (int)fread(buf, 1, io_size, fd);
8989
8990 /* This for loop processes what was read, but is also entered at end
8991 * of file so that either:
8992 * - an incomplete line gets written
8993 * - a "binary" file gets an empty line at the end if it ends in a
8994 * newline. */
8995 for (p = buf, start = buf;
8996 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8997 ++p)
8998 {
8999 if (*p == '\n' || readlen <= 0)
9000 {
9001 listitem_T *li;
9002 char_u *s = NULL;
9003 long_u len = p - start;
9004
9005 /* Finished a line. Remove CRs before NL. */
9006 if (readlen > 0 && !binary)
9007 {
9008 while (len > 0 && start[len - 1] == '\r')
9009 --len;
9010 /* removal may cross back to the "prev" string */
9011 if (len == 0)
9012 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9013 --prevlen;
9014 }
9015 if (prevlen == 0)
9016 s = vim_strnsave(start, (int)len);
9017 else
9018 {
9019 /* Change "prev" buffer to be the right size. This way
9020 * the bytes are only copied once, and very long lines are
9021 * allocated only once. */
9022 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9023 {
9024 mch_memmove(s + prevlen, start, len);
9025 s[prevlen + len] = NUL;
9026 prev = NULL; /* the list will own the string */
9027 prevlen = prevsize = 0;
9028 }
9029 }
9030 if (s == NULL)
9031 {
9032 do_outofmem_msg((long_u) prevlen + len + 1);
9033 failed = TRUE;
9034 break;
9035 }
9036
9037 if ((li = listitem_alloc()) == NULL)
9038 {
9039 vim_free(s);
9040 failed = TRUE;
9041 break;
9042 }
9043 li->li_tv.v_type = VAR_STRING;
9044 li->li_tv.v_lock = 0;
9045 li->li_tv.vval.v_string = s;
9046 list_append(rettv->vval.v_list, li);
9047
9048 start = p + 1; /* step over newline */
9049 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9050 break;
9051 }
9052 else if (*p == NUL)
9053 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9055 * when finding the BF and check the previous two bytes. */
9056 else if (*p == 0xbf && enc_utf8 && !binary)
9057 {
9058 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9059 * + 1, these may be in the "prev" string. */
9060 char_u back1 = p >= buf + 1 ? p[-1]
9061 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9062 char_u back2 = p >= buf + 2 ? p[-2]
9063 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9064 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9065
9066 if (back2 == 0xef && back1 == 0xbb)
9067 {
9068 char_u *dest = p - 2;
9069
9070 /* Usually a BOM is at the beginning of a file, and so at
9071 * the beginning of a line; then we can just step over it.
9072 */
9073 if (start == dest)
9074 start = p + 1;
9075 else
9076 {
9077 /* have to shuffle buf to close gap */
9078 int adjust_prevlen = 0;
9079
9080 if (dest < buf)
9081 {
9082 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9083 dest = buf;
9084 }
9085 if (readlen > p - buf + 1)
9086 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9087 readlen -= 3 - adjust_prevlen;
9088 prevlen -= adjust_prevlen;
9089 p = dest - 1;
9090 }
9091 }
9092 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 } /* for */
9094
9095 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9096 break;
9097 if (start < p)
9098 {
9099 /* There's part of a line in buf, store it in "prev". */
9100 if (p - start + prevlen >= prevsize)
9101 {
9102 /* need bigger "prev" buffer */
9103 char_u *newprev;
9104
9105 /* A common use case is ordinary text files and "prev" gets a
9106 * fragment of a line, so the first allocation is made
9107 * small, to avoid repeatedly 'allocing' large and
9108 * 'reallocing' small. */
9109 if (prevsize == 0)
9110 prevsize = (long)(p - start);
9111 else
9112 {
9113 long grow50pc = (prevsize * 3) / 2;
9114 long growmin = (long)((p - start) * 2 + prevlen);
9115 prevsize = grow50pc > growmin ? grow50pc : growmin;
9116 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009117 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 if (newprev == NULL)
9119 {
9120 do_outofmem_msg((long_u)prevsize);
9121 failed = TRUE;
9122 break;
9123 }
9124 prev = newprev;
9125 }
9126 /* Add the line part to end of "prev". */
9127 mch_memmove(prev + prevlen, start, p - start);
9128 prevlen += (long)(p - start);
9129 }
9130 } /* while */
9131
9132 /*
9133 * For a negative line count use only the lines at the end of the file,
9134 * free the rest.
9135 */
9136 if (!failed && maxline < 0)
9137 while (cnt > -maxline)
9138 {
9139 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9140 --cnt;
9141 }
9142
9143 if (failed)
9144 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009145 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009147 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 }
9149
9150 vim_free(prev);
9151 fclose(fd);
9152}
9153
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009154 static void
9155return_register(int regname, typval_T *rettv)
9156{
9157 char_u buf[2] = {0, 0};
9158
9159 buf[0] = (char_u)regname;
9160 rettv->v_type = VAR_STRING;
9161 rettv->vval.v_string = vim_strsave(buf);
9162}
9163
9164/*
9165 * "reg_executing()" function
9166 */
9167 static void
9168f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9169{
9170 return_register(reg_executing, rettv);
9171}
9172
9173/*
9174 * "reg_recording()" function
9175 */
9176 static void
9177f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9178{
9179 return_register(reg_recording, rettv);
9180}
9181
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183/*
9184 * Convert a List to proftime_T.
9185 * Return FAIL when there is something wrong.
9186 */
9187 static int
9188list2proftime(typval_T *arg, proftime_T *tm)
9189{
9190 long n1, n2;
9191 int error = FALSE;
9192
9193 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9194 || arg->vval.v_list->lv_len != 2)
9195 return FAIL;
9196 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9197 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009198# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 tm->HighPart = n1;
9200 tm->LowPart = n2;
9201# else
9202 tm->tv_sec = n1;
9203 tm->tv_usec = n2;
9204# endif
9205 return error ? FAIL : OK;
9206}
9207#endif /* FEAT_RELTIME */
9208
9209/*
9210 * "reltime()" function
9211 */
9212 static void
9213f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9214{
9215#ifdef FEAT_RELTIME
9216 proftime_T res;
9217 proftime_T start;
9218
9219 if (argvars[0].v_type == VAR_UNKNOWN)
9220 {
9221 /* No arguments: get current time. */
9222 profile_start(&res);
9223 }
9224 else if (argvars[1].v_type == VAR_UNKNOWN)
9225 {
9226 if (list2proftime(&argvars[0], &res) == FAIL)
9227 return;
9228 profile_end(&res);
9229 }
9230 else
9231 {
9232 /* Two arguments: compute the difference. */
9233 if (list2proftime(&argvars[0], &start) == FAIL
9234 || list2proftime(&argvars[1], &res) == FAIL)
9235 return;
9236 profile_sub(&res, &start);
9237 }
9238
9239 if (rettv_list_alloc(rettv) == OK)
9240 {
9241 long n1, n2;
9242
Bram Moolenaar4f974752019-02-17 17:44:42 +01009243# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 n1 = res.HighPart;
9245 n2 = res.LowPart;
9246# else
9247 n1 = res.tv_sec;
9248 n2 = res.tv_usec;
9249# endif
9250 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9251 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9252 }
9253#endif
9254}
9255
9256#ifdef FEAT_FLOAT
9257/*
9258 * "reltimefloat()" function
9259 */
9260 static void
9261f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9262{
9263# ifdef FEAT_RELTIME
9264 proftime_T tm;
9265# endif
9266
9267 rettv->v_type = VAR_FLOAT;
9268 rettv->vval.v_float = 0;
9269# ifdef FEAT_RELTIME
9270 if (list2proftime(&argvars[0], &tm) == OK)
9271 rettv->vval.v_float = profile_float(&tm);
9272# endif
9273}
9274#endif
9275
9276/*
9277 * "reltimestr()" function
9278 */
9279 static void
9280f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9281{
9282#ifdef FEAT_RELTIME
9283 proftime_T tm;
9284#endif
9285
9286 rettv->v_type = VAR_STRING;
9287 rettv->vval.v_string = NULL;
9288#ifdef FEAT_RELTIME
9289 if (list2proftime(&argvars[0], &tm) == OK)
9290 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9291#endif
9292}
9293
9294#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 static void
9296make_connection(void)
9297{
9298 if (X_DISPLAY == NULL
9299# ifdef FEAT_GUI
9300 && !gui.in_use
9301# endif
9302 )
9303 {
9304 x_force_connect = TRUE;
9305 setup_term_clip();
9306 x_force_connect = FALSE;
9307 }
9308}
9309
9310 static int
9311check_connection(void)
9312{
9313 make_connection();
9314 if (X_DISPLAY == NULL)
9315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009316 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317 return FAIL;
9318 }
9319 return OK;
9320}
9321#endif
9322
9323#ifdef FEAT_CLIENTSERVER
9324 static void
9325remote_common(typval_T *argvars, typval_T *rettv, int expr)
9326{
9327 char_u *server_name;
9328 char_u *keys;
9329 char_u *r = NULL;
9330 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009331 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009332# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 HWND w;
9334# else
9335 Window w;
9336# endif
9337
9338 if (check_restricted() || check_secure())
9339 return;
9340
9341# ifdef FEAT_X11
9342 if (check_connection() == FAIL)
9343 return;
9344# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009345 if (argvars[2].v_type != VAR_UNKNOWN
9346 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009347 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009349 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 if (server_name == NULL)
9351 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009353# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009354 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009356 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9357 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358# endif
9359 {
9360 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009362 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009363 vim_free(r);
9364 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009366 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 return;
9368 }
9369
9370 rettv->vval.v_string = r;
9371
9372 if (argvars[2].v_type != VAR_UNKNOWN)
9373 {
9374 dictitem_T v;
9375 char_u str[30];
9376 char_u *idvar;
9377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009378 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009379 if (idvar != NULL && *idvar != NUL)
9380 {
9381 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9382 v.di_tv.v_type = VAR_STRING;
9383 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009385 vim_free(v.di_tv.vval.v_string);
9386 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 }
9388}
9389#endif
9390
9391/*
9392 * "remote_expr()" function
9393 */
9394 static void
9395f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9396{
9397 rettv->v_type = VAR_STRING;
9398 rettv->vval.v_string = NULL;
9399#ifdef FEAT_CLIENTSERVER
9400 remote_common(argvars, rettv, TRUE);
9401#endif
9402}
9403
9404/*
9405 * "remote_foreground()" function
9406 */
9407 static void
9408f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9409{
9410#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009411# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 /* On Win32 it's done in this application. */
9413 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009414 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415
9416 if (server_name != NULL)
9417 serverForeground(server_name);
9418 }
9419# else
9420 /* Send a foreground() expression to the server. */
9421 argvars[1].v_type = VAR_STRING;
9422 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9423 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009424 rettv->v_type = VAR_STRING;
9425 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 remote_common(argvars, rettv, TRUE);
9427 vim_free(argvars[1].vval.v_string);
9428# endif
9429#endif
9430}
9431
9432 static void
9433f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9434{
9435#ifdef FEAT_CLIENTSERVER
9436 dictitem_T v;
9437 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009438# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439 long_u n = 0;
9440# endif
9441 char_u *serverid;
9442
9443 if (check_restricted() || check_secure())
9444 {
9445 rettv->vval.v_number = -1;
9446 return;
9447 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009448 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 if (serverid == NULL)
9450 {
9451 rettv->vval.v_number = -1;
9452 return; /* type error; errmsg already given */
9453 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009454# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9456 if (n == 0)
9457 rettv->vval.v_number = -1;
9458 else
9459 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009460 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461 rettv->vval.v_number = (s != NULL);
9462 }
9463# else
9464 if (check_connection() == FAIL)
9465 return;
9466
9467 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9468 serverStrToWin(serverid), &s);
9469# endif
9470
9471 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9472 {
9473 char_u *retvar;
9474
9475 v.di_tv.v_type = VAR_STRING;
9476 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 if (retvar != NULL)
9479 set_var(retvar, &v.di_tv, FALSE);
9480 vim_free(v.di_tv.vval.v_string);
9481 }
9482#else
9483 rettv->vval.v_number = -1;
9484#endif
9485}
9486
9487 static void
9488f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9489{
9490 char_u *r = NULL;
9491
9492#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494
9495 if (serverid != NULL && !check_restricted() && !check_secure())
9496 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009497 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009498# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009499 /* The server's HWND is encoded in the 'id' parameter */
9500 long_u n = 0;
9501# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009502
9503 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009504 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009505
Bram Moolenaar4f974752019-02-17 17:44:42 +01009506# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009507 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9508 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009509 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 if (r == NULL)
9511# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009512 if (check_connection() == FAIL
9513 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9514 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009516 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009517 }
9518#endif
9519 rettv->v_type = VAR_STRING;
9520 rettv->vval.v_string = r;
9521}
9522
9523/*
9524 * "remote_send()" function
9525 */
9526 static void
9527f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9528{
9529 rettv->v_type = VAR_STRING;
9530 rettv->vval.v_string = NULL;
9531#ifdef FEAT_CLIENTSERVER
9532 remote_common(argvars, rettv, FALSE);
9533#endif
9534}
9535
9536/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009537 * "remote_startserver()" function
9538 */
9539 static void
9540f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9541{
9542#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009544
9545 if (server == NULL)
9546 return; /* type error; errmsg already given */
9547 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009548 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009549 else
9550 {
9551# ifdef FEAT_X11
9552 if (check_connection() == OK)
9553 serverRegisterName(X_DISPLAY, server);
9554# else
9555 serverSetName(server);
9556# endif
9557 }
9558#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009559 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009560#endif
9561}
9562
9563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 * "remove()" function
9565 */
9566 static void
9567f_remove(typval_T *argvars, typval_T *rettv)
9568{
9569 list_T *l;
9570 listitem_T *item, *item2;
9571 listitem_T *li;
9572 long idx;
9573 long end;
9574 char_u *key;
9575 dict_T *d;
9576 dictitem_T *di;
9577 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009578 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579
9580 if (argvars[0].v_type == VAR_DICT)
9581 {
9582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009583 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009585 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009587 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 if (key != NULL)
9589 {
9590 di = dict_find(d, key, -1);
9591 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009592 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9594 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9595 {
9596 *rettv = di->di_tv;
9597 init_tv(&di->di_tv);
9598 dictitem_remove(d, di);
9599 }
9600 }
9601 }
9602 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009603 else if (argvars[0].v_type == VAR_BLOB)
9604 {
9605 idx = (long)tv_get_number_chk(&argvars[1], &error);
9606 if (!error)
9607 {
9608 blob_T *b = argvars[0].vval.v_blob;
9609 int len = blob_len(b);
9610 char_u *p;
9611
9612 if (idx < 0)
9613 // count from the end
9614 idx = len + idx;
9615 if (idx < 0 || idx >= len)
9616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009617 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009618 return;
9619 }
9620 if (argvars[2].v_type == VAR_UNKNOWN)
9621 {
9622 // Remove one item, return its value.
9623 p = (char_u *)b->bv_ga.ga_data;
9624 rettv->vval.v_number = (varnumber_T) *(p + idx);
9625 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9626 --b->bv_ga.ga_len;
9627 }
9628 else
9629 {
9630 blob_T *blob;
9631
9632 // Remove range of items, return list with values.
9633 end = (long)tv_get_number_chk(&argvars[2], &error);
9634 if (error)
9635 return;
9636 if (end < 0)
9637 // count from the end
9638 end = len + end;
9639 if (end >= len || idx > end)
9640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009641 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009642 return;
9643 }
9644 blob = blob_alloc();
9645 if (blob == NULL)
9646 return;
9647 blob->bv_ga.ga_len = end - idx + 1;
9648 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9649 {
9650 vim_free(blob);
9651 return;
9652 }
9653 p = (char_u *)b->bv_ga.ga_data;
9654 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9655 (size_t)(end - idx + 1));
9656 ++blob->bv_refcount;
9657 rettv->v_type = VAR_BLOB;
9658 rettv->vval.v_blob = blob;
9659
9660 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9661 b->bv_ga.ga_len -= end - idx + 1;
9662 }
9663 }
9664 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009666 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009668 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009670 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009672 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009673 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009674 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675 else
9676 {
9677 if (argvars[2].v_type == VAR_UNKNOWN)
9678 {
9679 /* Remove one item, return its value. */
9680 vimlist_remove(l, item, item);
9681 *rettv = item->li_tv;
9682 vim_free(item);
9683 }
9684 else
9685 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009686 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009687 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009689 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009691 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 else
9693 {
9694 int cnt = 0;
9695
9696 for (li = item; li != NULL; li = li->li_next)
9697 {
9698 ++cnt;
9699 if (li == item2)
9700 break;
9701 }
9702 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009703 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009704 else
9705 {
9706 vimlist_remove(l, item, item2);
9707 if (rettv_list_alloc(rettv) == OK)
9708 {
9709 l = rettv->vval.v_list;
9710 l->lv_first = item;
9711 l->lv_last = item2;
9712 item->li_prev = NULL;
9713 item2->li_next = NULL;
9714 l->lv_len = cnt;
9715 }
9716 }
9717 }
9718 }
9719 }
9720 }
9721}
9722
9723/*
9724 * "rename({from}, {to})" function
9725 */
9726 static void
9727f_rename(typval_T *argvars, typval_T *rettv)
9728{
9729 char_u buf[NUMBUFLEN];
9730
9731 if (check_restricted() || check_secure())
9732 rettv->vval.v_number = -1;
9733 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009734 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9735 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736}
9737
9738/*
9739 * "repeat()" function
9740 */
9741 static void
9742f_repeat(typval_T *argvars, typval_T *rettv)
9743{
9744 char_u *p;
9745 int n;
9746 int slen;
9747 int len;
9748 char_u *r;
9749 int i;
9750
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009751 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 if (argvars[0].v_type == VAR_LIST)
9753 {
9754 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9755 while (n-- > 0)
9756 if (list_extend(rettv->vval.v_list,
9757 argvars[0].vval.v_list, NULL) == FAIL)
9758 break;
9759 }
9760 else
9761 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009762 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 rettv->v_type = VAR_STRING;
9764 rettv->vval.v_string = NULL;
9765
9766 slen = (int)STRLEN(p);
9767 len = slen * n;
9768 if (len <= 0)
9769 return;
9770
9771 r = alloc(len + 1);
9772 if (r != NULL)
9773 {
9774 for (i = 0; i < n; i++)
9775 mch_memmove(r + i * slen, p, (size_t)slen);
9776 r[len] = NUL;
9777 }
9778
9779 rettv->vval.v_string = r;
9780 }
9781}
9782
9783/*
9784 * "resolve()" function
9785 */
9786 static void
9787f_resolve(typval_T *argvars, typval_T *rettv)
9788{
9789 char_u *p;
9790#ifdef HAVE_READLINK
9791 char_u *buf = NULL;
9792#endif
9793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009794 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795#ifdef FEAT_SHORTCUT
9796 {
9797 char_u *v = NULL;
9798
Bram Moolenaardce1e892019-02-10 23:18:53 +01009799 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 if (v != NULL)
9801 rettv->vval.v_string = v;
9802 else
9803 rettv->vval.v_string = vim_strsave(p);
9804 }
9805#else
9806# ifdef HAVE_READLINK
9807 {
9808 char_u *cpy;
9809 int len;
9810 char_u *remain = NULL;
9811 char_u *q;
9812 int is_relative_to_current = FALSE;
9813 int has_trailing_pathsep = FALSE;
9814 int limit = 100;
9815
9816 p = vim_strsave(p);
9817
9818 if (p[0] == '.' && (vim_ispathsep(p[1])
9819 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9820 is_relative_to_current = TRUE;
9821
9822 len = STRLEN(p);
9823 if (len > 0 && after_pathsep(p, p + len))
9824 {
9825 has_trailing_pathsep = TRUE;
9826 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9827 }
9828
9829 q = getnextcomp(p);
9830 if (*q != NUL)
9831 {
9832 /* Separate the first path component in "p", and keep the
9833 * remainder (beginning with the path separator). */
9834 remain = vim_strsave(q - 1);
9835 q[-1] = NUL;
9836 }
9837
9838 buf = alloc(MAXPATHL + 1);
9839 if (buf == NULL)
9840 goto fail;
9841
9842 for (;;)
9843 {
9844 for (;;)
9845 {
9846 len = readlink((char *)p, (char *)buf, MAXPATHL);
9847 if (len <= 0)
9848 break;
9849 buf[len] = NUL;
9850
9851 if (limit-- == 0)
9852 {
9853 vim_free(p);
9854 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009855 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 rettv->vval.v_string = NULL;
9857 goto fail;
9858 }
9859
9860 /* Ensure that the result will have a trailing path separator
9861 * if the argument has one. */
9862 if (remain == NULL && has_trailing_pathsep)
9863 add_pathsep(buf);
9864
9865 /* Separate the first path component in the link value and
9866 * concatenate the remainders. */
9867 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9868 if (*q != NUL)
9869 {
9870 if (remain == NULL)
9871 remain = vim_strsave(q - 1);
9872 else
9873 {
9874 cpy = concat_str(q - 1, remain);
9875 if (cpy != NULL)
9876 {
9877 vim_free(remain);
9878 remain = cpy;
9879 }
9880 }
9881 q[-1] = NUL;
9882 }
9883
9884 q = gettail(p);
9885 if (q > p && *q == NUL)
9886 {
9887 /* Ignore trailing path separator. */
9888 q[-1] = NUL;
9889 q = gettail(p);
9890 }
9891 if (q > p && !mch_isFullName(buf))
9892 {
9893 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009894 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (cpy != NULL)
9896 {
9897 STRCPY(cpy, p);
9898 STRCPY(gettail(cpy), buf);
9899 vim_free(p);
9900 p = cpy;
9901 }
9902 }
9903 else
9904 {
9905 vim_free(p);
9906 p = vim_strsave(buf);
9907 }
9908 }
9909
9910 if (remain == NULL)
9911 break;
9912
9913 /* Append the first path component of "remain" to "p". */
9914 q = getnextcomp(remain + 1);
9915 len = q - remain - (*q != NUL);
9916 cpy = vim_strnsave(p, STRLEN(p) + len);
9917 if (cpy != NULL)
9918 {
9919 STRNCAT(cpy, remain, len);
9920 vim_free(p);
9921 p = cpy;
9922 }
9923 /* Shorten "remain". */
9924 if (*q != NUL)
9925 STRMOVE(remain, q - 1);
9926 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009927 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 }
9929
9930 /* If the result is a relative path name, make it explicitly relative to
9931 * the current directory if and only if the argument had this form. */
9932 if (!vim_ispathsep(*p))
9933 {
9934 if (is_relative_to_current
9935 && *p != NUL
9936 && !(p[0] == '.'
9937 && (p[1] == NUL
9938 || vim_ispathsep(p[1])
9939 || (p[1] == '.'
9940 && (p[2] == NUL
9941 || vim_ispathsep(p[2]))))))
9942 {
9943 /* Prepend "./". */
9944 cpy = concat_str((char_u *)"./", p);
9945 if (cpy != NULL)
9946 {
9947 vim_free(p);
9948 p = cpy;
9949 }
9950 }
9951 else if (!is_relative_to_current)
9952 {
9953 /* Strip leading "./". */
9954 q = p;
9955 while (q[0] == '.' && vim_ispathsep(q[1]))
9956 q += 2;
9957 if (q > p)
9958 STRMOVE(p, p + 2);
9959 }
9960 }
9961
9962 /* Ensure that the result will have no trailing path separator
9963 * if the argument had none. But keep "/" or "//". */
9964 if (!has_trailing_pathsep)
9965 {
9966 q = p + STRLEN(p);
9967 if (after_pathsep(p, q))
9968 *gettail_sep(p) = NUL;
9969 }
9970
9971 rettv->vval.v_string = p;
9972 }
9973# else
9974 rettv->vval.v_string = vim_strsave(p);
9975# endif
9976#endif
9977
9978 simplify_filename(rettv->vval.v_string);
9979
9980#ifdef HAVE_READLINK
9981fail:
9982 vim_free(buf);
9983#endif
9984 rettv->v_type = VAR_STRING;
9985}
9986
9987/*
9988 * "reverse({list})" function
9989 */
9990 static void
9991f_reverse(typval_T *argvars, typval_T *rettv)
9992{
9993 list_T *l;
9994 listitem_T *li, *ni;
9995
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009996 if (argvars[0].v_type == VAR_BLOB)
9997 {
9998 blob_T *b = argvars[0].vval.v_blob;
9999 int i, len = blob_len(b);
10000
10001 for (i = 0; i < len / 2; i++)
10002 {
10003 int tmp = blob_get(b, i);
10004
10005 blob_set(b, i, blob_get(b, len - i - 1));
10006 blob_set(b, len - i - 1, tmp);
10007 }
10008 rettv_blob_set(rettv, b);
10009 return;
10010 }
10011
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010013 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010015 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016 (char_u *)N_("reverse() argument"), TRUE))
10017 {
10018 li = l->lv_last;
10019 l->lv_first = l->lv_last = NULL;
10020 l->lv_len = 0;
10021 while (li != NULL)
10022 {
10023 ni = li->li_prev;
10024 list_append(l, li);
10025 li = ni;
10026 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010027 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028 l->lv_idx = l->lv_len - l->lv_idx - 1;
10029 }
10030}
10031
10032#define SP_NOMOVE 0x01 /* don't move cursor */
10033#define SP_REPEAT 0x02 /* repeat to find outer pair */
10034#define SP_RETCOUNT 0x04 /* return matchcount */
10035#define SP_SETPCMARK 0x08 /* set previous context mark */
10036#define SP_START 0x10 /* accept match at start position */
10037#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10038#define SP_END 0x40 /* leave cursor at end of match */
10039#define SP_COLUMN 0x80 /* start at cursor column */
10040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010041/*
10042 * Get flags for a search function.
10043 * Possibly sets "p_ws".
10044 * Returns BACKWARD, FORWARD or zero (for an error).
10045 */
10046 static int
10047get_search_arg(typval_T *varp, int *flagsp)
10048{
10049 int dir = FORWARD;
10050 char_u *flags;
10051 char_u nbuf[NUMBUFLEN];
10052 int mask;
10053
10054 if (varp->v_type != VAR_UNKNOWN)
10055 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010056 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010057 if (flags == NULL)
10058 return 0; /* type error; errmsg already given */
10059 while (*flags != NUL)
10060 {
10061 switch (*flags)
10062 {
10063 case 'b': dir = BACKWARD; break;
10064 case 'w': p_ws = TRUE; break;
10065 case 'W': p_ws = FALSE; break;
10066 default: mask = 0;
10067 if (flagsp != NULL)
10068 switch (*flags)
10069 {
10070 case 'c': mask = SP_START; break;
10071 case 'e': mask = SP_END; break;
10072 case 'm': mask = SP_RETCOUNT; break;
10073 case 'n': mask = SP_NOMOVE; break;
10074 case 'p': mask = SP_SUBPAT; break;
10075 case 'r': mask = SP_REPEAT; break;
10076 case 's': mask = SP_SETPCMARK; break;
10077 case 'z': mask = SP_COLUMN; break;
10078 }
10079 if (mask == 0)
10080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010081 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 dir = 0;
10083 }
10084 else
10085 *flagsp |= mask;
10086 }
10087 if (dir == 0)
10088 break;
10089 ++flags;
10090 }
10091 }
10092 return dir;
10093}
10094
10095/*
10096 * Shared by search() and searchpos() functions.
10097 */
10098 static int
10099search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10100{
10101 int flags;
10102 char_u *pat;
10103 pos_T pos;
10104 pos_T save_cursor;
10105 int save_p_ws = p_ws;
10106 int dir;
10107 int retval = 0; /* default: FAIL */
10108 long lnum_stop = 0;
10109 proftime_T tm;
10110#ifdef FEAT_RELTIME
10111 long time_limit = 0;
10112#endif
10113 int options = SEARCH_KEEP;
10114 int subpatnum;
10115
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010116 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010117 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10118 if (dir == 0)
10119 goto theend;
10120 flags = *flagsp;
10121 if (flags & SP_START)
10122 options |= SEARCH_START;
10123 if (flags & SP_END)
10124 options |= SEARCH_END;
10125 if (flags & SP_COLUMN)
10126 options |= SEARCH_COL;
10127
10128 /* Optional arguments: line number to stop searching and timeout. */
10129 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10130 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010131 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132 if (lnum_stop < 0)
10133 goto theend;
10134#ifdef FEAT_RELTIME
10135 if (argvars[3].v_type != VAR_UNKNOWN)
10136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010137 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 if (time_limit < 0)
10139 goto theend;
10140 }
10141#endif
10142 }
10143
10144#ifdef FEAT_RELTIME
10145 /* Set the time limit, if there is one. */
10146 profile_setlimit(time_limit, &tm);
10147#endif
10148
10149 /*
10150 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10151 * Check to make sure only those flags are set.
10152 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10153 * flags cannot be set. Check for that condition also.
10154 */
10155 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10156 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010158 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 goto theend;
10160 }
10161
10162 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010163 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010164 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165 if (subpatnum != FAIL)
10166 {
10167 if (flags & SP_SUBPAT)
10168 retval = subpatnum;
10169 else
10170 retval = pos.lnum;
10171 if (flags & SP_SETPCMARK)
10172 setpcmark();
10173 curwin->w_cursor = pos;
10174 if (match_pos != NULL)
10175 {
10176 /* Store the match cursor position */
10177 match_pos->lnum = pos.lnum;
10178 match_pos->col = pos.col + 1;
10179 }
10180 /* "/$" will put the cursor after the end of the line, may need to
10181 * correct that here */
10182 check_cursor();
10183 }
10184
10185 /* If 'n' flag is used: restore cursor position. */
10186 if (flags & SP_NOMOVE)
10187 curwin->w_cursor = save_cursor;
10188 else
10189 curwin->w_set_curswant = TRUE;
10190theend:
10191 p_ws = save_p_ws;
10192
10193 return retval;
10194}
10195
10196#ifdef FEAT_FLOAT
10197
10198/*
10199 * round() is not in C90, use ceil() or floor() instead.
10200 */
10201 float_T
10202vim_round(float_T f)
10203{
10204 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10205}
10206
10207/*
10208 * "round({float})" function
10209 */
10210 static void
10211f_round(typval_T *argvars, typval_T *rettv)
10212{
10213 float_T f = 0.0;
10214
10215 rettv->v_type = VAR_FLOAT;
10216 if (get_float_arg(argvars, &f) == OK)
10217 rettv->vval.v_float = vim_round(f);
10218 else
10219 rettv->vval.v_float = 0.0;
10220}
10221#endif
10222
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010223#ifdef FEAT_RUBY
10224/*
10225 * "rubyeval()" function
10226 */
10227 static void
10228f_rubyeval(typval_T *argvars, typval_T *rettv)
10229{
10230 char_u *str;
10231 char_u buf[NUMBUFLEN];
10232
10233 str = tv_get_string_buf(&argvars[0], buf);
10234 do_rubyeval(str, rettv);
10235}
10236#endif
10237
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238/*
10239 * "screenattr()" function
10240 */
10241 static void
10242f_screenattr(typval_T *argvars, typval_T *rettv)
10243{
10244 int row;
10245 int col;
10246 int c;
10247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10249 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 if (row < 0 || row >= screen_Rows
10251 || col < 0 || col >= screen_Columns)
10252 c = -1;
10253 else
10254 c = ScreenAttrs[LineOffset[row] + col];
10255 rettv->vval.v_number = c;
10256}
10257
10258/*
10259 * "screenchar()" function
10260 */
10261 static void
10262f_screenchar(typval_T *argvars, typval_T *rettv)
10263{
10264 int row;
10265 int col;
10266 int off;
10267 int c;
10268
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010269 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10270 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010271 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 c = -1;
10273 else
10274 {
10275 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 if (enc_utf8 && ScreenLinesUC[off] != 0)
10277 c = ScreenLinesUC[off];
10278 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010279 c = ScreenLines[off];
10280 }
10281 rettv->vval.v_number = c;
10282}
10283
10284/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010285 * "screenchars()" function
10286 */
10287 static void
10288f_screenchars(typval_T *argvars, typval_T *rettv)
10289{
10290 int row;
10291 int col;
10292 int off;
10293 int c;
10294 int i;
10295
10296 if (rettv_list_alloc(rettv) == FAIL)
10297 return;
10298 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10299 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10300 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10301 return;
10302
10303 off = LineOffset[row] + col;
10304 if (enc_utf8 && ScreenLinesUC[off] != 0)
10305 c = ScreenLinesUC[off];
10306 else
10307 c = ScreenLines[off];
10308 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10309
10310 if (enc_utf8)
10311
10312 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10313 list_append_number(rettv->vval.v_list,
10314 (varnumber_T)ScreenLinesC[i][off]);
10315}
10316
10317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010318 * "screencol()" function
10319 *
10320 * First column is 1 to be consistent with virtcol().
10321 */
10322 static void
10323f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10324{
10325 rettv->vval.v_number = screen_screencol() + 1;
10326}
10327
10328/*
10329 * "screenrow()" function
10330 */
10331 static void
10332f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10333{
10334 rettv->vval.v_number = screen_screenrow() + 1;
10335}
10336
10337/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010338 * "screenstring()" function
10339 */
10340 static void
10341f_screenstring(typval_T *argvars, typval_T *rettv)
10342{
10343 int row;
10344 int col;
10345 int off;
10346 int c;
10347 int i;
10348 char_u buf[MB_MAXBYTES + 1];
10349 int buflen = 0;
10350
10351 rettv->vval.v_string = NULL;
10352 rettv->v_type = VAR_STRING;
10353
10354 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10355 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10356 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10357 return;
10358
10359 off = LineOffset[row] + col;
10360 if (enc_utf8 && ScreenLinesUC[off] != 0)
10361 c = ScreenLinesUC[off];
10362 else
10363 c = ScreenLines[off];
10364 buflen += mb_char2bytes(c, buf);
10365
10366 if (enc_utf8)
10367 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10368 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10369
10370 buf[buflen] = NUL;
10371 rettv->vval.v_string = vim_strsave(buf);
10372}
10373
10374/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010375 * "search()" function
10376 */
10377 static void
10378f_search(typval_T *argvars, typval_T *rettv)
10379{
10380 int flags = 0;
10381
10382 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10383}
10384
10385/*
10386 * "searchdecl()" function
10387 */
10388 static void
10389f_searchdecl(typval_T *argvars, typval_T *rettv)
10390{
10391 int locally = 1;
10392 int thisblock = 0;
10393 int error = FALSE;
10394 char_u *name;
10395
10396 rettv->vval.v_number = 1; /* default: FAIL */
10397
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010398 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010399 if (argvars[1].v_type != VAR_UNKNOWN)
10400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010401 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010403 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010404 }
10405 if (!error && name != NULL)
10406 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10407 locally, thisblock, SEARCH_KEEP) == FAIL;
10408}
10409
10410/*
10411 * Used by searchpair() and searchpairpos()
10412 */
10413 static int
10414searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10415{
10416 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010417 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 int save_p_ws = p_ws;
10419 int dir;
10420 int flags = 0;
10421 char_u nbuf1[NUMBUFLEN];
10422 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010423 int retval = 0; /* default: FAIL */
10424 long lnum_stop = 0;
10425 long time_limit = 0;
10426
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010427 /* Get the three pattern arguments: start, middle, end. Will result in an
10428 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010429 spat = tv_get_string_chk(&argvars[0]);
10430 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10431 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 if (spat == NULL || mpat == NULL || epat == NULL)
10433 goto theend; /* type error */
10434
10435 /* Handle the optional fourth argument: flags */
10436 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10437 if (dir == 0)
10438 goto theend;
10439
10440 /* Don't accept SP_END or SP_SUBPAT.
10441 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10442 */
10443 if ((flags & (SP_END | SP_SUBPAT)) != 0
10444 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010446 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010447 goto theend;
10448 }
10449
10450 /* Using 'r' implies 'W', otherwise it doesn't work. */
10451 if (flags & SP_REPEAT)
10452 p_ws = FALSE;
10453
10454 /* Optional fifth argument: skip expression */
10455 if (argvars[3].v_type == VAR_UNKNOWN
10456 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010457 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458 else
10459 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010460 skip = &argvars[4];
10461 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10462 && skip->v_type != VAR_STRING)
10463 {
10464 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010465 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010466 goto theend;
10467 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 if (argvars[5].v_type != VAR_UNKNOWN)
10469 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010470 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010471 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010473 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010475 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476#ifdef FEAT_RELTIME
10477 if (argvars[6].v_type != VAR_UNKNOWN)
10478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010479 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010482 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010483 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010484 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485 }
10486#endif
10487 }
10488 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010489
10490 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10491 match_pos, lnum_stop, time_limit);
10492
10493theend:
10494 p_ws = save_p_ws;
10495
10496 return retval;
10497}
10498
10499/*
10500 * "searchpair()" function
10501 */
10502 static void
10503f_searchpair(typval_T *argvars, typval_T *rettv)
10504{
10505 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10506}
10507
10508/*
10509 * "searchpairpos()" function
10510 */
10511 static void
10512f_searchpairpos(typval_T *argvars, typval_T *rettv)
10513{
10514 pos_T match_pos;
10515 int lnum = 0;
10516 int col = 0;
10517
10518 if (rettv_list_alloc(rettv) == FAIL)
10519 return;
10520
10521 if (searchpair_cmn(argvars, &match_pos) > 0)
10522 {
10523 lnum = match_pos.lnum;
10524 col = match_pos.col;
10525 }
10526
10527 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10528 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10529}
10530
10531/*
10532 * Search for a start/middle/end thing.
10533 * Used by searchpair(), see its documentation for the details.
10534 * Returns 0 or -1 for no match,
10535 */
10536 long
10537do_searchpair(
10538 char_u *spat, /* start pattern */
10539 char_u *mpat, /* middle pattern */
10540 char_u *epat, /* end pattern */
10541 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010542 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010543 int flags, /* SP_SETPCMARK and other SP_ values */
10544 pos_T *match_pos,
10545 linenr_T lnum_stop, /* stop at this line if not zero */
10546 long time_limit UNUSED) /* stop after this many msec */
10547{
10548 char_u *save_cpo;
10549 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10550 long retval = 0;
10551 pos_T pos;
10552 pos_T firstpos;
10553 pos_T foundpos;
10554 pos_T save_cursor;
10555 pos_T save_pos;
10556 int n;
10557 int r;
10558 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010559 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010560 int err;
10561 int options = SEARCH_KEEP;
10562 proftime_T tm;
10563
10564 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10565 save_cpo = p_cpo;
10566 p_cpo = empty_option;
10567
10568#ifdef FEAT_RELTIME
10569 /* Set the time limit, if there is one. */
10570 profile_setlimit(time_limit, &tm);
10571#endif
10572
10573 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10574 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010575 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10576 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 if (pat2 == NULL || pat3 == NULL)
10578 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010579 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 if (*mpat == NUL)
10581 STRCPY(pat3, pat2);
10582 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010583 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 spat, epat, mpat);
10585 if (flags & SP_START)
10586 options |= SEARCH_START;
10587
Bram Moolenaar48570482017-10-30 21:48:41 +010010588 if (skip != NULL)
10589 {
10590 /* Empty string means to not use the skip expression. */
10591 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10592 use_skip = skip->vval.v_string != NULL
10593 && *skip->vval.v_string != NUL;
10594 }
10595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596 save_cursor = curwin->w_cursor;
10597 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010598 CLEAR_POS(&firstpos);
10599 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 pat = pat3;
10601 for (;;)
10602 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010603 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010604 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010605 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 /* didn't find it or found the first match again: FAIL */
10607 break;
10608
10609 if (firstpos.lnum == 0)
10610 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010611 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010612 {
10613 /* Found the same position again. Can happen with a pattern that
10614 * has "\zs" at the end and searching backwards. Advance one
10615 * character and try again. */
10616 if (dir == BACKWARD)
10617 decl(&pos);
10618 else
10619 incl(&pos);
10620 }
10621 foundpos = pos;
10622
10623 /* clear the start flag to avoid getting stuck here */
10624 options &= ~SEARCH_START;
10625
10626 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010627 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 {
10629 save_pos = curwin->w_cursor;
10630 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010631 err = FALSE;
10632 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 curwin->w_cursor = save_pos;
10634 if (err)
10635 {
10636 /* Evaluating {skip} caused an error, break here. */
10637 curwin->w_cursor = save_cursor;
10638 retval = -1;
10639 break;
10640 }
10641 if (r)
10642 continue;
10643 }
10644
10645 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10646 {
10647 /* Found end when searching backwards or start when searching
10648 * forward: nested pair. */
10649 ++nest;
10650 pat = pat2; /* nested, don't search for middle */
10651 }
10652 else
10653 {
10654 /* Found end when searching forward or start when searching
10655 * backward: end of (nested) pair; or found middle in outer pair. */
10656 if (--nest == 1)
10657 pat = pat3; /* outer level, search for middle */
10658 }
10659
10660 if (nest == 0)
10661 {
10662 /* Found the match: return matchcount or line number. */
10663 if (flags & SP_RETCOUNT)
10664 ++retval;
10665 else
10666 retval = pos.lnum;
10667 if (flags & SP_SETPCMARK)
10668 setpcmark();
10669 curwin->w_cursor = pos;
10670 if (!(flags & SP_REPEAT))
10671 break;
10672 nest = 1; /* search for next unmatched */
10673 }
10674 }
10675
10676 if (match_pos != NULL)
10677 {
10678 /* Store the match cursor position */
10679 match_pos->lnum = curwin->w_cursor.lnum;
10680 match_pos->col = curwin->w_cursor.col + 1;
10681 }
10682
10683 /* If 'n' flag is used or search failed: restore cursor position. */
10684 if ((flags & SP_NOMOVE) || retval == 0)
10685 curwin->w_cursor = save_cursor;
10686
10687theend:
10688 vim_free(pat2);
10689 vim_free(pat3);
10690 if (p_cpo == empty_option)
10691 p_cpo = save_cpo;
10692 else
10693 /* Darn, evaluating the {skip} expression changed the value. */
10694 free_string_option(save_cpo);
10695
10696 return retval;
10697}
10698
10699/*
10700 * "searchpos()" function
10701 */
10702 static void
10703f_searchpos(typval_T *argvars, typval_T *rettv)
10704{
10705 pos_T match_pos;
10706 int lnum = 0;
10707 int col = 0;
10708 int n;
10709 int flags = 0;
10710
10711 if (rettv_list_alloc(rettv) == FAIL)
10712 return;
10713
10714 n = search_cmn(argvars, &match_pos, &flags);
10715 if (n > 0)
10716 {
10717 lnum = match_pos.lnum;
10718 col = match_pos.col;
10719 }
10720
10721 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10722 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10723 if (flags & SP_SUBPAT)
10724 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10725}
10726
10727 static void
10728f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10729{
10730#ifdef FEAT_CLIENTSERVER
10731 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010732 char_u *server = tv_get_string_chk(&argvars[0]);
10733 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734
10735 rettv->vval.v_number = -1;
10736 if (server == NULL || reply == NULL)
10737 return;
10738 if (check_restricted() || check_secure())
10739 return;
10740# ifdef FEAT_X11
10741 if (check_connection() == FAIL)
10742 return;
10743# endif
10744
10745 if (serverSendReply(server, reply) < 0)
10746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010747 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 return;
10749 }
10750 rettv->vval.v_number = 0;
10751#else
10752 rettv->vval.v_number = -1;
10753#endif
10754}
10755
10756 static void
10757f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10758{
10759 char_u *r = NULL;
10760
10761#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010762# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 r = serverGetVimNames();
10764# else
10765 make_connection();
10766 if (X_DISPLAY != NULL)
10767 r = serverGetVimNames(X_DISPLAY);
10768# endif
10769#endif
10770 rettv->v_type = VAR_STRING;
10771 rettv->vval.v_string = r;
10772}
10773
10774/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010775 * "setbufline()" function
10776 */
10777 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010778f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010779{
10780 linenr_T lnum;
10781 buf_T *buf;
10782
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010783 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010784 if (buf == NULL)
10785 rettv->vval.v_number = 1; /* FAIL */
10786 else
10787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010788 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010789 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010790 }
10791}
10792
10793/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 * "setbufvar()" function
10795 */
10796 static void
10797f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10798{
10799 buf_T *buf;
10800 char_u *varname, *bufvarname;
10801 typval_T *varp;
10802 char_u nbuf[NUMBUFLEN];
10803
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010804 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010805 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010806 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10807 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010808 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809 varp = &argvars[2];
10810
10811 if (buf != NULL && varname != NULL && varp != NULL)
10812 {
10813 if (*varname == '&')
10814 {
10815 long numval;
10816 char_u *strval;
10817 int error = FALSE;
10818 aco_save_T aco;
10819
10820 /* set curbuf to be our buf, temporarily */
10821 aucmd_prepbuf(&aco, buf);
10822
10823 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010824 numval = (long)tv_get_number_chk(varp, &error);
10825 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826 if (!error && strval != NULL)
10827 set_option_value(varname, numval, strval, OPT_LOCAL);
10828
10829 /* reset notion of buffer */
10830 aucmd_restbuf(&aco);
10831 }
10832 else
10833 {
10834 buf_T *save_curbuf = curbuf;
10835
Bram Moolenaar964b3742019-05-24 18:54:09 +020010836 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837 if (bufvarname != NULL)
10838 {
10839 curbuf = buf;
10840 STRCPY(bufvarname, "b:");
10841 STRCPY(bufvarname + 2, varname);
10842 set_var(bufvarname, varp, TRUE);
10843 vim_free(bufvarname);
10844 curbuf = save_curbuf;
10845 }
10846 }
10847 }
10848}
10849
10850 static void
10851f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10852{
10853 dict_T *d;
10854 dictitem_T *di;
10855 char_u *csearch;
10856
10857 if (argvars[0].v_type != VAR_DICT)
10858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010859 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 return;
10861 }
10862
10863 if ((d = argvars[0].vval.v_dict) != NULL)
10864 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010865 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010866 if (csearch != NULL)
10867 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010868 if (enc_utf8)
10869 {
10870 int pcc[MAX_MCO];
10871 int c = utfc_ptr2char(csearch, pcc);
10872
10873 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10874 }
10875 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 set_last_csearch(PTR2CHAR(csearch),
10877 csearch, MB_PTR2LEN(csearch));
10878 }
10879
10880 di = dict_find(d, (char_u *)"forward", -1);
10881 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010882 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010883 ? FORWARD : BACKWARD);
10884
10885 di = dict_find(d, (char_u *)"until", -1);
10886 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010887 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 }
10889}
10890
10891/*
10892 * "setcmdpos()" function
10893 */
10894 static void
10895f_setcmdpos(typval_T *argvars, typval_T *rettv)
10896{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010897 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898
10899 if (pos >= 0)
10900 rettv->vval.v_number = set_cmdline_pos(pos);
10901}
10902
10903/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010904 * "setenv()" function
10905 */
10906 static void
10907f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10908{
10909 char_u namebuf[NUMBUFLEN];
10910 char_u valbuf[NUMBUFLEN];
10911 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10912
10913 if (argvars[1].v_type == VAR_SPECIAL
10914 && argvars[1].vval.v_number == VVAL_NULL)
10915 vim_unsetenv(name);
10916 else
10917 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10918}
10919
10920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921 * "setfperm({fname}, {mode})" function
10922 */
10923 static void
10924f_setfperm(typval_T *argvars, typval_T *rettv)
10925{
10926 char_u *fname;
10927 char_u modebuf[NUMBUFLEN];
10928 char_u *mode_str;
10929 int i;
10930 int mask;
10931 int mode = 0;
10932
10933 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010934 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 if (fname == NULL)
10936 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010937 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010938 if (mode_str == NULL)
10939 return;
10940 if (STRLEN(mode_str) != 9)
10941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010942 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010943 return;
10944 }
10945
10946 mask = 1;
10947 for (i = 8; i >= 0; --i)
10948 {
10949 if (mode_str[i] != '-')
10950 mode |= mask;
10951 mask = mask << 1;
10952 }
10953 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10954}
10955
10956/*
10957 * "setline()" function
10958 */
10959 static void
10960f_setline(typval_T *argvars, typval_T *rettv)
10961{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010962 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963
Bram Moolenaarca851592018-06-06 21:04:07 +020010964 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965}
10966
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967/*
10968 * Used by "setqflist()" and "setloclist()" functions
10969 */
10970 static void
10971set_qf_ll_list(
10972 win_T *wp UNUSED,
10973 typval_T *list_arg UNUSED,
10974 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010975 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010976 typval_T *rettv)
10977{
10978#ifdef FEAT_QUICKFIX
10979 static char *e_invact = N_("E927: Invalid action: '%s'");
10980 char_u *act;
10981 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010982 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983#endif
10984
10985 rettv->vval.v_number = -1;
10986
10987#ifdef FEAT_QUICKFIX
10988 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010989 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010990 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010991 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 else
10993 {
10994 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010995 dict_T *d = NULL;
10996 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997
10998 if (action_arg->v_type == VAR_STRING)
10999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011000 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 if (act == NULL)
11002 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011003 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11004 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 action = *act;
11006 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011007 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 }
11009 else if (action_arg->v_type == VAR_UNKNOWN)
11010 action = ' ';
11011 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011012 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013
Bram Moolenaard823fa92016-08-12 16:29:27 +020011014 if (action_arg->v_type != VAR_UNKNOWN
11015 && what_arg->v_type != VAR_UNKNOWN)
11016 {
11017 if (what_arg->v_type == VAR_DICT)
11018 d = what_arg->vval.v_dict;
11019 else
11020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011022 valid_dict = FALSE;
11023 }
11024 }
11025
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011026 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011027 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011028 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11029 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011031 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 }
11033#endif
11034}
11035
11036/*
11037 * "setloclist()" function
11038 */
11039 static void
11040f_setloclist(typval_T *argvars, typval_T *rettv)
11041{
11042 win_T *win;
11043
11044 rettv->vval.v_number = -1;
11045
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011046 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011048 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049}
11050
11051/*
11052 * "setmatches()" function
11053 */
11054 static void
11055f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11056{
11057#ifdef FEAT_SEARCH_EXTRA
11058 list_T *l;
11059 listitem_T *li;
11060 dict_T *d;
11061 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011062 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063
11064 rettv->vval.v_number = -1;
11065 if (argvars[0].v_type != VAR_LIST)
11066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011067 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 return;
11069 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011070 if (win == NULL)
11071 return;
11072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 if ((l = argvars[0].vval.v_list) != NULL)
11074 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011075 /* To some extent make sure that we are dealing with a list from
11076 * "getmatches()". */
11077 li = l->lv_first;
11078 while (li != NULL)
11079 {
11080 if (li->li_tv.v_type != VAR_DICT
11081 || (d = li->li_tv.vval.v_dict) == NULL)
11082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011083 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 return;
11085 }
11086 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11087 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11088 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11089 && dict_find(d, (char_u *)"priority", -1) != NULL
11090 && dict_find(d, (char_u *)"id", -1) != NULL))
11091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011092 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093 return;
11094 }
11095 li = li->li_next;
11096 }
11097
Bram Moolenaaraff74912019-03-30 18:11:49 +010011098 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011099 li = l->lv_first;
11100 while (li != NULL)
11101 {
11102 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011103 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011104 dictitem_T *di;
11105 char_u *group;
11106 int priority;
11107 int id;
11108 char_u *conceal;
11109
11110 d = li->li_tv.vval.v_dict;
11111 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11112 {
11113 if (s == NULL)
11114 {
11115 s = list_alloc();
11116 if (s == NULL)
11117 return;
11118 }
11119
11120 /* match from matchaddpos() */
11121 for (i = 1; i < 9; i++)
11122 {
11123 sprintf((char *)buf, (char *)"pos%d", i);
11124 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11125 {
11126 if (di->di_tv.v_type != VAR_LIST)
11127 return;
11128
11129 list_append_tv(s, &di->di_tv);
11130 s->lv_refcount++;
11131 }
11132 else
11133 break;
11134 }
11135 }
11136
Bram Moolenaar8f667172018-12-14 15:38:31 +010011137 group = dict_get_string(d, (char_u *)"group", TRUE);
11138 priority = (int)dict_get_number(d, (char_u *)"priority");
11139 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011141 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142 : NULL;
11143 if (i == 0)
11144 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011145 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011146 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011147 priority, id, NULL, conceal);
11148 }
11149 else
11150 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011151 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011152 list_unref(s);
11153 s = NULL;
11154 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011155 vim_free(group);
11156 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157
11158 li = li->li_next;
11159 }
11160 rettv->vval.v_number = 0;
11161 }
11162#endif
11163}
11164
11165/*
11166 * "setpos()" function
11167 */
11168 static void
11169f_setpos(typval_T *argvars, typval_T *rettv)
11170{
11171 pos_T pos;
11172 int fnum;
11173 char_u *name;
11174 colnr_T curswant = -1;
11175
11176 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011177 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 if (name != NULL)
11179 {
11180 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11181 {
11182 if (--pos.col < 0)
11183 pos.col = 0;
11184 if (name[0] == '.' && name[1] == NUL)
11185 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011186 /* set cursor; "fnum" is ignored */
11187 curwin->w_cursor = pos;
11188 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011190 curwin->w_curswant = curswant - 1;
11191 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011192 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011193 check_cursor();
11194 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 }
11196 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11197 {
11198 /* set mark */
11199 if (setmark_pos(name[1], &pos, fnum) == OK)
11200 rettv->vval.v_number = 0;
11201 }
11202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011203 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 }
11205 }
11206}
11207
11208/*
11209 * "setqflist()" function
11210 */
11211 static void
11212f_setqflist(typval_T *argvars, typval_T *rettv)
11213{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011214 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011215}
11216
11217/*
11218 * "setreg()" function
11219 */
11220 static void
11221f_setreg(typval_T *argvars, typval_T *rettv)
11222{
11223 int regname;
11224 char_u *strregname;
11225 char_u *stropt;
11226 char_u *strval;
11227 int append;
11228 char_u yank_type;
11229 long block_len;
11230
11231 block_len = -1;
11232 yank_type = MAUTO;
11233 append = FALSE;
11234
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011235 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 rettv->vval.v_number = 1; /* FAIL is default */
11237
11238 if (strregname == NULL)
11239 return; /* type error; errmsg already given */
11240 regname = *strregname;
11241 if (regname == 0 || regname == '@')
11242 regname = '"';
11243
11244 if (argvars[2].v_type != VAR_UNKNOWN)
11245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011246 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011247 if (stropt == NULL)
11248 return; /* type error */
11249 for (; *stropt != NUL; ++stropt)
11250 switch (*stropt)
11251 {
11252 case 'a': case 'A': /* append */
11253 append = TRUE;
11254 break;
11255 case 'v': case 'c': /* character-wise selection */
11256 yank_type = MCHAR;
11257 break;
11258 case 'V': case 'l': /* line-wise selection */
11259 yank_type = MLINE;
11260 break;
11261 case 'b': case Ctrl_V: /* block-wise selection */
11262 yank_type = MBLOCK;
11263 if (VIM_ISDIGIT(stropt[1]))
11264 {
11265 ++stropt;
11266 block_len = getdigits(&stropt) - 1;
11267 --stropt;
11268 }
11269 break;
11270 }
11271 }
11272
11273 if (argvars[1].v_type == VAR_LIST)
11274 {
11275 char_u **lstval;
11276 char_u **allocval;
11277 char_u buf[NUMBUFLEN];
11278 char_u **curval;
11279 char_u **curallocval;
11280 list_T *ll = argvars[1].vval.v_list;
11281 listitem_T *li;
11282 int len;
11283
11284 /* If the list is NULL handle like an empty list. */
11285 len = ll == NULL ? 0 : ll->lv_len;
11286
11287 /* First half: use for pointers to result lines; second half: use for
11288 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011289 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011290 if (lstval == NULL)
11291 return;
11292 curval = lstval;
11293 allocval = lstval + len + 2;
11294 curallocval = allocval;
11295
11296 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11297 li = li->li_next)
11298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011299 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 if (strval == NULL)
11301 goto free_lstval;
11302 if (strval == buf)
11303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 * overwrite the string. */
11306 strval = vim_strsave(buf);
11307 if (strval == NULL)
11308 goto free_lstval;
11309 *curallocval++ = strval;
11310 }
11311 *curval++ = strval;
11312 }
11313 *curval++ = NULL;
11314
11315 write_reg_contents_lst(regname, lstval, -1,
11316 append, yank_type, block_len);
11317free_lstval:
11318 while (curallocval > allocval)
11319 vim_free(*--curallocval);
11320 vim_free(lstval);
11321 }
11322 else
11323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011324 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011325 if (strval == NULL)
11326 return;
11327 write_reg_contents_ex(regname, strval, -1,
11328 append, yank_type, block_len);
11329 }
11330 rettv->vval.v_number = 0;
11331}
11332
11333/*
11334 * "settabvar()" function
11335 */
11336 static void
11337f_settabvar(typval_T *argvars, typval_T *rettv)
11338{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 tabpage_T *save_curtab;
11340 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 char_u *varname, *tabvarname;
11342 typval_T *varp;
11343
11344 rettv->vval.v_number = 0;
11345
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011346 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011347 return;
11348
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011349 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11350 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011351 varp = &argvars[2];
11352
Bram Moolenaar4033c552017-09-16 20:54:51 +020011353 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355 save_curtab = curtab;
11356 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011357
Bram Moolenaar964b3742019-05-24 18:54:09 +020011358 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011359 if (tabvarname != NULL)
11360 {
11361 STRCPY(tabvarname, "t:");
11362 STRCPY(tabvarname + 2, varname);
11363 set_var(tabvarname, varp, TRUE);
11364 vim_free(tabvarname);
11365 }
11366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 /* Restore current tabpage */
11368 if (valid_tabpage(save_curtab))
11369 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 }
11371}
11372
11373/*
11374 * "settabwinvar()" function
11375 */
11376 static void
11377f_settabwinvar(typval_T *argvars, typval_T *rettv)
11378{
11379 setwinvar(argvars, rettv, 1);
11380}
11381
11382/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011383 * "settagstack()" function
11384 */
11385 static void
11386f_settagstack(typval_T *argvars, typval_T *rettv)
11387{
11388 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11389 win_T *wp;
11390 dict_T *d;
11391 int action = 'r';
11392
11393 rettv->vval.v_number = -1;
11394
11395 // first argument: window number or id
11396 wp = find_win_by_nr_or_id(&argvars[0]);
11397 if (wp == NULL)
11398 return;
11399
11400 // second argument: dict with items to set in the tag stack
11401 if (argvars[1].v_type != VAR_DICT)
11402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011403 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011404 return;
11405 }
11406 d = argvars[1].vval.v_dict;
11407 if (d == NULL)
11408 return;
11409
11410 // third argument: action - 'a' for append and 'r' for replace.
11411 // default is to replace the stack.
11412 if (argvars[2].v_type == VAR_UNKNOWN)
11413 action = 'r';
11414 else if (argvars[2].v_type == VAR_STRING)
11415 {
11416 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011417 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011418 if (actstr == NULL)
11419 return;
11420 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11421 action = *actstr;
11422 else
11423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011424 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011425 return;
11426 }
11427 }
11428 else
11429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011430 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011431 return;
11432 }
11433
11434 if (set_tagstack(wp, d, action) == OK)
11435 rettv->vval.v_number = 0;
11436}
11437
11438/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439 * "setwinvar()" function
11440 */
11441 static void
11442f_setwinvar(typval_T *argvars, typval_T *rettv)
11443{
11444 setwinvar(argvars, rettv, 0);
11445}
11446
11447#ifdef FEAT_CRYPT
11448/*
11449 * "sha256({string})" function
11450 */
11451 static void
11452f_sha256(typval_T *argvars, typval_T *rettv)
11453{
11454 char_u *p;
11455
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011456 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457 rettv->vval.v_string = vim_strsave(
11458 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11459 rettv->v_type = VAR_STRING;
11460}
11461#endif /* FEAT_CRYPT */
11462
11463/*
11464 * "shellescape({string})" function
11465 */
11466 static void
11467f_shellescape(typval_T *argvars, typval_T *rettv)
11468{
Bram Moolenaar20615522017-06-05 18:46:26 +020011469 int do_special = non_zero_arg(&argvars[1]);
11470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011472 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 rettv->v_type = VAR_STRING;
11474}
11475
11476/*
11477 * shiftwidth() function
11478 */
11479 static void
11480f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11481{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011482 rettv->vval.v_number = 0;
11483
11484 if (argvars[0].v_type != VAR_UNKNOWN)
11485 {
11486 long col;
11487
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011488 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011489 if (col < 0)
11490 return; // type error; errmsg already given
11491#ifdef FEAT_VARTABS
11492 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11493 return;
11494#endif
11495 }
11496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 rettv->vval.v_number = get_sw_value(curbuf);
11498}
11499
11500/*
11501 * "simplify()" function
11502 */
11503 static void
11504f_simplify(typval_T *argvars, typval_T *rettv)
11505{
11506 char_u *p;
11507
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011508 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 rettv->vval.v_string = vim_strsave(p);
11510 simplify_filename(rettv->vval.v_string); /* simplify in place */
11511 rettv->v_type = VAR_STRING;
11512}
11513
11514#ifdef FEAT_FLOAT
11515/*
11516 * "sin()" function
11517 */
11518 static void
11519f_sin(typval_T *argvars, typval_T *rettv)
11520{
11521 float_T f = 0.0;
11522
11523 rettv->v_type = VAR_FLOAT;
11524 if (get_float_arg(argvars, &f) == OK)
11525 rettv->vval.v_float = sin(f);
11526 else
11527 rettv->vval.v_float = 0.0;
11528}
11529
11530/*
11531 * "sinh()" function
11532 */
11533 static void
11534f_sinh(typval_T *argvars, typval_T *rettv)
11535{
11536 float_T f = 0.0;
11537
11538 rettv->v_type = VAR_FLOAT;
11539 if (get_float_arg(argvars, &f) == OK)
11540 rettv->vval.v_float = sinh(f);
11541 else
11542 rettv->vval.v_float = 0.0;
11543}
11544#endif
11545
Bram Moolenaareae1b912019-05-09 15:12:55 +020011546static int item_compare(const void *s1, const void *s2);
11547static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548
11549/* struct used in the array that's given to qsort() */
11550typedef struct
11551{
11552 listitem_T *item;
11553 int idx;
11554} sortItem_T;
11555
11556/* struct storing information about current sort */
11557typedef struct
11558{
11559 int item_compare_ic;
11560 int item_compare_numeric;
11561 int item_compare_numbers;
11562#ifdef FEAT_FLOAT
11563 int item_compare_float;
11564#endif
11565 char_u *item_compare_func;
11566 partial_T *item_compare_partial;
11567 dict_T *item_compare_selfdict;
11568 int item_compare_func_err;
11569 int item_compare_keep_zero;
11570} sortinfo_T;
11571static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572#define ITEM_COMPARE_FAIL 999
11573
11574/*
11575 * Compare functions for f_sort() and f_uniq() below.
11576 */
11577 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578item_compare(const void *s1, const void *s2)
11579{
11580 sortItem_T *si1, *si2;
11581 typval_T *tv1, *tv2;
11582 char_u *p1, *p2;
11583 char_u *tofree1 = NULL, *tofree2 = NULL;
11584 int res;
11585 char_u numbuf1[NUMBUFLEN];
11586 char_u numbuf2[NUMBUFLEN];
11587
11588 si1 = (sortItem_T *)s1;
11589 si2 = (sortItem_T *)s2;
11590 tv1 = &si1->item->li_tv;
11591 tv2 = &si2->item->li_tv;
11592
11593 if (sortinfo->item_compare_numbers)
11594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011595 varnumber_T v1 = tv_get_number(tv1);
11596 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597
11598 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11599 }
11600
11601#ifdef FEAT_FLOAT
11602 if (sortinfo->item_compare_float)
11603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011604 float_T v1 = tv_get_float(tv1);
11605 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606
11607 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11608 }
11609#endif
11610
11611 /* tv2string() puts quotes around a string and allocates memory. Don't do
11612 * that for string variables. Use a single quote when comparing with a
11613 * non-string to do what the docs promise. */
11614 if (tv1->v_type == VAR_STRING)
11615 {
11616 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11617 p1 = (char_u *)"'";
11618 else
11619 p1 = tv1->vval.v_string;
11620 }
11621 else
11622 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11623 if (tv2->v_type == VAR_STRING)
11624 {
11625 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11626 p2 = (char_u *)"'";
11627 else
11628 p2 = tv2->vval.v_string;
11629 }
11630 else
11631 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11632 if (p1 == NULL)
11633 p1 = (char_u *)"";
11634 if (p2 == NULL)
11635 p2 = (char_u *)"";
11636 if (!sortinfo->item_compare_numeric)
11637 {
11638 if (sortinfo->item_compare_ic)
11639 res = STRICMP(p1, p2);
11640 else
11641 res = STRCMP(p1, p2);
11642 }
11643 else
11644 {
11645 double n1, n2;
11646 n1 = strtod((char *)p1, (char **)&p1);
11647 n2 = strtod((char *)p2, (char **)&p2);
11648 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11649 }
11650
11651 /* When the result would be zero, compare the item indexes. Makes the
11652 * sort stable. */
11653 if (res == 0 && !sortinfo->item_compare_keep_zero)
11654 res = si1->idx > si2->idx ? 1 : -1;
11655
11656 vim_free(tofree1);
11657 vim_free(tofree2);
11658 return res;
11659}
11660
11661 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011662item_compare2(const void *s1, const void *s2)
11663{
11664 sortItem_T *si1, *si2;
11665 int res;
11666 typval_T rettv;
11667 typval_T argv[3];
11668 int dummy;
11669 char_u *func_name;
11670 partial_T *partial = sortinfo->item_compare_partial;
11671
11672 /* shortcut after failure in previous call; compare all items equal */
11673 if (sortinfo->item_compare_func_err)
11674 return 0;
11675
11676 si1 = (sortItem_T *)s1;
11677 si2 = (sortItem_T *)s2;
11678
11679 if (partial == NULL)
11680 func_name = sortinfo->item_compare_func;
11681 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011682 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011683
11684 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11685 * in the copy without changing the original list items. */
11686 copy_tv(&si1->item->li_tv, &argv[0]);
11687 copy_tv(&si2->item->li_tv, &argv[1]);
11688
11689 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011690 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011691 partial, sortinfo->item_compare_selfdict);
11692 clear_tv(&argv[0]);
11693 clear_tv(&argv[1]);
11694
11695 if (res == FAIL)
11696 res = ITEM_COMPARE_FAIL;
11697 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011698 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011699 if (sortinfo->item_compare_func_err)
11700 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11701 clear_tv(&rettv);
11702
11703 /* When the result would be zero, compare the pointers themselves. Makes
11704 * the sort stable. */
11705 if (res == 0 && !sortinfo->item_compare_keep_zero)
11706 res = si1->idx > si2->idx ? 1 : -1;
11707
11708 return res;
11709}
11710
11711/*
11712 * "sort({list})" function
11713 */
11714 static void
11715do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11716{
11717 list_T *l;
11718 listitem_T *li;
11719 sortItem_T *ptrs;
11720 sortinfo_T *old_sortinfo;
11721 sortinfo_T info;
11722 long len;
11723 long i;
11724
11725 /* Pointer to current info struct used in compare function. Save and
11726 * restore the current one for nested calls. */
11727 old_sortinfo = sortinfo;
11728 sortinfo = &info;
11729
11730 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011731 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732 else
11733 {
11734 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011735 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011736 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11737 TRUE))
11738 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011739 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011740
11741 len = list_len(l);
11742 if (len <= 1)
11743 goto theend; /* short list sorts pretty quickly */
11744
11745 info.item_compare_ic = FALSE;
11746 info.item_compare_numeric = FALSE;
11747 info.item_compare_numbers = FALSE;
11748#ifdef FEAT_FLOAT
11749 info.item_compare_float = FALSE;
11750#endif
11751 info.item_compare_func = NULL;
11752 info.item_compare_partial = NULL;
11753 info.item_compare_selfdict = NULL;
11754 if (argvars[1].v_type != VAR_UNKNOWN)
11755 {
11756 /* optional second argument: {func} */
11757 if (argvars[1].v_type == VAR_FUNC)
11758 info.item_compare_func = argvars[1].vval.v_string;
11759 else if (argvars[1].v_type == VAR_PARTIAL)
11760 info.item_compare_partial = argvars[1].vval.v_partial;
11761 else
11762 {
11763 int error = FALSE;
11764
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011765 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011766 if (error)
11767 goto theend; /* type error; errmsg already given */
11768 if (i == 1)
11769 info.item_compare_ic = TRUE;
11770 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011771 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 else if (i != 0)
11773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011774 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011775 goto theend;
11776 }
11777 if (info.item_compare_func != NULL)
11778 {
11779 if (*info.item_compare_func == NUL)
11780 {
11781 /* empty string means default sort */
11782 info.item_compare_func = NULL;
11783 }
11784 else if (STRCMP(info.item_compare_func, "n") == 0)
11785 {
11786 info.item_compare_func = NULL;
11787 info.item_compare_numeric = TRUE;
11788 }
11789 else if (STRCMP(info.item_compare_func, "N") == 0)
11790 {
11791 info.item_compare_func = NULL;
11792 info.item_compare_numbers = TRUE;
11793 }
11794#ifdef FEAT_FLOAT
11795 else if (STRCMP(info.item_compare_func, "f") == 0)
11796 {
11797 info.item_compare_func = NULL;
11798 info.item_compare_float = TRUE;
11799 }
11800#endif
11801 else if (STRCMP(info.item_compare_func, "i") == 0)
11802 {
11803 info.item_compare_func = NULL;
11804 info.item_compare_ic = TRUE;
11805 }
11806 }
11807 }
11808
11809 if (argvars[2].v_type != VAR_UNKNOWN)
11810 {
11811 /* optional third argument: {dict} */
11812 if (argvars[2].v_type != VAR_DICT)
11813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011814 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815 goto theend;
11816 }
11817 info.item_compare_selfdict = argvars[2].vval.v_dict;
11818 }
11819 }
11820
11821 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011822 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011823 if (ptrs == NULL)
11824 goto theend;
11825
11826 i = 0;
11827 if (sort)
11828 {
11829 /* sort(): ptrs will be the list to sort */
11830 for (li = l->lv_first; li != NULL; li = li->li_next)
11831 {
11832 ptrs[i].item = li;
11833 ptrs[i].idx = i;
11834 ++i;
11835 }
11836
11837 info.item_compare_func_err = FALSE;
11838 info.item_compare_keep_zero = FALSE;
11839 /* test the compare function */
11840 if ((info.item_compare_func != NULL
11841 || info.item_compare_partial != NULL)
11842 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11843 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011844 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011845 else
11846 {
11847 /* Sort the array with item pointers. */
11848 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11849 info.item_compare_func == NULL
11850 && info.item_compare_partial == NULL
11851 ? item_compare : item_compare2);
11852
11853 if (!info.item_compare_func_err)
11854 {
11855 /* Clear the List and append the items in sorted order. */
11856 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11857 l->lv_len = 0;
11858 for (i = 0; i < len; ++i)
11859 list_append(l, ptrs[i].item);
11860 }
11861 }
11862 }
11863 else
11864 {
11865 int (*item_compare_func_ptr)(const void *, const void *);
11866
11867 /* f_uniq(): ptrs will be a stack of items to remove */
11868 info.item_compare_func_err = FALSE;
11869 info.item_compare_keep_zero = TRUE;
11870 item_compare_func_ptr = info.item_compare_func != NULL
11871 || info.item_compare_partial != NULL
11872 ? item_compare2 : item_compare;
11873
11874 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11875 li = li->li_next)
11876 {
11877 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11878 == 0)
11879 ptrs[i++].item = li;
11880 if (info.item_compare_func_err)
11881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011882 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883 break;
11884 }
11885 }
11886
11887 if (!info.item_compare_func_err)
11888 {
11889 while (--i >= 0)
11890 {
11891 li = ptrs[i].item->li_next;
11892 ptrs[i].item->li_next = li->li_next;
11893 if (li->li_next != NULL)
11894 li->li_next->li_prev = ptrs[i].item;
11895 else
11896 l->lv_last = ptrs[i].item;
11897 list_fix_watch(l, li);
11898 listitem_free(li);
11899 l->lv_len--;
11900 }
11901 }
11902 }
11903
11904 vim_free(ptrs);
11905 }
11906theend:
11907 sortinfo = old_sortinfo;
11908}
11909
11910/*
11911 * "sort({list})" function
11912 */
11913 static void
11914f_sort(typval_T *argvars, typval_T *rettv)
11915{
11916 do_sort_uniq(argvars, rettv, TRUE);
11917}
11918
11919/*
11920 * "uniq({list})" function
11921 */
11922 static void
11923f_uniq(typval_T *argvars, typval_T *rettv)
11924{
11925 do_sort_uniq(argvars, rettv, FALSE);
11926}
11927
11928/*
11929 * "soundfold({word})" function
11930 */
11931 static void
11932f_soundfold(typval_T *argvars, typval_T *rettv)
11933{
11934 char_u *s;
11935
11936 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011937 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011938#ifdef FEAT_SPELL
11939 rettv->vval.v_string = eval_soundfold(s);
11940#else
11941 rettv->vval.v_string = vim_strsave(s);
11942#endif
11943}
11944
11945/*
11946 * "spellbadword()" function
11947 */
11948 static void
11949f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11950{
11951 char_u *word = (char_u *)"";
11952 hlf_T attr = HLF_COUNT;
11953 int len = 0;
11954
11955 if (rettv_list_alloc(rettv) == FAIL)
11956 return;
11957
11958#ifdef FEAT_SPELL
11959 if (argvars[0].v_type == VAR_UNKNOWN)
11960 {
11961 /* Find the start and length of the badly spelled word. */
11962 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11963 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011964 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011965 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011966 curwin->w_set_curswant = TRUE;
11967 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011968 }
11969 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011971 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011972 int capcol = -1;
11973
11974 if (str != NULL)
11975 {
11976 /* Check the argument for spelling. */
11977 while (*str != NUL)
11978 {
11979 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11980 if (attr != HLF_COUNT)
11981 {
11982 word = str;
11983 break;
11984 }
11985 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011986 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011987 }
11988 }
11989 }
11990#endif
11991
11992 list_append_string(rettv->vval.v_list, word, len);
11993 list_append_string(rettv->vval.v_list, (char_u *)(
11994 attr == HLF_SPB ? "bad" :
11995 attr == HLF_SPR ? "rare" :
11996 attr == HLF_SPL ? "local" :
11997 attr == HLF_SPC ? "caps" :
11998 ""), -1);
11999}
12000
12001/*
12002 * "spellsuggest()" function
12003 */
12004 static void
12005f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12006{
12007#ifdef FEAT_SPELL
12008 char_u *str;
12009 int typeerr = FALSE;
12010 int maxcount;
12011 garray_T ga;
12012 int i;
12013 listitem_T *li;
12014 int need_capital = FALSE;
12015#endif
12016
12017 if (rettv_list_alloc(rettv) == FAIL)
12018 return;
12019
12020#ifdef FEAT_SPELL
12021 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012023 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012024 if (argvars[1].v_type != VAR_UNKNOWN)
12025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012026 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012027 if (maxcount <= 0)
12028 return;
12029 if (argvars[2].v_type != VAR_UNKNOWN)
12030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012031 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012032 if (typeerr)
12033 return;
12034 }
12035 }
12036 else
12037 maxcount = 25;
12038
12039 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12040
12041 for (i = 0; i < ga.ga_len; ++i)
12042 {
12043 str = ((char_u **)ga.ga_data)[i];
12044
12045 li = listitem_alloc();
12046 if (li == NULL)
12047 vim_free(str);
12048 else
12049 {
12050 li->li_tv.v_type = VAR_STRING;
12051 li->li_tv.v_lock = 0;
12052 li->li_tv.vval.v_string = str;
12053 list_append(rettv->vval.v_list, li);
12054 }
12055 }
12056 ga_clear(&ga);
12057 }
12058#endif
12059}
12060
12061 static void
12062f_split(typval_T *argvars, typval_T *rettv)
12063{
12064 char_u *str;
12065 char_u *end;
12066 char_u *pat = NULL;
12067 regmatch_T regmatch;
12068 char_u patbuf[NUMBUFLEN];
12069 char_u *save_cpo;
12070 int match;
12071 colnr_T col = 0;
12072 int keepempty = FALSE;
12073 int typeerr = FALSE;
12074
12075 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12076 save_cpo = p_cpo;
12077 p_cpo = (char_u *)"";
12078
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012079 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012080 if (argvars[1].v_type != VAR_UNKNOWN)
12081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012082 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012083 if (pat == NULL)
12084 typeerr = TRUE;
12085 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012086 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012087 }
12088 if (pat == NULL || *pat == NUL)
12089 pat = (char_u *)"[\\x01- ]\\+";
12090
12091 if (rettv_list_alloc(rettv) == FAIL)
12092 return;
12093 if (typeerr)
12094 return;
12095
12096 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12097 if (regmatch.regprog != NULL)
12098 {
12099 regmatch.rm_ic = FALSE;
12100 while (*str != NUL || keepempty)
12101 {
12102 if (*str == NUL)
12103 match = FALSE; /* empty item at the end */
12104 else
12105 match = vim_regexec_nl(&regmatch, str, col);
12106 if (match)
12107 end = regmatch.startp[0];
12108 else
12109 end = str + STRLEN(str);
12110 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12111 && *str != NUL && match && end < regmatch.endp[0]))
12112 {
12113 if (list_append_string(rettv->vval.v_list, str,
12114 (int)(end - str)) == FAIL)
12115 break;
12116 }
12117 if (!match)
12118 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012119 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012120 if (regmatch.endp[0] > str)
12121 col = 0;
12122 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012123 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012124 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012125 str = regmatch.endp[0];
12126 }
12127
12128 vim_regfree(regmatch.regprog);
12129 }
12130
12131 p_cpo = save_cpo;
12132}
12133
12134#ifdef FEAT_FLOAT
12135/*
12136 * "sqrt()" function
12137 */
12138 static void
12139f_sqrt(typval_T *argvars, typval_T *rettv)
12140{
12141 float_T f = 0.0;
12142
12143 rettv->v_type = VAR_FLOAT;
12144 if (get_float_arg(argvars, &f) == OK)
12145 rettv->vval.v_float = sqrt(f);
12146 else
12147 rettv->vval.v_float = 0.0;
12148}
12149
12150/*
12151 * "str2float()" function
12152 */
12153 static void
12154f_str2float(typval_T *argvars, typval_T *rettv)
12155{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012156 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012157 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158
Bram Moolenaar08243d22017-01-10 16:12:29 +010012159 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012160 p = skipwhite(p + 1);
12161 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012162 if (isneg)
12163 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012164 rettv->v_type = VAR_FLOAT;
12165}
12166#endif
12167
12168/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012169 * "str2list()" function
12170 */
12171 static void
12172f_str2list(typval_T *argvars, typval_T *rettv)
12173{
12174 char_u *p;
12175 int utf8 = FALSE;
12176
12177 if (rettv_list_alloc(rettv) == FAIL)
12178 return;
12179
12180 if (argvars[1].v_type != VAR_UNKNOWN)
12181 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12182
12183 p = tv_get_string(&argvars[0]);
12184
12185 if (has_mbyte || utf8)
12186 {
12187 int (*ptr2len)(char_u *);
12188 int (*ptr2char)(char_u *);
12189
12190 if (utf8 || enc_utf8)
12191 {
12192 ptr2len = utf_ptr2len;
12193 ptr2char = utf_ptr2char;
12194 }
12195 else
12196 {
12197 ptr2len = mb_ptr2len;
12198 ptr2char = mb_ptr2char;
12199 }
12200
12201 for ( ; *p != NUL; p += (*ptr2len)(p))
12202 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12203 }
12204 else
12205 for ( ; *p != NUL; ++p)
12206 list_append_number(rettv->vval.v_list, *p);
12207}
12208
12209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 * "str2nr()" function
12211 */
12212 static void
12213f_str2nr(typval_T *argvars, typval_T *rettv)
12214{
12215 int base = 10;
12216 char_u *p;
12217 varnumber_T n;
12218 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012219 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012220
12221 if (argvars[1].v_type != VAR_UNKNOWN)
12222 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012223 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012224 if (base != 2 && base != 8 && base != 10 && base != 16)
12225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012226 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012227 return;
12228 }
12229 }
12230
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012231 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012232 isneg = (*p == '-');
12233 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012234 p = skipwhite(p + 1);
12235 switch (base)
12236 {
12237 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12238 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12239 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12240 default: what = 0;
12241 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012242 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12243 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012244 if (isneg)
12245 rettv->vval.v_number = -n;
12246 else
12247 rettv->vval.v_number = n;
12248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012249}
12250
12251#ifdef HAVE_STRFTIME
12252/*
12253 * "strftime({format}[, {time}])" function
12254 */
12255 static void
12256f_strftime(typval_T *argvars, typval_T *rettv)
12257{
12258 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012259 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260 struct tm *curtime;
12261 time_t seconds;
12262 char_u *p;
12263
12264 rettv->v_type = VAR_STRING;
12265
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012266 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 if (argvars[1].v_type == VAR_UNKNOWN)
12268 seconds = time(NULL);
12269 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012270 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012271 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012272 /* MSVC returns NULL for an invalid value of seconds. */
12273 if (curtime == NULL)
12274 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12275 else
12276 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012277 vimconv_T conv;
12278 char_u *enc;
12279
12280 conv.vc_type = CONV_NONE;
12281 enc = enc_locale();
12282 convert_setup(&conv, p_enc, enc);
12283 if (conv.vc_type != CONV_NONE)
12284 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012285 if (p != NULL)
12286 (void)strftime((char *)result_buf, sizeof(result_buf),
12287 (char *)p, curtime);
12288 else
12289 result_buf[0] = NUL;
12290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012291 if (conv.vc_type != CONV_NONE)
12292 vim_free(p);
12293 convert_setup(&conv, enc, p_enc);
12294 if (conv.vc_type != CONV_NONE)
12295 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12296 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012297 rettv->vval.v_string = vim_strsave(result_buf);
12298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012299 /* Release conversion descriptors */
12300 convert_setup(&conv, NULL, NULL);
12301 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012302 }
12303}
12304#endif
12305
12306/*
12307 * "strgetchar()" function
12308 */
12309 static void
12310f_strgetchar(typval_T *argvars, typval_T *rettv)
12311{
12312 char_u *str;
12313 int len;
12314 int error = FALSE;
12315 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012316 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012317
12318 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012319 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012320 if (str == NULL)
12321 return;
12322 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012323 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324 if (error)
12325 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012326
Bram Moolenaar13505972019-01-24 15:04:48 +010012327 while (charidx >= 0 && byteidx < len)
12328 {
12329 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012331 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12332 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012334 --charidx;
12335 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337}
12338
12339/*
12340 * "stridx()" function
12341 */
12342 static void
12343f_stridx(typval_T *argvars, typval_T *rettv)
12344{
12345 char_u buf[NUMBUFLEN];
12346 char_u *needle;
12347 char_u *haystack;
12348 char_u *save_haystack;
12349 char_u *pos;
12350 int start_idx;
12351
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012352 needle = tv_get_string_chk(&argvars[1]);
12353 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354 rettv->vval.v_number = -1;
12355 if (needle == NULL || haystack == NULL)
12356 return; /* type error; errmsg already given */
12357
12358 if (argvars[2].v_type != VAR_UNKNOWN)
12359 {
12360 int error = FALSE;
12361
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012362 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 if (error || start_idx >= (int)STRLEN(haystack))
12364 return;
12365 if (start_idx >= 0)
12366 haystack += start_idx;
12367 }
12368
12369 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12370 if (pos != NULL)
12371 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12372}
12373
12374/*
12375 * "string()" function
12376 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012377 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012378f_string(typval_T *argvars, typval_T *rettv)
12379{
12380 char_u *tofree;
12381 char_u numbuf[NUMBUFLEN];
12382
12383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12385 get_copyID());
12386 /* Make a copy if we have a value but it's not in allocated memory. */
12387 if (rettv->vval.v_string != NULL && tofree == NULL)
12388 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12389}
12390
12391/*
12392 * "strlen()" function
12393 */
12394 static void
12395f_strlen(typval_T *argvars, typval_T *rettv)
12396{
12397 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012398 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012399}
12400
12401/*
12402 * "strchars()" function
12403 */
12404 static void
12405f_strchars(typval_T *argvars, typval_T *rettv)
12406{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012407 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012408 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012409 varnumber_T len = 0;
12410 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411
12412 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012413 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012414 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012415 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012416 else
12417 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12419 while (*s != NUL)
12420 {
12421 func_mb_ptr2char_adv(&s);
12422 ++len;
12423 }
12424 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 }
12426}
12427
12428/*
12429 * "strdisplaywidth()" function
12430 */
12431 static void
12432f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12433{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012434 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012435 int col = 0;
12436
12437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012438 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439
12440 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12441}
12442
12443/*
12444 * "strwidth()" function
12445 */
12446 static void
12447f_strwidth(typval_T *argvars, typval_T *rettv)
12448{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
Bram Moolenaar13505972019-01-24 15:04:48 +010012451 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452}
12453
12454/*
12455 * "strcharpart()" function
12456 */
12457 static void
12458f_strcharpart(typval_T *argvars, typval_T *rettv)
12459{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460 char_u *p;
12461 int nchar;
12462 int nbyte = 0;
12463 int charlen;
12464 int len = 0;
12465 int slen;
12466 int error = FALSE;
12467
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012468 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 slen = (int)STRLEN(p);
12470
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012471 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472 if (!error)
12473 {
12474 if (nchar > 0)
12475 while (nchar > 0 && nbyte < slen)
12476 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012477 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478 --nchar;
12479 }
12480 else
12481 nbyte = nchar;
12482 if (argvars[2].v_type != VAR_UNKNOWN)
12483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012484 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012485 while (charlen > 0 && nbyte + len < slen)
12486 {
12487 int off = nbyte + len;
12488
12489 if (off < 0)
12490 len += 1;
12491 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012492 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493 --charlen;
12494 }
12495 }
12496 else
12497 len = slen - nbyte; /* default: all bytes that are available. */
12498 }
12499
12500 /*
12501 * Only return the overlap between the specified part and the actual
12502 * string.
12503 */
12504 if (nbyte < 0)
12505 {
12506 len += nbyte;
12507 nbyte = 0;
12508 }
12509 else if (nbyte > slen)
12510 nbyte = slen;
12511 if (len < 0)
12512 len = 0;
12513 else if (nbyte + len > slen)
12514 len = slen - nbyte;
12515
12516 rettv->v_type = VAR_STRING;
12517 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012518}
12519
12520/*
12521 * "strpart()" function
12522 */
12523 static void
12524f_strpart(typval_T *argvars, typval_T *rettv)
12525{
12526 char_u *p;
12527 int n;
12528 int len;
12529 int slen;
12530 int error = FALSE;
12531
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012532 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533 slen = (int)STRLEN(p);
12534
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012535 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536 if (error)
12537 len = 0;
12538 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 else
12541 len = slen - n; /* default len: all bytes that are available. */
12542
12543 /*
12544 * Only return the overlap between the specified part and the actual
12545 * string.
12546 */
12547 if (n < 0)
12548 {
12549 len += n;
12550 n = 0;
12551 }
12552 else if (n > slen)
12553 n = slen;
12554 if (len < 0)
12555 len = 0;
12556 else if (n + len > slen)
12557 len = slen - n;
12558
12559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = vim_strnsave(p + n, len);
12561}
12562
12563/*
12564 * "strridx()" function
12565 */
12566 static void
12567f_strridx(typval_T *argvars, typval_T *rettv)
12568{
12569 char_u buf[NUMBUFLEN];
12570 char_u *needle;
12571 char_u *haystack;
12572 char_u *rest;
12573 char_u *lastmatch = NULL;
12574 int haystack_len, end_idx;
12575
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012576 needle = tv_get_string_chk(&argvars[1]);
12577 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012578
12579 rettv->vval.v_number = -1;
12580 if (needle == NULL || haystack == NULL)
12581 return; /* type error; errmsg already given */
12582
12583 haystack_len = (int)STRLEN(haystack);
12584 if (argvars[2].v_type != VAR_UNKNOWN)
12585 {
12586 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012587 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588 if (end_idx < 0)
12589 return; /* can never find a match */
12590 }
12591 else
12592 end_idx = haystack_len;
12593
12594 if (*needle == NUL)
12595 {
12596 /* Empty string matches past the end. */
12597 lastmatch = haystack + end_idx;
12598 }
12599 else
12600 {
12601 for (rest = haystack; *rest != '\0'; ++rest)
12602 {
12603 rest = (char_u *)strstr((char *)rest, (char *)needle);
12604 if (rest == NULL || rest > haystack + end_idx)
12605 break;
12606 lastmatch = rest;
12607 }
12608 }
12609
12610 if (lastmatch == NULL)
12611 rettv->vval.v_number = -1;
12612 else
12613 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12614}
12615
12616/*
12617 * "strtrans()" function
12618 */
12619 static void
12620f_strtrans(typval_T *argvars, typval_T *rettv)
12621{
12622 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012623 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012624}
12625
12626/*
12627 * "submatch()" function
12628 */
12629 static void
12630f_submatch(typval_T *argvars, typval_T *rettv)
12631{
12632 int error = FALSE;
12633 int no;
12634 int retList = 0;
12635
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012636 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012637 if (error)
12638 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012639 if (no < 0 || no >= NSUBEXP)
12640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012641 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012642 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012643 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012645 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012646 if (error)
12647 return;
12648
12649 if (retList == 0)
12650 {
12651 rettv->v_type = VAR_STRING;
12652 rettv->vval.v_string = reg_submatch(no);
12653 }
12654 else
12655 {
12656 rettv->v_type = VAR_LIST;
12657 rettv->vval.v_list = reg_submatch_list(no);
12658 }
12659}
12660
12661/*
12662 * "substitute()" function
12663 */
12664 static void
12665f_substitute(typval_T *argvars, typval_T *rettv)
12666{
12667 char_u patbuf[NUMBUFLEN];
12668 char_u subbuf[NUMBUFLEN];
12669 char_u flagsbuf[NUMBUFLEN];
12670
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012671 char_u *str = tv_get_string_chk(&argvars[0]);
12672 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012673 char_u *sub = NULL;
12674 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012675 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012676
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012677 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12678 expr = &argvars[2];
12679 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012680 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012683 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12684 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012685 rettv->vval.v_string = NULL;
12686 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012687 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688}
12689
12690/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012691 * "swapinfo(swap_filename)" function
12692 */
12693 static void
12694f_swapinfo(typval_T *argvars, typval_T *rettv)
12695{
12696 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012697 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012698}
12699
12700/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012701 * "swapname(expr)" function
12702 */
12703 static void
12704f_swapname(typval_T *argvars, typval_T *rettv)
12705{
12706 buf_T *buf;
12707
12708 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012709 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012710 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12711 || buf->b_ml.ml_mfp->mf_fname == NULL)
12712 rettv->vval.v_string = NULL;
12713 else
12714 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12715}
12716
12717/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012718 * "synID(lnum, col, trans)" function
12719 */
12720 static void
12721f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12722{
12723 int id = 0;
12724#ifdef FEAT_SYN_HL
12725 linenr_T lnum;
12726 colnr_T col;
12727 int trans;
12728 int transerr = FALSE;
12729
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012730 lnum = tv_get_lnum(argvars); /* -1 on type error */
12731 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12732 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733
12734 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12735 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12736 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12737#endif
12738
12739 rettv->vval.v_number = id;
12740}
12741
12742/*
12743 * "synIDattr(id, what [, mode])" function
12744 */
12745 static void
12746f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12747{
12748 char_u *p = NULL;
12749#ifdef FEAT_SYN_HL
12750 int id;
12751 char_u *what;
12752 char_u *mode;
12753 char_u modebuf[NUMBUFLEN];
12754 int modec;
12755
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012756 id = (int)tv_get_number(&argvars[0]);
12757 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012758 if (argvars[2].v_type != VAR_UNKNOWN)
12759 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012760 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012761 modec = TOLOWER_ASC(mode[0]);
12762 if (modec != 't' && modec != 'c' && modec != 'g')
12763 modec = 0; /* replace invalid with current */
12764 }
12765 else
12766 {
12767#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12768 if (USE_24BIT)
12769 modec = 'g';
12770 else
12771#endif
12772 if (t_colors > 1)
12773 modec = 'c';
12774 else
12775 modec = 't';
12776 }
12777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012778 switch (TOLOWER_ASC(what[0]))
12779 {
12780 case 'b':
12781 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12782 p = highlight_color(id, what, modec);
12783 else /* bold */
12784 p = highlight_has_attr(id, HL_BOLD, modec);
12785 break;
12786
12787 case 'f': /* fg[#] or font */
12788 p = highlight_color(id, what, modec);
12789 break;
12790
12791 case 'i':
12792 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12793 p = highlight_has_attr(id, HL_INVERSE, modec);
12794 else /* italic */
12795 p = highlight_has_attr(id, HL_ITALIC, modec);
12796 break;
12797
12798 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012799 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012800 break;
12801
12802 case 'r': /* reverse */
12803 p = highlight_has_attr(id, HL_INVERSE, modec);
12804 break;
12805
12806 case 's':
12807 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12808 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012809 /* strikeout */
12810 else if (TOLOWER_ASC(what[1]) == 't' &&
12811 TOLOWER_ASC(what[2]) == 'r')
12812 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012813 else /* standout */
12814 p = highlight_has_attr(id, HL_STANDOUT, modec);
12815 break;
12816
12817 case 'u':
12818 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12819 /* underline */
12820 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12821 else
12822 /* undercurl */
12823 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12824 break;
12825 }
12826
12827 if (p != NULL)
12828 p = vim_strsave(p);
12829#endif
12830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = p;
12832}
12833
12834/*
12835 * "synIDtrans(id)" function
12836 */
12837 static void
12838f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12839{
12840 int id;
12841
12842#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012843 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012844
12845 if (id > 0)
12846 id = syn_get_final_id(id);
12847 else
12848#endif
12849 id = 0;
12850
12851 rettv->vval.v_number = id;
12852}
12853
12854/*
12855 * "synconcealed(lnum, col)" function
12856 */
12857 static void
12858f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12859{
12860#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12861 linenr_T lnum;
12862 colnr_T col;
12863 int syntax_flags = 0;
12864 int cchar;
12865 int matchid = 0;
12866 char_u str[NUMBUFLEN];
12867#endif
12868
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012869 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012870
12871#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012872 lnum = tv_get_lnum(argvars); /* -1 on type error */
12873 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874
12875 vim_memset(str, NUL, sizeof(str));
12876
12877 if (rettv_list_alloc(rettv) != FAIL)
12878 {
12879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12880 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12881 && curwin->w_p_cole > 0)
12882 {
12883 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12884 syntax_flags = get_syntax_info(&matchid);
12885
12886 /* get the conceal character */
12887 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12888 {
12889 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012890 if (cchar == NUL && curwin->w_p_cole == 1)
12891 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 if (cchar != NUL)
12893 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 if (has_mbyte)
12895 (*mb_char2bytes)(cchar, str);
12896 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012897 str[0] = cchar;
12898 }
12899 }
12900 }
12901
12902 list_append_number(rettv->vval.v_list,
12903 (syntax_flags & HL_CONCEAL) != 0);
12904 /* -1 to auto-determine strlen */
12905 list_append_string(rettv->vval.v_list, str, -1);
12906 list_append_number(rettv->vval.v_list, matchid);
12907 }
12908#endif
12909}
12910
12911/*
12912 * "synstack(lnum, col)" function
12913 */
12914 static void
12915f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12916{
12917#ifdef FEAT_SYN_HL
12918 linenr_T lnum;
12919 colnr_T col;
12920 int i;
12921 int id;
12922#endif
12923
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012924 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925
12926#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012927 lnum = tv_get_lnum(argvars); /* -1 on type error */
12928 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012929
12930 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12931 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12932 && rettv_list_alloc(rettv) != FAIL)
12933 {
12934 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12935 for (i = 0; ; ++i)
12936 {
12937 id = syn_get_stack_item(i);
12938 if (id < 0)
12939 break;
12940 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12941 break;
12942 }
12943 }
12944#endif
12945}
12946
12947 static void
12948get_cmd_output_as_rettv(
12949 typval_T *argvars,
12950 typval_T *rettv,
12951 int retlist)
12952{
12953 char_u *res = NULL;
12954 char_u *p;
12955 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012956 int err = FALSE;
12957 FILE *fd;
12958 list_T *list = NULL;
12959 int flags = SHELL_SILENT;
12960
12961 rettv->v_type = VAR_STRING;
12962 rettv->vval.v_string = NULL;
12963 if (check_restricted() || check_secure())
12964 goto errret;
12965
12966 if (argvars[1].v_type != VAR_UNKNOWN)
12967 {
12968 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012969 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012970 * command.
12971 */
12972 if ((infile = vim_tempname('i', TRUE)) == NULL)
12973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012974 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 goto errret;
12976 }
12977
12978 fd = mch_fopen((char *)infile, WRITEBIN);
12979 if (fd == NULL)
12980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012981 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982 goto errret;
12983 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012984 if (argvars[1].v_type == VAR_NUMBER)
12985 {
12986 linenr_T lnum;
12987 buf_T *buf;
12988
12989 buf = buflist_findnr(argvars[1].vval.v_number);
12990 if (buf == NULL)
12991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012992 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012993 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012994 goto errret;
12995 }
12996
12997 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12998 {
12999 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13000 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13001 {
13002 err = TRUE;
13003 break;
13004 }
13005 if (putc(NL, fd) == EOF)
13006 {
13007 err = TRUE;
13008 break;
13009 }
13010 }
13011 }
13012 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013 {
13014 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13015 err = TRUE;
13016 }
13017 else
13018 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013019 size_t len;
13020 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013022 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023 if (p == NULL)
13024 {
13025 fclose(fd);
13026 goto errret; /* type error; errmsg already given */
13027 }
13028 len = STRLEN(p);
13029 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13030 err = TRUE;
13031 }
13032 if (fclose(fd) != 0)
13033 err = TRUE;
13034 if (err)
13035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013036 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013037 goto errret;
13038 }
13039 }
13040
13041 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13042 * echoes typeahead, that messes up the display. */
13043 if (!msg_silent)
13044 flags += SHELL_COOKED;
13045
13046 if (retlist)
13047 {
13048 int len;
13049 listitem_T *li;
13050 char_u *s = NULL;
13051 char_u *start;
13052 char_u *end;
13053 int i;
13054
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013055 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013056 if (res == NULL)
13057 goto errret;
13058
13059 list = list_alloc();
13060 if (list == NULL)
13061 goto errret;
13062
13063 for (i = 0; i < len; ++i)
13064 {
13065 start = res + i;
13066 while (i < len && res[i] != NL)
13067 ++i;
13068 end = res + i;
13069
Bram Moolenaar964b3742019-05-24 18:54:09 +020013070 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 if (s == NULL)
13072 goto errret;
13073
13074 for (p = s; start < end; ++p, ++start)
13075 *p = *start == NUL ? NL : *start;
13076 *p = NUL;
13077
13078 li = listitem_alloc();
13079 if (li == NULL)
13080 {
13081 vim_free(s);
13082 goto errret;
13083 }
13084 li->li_tv.v_type = VAR_STRING;
13085 li->li_tv.v_lock = 0;
13086 li->li_tv.vval.v_string = s;
13087 list_append(list, li);
13088 }
13089
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013090 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 list = NULL;
13092 }
13093 else
13094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013095 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013096#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013097 /* translate <CR><NL> into <NL> */
13098 if (res != NULL)
13099 {
13100 char_u *s, *d;
13101
13102 d = res;
13103 for (s = res; *s; ++s)
13104 {
13105 if (s[0] == CAR && s[1] == NL)
13106 ++s;
13107 *d++ = *s;
13108 }
13109 *d = NUL;
13110 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013111#endif
13112 rettv->vval.v_string = res;
13113 res = NULL;
13114 }
13115
13116errret:
13117 if (infile != NULL)
13118 {
13119 mch_remove(infile);
13120 vim_free(infile);
13121 }
13122 if (res != NULL)
13123 vim_free(res);
13124 if (list != NULL)
13125 list_free(list);
13126}
13127
13128/*
13129 * "system()" function
13130 */
13131 static void
13132f_system(typval_T *argvars, typval_T *rettv)
13133{
13134 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13135}
13136
13137/*
13138 * "systemlist()" function
13139 */
13140 static void
13141f_systemlist(typval_T *argvars, typval_T *rettv)
13142{
13143 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13144}
13145
13146/*
13147 * "tabpagebuflist()" function
13148 */
13149 static void
13150f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13151{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 tabpage_T *tp;
13153 win_T *wp = NULL;
13154
13155 if (argvars[0].v_type == VAR_UNKNOWN)
13156 wp = firstwin;
13157 else
13158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013159 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013160 if (tp != NULL)
13161 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13162 }
13163 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13164 {
13165 for (; wp != NULL; wp = wp->w_next)
13166 if (list_append_number(rettv->vval.v_list,
13167 wp->w_buffer->b_fnum) == FAIL)
13168 break;
13169 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170}
13171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013172/*
13173 * "tabpagenr()" function
13174 */
13175 static void
13176f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13177{
13178 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013179 char_u *arg;
13180
13181 if (argvars[0].v_type != VAR_UNKNOWN)
13182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013183 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 nr = 0;
13185 if (arg != NULL)
13186 {
13187 if (STRCMP(arg, "$") == 0)
13188 nr = tabpage_index(NULL) - 1;
13189 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013190 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 }
13192 }
13193 else
13194 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 rettv->vval.v_number = nr;
13196}
13197
13198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199/*
13200 * Common code for tabpagewinnr() and winnr().
13201 */
13202 static int
13203get_winnr(tabpage_T *tp, typval_T *argvar)
13204{
13205 win_T *twin;
13206 int nr = 1;
13207 win_T *wp;
13208 char_u *arg;
13209
13210 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13211 if (argvar->v_type != VAR_UNKNOWN)
13212 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013213 int invalid_arg = FALSE;
13214
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013215 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 if (arg == NULL)
13217 nr = 0; /* type error; errmsg already given */
13218 else if (STRCMP(arg, "$") == 0)
13219 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13220 else if (STRCMP(arg, "#") == 0)
13221 {
13222 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13223 if (twin == NULL)
13224 nr = 0;
13225 }
13226 else
13227 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013228 long count;
13229 char_u *endp;
13230
13231 // Extract the window count (if specified). e.g. winnr('3j')
13232 count = strtol((char *)arg, (char **)&endp, 10);
13233 if (count <= 0)
13234 count = 1; // if count is not specified, default to 1
13235 if (endp != NULL && *endp != '\0')
13236 {
13237 if (STRCMP(endp, "j") == 0)
13238 twin = win_vert_neighbor(tp, twin, FALSE, count);
13239 else if (STRCMP(endp, "k") == 0)
13240 twin = win_vert_neighbor(tp, twin, TRUE, count);
13241 else if (STRCMP(endp, "h") == 0)
13242 twin = win_horz_neighbor(tp, twin, TRUE, count);
13243 else if (STRCMP(endp, "l") == 0)
13244 twin = win_horz_neighbor(tp, twin, FALSE, count);
13245 else
13246 invalid_arg = TRUE;
13247 }
13248 else
13249 invalid_arg = TRUE;
13250 }
13251
13252 if (invalid_arg)
13253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013254 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 nr = 0;
13256 }
13257 }
13258
13259 if (nr > 0)
13260 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13261 wp != twin; wp = wp->w_next)
13262 {
13263 if (wp == NULL)
13264 {
13265 /* didn't find it in this tabpage */
13266 nr = 0;
13267 break;
13268 }
13269 ++nr;
13270 }
13271 return nr;
13272}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273
13274/*
13275 * "tabpagewinnr()" function
13276 */
13277 static void
13278f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13279{
13280 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 tabpage_T *tp;
13282
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013283 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 if (tp == NULL)
13285 nr = 0;
13286 else
13287 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 rettv->vval.v_number = nr;
13289}
13290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291/*
13292 * "tagfiles()" function
13293 */
13294 static void
13295f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13296{
13297 char_u *fname;
13298 tagname_T tn;
13299 int first;
13300
13301 if (rettv_list_alloc(rettv) == FAIL)
13302 return;
13303 fname = alloc(MAXPATHL);
13304 if (fname == NULL)
13305 return;
13306
13307 for (first = TRUE; ; first = FALSE)
13308 if (get_tagfname(&tn, first, fname) == FAIL
13309 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13310 break;
13311 tagname_free(&tn);
13312 vim_free(fname);
13313}
13314
13315/*
13316 * "taglist()" function
13317 */
13318 static void
13319f_taglist(typval_T *argvars, typval_T *rettv)
13320{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013321 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013322 char_u *tag_pattern;
13323
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013324 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325
13326 rettv->vval.v_number = FALSE;
13327 if (*tag_pattern == NUL)
13328 return;
13329
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013330 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013333 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334}
13335
13336/*
13337 * "tempname()" function
13338 */
13339 static void
13340f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13341{
13342 static int x = 'A';
13343
13344 rettv->v_type = VAR_STRING;
13345 rettv->vval.v_string = vim_tempname(x, FALSE);
13346
13347 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13348 * names. Skip 'I' and 'O', they are used for shell redirection. */
13349 do
13350 {
13351 if (x == 'Z')
13352 x = '0';
13353 else if (x == '9')
13354 x = 'A';
13355 else
13356 {
13357#ifdef EBCDIC
13358 if (x == 'I')
13359 x = 'J';
13360 else if (x == 'R')
13361 x = 'S';
13362 else
13363#endif
13364 ++x;
13365 }
13366 } while (x == 'I' || x == 'O');
13367}
13368
13369#ifdef FEAT_FLOAT
13370/*
13371 * "tan()" function
13372 */
13373 static void
13374f_tan(typval_T *argvars, typval_T *rettv)
13375{
13376 float_T f = 0.0;
13377
13378 rettv->v_type = VAR_FLOAT;
13379 if (get_float_arg(argvars, &f) == OK)
13380 rettv->vval.v_float = tan(f);
13381 else
13382 rettv->vval.v_float = 0.0;
13383}
13384
13385/*
13386 * "tanh()" function
13387 */
13388 static void
13389f_tanh(typval_T *argvars, typval_T *rettv)
13390{
13391 float_T f = 0.0;
13392
13393 rettv->v_type = VAR_FLOAT;
13394 if (get_float_arg(argvars, &f) == OK)
13395 rettv->vval.v_float = tanh(f);
13396 else
13397 rettv->vval.v_float = 0.0;
13398}
13399#endif
13400
13401/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013402 * Get a callback from "arg". It can be a Funcref or a function name.
13403 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013404 * "cb_name" is not allocated.
13405 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013406 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013407 callback_T
13408get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013410 callback_T res;
13411
13412 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13414 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013415 res.cb_partial = arg->vval.v_partial;
13416 ++res.cb_partial->pt_refcount;
13417 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013419 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013421 res.cb_partial = NULL;
13422 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13423 {
13424 // Note that we don't make a copy of the string.
13425 res.cb_name = arg->vval.v_string;
13426 func_ref(res.cb_name);
13427 }
13428 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13429 {
13430 res.cb_name = (char_u *)"";
13431 }
13432 else
13433 {
13434 emsg(_("E921: Invalid callback argument"));
13435 res.cb_name = NULL;
13436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013438 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013439}
13440
13441/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013442 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013443 */
13444 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013445put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013446{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013447 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013449 tv->v_type = VAR_PARTIAL;
13450 tv->vval.v_partial = cb->cb_partial;
13451 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013452 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013453 else
13454 {
13455 tv->v_type = VAR_FUNC;
13456 tv->vval.v_string = vim_strsave(cb->cb_name);
13457 func_ref(cb->cb_name);
13458 }
13459}
13460
13461/*
13462 * Make a copy of "src" into "dest", allocating the function name if needed,
13463 * without incrementing the refcount.
13464 */
13465 void
13466set_callback(callback_T *dest, callback_T *src)
13467{
13468 if (src->cb_partial == NULL)
13469 {
13470 // just a function name, make a copy
13471 dest->cb_name = vim_strsave(src->cb_name);
13472 dest->cb_free_name = TRUE;
13473 }
13474 else
13475 {
13476 // cb_name is a pointer into cb_partial
13477 dest->cb_name = src->cb_name;
13478 dest->cb_free_name = FALSE;
13479 }
13480 dest->cb_partial = src->cb_partial;
13481}
13482
13483/*
13484 * Unref/free "callback" returned by get_callback() or set_callback().
13485 */
13486 void
13487free_callback(callback_T *callback)
13488{
13489 if (callback->cb_partial != NULL)
13490 {
13491 partial_unref(callback->cb_partial);
13492 callback->cb_partial = NULL;
13493 }
13494 else if (callback->cb_name != NULL)
13495 func_unref(callback->cb_name);
13496 if (callback->cb_free_name)
13497 {
13498 vim_free(callback->cb_name);
13499 callback->cb_free_name = FALSE;
13500 }
13501 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503
13504#ifdef FEAT_TIMERS
13505/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013506 * "timer_info([timer])" function
13507 */
13508 static void
13509f_timer_info(typval_T *argvars, typval_T *rettv)
13510{
13511 timer_T *timer = NULL;
13512
13513 if (rettv_list_alloc(rettv) != OK)
13514 return;
13515 if (argvars[0].v_type != VAR_UNKNOWN)
13516 {
13517 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013518 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013519 else
13520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013521 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013522 if (timer != NULL)
13523 add_timer_info(rettv, timer);
13524 }
13525 }
13526 else
13527 add_timer_info_all(rettv);
13528}
13529
13530/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013531 * "timer_pause(timer, paused)" function
13532 */
13533 static void
13534f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13535{
13536 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013537 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013538
13539 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013540 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013541 else
13542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013543 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013544 if (timer != NULL)
13545 timer->tr_paused = paused;
13546 }
13547}
13548
13549/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013550 * "timer_start(time, callback [, options])" function
13551 */
13552 static void
13553f_timer_start(typval_T *argvars, typval_T *rettv)
13554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013555 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013556 timer_T *timer;
13557 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013558 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013559 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013560
Bram Moolenaar75537a92016-09-05 22:45:28 +020013561 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562 if (check_secure())
13563 return;
13564 if (argvars[2].v_type != VAR_UNKNOWN)
13565 {
13566 if (argvars[2].v_type != VAR_DICT
13567 || (dict = argvars[2].vval.v_dict) == NULL)
13568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013569 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013570 return;
13571 }
13572 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013573 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574 }
13575
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013576 callback = get_callback(&argvars[1]);
13577 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013578 return;
13579
13580 timer = create_timer(msec, repeat);
13581 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013582 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583 else
13584 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013585 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013586 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587 }
13588}
13589
13590/*
13591 * "timer_stop(timer)" function
13592 */
13593 static void
13594f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13595{
13596 timer_T *timer;
13597
13598 if (argvars[0].v_type != VAR_NUMBER)
13599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013600 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013601 return;
13602 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013603 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013604 if (timer != NULL)
13605 stop_timer(timer);
13606}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013607
13608/*
13609 * "timer_stopall()" function
13610 */
13611 static void
13612f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13613{
13614 stop_all_timers();
13615}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616#endif
13617
13618/*
13619 * "tolower(string)" function
13620 */
13621 static void
13622f_tolower(typval_T *argvars, typval_T *rettv)
13623{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013625 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013626}
13627
13628/*
13629 * "toupper(string)" function
13630 */
13631 static void
13632f_toupper(typval_T *argvars, typval_T *rettv)
13633{
13634 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013635 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636}
13637
13638/*
13639 * "tr(string, fromstr, tostr)" function
13640 */
13641 static void
13642f_tr(typval_T *argvars, typval_T *rettv)
13643{
13644 char_u *in_str;
13645 char_u *fromstr;
13646 char_u *tostr;
13647 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013648 int inlen;
13649 int fromlen;
13650 int tolen;
13651 int idx;
13652 char_u *cpstr;
13653 int cplen;
13654 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655 char_u buf[NUMBUFLEN];
13656 char_u buf2[NUMBUFLEN];
13657 garray_T ga;
13658
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013659 in_str = tv_get_string(&argvars[0]);
13660 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13661 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662
13663 /* Default return value: empty string. */
13664 rettv->v_type = VAR_STRING;
13665 rettv->vval.v_string = NULL;
13666 if (fromstr == NULL || tostr == NULL)
13667 return; /* type error; errmsg already given */
13668 ga_init2(&ga, (int)sizeof(char), 80);
13669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013670 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 /* not multi-byte: fromstr and tostr must be the same length */
13672 if (STRLEN(fromstr) != STRLEN(tostr))
13673 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013675 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013676 ga_clear(&ga);
13677 return;
13678 }
13679
13680 /* fromstr and tostr have to contain the same number of chars */
13681 while (*in_str != NUL)
13682 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013683 if (has_mbyte)
13684 {
13685 inlen = (*mb_ptr2len)(in_str);
13686 cpstr = in_str;
13687 cplen = inlen;
13688 idx = 0;
13689 for (p = fromstr; *p != NUL; p += fromlen)
13690 {
13691 fromlen = (*mb_ptr2len)(p);
13692 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13693 {
13694 for (p = tostr; *p != NUL; p += tolen)
13695 {
13696 tolen = (*mb_ptr2len)(p);
13697 if (idx-- == 0)
13698 {
13699 cplen = tolen;
13700 cpstr = p;
13701 break;
13702 }
13703 }
13704 if (*p == NUL) /* tostr is shorter than fromstr */
13705 goto error;
13706 break;
13707 }
13708 ++idx;
13709 }
13710
13711 if (first && cpstr == in_str)
13712 {
13713 /* Check that fromstr and tostr have the same number of
13714 * (multi-byte) characters. Done only once when a character
13715 * of in_str doesn't appear in fromstr. */
13716 first = FALSE;
13717 for (p = tostr; *p != NUL; p += tolen)
13718 {
13719 tolen = (*mb_ptr2len)(p);
13720 --idx;
13721 }
13722 if (idx != 0)
13723 goto error;
13724 }
13725
13726 (void)ga_grow(&ga, cplen);
13727 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13728 ga.ga_len += cplen;
13729
13730 in_str += inlen;
13731 }
13732 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013733 {
13734 /* When not using multi-byte chars we can do it faster. */
13735 p = vim_strchr(fromstr, *in_str);
13736 if (p != NULL)
13737 ga_append(&ga, tostr[p - fromstr]);
13738 else
13739 ga_append(&ga, *in_str);
13740 ++in_str;
13741 }
13742 }
13743
13744 /* add a terminating NUL */
13745 (void)ga_grow(&ga, 1);
13746 ga_append(&ga, NUL);
13747
13748 rettv->vval.v_string = ga.ga_data;
13749}
13750
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013751/*
13752 * "trim({expr})" function
13753 */
13754 static void
13755f_trim(typval_T *argvars, typval_T *rettv)
13756{
13757 char_u buf1[NUMBUFLEN];
13758 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013759 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013760 char_u *mask = NULL;
13761 char_u *tail;
13762 char_u *prev;
13763 char_u *p;
13764 int c1;
13765
13766 rettv->v_type = VAR_STRING;
13767 if (head == NULL)
13768 {
13769 rettv->vval.v_string = NULL;
13770 return;
13771 }
13772
13773 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013774 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013775
13776 while (*head != NUL)
13777 {
13778 c1 = PTR2CHAR(head);
13779 if (mask == NULL)
13780 {
13781 if (c1 > ' ' && c1 != 0xa0)
13782 break;
13783 }
13784 else
13785 {
13786 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13787 if (c1 == PTR2CHAR(p))
13788 break;
13789 if (*p == NUL)
13790 break;
13791 }
13792 MB_PTR_ADV(head);
13793 }
13794
13795 for (tail = head + STRLEN(head); tail > head; tail = prev)
13796 {
13797 prev = tail;
13798 MB_PTR_BACK(head, prev);
13799 c1 = PTR2CHAR(prev);
13800 if (mask == NULL)
13801 {
13802 if (c1 > ' ' && c1 != 0xa0)
13803 break;
13804 }
13805 else
13806 {
13807 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13808 if (c1 == PTR2CHAR(p))
13809 break;
13810 if (*p == NUL)
13811 break;
13812 }
13813 }
13814 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13815}
13816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013817#ifdef FEAT_FLOAT
13818/*
13819 * "trunc({float})" function
13820 */
13821 static void
13822f_trunc(typval_T *argvars, typval_T *rettv)
13823{
13824 float_T f = 0.0;
13825
13826 rettv->v_type = VAR_FLOAT;
13827 if (get_float_arg(argvars, &f) == OK)
13828 /* trunc() is not in C90, use floor() or ceil() instead. */
13829 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13830 else
13831 rettv->vval.v_float = 0.0;
13832}
13833#endif
13834
13835/*
13836 * "type(expr)" function
13837 */
13838 static void
13839f_type(typval_T *argvars, typval_T *rettv)
13840{
13841 int n = -1;
13842
13843 switch (argvars[0].v_type)
13844 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013845 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13846 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013847 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013848 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13849 case VAR_LIST: n = VAR_TYPE_LIST; break;
13850 case VAR_DICT: n = VAR_TYPE_DICT; break;
13851 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013852 case VAR_SPECIAL:
13853 if (argvars[0].vval.v_number == VVAL_FALSE
13854 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013855 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013856 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013857 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013858 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013859 case VAR_JOB: n = VAR_TYPE_JOB; break;
13860 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013861 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013863 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013864 n = -1;
13865 break;
13866 }
13867 rettv->vval.v_number = n;
13868}
13869
13870/*
13871 * "undofile(name)" function
13872 */
13873 static void
13874f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13875{
13876 rettv->v_type = VAR_STRING;
13877#ifdef FEAT_PERSISTENT_UNDO
13878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013879 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880
13881 if (*fname == NUL)
13882 {
13883 /* If there is no file name there will be no undo file. */
13884 rettv->vval.v_string = NULL;
13885 }
13886 else
13887 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020013888 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889
13890 if (ffname != NULL)
13891 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13892 vim_free(ffname);
13893 }
13894 }
13895#else
13896 rettv->vval.v_string = NULL;
13897#endif
13898}
13899
13900/*
13901 * "undotree()" function
13902 */
13903 static void
13904f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13905{
13906 if (rettv_dict_alloc(rettv) == OK)
13907 {
13908 dict_T *dict = rettv->vval.v_dict;
13909 list_T *list;
13910
Bram Moolenaare0be1672018-07-08 16:50:37 +020013911 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13912 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13913 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13914 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13915 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13916 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917
13918 list = list_alloc();
13919 if (list != NULL)
13920 {
13921 u_eval_tree(curbuf->b_u_oldhead, list);
13922 dict_add_list(dict, "entries", list);
13923 }
13924 }
13925}
13926
13927/*
13928 * "values(dict)" function
13929 */
13930 static void
13931f_values(typval_T *argvars, typval_T *rettv)
13932{
13933 dict_list(argvars, rettv, 1);
13934}
13935
13936/*
13937 * "virtcol(string)" function
13938 */
13939 static void
13940f_virtcol(typval_T *argvars, typval_T *rettv)
13941{
13942 colnr_T vcol = 0;
13943 pos_T *fp;
13944 int fnum = curbuf->b_fnum;
13945
13946 fp = var2fpos(&argvars[0], FALSE, &fnum);
13947 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13948 && fnum == curbuf->b_fnum)
13949 {
13950 getvvcol(curwin, fp, NULL, NULL, &vcol);
13951 ++vcol;
13952 }
13953
13954 rettv->vval.v_number = vcol;
13955}
13956
13957/*
13958 * "visualmode()" function
13959 */
13960 static void
13961f_visualmode(typval_T *argvars, typval_T *rettv)
13962{
13963 char_u str[2];
13964
13965 rettv->v_type = VAR_STRING;
13966 str[0] = curbuf->b_visual_mode_eval;
13967 str[1] = NUL;
13968 rettv->vval.v_string = vim_strsave(str);
13969
13970 /* A non-zero number or non-empty string argument: reset mode. */
13971 if (non_zero_arg(&argvars[0]))
13972 curbuf->b_visual_mode_eval = NUL;
13973}
13974
13975/*
13976 * "wildmenumode()" function
13977 */
13978 static void
13979f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13980{
13981#ifdef FEAT_WILDMENU
13982 if (wild_menu_showing)
13983 rettv->vval.v_number = 1;
13984#endif
13985}
13986
13987/*
13988 * "winbufnr(nr)" function
13989 */
13990 static void
13991f_winbufnr(typval_T *argvars, typval_T *rettv)
13992{
13993 win_T *wp;
13994
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013995 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 if (wp == NULL)
13997 rettv->vval.v_number = -1;
13998 else
13999 rettv->vval.v_number = wp->w_buffer->b_fnum;
14000}
14001
14002/*
14003 * "wincol()" function
14004 */
14005 static void
14006f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14007{
14008 validate_cursor();
14009 rettv->vval.v_number = curwin->w_wcol + 1;
14010}
14011
14012/*
14013 * "winheight(nr)" function
14014 */
14015 static void
14016f_winheight(typval_T *argvars, typval_T *rettv)
14017{
14018 win_T *wp;
14019
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014020 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 if (wp == NULL)
14022 rettv->vval.v_number = -1;
14023 else
14024 rettv->vval.v_number = wp->w_height;
14025}
14026
14027/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014028 * "winlayout()" function
14029 */
14030 static void
14031f_winlayout(typval_T *argvars, typval_T *rettv)
14032{
14033 tabpage_T *tp;
14034
14035 if (rettv_list_alloc(rettv) != OK)
14036 return;
14037
14038 if (argvars[0].v_type == VAR_UNKNOWN)
14039 tp = curtab;
14040 else
14041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014042 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014043 if (tp == NULL)
14044 return;
14045 }
14046
14047 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14048}
14049
14050/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014051 * "winline()" function
14052 */
14053 static void
14054f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14055{
14056 validate_cursor();
14057 rettv->vval.v_number = curwin->w_wrow + 1;
14058}
14059
14060/*
14061 * "winnr()" function
14062 */
14063 static void
14064f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14065{
14066 int nr = 1;
14067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014069 rettv->vval.v_number = nr;
14070}
14071
14072/*
14073 * "winrestcmd()" function
14074 */
14075 static void
14076f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14077{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014078 win_T *wp;
14079 int winnr = 1;
14080 garray_T ga;
14081 char_u buf[50];
14082
14083 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014084 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014085 {
14086 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14087 ga_concat(&ga, buf);
14088 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14089 ga_concat(&ga, buf);
14090 ++winnr;
14091 }
14092 ga_append(&ga, NUL);
14093
14094 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095 rettv->v_type = VAR_STRING;
14096}
14097
14098/*
14099 * "winrestview()" function
14100 */
14101 static void
14102f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14103{
14104 dict_T *dict;
14105
14106 if (argvars[0].v_type != VAR_DICT
14107 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014108 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109 else
14110 {
14111 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014112 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014113 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014114 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014116 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14118 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014119 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120 curwin->w_set_curswant = FALSE;
14121 }
14122
14123 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014124 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014125#ifdef FEAT_DIFF
14126 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014127 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014128#endif
14129 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014130 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014131 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014132 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133
14134 check_cursor();
14135 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014136 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137 changed_window_setting();
14138
14139 if (curwin->w_topline <= 0)
14140 curwin->w_topline = 1;
14141 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14142 curwin->w_topline = curbuf->b_ml.ml_line_count;
14143#ifdef FEAT_DIFF
14144 check_topfill(curwin, TRUE);
14145#endif
14146 }
14147}
14148
14149/*
14150 * "winsaveview()" function
14151 */
14152 static void
14153f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14154{
14155 dict_T *dict;
14156
14157 if (rettv_dict_alloc(rettv) == FAIL)
14158 return;
14159 dict = rettv->vval.v_dict;
14160
Bram Moolenaare0be1672018-07-08 16:50:37 +020014161 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14162 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014163 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014165 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166
Bram Moolenaare0be1672018-07-08 16:50:37 +020014167 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014169 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014170#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014171 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14172 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014173}
14174
14175/*
14176 * "winwidth(nr)" function
14177 */
14178 static void
14179f_winwidth(typval_T *argvars, typval_T *rettv)
14180{
14181 win_T *wp;
14182
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014183 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184 if (wp == NULL)
14185 rettv->vval.v_number = -1;
14186 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014187 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014188}
14189
14190/*
14191 * "wordcount()" function
14192 */
14193 static void
14194f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14195{
14196 if (rettv_dict_alloc(rettv) == FAIL)
14197 return;
14198 cursor_pos_info(rettv->vval.v_dict);
14199}
14200
14201/*
14202 * "writefile()" function
14203 */
14204 static void
14205f_writefile(typval_T *argvars, typval_T *rettv)
14206{
14207 int binary = FALSE;
14208 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014209#ifdef HAVE_FSYNC
14210 int do_fsync = p_fs;
14211#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014212 char_u *fname;
14213 FILE *fd;
14214 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014215 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014216 list_T *list = NULL;
14217 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014219 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014220 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014221 return;
14222
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014223 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014224 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014225 list = argvars[0].vval.v_list;
14226 if (list == NULL)
14227 return;
14228 for (li = list->lv_first; li != NULL; li = li->li_next)
14229 if (tv_get_string_chk(&li->li_tv) == NULL)
14230 return;
14231 }
14232 else if (argvars[0].v_type == VAR_BLOB)
14233 {
14234 blob = argvars[0].vval.v_blob;
14235 if (blob == NULL)
14236 return;
14237 }
14238 else
14239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014240 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014241 return;
14242 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014243
14244 if (argvars[2].v_type != VAR_UNKNOWN)
14245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014246 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014247
14248 if (arg2 == NULL)
14249 return;
14250 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014251 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014252 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014253 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014254#ifdef HAVE_FSYNC
14255 if (vim_strchr(arg2, 's') != NULL)
14256 do_fsync = TRUE;
14257 else if (vim_strchr(arg2, 'S') != NULL)
14258 do_fsync = FALSE;
14259#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014260 }
14261
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014262 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014263 if (fname == NULL)
14264 return;
14265
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014266 /* Always open the file in binary mode, library functions have a mind of
14267 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014268 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14269 append ? APPENDBIN : WRITEBIN)) == NULL)
14270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014271 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014272 ret = -1;
14273 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014274 else if (blob)
14275 {
14276 if (write_blob(fd, blob) == FAIL)
14277 ret = -1;
14278#ifdef HAVE_FSYNC
14279 else if (do_fsync)
14280 // Ignore the error, the user wouldn't know what to do about it.
14281 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014282 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014283#endif
14284 fclose(fd);
14285 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014286 else
14287 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014288 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014289 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014290#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014291 else if (do_fsync)
14292 /* Ignore the error, the user wouldn't know what to do about it.
14293 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014294 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014295#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014296 fclose(fd);
14297 }
14298
14299 rettv->vval.v_number = ret;
14300}
14301
14302/*
14303 * "xor(expr, expr)" function
14304 */
14305 static void
14306f_xor(typval_T *argvars, typval_T *rettv)
14307{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014308 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14309 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014310}
14311
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014312#endif /* FEAT_EVAL */