blob: e7f870c6fb8bb7489f19d53480b75b649e21a261 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010027static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029
30#ifdef FEAT_FLOAT
31static void f_abs(typval_T *argvars, typval_T *rettv);
32static void f_acos(typval_T *argvars, typval_T *rettv);
33#endif
34static void f_add(typval_T *argvars, typval_T *rettv);
35static void f_and(typval_T *argvars, typval_T *rettv);
36static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020037static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020038static void f_argc(typval_T *argvars, typval_T *rettv);
39static void f_argidx(typval_T *argvars, typval_T *rettv);
40static void f_arglistid(typval_T *argvars, typval_T *rettv);
41static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042#ifdef FEAT_FLOAT
43static void f_asin(typval_T *argvars, typval_T *rettv);
44static void f_atan(typval_T *argvars, typval_T *rettv);
45static void f_atan2(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010047#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020048static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010049static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010050# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010051static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010052# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010053#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_browse(typval_T *argvars, typval_T *rettv);
55static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020056static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_bufexists(typval_T *argvars, typval_T *rettv);
58static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020059static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_bufloaded(typval_T *argvars, typval_T *rettv);
61static void f_bufname(typval_T *argvars, typval_T *rettv);
62static void f_bufnr(typval_T *argvars, typval_T *rettv);
63static void f_bufwinid(typval_T *argvars, typval_T *rettv);
64static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
65static void f_byte2line(typval_T *argvars, typval_T *rettv);
66static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
67static void f_byteidx(typval_T *argvars, typval_T *rettv);
68static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
69static void f_call(typval_T *argvars, typval_T *rettv);
70#ifdef FEAT_FLOAT
71static void f_ceil(typval_T *argvars, typval_T *rettv);
72#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020073static void f_changenr(typval_T *argvars, typval_T *rettv);
74static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020075static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_cindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_confirm(typval_T *argvars, typval_T *rettv);
79static void f_copy(typval_T *argvars, typval_T *rettv);
80#ifdef FEAT_FLOAT
81static void f_cos(typval_T *argvars, typval_T *rettv);
82static void f_cosh(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_count(typval_T *argvars, typval_T *rettv);
85static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
86static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010087#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020088static void f_debugbreak(typval_T *argvars, typval_T *rettv);
89#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_deepcopy(typval_T *argvars, typval_T *rettv);
91static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +020092static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_did_filetype(typval_T *argvars, typval_T *rettv);
94static void f_diff_filler(typval_T *argvars, typval_T *rettv);
95static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
96static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020097static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_escape(typval_T *argvars, typval_T *rettv);
99static void f_eval(typval_T *argvars, typval_T *rettv);
100static void f_eventhandler(typval_T *argvars, typval_T *rettv);
101static void f_executable(typval_T *argvars, typval_T *rettv);
102static void f_execute(typval_T *argvars, typval_T *rettv);
103static void f_exepath(typval_T *argvars, typval_T *rettv);
104static void f_exists(typval_T *argvars, typval_T *rettv);
105#ifdef FEAT_FLOAT
106static void f_exp(typval_T *argvars, typval_T *rettv);
107#endif
108static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200109static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200110static void f_extend(typval_T *argvars, typval_T *rettv);
111static void f_feedkeys(typval_T *argvars, typval_T *rettv);
112static void f_filereadable(typval_T *argvars, typval_T *rettv);
113static void f_filewritable(typval_T *argvars, typval_T *rettv);
114static void f_filter(typval_T *argvars, typval_T *rettv);
115static void f_finddir(typval_T *argvars, typval_T *rettv);
116static void f_findfile(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_float2nr(typval_T *argvars, typval_T *rettv);
119static void f_floor(typval_T *argvars, typval_T *rettv);
120static void f_fmod(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_fnameescape(typval_T *argvars, typval_T *rettv);
123static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
124static void f_foldclosed(typval_T *argvars, typval_T *rettv);
125static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
126static void f_foldlevel(typval_T *argvars, typval_T *rettv);
127static void f_foldtext(typval_T *argvars, typval_T *rettv);
128static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
129static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200130static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_function(typval_T *argvars, typval_T *rettv);
132static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
133static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200134static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200135static void f_getbufline(typval_T *argvars, typval_T *rettv);
136static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100137static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_getchar(typval_T *argvars, typval_T *rettv);
139static void f_getcharmod(typval_T *argvars, typval_T *rettv);
140static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
141static void f_getcmdline(typval_T *argvars, typval_T *rettv);
142#if defined(FEAT_CMDL_COMPL)
143static void f_getcompletion(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
146static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
147static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
148static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200149static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200150static void f_getfontname(typval_T *argvars, typval_T *rettv);
151static void f_getfperm(typval_T *argvars, typval_T *rettv);
152static void f_getfsize(typval_T *argvars, typval_T *rettv);
153static void f_getftime(typval_T *argvars, typval_T *rettv);
154static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100155static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200157static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_getpid(typval_T *argvars, typval_T *rettv);
159static void f_getcurpos(typval_T *argvars, typval_T *rettv);
160static void f_getpos(typval_T *argvars, typval_T *rettv);
161static void f_getqflist(typval_T *argvars, typval_T *rettv);
162static void f_getreg(typval_T *argvars, typval_T *rettv);
163static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_gettabvar(typval_T *argvars, typval_T *rettv);
166static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100167static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200168static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100169static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getwinposx(typval_T *argvars, typval_T *rettv);
171static void f_getwinposy(typval_T *argvars, typval_T *rettv);
172static void f_getwinvar(typval_T *argvars, typval_T *rettv);
173static void f_glob(typval_T *argvars, typval_T *rettv);
174static void f_globpath(typval_T *argvars, typval_T *rettv);
175static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
176static void f_has(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
178static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_hlID(typval_T *argvars, typval_T *rettv);
180static void f_hlexists(typval_T *argvars, typval_T *rettv);
181static void f_hostname(typval_T *argvars, typval_T *rettv);
182static void f_iconv(typval_T *argvars, typval_T *rettv);
183static void f_indent(typval_T *argvars, typval_T *rettv);
184static void f_index(typval_T *argvars, typval_T *rettv);
185static void f_input(typval_T *argvars, typval_T *rettv);
186static void f_inputdialog(typval_T *argvars, typval_T *rettv);
187static void f_inputlist(typval_T *argvars, typval_T *rettv);
188static void f_inputrestore(typval_T *argvars, typval_T *rettv);
189static void f_inputsave(typval_T *argvars, typval_T *rettv);
190static void f_inputsecret(typval_T *argvars, typval_T *rettv);
191static void f_insert(typval_T *argvars, typval_T *rettv);
192static void f_invert(typval_T *argvars, typval_T *rettv);
193static void f_isdirectory(typval_T *argvars, typval_T *rettv);
194static void f_islocked(typval_T *argvars, typval_T *rettv);
195#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200196static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_isnan(typval_T *argvars, typval_T *rettv);
198#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
200static void f_len(typval_T *argvars, typval_T *rettv);
201static void f_libcall(typval_T *argvars, typval_T *rettv);
202static void f_libcallnr(typval_T *argvars, typval_T *rettv);
203static void f_line(typval_T *argvars, typval_T *rettv);
204static void f_line2byte(typval_T *argvars, typval_T *rettv);
205static void f_lispindent(typval_T *argvars, typval_T *rettv);
206static void f_localtime(typval_T *argvars, typval_T *rettv);
207#ifdef FEAT_FLOAT
208static void f_log(typval_T *argvars, typval_T *rettv);
209static void f_log10(typval_T *argvars, typval_T *rettv);
210#endif
211#ifdef FEAT_LUA
212static void f_luaeval(typval_T *argvars, typval_T *rettv);
213#endif
214static void f_map(typval_T *argvars, typval_T *rettv);
215static void f_maparg(typval_T *argvars, typval_T *rettv);
216static void f_mapcheck(typval_T *argvars, typval_T *rettv);
217static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_matchend(typval_T *argvars, typval_T *rettv);
219static void f_matchlist(typval_T *argvars, typval_T *rettv);
220static void f_matchstr(typval_T *argvars, typval_T *rettv);
221static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
222static void f_max(typval_T *argvars, typval_T *rettv);
223static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200224static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_mode(typval_T *argvars, typval_T *rettv);
226#ifdef FEAT_MZSCHEME
227static void f_mzeval(typval_T *argvars, typval_T *rettv);
228#endif
229static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
230static void f_nr2char(typval_T *argvars, typval_T *rettv);
231static void f_or(typval_T *argvars, typval_T *rettv);
232static void f_pathshorten(typval_T *argvars, typval_T *rettv);
233#ifdef FEAT_PERL
234static void f_perleval(typval_T *argvars, typval_T *rettv);
235#endif
236#ifdef FEAT_FLOAT
237static void f_pow(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
240static void f_printf(typval_T *argvars, typval_T *rettv);
241static void f_pumvisible(typval_T *argvars, typval_T *rettv);
242#ifdef FEAT_PYTHON3
243static void f_py3eval(typval_T *argvars, typval_T *rettv);
244#endif
245#ifdef FEAT_PYTHON
246static void f_pyeval(typval_T *argvars, typval_T *rettv);
247#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100248#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
249static void f_pyxeval(typval_T *argvars, typval_T *rettv);
250#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200251static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200254static void f_reg_executing(typval_T *argvars, typval_T *rettv);
255static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256static void f_reltime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_reltimestr(typval_T *argvars, typval_T *rettv);
261static void f_remote_expr(typval_T *argvars, typval_T *rettv);
262static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
263static void f_remote_peek(typval_T *argvars, typval_T *rettv);
264static void f_remote_read(typval_T *argvars, typval_T *rettv);
265static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100266static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200267static void f_remove(typval_T *argvars, typval_T *rettv);
268static void f_rename(typval_T *argvars, typval_T *rettv);
269static void f_repeat(typval_T *argvars, typval_T *rettv);
270static void f_resolve(typval_T *argvars, typval_T *rettv);
271static void f_reverse(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_round(typval_T *argvars, typval_T *rettv);
274#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100275#ifdef FEAT_RUBY
276static void f_rubyeval(typval_T *argvars, typval_T *rettv);
277#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200278static void f_screenattr(typval_T *argvars, typval_T *rettv);
279static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100280static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200281static void f_screencol(typval_T *argvars, typval_T *rettv);
282static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100283static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200284static void f_search(typval_T *argvars, typval_T *rettv);
285static void f_searchdecl(typval_T *argvars, typval_T *rettv);
286static void f_searchpair(typval_T *argvars, typval_T *rettv);
287static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
288static void f_searchpos(typval_T *argvars, typval_T *rettv);
289static void f_server2client(typval_T *argvars, typval_T *rettv);
290static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200291static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200292static void f_setbufvar(typval_T *argvars, typval_T *rettv);
293static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
294static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200295static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_setfperm(typval_T *argvars, typval_T *rettv);
297static void f_setline(typval_T *argvars, typval_T *rettv);
298static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_setpos(typval_T *argvars, typval_T *rettv);
300static void f_setqflist(typval_T *argvars, typval_T *rettv);
301static void f_setreg(typval_T *argvars, typval_T *rettv);
302static void f_settabvar(typval_T *argvars, typval_T *rettv);
303static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100304static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200305static void f_setwinvar(typval_T *argvars, typval_T *rettv);
306#ifdef FEAT_CRYPT
307static void f_sha256(typval_T *argvars, typval_T *rettv);
308#endif /* FEAT_CRYPT */
309static void f_shellescape(typval_T *argvars, typval_T *rettv);
310static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
311static void f_simplify(typval_T *argvars, typval_T *rettv);
312#ifdef FEAT_FLOAT
313static void f_sin(typval_T *argvars, typval_T *rettv);
314static void f_sinh(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_soundfold(typval_T *argvars, typval_T *rettv);
317static void f_spellbadword(typval_T *argvars, typval_T *rettv);
318static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
319static void f_split(typval_T *argvars, typval_T *rettv);
320#ifdef FEAT_FLOAT
321static void f_sqrt(typval_T *argvars, typval_T *rettv);
322static void f_str2float(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200324static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_str2nr(typval_T *argvars, typval_T *rettv);
326static void f_strchars(typval_T *argvars, typval_T *rettv);
327#ifdef HAVE_STRFTIME
328static void f_strftime(typval_T *argvars, typval_T *rettv);
329#endif
330static void f_strgetchar(typval_T *argvars, typval_T *rettv);
331static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_strlen(typval_T *argvars, typval_T *rettv);
333static void f_strcharpart(typval_T *argvars, typval_T *rettv);
334static void f_strpart(typval_T *argvars, typval_T *rettv);
335static void f_strridx(typval_T *argvars, typval_T *rettv);
336static void f_strtrans(typval_T *argvars, typval_T *rettv);
337static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
338static void f_strwidth(typval_T *argvars, typval_T *rettv);
339static void f_submatch(typval_T *argvars, typval_T *rettv);
340static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200341static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200342static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_synID(typval_T *argvars, typval_T *rettv);
344static void f_synIDattr(typval_T *argvars, typval_T *rettv);
345static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
346static void f_synstack(typval_T *argvars, typval_T *rettv);
347static void f_synconcealed(typval_T *argvars, typval_T *rettv);
348static void f_system(typval_T *argvars, typval_T *rettv);
349static void f_systemlist(typval_T *argvars, typval_T *rettv);
350static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
351static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
352static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
353static void f_taglist(typval_T *argvars, typval_T *rettv);
354static void f_tagfiles(typval_T *argvars, typval_T *rettv);
355static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356#ifdef FEAT_FLOAT
357static void f_tan(typval_T *argvars, typval_T *rettv);
358static void f_tanh(typval_T *argvars, typval_T *rettv);
359#endif
360#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200361static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200362static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200363static void f_timer_start(typval_T *argvars, typval_T *rettv);
364static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200365static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366#endif
367static void f_tolower(typval_T *argvars, typval_T *rettv);
368static void f_toupper(typval_T *argvars, typval_T *rettv);
369static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100370static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371#ifdef FEAT_FLOAT
372static void f_trunc(typval_T *argvars, typval_T *rettv);
373#endif
374static void f_type(typval_T *argvars, typval_T *rettv);
375static void f_undofile(typval_T *argvars, typval_T *rettv);
376static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200377static void f_virtcol(typval_T *argvars, typval_T *rettv);
378static void f_visualmode(typval_T *argvars, typval_T *rettv);
379static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200380static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
382static void f_win_getid(typval_T *argvars, typval_T *rettv);
383static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
384static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
385static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100386static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387static void f_winbufnr(typval_T *argvars, typval_T *rettv);
388static void f_wincol(typval_T *argvars, typval_T *rettv);
389static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200390static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391static void f_winline(typval_T *argvars, typval_T *rettv);
392static void f_winnr(typval_T *argvars, typval_T *rettv);
393static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
394static void f_winrestview(typval_T *argvars, typval_T *rettv);
395static void f_winsaveview(typval_T *argvars, typval_T *rettv);
396static void f_winwidth(typval_T *argvars, typval_T *rettv);
397static void f_writefile(typval_T *argvars, typval_T *rettv);
398static void f_wordcount(typval_T *argvars, typval_T *rettv);
399static void f_xor(typval_T *argvars, typval_T *rettv);
400
401/*
402 * Array with names and number of arguments of all internal functions
403 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
404 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200405typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200407 char *f_name; // function name
408 char f_min_argc; // minimal number of arguments
409 char f_max_argc; // maximal number of arguments
410 char f_argtype; // for method: FEARG_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200412 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200413} funcentry_T;
414
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200415// values for f_argtype; zero means it cannot be used as a method
416#define FEARG_1 1 // base is the first argument
417#define FEARG_2 2 // base is the second argument
418#define FEARG_LAST 9 // base is the last argument
419
Bram Moolenaarac92e252019-08-03 21:58:38 +0200420static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421{
422#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200423 {"abs", 1, 1, 0, f_abs},
424 {"acos", 1, 1, 0, f_acos}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200426 {"add", 2, 2, FEARG_1, f_add},
427 {"and", 2, 2, 0, f_and},
428 {"append", 2, 2, FEARG_LAST, f_append},
429 {"appendbufline", 3, 3, FEARG_LAST, f_appendbufline},
430 {"argc", 0, 1, 0, f_argc},
431 {"argidx", 0, 0, 0, f_argidx},
432 {"arglistid", 0, 2, 0, f_arglistid},
433 {"argv", 0, 2, 0, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200435 {"asin", 1, 1, 0, f_asin}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200437 {"assert_beeps", 1, 2, 0, f_assert_beeps},
438 {"assert_equal", 2, 3, FEARG_2, f_assert_equal},
439 {"assert_equalfile", 2, 2, 0, f_assert_equalfile},
440 {"assert_exception", 1, 2, 0, f_assert_exception},
441 {"assert_fails", 1, 3, 0, f_assert_fails},
442 {"assert_false", 1, 2, 0, f_assert_false},
443 {"assert_inrange", 3, 4, 0, f_assert_inrange},
444 {"assert_match", 2, 3, 0, f_assert_match},
445 {"assert_notequal", 2, 3, FEARG_2, f_assert_notequal},
446 {"assert_notmatch", 2, 3, 0, f_assert_notmatch},
447 {"assert_report", 1, 1, 0, f_assert_report},
448 {"assert_true", 1, 2, 0, f_assert_true},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200450 {"atan", 1, 1, 0, f_atan},
451 {"atan2", 2, 2, 0, f_atan2},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100453#ifdef FEAT_BEVAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200454 {"balloon_gettext", 0, 0, 0, f_balloon_gettext},
455 {"balloon_show", 1, 1, 0, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100456# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200457 {"balloon_split", 1, 1, 0, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100458# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100459#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200460 {"browse", 4, 4, 0, f_browse},
461 {"browsedir", 2, 2, 0, f_browsedir},
462 {"bufadd", 1, 1, 0, f_bufadd},
463 {"bufexists", 1, 1, 0, f_bufexists},
464 {"buffer_exists", 1, 1, 0, f_bufexists}, // obsolete
465 {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
466 {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
467 {"buflisted", 1, 1, 0, f_buflisted},
468 {"bufload", 1, 1, 0, f_bufload},
469 {"bufloaded", 1, 1, 0, f_bufloaded},
470 {"bufname", 1, 1, 0, f_bufname},
471 {"bufnr", 1, 2, 0, f_bufnr},
472 {"bufwinid", 1, 1, 0, f_bufwinid},
473 {"bufwinnr", 1, 1, 0, f_bufwinnr},
474 {"byte2line", 1, 1, 0, f_byte2line},
475 {"byteidx", 2, 2, 0, f_byteidx},
476 {"byteidxcomp", 2, 2, 0, f_byteidxcomp},
477 {"call", 2, 3, 0, f_call},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200478#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200479 {"ceil", 1, 1, 0, f_ceil},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200482 {"ch_canread", 1, 1, 0, f_ch_canread},
483 {"ch_close", 1, 1, 0, f_ch_close},
484 {"ch_close_in", 1, 1, 0, f_ch_close_in},
485 {"ch_evalexpr", 2, 3, 0, f_ch_evalexpr},
486 {"ch_evalraw", 2, 3, 0, f_ch_evalraw},
487 {"ch_getbufnr", 2, 2, 0, f_ch_getbufnr},
488 {"ch_getjob", 1, 1, 0, f_ch_getjob},
489 {"ch_info", 1, 1, 0, f_ch_info},
490 {"ch_log", 1, 2, 0, f_ch_log},
491 {"ch_logfile", 1, 2, 0, f_ch_logfile},
492 {"ch_open", 1, 2, 0, f_ch_open},
493 {"ch_read", 1, 2, 0, f_ch_read},
494 {"ch_readblob", 1, 2, 0, f_ch_readblob},
495 {"ch_readraw", 1, 2, 0, f_ch_readraw},
496 {"ch_sendexpr", 2, 3, 0, f_ch_sendexpr},
497 {"ch_sendraw", 2, 3, 0, f_ch_sendraw},
498 {"ch_setoptions", 2, 2, 0, f_ch_setoptions},
499 {"ch_status", 1, 2, 0, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200501 {"changenr", 0, 0, 0, f_changenr},
502 {"char2nr", 1, 2, 0, f_char2nr},
503 {"chdir", 1, 1, 0, f_chdir},
504 {"cindent", 1, 1, 0, f_cindent},
505 {"clearmatches", 0, 1, 0, f_clearmatches},
506 {"col", 1, 1, 0, f_col},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507#if defined(FEAT_INS_EXPAND)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200508 {"complete", 2, 2, 0, f_complete},
509 {"complete_add", 1, 1, 0, f_complete_add},
510 {"complete_check", 0, 0, 0, f_complete_check},
511 {"complete_info", 0, 1, 0, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200513 {"confirm", 1, 4, 0, f_confirm},
514 {"copy", 1, 1, FEARG_1, f_copy},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200516 {"cos", 1, 1, 0, f_cos},
517 {"cosh", 1, 1, 0, f_cosh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200519 {"count", 2, 4, FEARG_1, f_count},
520 {"cscope_connection",0,3, 0, f_cscope_connection},
521 {"cursor", 1, 3, 0, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100522#ifdef MSWIN
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200523 {"debugbreak", 1, 1, 0, f_debugbreak},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200524#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200525 {"deepcopy", 1, 2, 0, f_deepcopy},
526 {"delete", 1, 2, 0, f_delete},
527 {"deletebufline", 2, 3, 0, f_deletebufline},
528 {"did_filetype", 0, 0, 0, f_did_filetype},
529 {"diff_filler", 1, 1, 0, f_diff_filler},
530 {"diff_hlID", 2, 2, 0, f_diff_hlID},
531 {"empty", 1, 1, FEARG_1, f_empty},
532 {"environ", 0, 0, 0, f_environ},
533 {"escape", 2, 2, 0, f_escape},
534 {"eval", 1, 1, FEARG_1, f_eval},
535 {"eventhandler", 0, 0, 0, f_eventhandler},
536 {"executable", 1, 1, 0, f_executable},
537 {"execute", 1, 2, 0, f_execute},
538 {"exepath", 1, 1, 0, f_exepath},
539 {"exists", 1, 1, 0, f_exists},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200541 {"exp", 1, 1, 0, f_exp},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200543 {"expand", 1, 3, 0, f_expand},
544 {"expandcmd", 1, 1, 0, f_expandcmd},
545 {"extend", 2, 3, FEARG_1, f_extend},
546 {"feedkeys", 1, 2, 0, f_feedkeys},
547 {"file_readable", 1, 1, 0, f_filereadable}, // obsolete
548 {"filereadable", 1, 1, 0, f_filereadable},
549 {"filewritable", 1, 1, 0, f_filewritable},
550 {"filter", 2, 2, FEARG_1, f_filter},
551 {"finddir", 1, 3, 0, f_finddir},
552 {"findfile", 1, 3, 0, f_findfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200553#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200554 {"float2nr", 1, 1, 0, f_float2nr},
555 {"floor", 1, 1, 0, f_floor},
556 {"fmod", 2, 2, 0, f_fmod},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200557#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200558 {"fnameescape", 1, 1, 0, f_fnameescape},
559 {"fnamemodify", 2, 2, 0, f_fnamemodify},
560 {"foldclosed", 1, 1, 0, f_foldclosed},
561 {"foldclosedend", 1, 1, 0, f_foldclosedend},
562 {"foldlevel", 1, 1, 0, f_foldlevel},
563 {"foldtext", 0, 0, 0, f_foldtext},
564 {"foldtextresult", 1, 1, 0, f_foldtextresult},
565 {"foreground", 0, 0, 0, f_foreground},
566 {"funcref", 1, 3, 0, f_funcref},
567 {"function", 1, 3, 0, f_function},
568 {"garbagecollect", 0, 1, 0, f_garbagecollect},
569 {"get", 2, 3, FEARG_1, f_get},
570 {"getbufinfo", 0, 1, 0, f_getbufinfo},
571 {"getbufline", 2, 3, 0, f_getbufline},
572 {"getbufvar", 2, 3, 0, f_getbufvar},
573 {"getchangelist", 1, 1, 0, f_getchangelist},
574 {"getchar", 0, 1, 0, f_getchar},
575 {"getcharmod", 0, 0, 0, f_getcharmod},
576 {"getcharsearch", 0, 0, 0, f_getcharsearch},
577 {"getcmdline", 0, 0, 0, f_getcmdline},
578 {"getcmdpos", 0, 0, 0, f_getcmdpos},
579 {"getcmdtype", 0, 0, 0, f_getcmdtype},
580 {"getcmdwintype", 0, 0, 0, f_getcmdwintype},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200582 {"getcompletion", 2, 3, 0, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200583#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200584 {"getcurpos", 0, 0, 0, f_getcurpos},
585 {"getcwd", 0, 2, 0, f_getcwd},
586 {"getenv", 1, 1, 0, f_getenv},
587 {"getfontname", 0, 1, 0, f_getfontname},
588 {"getfperm", 1, 1, 0, f_getfperm},
589 {"getfsize", 1, 1, 0, f_getfsize},
590 {"getftime", 1, 1, 0, f_getftime},
591 {"getftype", 1, 1, 0, f_getftype},
592 {"getjumplist", 0, 2, 0, f_getjumplist},
593 {"getline", 1, 2, 0, f_getline},
594 {"getloclist", 1, 2, 0, f_getloclist},
595 {"getmatches", 0, 1, 0, f_getmatches},
596 {"getpid", 0, 0, 0, f_getpid},
597 {"getpos", 1, 1, 0, f_getpos},
598 {"getqflist", 0, 1, 0, f_getqflist},
599 {"getreg", 0, 3, 0, f_getreg},
600 {"getregtype", 0, 1, 0, f_getregtype},
601 {"gettabinfo", 0, 1, 0, f_gettabinfo},
602 {"gettabvar", 2, 3, 0, f_gettabvar},
603 {"gettabwinvar", 3, 4, 0, f_gettabwinvar},
604 {"gettagstack", 0, 1, 0, f_gettagstack},
605 {"getwininfo", 0, 1, 0, f_getwininfo},
606 {"getwinpos", 0, 1, 0, f_getwinpos},
607 {"getwinposx", 0, 0, 0, f_getwinposx},
608 {"getwinposy", 0, 0, 0, f_getwinposy},
609 {"getwinvar", 2, 3, 0, f_getwinvar},
610 {"glob", 1, 4, 0, f_glob},
611 {"glob2regpat", 1, 1, 0, f_glob2regpat},
612 {"globpath", 2, 5, 0, f_globpath},
613 {"has", 1, 1, 0, f_has},
614 {"has_key", 2, 2, FEARG_1, f_has_key},
615 {"haslocaldir", 0, 2, 0, f_haslocaldir},
616 {"hasmapto", 1, 3, 0, f_hasmapto},
617 {"highlightID", 1, 1, 0, f_hlID}, // obsolete
618 {"highlight_exists",1, 1, 0, f_hlexists}, // obsolete
619 {"histadd", 2, 2, 0, f_histadd},
620 {"histdel", 1, 2, 0, f_histdel},
621 {"histget", 1, 2, 0, f_histget},
622 {"histnr", 1, 1, 0, f_histnr},
623 {"hlID", 1, 1, 0, f_hlID},
624 {"hlexists", 1, 1, 0, f_hlexists},
625 {"hostname", 0, 0, 0, f_hostname},
626 {"iconv", 3, 3, 0, f_iconv},
627 {"indent", 1, 1, 0, f_indent},
628 {"index", 2, 4, FEARG_1, f_index},
629 {"input", 1, 3, 0, f_input},
630 {"inputdialog", 1, 3, 0, f_inputdialog},
631 {"inputlist", 1, 1, 0, f_inputlist},
632 {"inputrestore", 0, 0, 0, f_inputrestore},
633 {"inputsave", 0, 0, 0, f_inputsave},
634 {"inputsecret", 1, 2, 0, f_inputsecret},
635 {"insert", 2, 3, FEARG_1, f_insert},
636 {"invert", 1, 1, 0, f_invert},
637 {"isdirectory", 1, 1, 0, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200638#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200639 {"isinf", 1, 1, 0, f_isinf},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200640#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200641 {"islocked", 1, 1, 0, f_islocked},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200643 {"isnan", 1, 1, 0, f_isnan},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200645 {"items", 1, 1, FEARG_1, f_items},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200647 {"job_getchannel", 1, 1, 0, f_job_getchannel},
648 {"job_info", 0, 1, 0, f_job_info},
649 {"job_setoptions", 2, 2, 0, f_job_setoptions},
650 {"job_start", 1, 2, 0, f_job_start},
651 {"job_status", 1, 1, 0, f_job_status},
652 {"job_stop", 1, 2, 0, f_job_stop},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200654 {"join", 1, 2, FEARG_1, f_join},
655 {"js_decode", 1, 1, 0, f_js_decode},
656 {"js_encode", 1, 1, 0, f_js_encode},
657 {"json_decode", 1, 1, 0, f_json_decode},
658 {"json_encode", 1, 1, 0, f_json_encode},
659 {"keys", 1, 1, FEARG_1, f_keys},
660 {"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
661 {"len", 1, 1, FEARG_1, f_len},
662 {"libcall", 3, 3, 0, f_libcall},
663 {"libcallnr", 3, 3, 0, f_libcallnr},
664 {"line", 1, 1, 0, f_line},
665 {"line2byte", 1, 1, 0, f_line2byte},
666 {"lispindent", 1, 1, 0, f_lispindent},
667 {"list2str", 1, 2, 0, f_list2str},
668 {"listener_add", 1, 2, 0, f_listener_add},
669 {"listener_flush", 0, 1, 0, f_listener_flush},
670 {"listener_remove", 1, 1, 0, f_listener_remove},
671 {"localtime", 0, 0, 0, f_localtime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200673 {"log", 1, 1, 0, f_log},
674 {"log10", 1, 1, 0, f_log10},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675#endif
676#ifdef FEAT_LUA
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200677 {"luaeval", 1, 2, 0, f_luaeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200678#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200679 {"map", 2, 2, FEARG_1, f_map},
680 {"maparg", 1, 4, 0, f_maparg},
681 {"mapcheck", 1, 3, 0, f_mapcheck},
682 {"match", 2, 4, 0, f_match},
683 {"matchadd", 2, 5, 0, f_matchadd},
684 {"matchaddpos", 2, 5, 0, f_matchaddpos},
685 {"matcharg", 1, 1, 0, f_matcharg},
686 {"matchdelete", 1, 2, 0, f_matchdelete},
687 {"matchend", 2, 4, 0, f_matchend},
688 {"matchlist", 2, 4, 0, f_matchlist},
689 {"matchstr", 2, 4, 0, f_matchstr},
690 {"matchstrpos", 2, 4, 0, f_matchstrpos},
691 {"max", 1, 1, FEARG_1, f_max},
692 {"min", 1, 1, FEARG_1, f_min},
693 {"mkdir", 1, 3, 0, f_mkdir},
694 {"mode", 0, 1, 0, f_mode},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200695#ifdef FEAT_MZSCHEME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200696 {"mzeval", 1, 1, 0, f_mzeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200698 {"nextnonblank", 1, 1, 0, f_nextnonblank},
699 {"nr2char", 1, 2, 0, f_nr2char},
700 {"or", 2, 2, 0, f_or},
701 {"pathshorten", 1, 1, 0, f_pathshorten},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702#ifdef FEAT_PERL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200703 {"perleval", 1, 1, 0, f_perleval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200704#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200705#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200706 {"popup_atcursor", 2, 2, 0, f_popup_atcursor},
707 {"popup_beval", 2, 2, 0, f_popup_beval},
708 {"popup_clear", 0, 0, 0, f_popup_clear},
709 {"popup_close", 1, 2, 0, f_popup_close},
710 {"popup_create", 2, 2, 0, f_popup_create},
711 {"popup_dialog", 2, 2, 0, f_popup_dialog},
712 {"popup_filter_menu", 2, 2, 0, f_popup_filter_menu},
713 {"popup_filter_yesno", 2, 2, 0, f_popup_filter_yesno},
714 {"popup_getoptions", 1, 1, 0, f_popup_getoptions},
715 {"popup_getpos", 1, 1, 0, f_popup_getpos},
716 {"popup_getpreview", 0, 0, 0, f_popup_getpreview},
717 {"popup_hide", 1, 1, 0, f_popup_hide},
718 {"popup_locate", 2, 2, 0, f_popup_locate},
719 {"popup_menu", 2, 2, 0, f_popup_menu},
720 {"popup_move", 2, 2, 0, f_popup_move},
721 {"popup_notification", 2, 2, 0, f_popup_notification},
722 {"popup_setoptions", 2, 2, 0, f_popup_setoptions},
723 {"popup_settext", 2, 2, 0, f_popup_settext},
724 {"popup_show", 1, 1, 0, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200725#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200726#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200727 {"pow", 2, 2, 0, f_pow},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200728#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200729 {"prevnonblank", 1, 1, 0, f_prevnonblank},
Bram Moolenaarfd8ca212019-08-10 00:13:30 +0200730 {"printf", 1, 19, FEARG_2, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200731#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200732 {"prompt_setcallback", 2, 2, 0, f_prompt_setcallback},
733 {"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt},
734 {"prompt_setprompt", 2, 2, 0, f_prompt_setprompt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200735#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100736#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200737 {"prop_add", 3, 3, 0, f_prop_add},
738 {"prop_clear", 1, 3, 0, f_prop_clear},
739 {"prop_list", 1, 2, 0, f_prop_list},
740 {"prop_remove", 1, 3, 0, f_prop_remove},
741 {"prop_type_add", 2, 2, 0, f_prop_type_add},
742 {"prop_type_change", 2, 2, 0, f_prop_type_change},
743 {"prop_type_delete", 1, 2, 0, f_prop_type_delete},
744 {"prop_type_get", 1, 2, 0, f_prop_type_get},
745 {"prop_type_list", 0, 1, 0, f_prop_type_list},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100746#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200747 {"pumvisible", 0, 0, 0, f_pumvisible},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200748#ifdef FEAT_PYTHON3
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200749 {"py3eval", 1, 1, 0, f_py3eval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200750#endif
751#ifdef FEAT_PYTHON
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200752 {"pyeval", 1, 1, 0, f_pyeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200753#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100754#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200755 {"pyxeval", 1, 1, 0, f_pyxeval},
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100756#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200757 {"range", 1, 3, 0, f_range},
758 {"readdir", 1, 2, 0, f_readdir},
759 {"readfile", 1, 3, 0, f_readfile},
760 {"reg_executing", 0, 0, 0, f_reg_executing},
761 {"reg_recording", 0, 0, 0, f_reg_recording},
762 {"reltime", 0, 2, 0, f_reltime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200764 {"reltimefloat", 1, 1, 0, f_reltimefloat},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200766 {"reltimestr", 1, 1, 0, f_reltimestr},
767 {"remote_expr", 2, 4, 0, f_remote_expr},
768 {"remote_foreground", 1, 1, 0, f_remote_foreground},
769 {"remote_peek", 1, 2, 0, f_remote_peek},
770 {"remote_read", 1, 2, 0, f_remote_read},
771 {"remote_send", 2, 3, 0, f_remote_send},
772 {"remote_startserver", 1, 1, 0, f_remote_startserver},
773 {"remove", 2, 3, FEARG_1, f_remove},
774 {"rename", 2, 2, 0, f_rename},
775 {"repeat", 2, 2, FEARG_1, f_repeat},
776 {"resolve", 1, 1, 0, f_resolve},
777 {"reverse", 1, 1, FEARG_1, f_reverse},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200778#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200779 {"round", 1, 1, 0, f_round},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100781#ifdef FEAT_RUBY
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200782 {"rubyeval", 1, 1, 0, f_rubyeval},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100783#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200784 {"screenattr", 2, 2, 0, f_screenattr},
785 {"screenchar", 2, 2, 0, f_screenchar},
786 {"screenchars", 2, 2, 0, f_screenchars},
787 {"screencol", 0, 0, 0, f_screencol},
788 {"screenpos", 3, 3, 0, f_screenpos},
789 {"screenrow", 0, 0, 0, f_screenrow},
790 {"screenstring", 2, 2, 0, f_screenstring},
791 {"search", 1, 4, 0, f_search},
792 {"searchdecl", 1, 3, 0, f_searchdecl},
793 {"searchpair", 3, 7, 0, f_searchpair},
794 {"searchpairpos", 3, 7, 0, f_searchpairpos},
795 {"searchpos", 1, 4, 0, f_searchpos},
796 {"server2client", 2, 2, 0, f_server2client},
797 {"serverlist", 0, 0, 0, f_serverlist},
798 {"setbufline", 3, 3, 0, f_setbufline},
799 {"setbufvar", 3, 3, 0, f_setbufvar},
800 {"setcharsearch", 1, 1, 0, f_setcharsearch},
801 {"setcmdpos", 1, 1, 0, f_setcmdpos},
802 {"setenv", 2, 2, 0, f_setenv},
803 {"setfperm", 2, 2, 0, f_setfperm},
804 {"setline", 2, 2, 0, f_setline},
805 {"setloclist", 2, 4, 0, f_setloclist},
806 {"setmatches", 1, 2, 0, f_setmatches},
807 {"setpos", 2, 2, 0, f_setpos},
808 {"setqflist", 1, 3, 0, f_setqflist},
809 {"setreg", 2, 3, 0, f_setreg},
810 {"settabvar", 3, 3, 0, f_settabvar},
811 {"settabwinvar", 4, 4, 0, f_settabwinvar},
812 {"settagstack", 2, 3, 0, f_settagstack},
813 {"setwinvar", 3, 3, 0, f_setwinvar},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200814#ifdef FEAT_CRYPT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200815 {"sha256", 1, 1, 0, f_sha256},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200817 {"shellescape", 1, 2, 0, f_shellescape},
818 {"shiftwidth", 0, 1, 0, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100819#ifdef FEAT_SIGNS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200820 {"sign_define", 1, 2, 0, f_sign_define},
821 {"sign_getdefined", 0, 1, 0, f_sign_getdefined},
822 {"sign_getplaced", 0, 2, 0, f_sign_getplaced},
823 {"sign_jump", 3, 3, 0, f_sign_jump},
824 {"sign_place", 4, 5, 0, f_sign_place},
825 {"sign_placelist", 1, 1, 0, f_sign_placelist},
826 {"sign_undefine", 0, 1, 0, f_sign_undefine},
827 {"sign_unplace", 1, 2, 0, f_sign_unplace},
828 {"sign_unplacelist", 1, 2, 0, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100829#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200830 {"simplify", 1, 1, 0, f_simplify},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200832 {"sin", 1, 1, 0, f_sin},
833 {"sinh", 1, 1, 0, f_sinh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200835 {"sort", 1, 3, FEARG_1, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200836#ifdef FEAT_SOUND
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200837 {"sound_clear", 0, 0, 0, f_sound_clear},
838 {"sound_playevent", 1, 2, 0, f_sound_playevent},
839 {"sound_playfile", 1, 2, 0, f_sound_playfile},
840 {"sound_stop", 1, 1, 0, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200841#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200842 {"soundfold", 1, 1, 0, f_soundfold},
843 {"spellbadword", 0, 1, 0, f_spellbadword},
844 {"spellsuggest", 1, 3, 0, f_spellsuggest},
845 {"split", 1, 3, FEARG_1, f_split},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200847 {"sqrt", 1, 1, 0, f_sqrt},
848 {"str2float", 1, 1, 0, f_str2float},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200850 {"str2list", 1, 2, FEARG_1, f_str2list},
851 {"str2nr", 1, 2, 0, f_str2nr},
852 {"strcharpart", 2, 3, 0, f_strcharpart},
853 {"strchars", 1, 2, 0, f_strchars},
854 {"strdisplaywidth", 1, 2, 0, f_strdisplaywidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855#ifdef HAVE_STRFTIME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200856 {"strftime", 1, 2, 0, f_strftime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200858 {"strgetchar", 2, 2, 0, f_strgetchar},
859 {"stridx", 2, 3, 0, f_stridx},
860 {"string", 1, 1, FEARG_1, f_string},
861 {"strlen", 1, 1, FEARG_1, f_strlen},
862 {"strpart", 2, 3, 0, f_strpart},
863 {"strridx", 2, 3, 0, f_strridx},
864 {"strtrans", 1, 1, FEARG_1, f_strtrans},
865 {"strwidth", 1, 1, FEARG_1, f_strwidth},
866 {"submatch", 1, 2, 0, f_submatch},
867 {"substitute", 4, 4, FEARG_1, f_substitute},
868 {"swapinfo", 1, 1, 0, f_swapinfo},
869 {"swapname", 1, 1, 0, f_swapname},
870 {"synID", 3, 3, 0, f_synID},
871 {"synIDattr", 2, 3, FEARG_1, f_synIDattr},
872 {"synIDtrans", 1, 1, FEARG_1, f_synIDtrans},
873 {"synconcealed", 2, 2, 0, f_synconcealed},
874 {"synstack", 2, 2, 0, f_synstack},
875 {"system", 1, 2, FEARG_1, f_system},
876 {"systemlist", 1, 2, FEARG_1, f_systemlist},
877 {"tabpagebuflist", 0, 1, 0, f_tabpagebuflist},
878 {"tabpagenr", 0, 1, 0, f_tabpagenr},
879 {"tabpagewinnr", 1, 2, 0, f_tabpagewinnr},
880 {"tagfiles", 0, 0, 0, f_tagfiles},
881 {"taglist", 1, 2, 0, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200883 {"tan", 1, 1, 0, f_tan},
884 {"tanh", 1, 1, 0, f_tanh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200886 {"tempname", 0, 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200887#ifdef FEAT_TERMINAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200888 {"term_dumpdiff", 2, 3, 0, f_term_dumpdiff},
889 {"term_dumpload", 1, 2, 0, f_term_dumpload},
890 {"term_dumpwrite", 2, 3, 0, f_term_dumpwrite},
891 {"term_getaltscreen", 1, 1, 0, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200892# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200893 {"term_getansicolors", 1, 1, 0, f_term_getansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200894# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200895 {"term_getattr", 2, 2, 0, f_term_getattr},
896 {"term_getcursor", 1, 1, 0, f_term_getcursor},
897 {"term_getjob", 1, 1, 0, f_term_getjob},
898 {"term_getline", 2, 2, 0, f_term_getline},
899 {"term_getscrolled", 1, 1, 0, f_term_getscrolled},
900 {"term_getsize", 1, 1, 0, f_term_getsize},
901 {"term_getstatus", 1, 1, 0, f_term_getstatus},
902 {"term_gettitle", 1, 1, 0, f_term_gettitle},
903 {"term_gettty", 1, 2, 0, f_term_gettty},
904 {"term_list", 0, 0, 0, f_term_list},
905 {"term_scrape", 2, 2, 0, f_term_scrape},
906 {"term_sendkeys", 2, 2, 0, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200907# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200908 {"term_setansicolors", 2, 2, 0, f_term_setansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200909# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200910 {"term_setkill", 2, 2, 0, f_term_setkill},
911 {"term_setrestore", 2, 2, 0, f_term_setrestore},
912 {"term_setsize", 3, 3, 0, f_term_setsize},
913 {"term_start", 1, 2, 0, f_term_start},
914 {"term_wait", 1, 2, 0, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200916 {"test_alloc_fail", 3, 3, 0, f_test_alloc_fail},
917 {"test_autochdir", 0, 0, 0, f_test_autochdir},
918 {"test_feedinput", 1, 1, 0, f_test_feedinput},
919 {"test_garbagecollect_now", 0, 0, 0, f_test_garbagecollect_now},
920 {"test_garbagecollect_soon", 0, 0, 0, f_test_garbagecollect_soon},
921 {"test_getvalue", 1, 1, 0, f_test_getvalue},
922 {"test_ignore_error", 1, 1, 0, f_test_ignore_error},
923 {"test_null_blob", 0, 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200925 {"test_null_channel", 0, 0, 0, f_test_null_channel},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200927 {"test_null_dict", 0, 0, 0, f_test_null_dict},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200929 {"test_null_job", 0, 0, 0, f_test_null_job},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200930#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200931 {"test_null_list", 0, 0, 0, f_test_null_list},
932 {"test_null_partial", 0, 0, 0, f_test_null_partial},
933 {"test_null_string", 0, 0, 0, f_test_null_string},
934 {"test_option_not_set", 1, 1, 0, f_test_option_not_set},
935 {"test_override", 2, 2, 0, f_test_override},
936 {"test_refcount", 1, 1, 0, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200937#ifdef FEAT_GUI
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200938 {"test_scrollbar", 3, 3, 0, f_test_scrollbar},
Bram Moolenaarab186732018-09-14 21:27:06 +0200939#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200940#ifdef FEAT_MOUSE
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200941 {"test_setmouse", 2, 2, 0, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200942#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200943 {"test_settime", 1, 1, 0, f_test_settime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944#ifdef FEAT_TIMERS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200945 {"timer_info", 0, 1, 0, f_timer_info},
946 {"timer_pause", 2, 2, 0, f_timer_pause},
947 {"timer_start", 2, 3, 0, f_timer_start},
948 {"timer_stop", 1, 1, 0, f_timer_stop},
949 {"timer_stopall", 0, 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200951 {"tolower", 1, 1, 0, f_tolower},
952 {"toupper", 1, 1, 0, f_toupper},
953 {"tr", 3, 3, 0, f_tr},
954 {"trim", 1, 2, 0, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200955#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200956 {"trunc", 1, 1, 0, f_trunc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200958 {"type", 1, 1, FEARG_1, f_type},
959 {"undofile", 1, 1, 0, f_undofile},
960 {"undotree", 0, 0, 0, f_undotree},
961 {"uniq", 1, 3, FEARG_1, f_uniq},
962 {"values", 1, 1, FEARG_1, f_values},
963 {"virtcol", 1, 1, 0, f_virtcol},
964 {"visualmode", 0, 1, 0, f_visualmode},
965 {"wildmenumode", 0, 0, 0, f_wildmenumode},
966 {"win_execute", 2, 3, 0, f_win_execute},
967 {"win_findbuf", 1, 1, 0, f_win_findbuf},
968 {"win_getid", 0, 2, 0, f_win_getid},
969 {"win_gotoid", 1, 1, 0, f_win_gotoid},
970 {"win_id2tabwin", 1, 1, 0, f_win_id2tabwin},
971 {"win_id2win", 1, 1, 0, f_win_id2win},
972 {"win_screenpos", 1, 1, 0, f_win_screenpos},
973 {"winbufnr", 1, 1, 0, f_winbufnr},
974 {"wincol", 0, 0, 0, f_wincol},
975 {"winheight", 1, 1, 0, f_winheight},
976 {"winlayout", 0, 1, 0, f_winlayout},
977 {"winline", 0, 0, 0, f_winline},
978 {"winnr", 0, 1, 0, f_winnr},
979 {"winrestcmd", 0, 0, 0, f_winrestcmd},
980 {"winrestview", 1, 1, 0, f_winrestview},
981 {"winsaveview", 0, 0, 0, f_winsaveview},
982 {"winwidth", 1, 1, 0, f_winwidth},
983 {"wordcount", 0, 0, 0, f_wordcount},
984 {"writefile", 2, 3, 0, f_writefile},
985 {"xor", 2, 2, 0, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +0200986};
987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200988#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
989
990/*
991 * Function given to ExpandGeneric() to obtain the list of internal
992 * or user defined function names.
993 */
994 char_u *
995get_function_name(expand_T *xp, int idx)
996{
997 static int intidx = -1;
998 char_u *name;
999
1000 if (idx == 0)
1001 intidx = -1;
1002 if (intidx < 0)
1003 {
1004 name = get_user_func_name(xp, idx);
1005 if (name != NULL)
1006 return name;
1007 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001008 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001010 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001012 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001013 STRCAT(IObuff, ")");
1014 return IObuff;
1015 }
1016
1017 return NULL;
1018}
1019
1020/*
1021 * Function given to ExpandGeneric() to obtain the list of internal or
1022 * user defined variable or function names.
1023 */
1024 char_u *
1025get_expr_name(expand_T *xp, int idx)
1026{
1027 static int intidx = -1;
1028 char_u *name;
1029
1030 if (idx == 0)
1031 intidx = -1;
1032 if (intidx < 0)
1033 {
1034 name = get_function_name(xp, idx);
1035 if (name != NULL)
1036 return name;
1037 }
1038 return get_user_var_name(xp, ++intidx);
1039}
1040
1041#endif /* FEAT_CMDL_COMPL */
1042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001043/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001044 * Find internal function "name" in table "global_functions".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001045 * Return index, or -1 if not found
1046 */
Bram Moolenaarac92e252019-08-03 21:58:38 +02001047 static int
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001048find_internal_func(char_u *name)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001049{
1050 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001051 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001052 int cmp;
1053 int x;
1054
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001055 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001056
1057 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058 while (first <= last)
1059 {
1060 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001061 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001062 if (cmp < 0)
1063 last = x - 1;
1064 else if (cmp > 0)
1065 first = x + 1;
1066 else
1067 return x;
1068 }
1069 return -1;
1070}
1071
1072 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001073has_internal_func(char_u *name)
1074{
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001075 return find_internal_func(name) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001076}
1077
1078 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001079call_internal_func(
1080 char_u *name,
1081 int argcount,
1082 typval_T *argvars,
1083 typval_T *rettv)
1084{
1085 int i;
1086
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001087 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001088 if (i < 0)
1089 return ERROR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001090 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001091 return ERROR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001092 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001093 return ERROR_TOOMANY;
1094 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001095 global_functions[i].f_func(argvars, rettv);
1096 return ERROR_NONE;
1097}
1098
1099/*
1100 * Invoke a method for base->method().
1101 */
1102 int
1103call_internal_method(
1104 char_u *name,
1105 int argcount,
1106 typval_T *argvars,
1107 typval_T *rettv,
1108 typval_T *basetv)
1109{
1110 int i;
1111 int fi;
1112 typval_T argv[MAX_FUNC_ARGS + 1];
1113
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001114 fi = find_internal_func(name);
1115 if (fi < 0 || global_functions[fi].f_argtype == 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001116 return ERROR_UNKNOWN;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001117 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001118 return ERROR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001119 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001120 return ERROR_TOOMANY;
1121
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001122 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001123 {
1124 // base value goes last
1125 for (i = 0; i < argcount; ++i)
1126 argv[i] = argvars[i];
1127 argv[argcount] = *basetv;
1128 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001129 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001130 {
1131 // base value goes second
1132 argv[0] = argvars[0];
1133 argv[1] = *basetv;
1134 for (i = 1; i < argcount; ++i)
1135 argv[i + 1] = argvars[i];
1136 }
1137 else
1138 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001139 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001140 argv[0] = *basetv;
1141 for (i = 0; i < argcount; ++i)
1142 argv[i + 1] = argvars[i];
1143 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001144 argv[argcount + 1].v_type = VAR_UNKNOWN;
1145
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001146 global_functions[fi].f_func(argv, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001147 return ERROR_NONE;
1148}
1149
1150/*
1151 * Return TRUE for a non-zero Number and a non-empty String.
1152 */
1153 static int
1154non_zero_arg(typval_T *argvars)
1155{
1156 return ((argvars[0].v_type == VAR_NUMBER
1157 && argvars[0].vval.v_number != 0)
1158 || (argvars[0].v_type == VAR_SPECIAL
1159 && argvars[0].vval.v_number == VVAL_TRUE)
1160 || (argvars[0].v_type == VAR_STRING
1161 && argvars[0].vval.v_string != NULL
1162 && *argvars[0].vval.v_string != NUL));
1163}
1164
1165/*
1166 * Get the lnum from the first argument.
1167 * Also accepts ".", "$", etc., but that only works for the current buffer.
1168 * Returns -1 on error.
1169 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001170 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001171tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001172{
1173 typval_T rettv;
1174 linenr_T lnum;
1175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001176 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177 if (lnum == 0) /* no valid number, try using line() */
1178 {
1179 rettv.v_type = VAR_NUMBER;
1180 f_line(argvars, &rettv);
1181 lnum = (linenr_T)rettv.vval.v_number;
1182 clear_tv(&rettv);
1183 }
1184 return lnum;
1185}
1186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001187/*
1188 * Get the lnum from the first argument.
1189 * Also accepts "$", then "buf" is used.
1190 * Returns 0 on error.
1191 */
1192 static linenr_T
1193tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1194{
1195 if (argvars[0].v_type == VAR_STRING
1196 && argvars[0].vval.v_string != NULL
1197 && argvars[0].vval.v_string[0] == '$'
1198 && buf != NULL)
1199 return buf->b_ml.ml_line_count;
1200 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1201}
1202
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001203#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001204/*
1205 * Get the float value of "argvars[0]" into "f".
1206 * Returns FAIL when the argument is not a Number or Float.
1207 */
1208 static int
1209get_float_arg(typval_T *argvars, float_T *f)
1210{
1211 if (argvars[0].v_type == VAR_FLOAT)
1212 {
1213 *f = argvars[0].vval.v_float;
1214 return OK;
1215 }
1216 if (argvars[0].v_type == VAR_NUMBER)
1217 {
1218 *f = (float_T)argvars[0].vval.v_number;
1219 return OK;
1220 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001222 return FAIL;
1223}
1224
1225/*
1226 * "abs(expr)" function
1227 */
1228 static void
1229f_abs(typval_T *argvars, typval_T *rettv)
1230{
1231 if (argvars[0].v_type == VAR_FLOAT)
1232 {
1233 rettv->v_type = VAR_FLOAT;
1234 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1235 }
1236 else
1237 {
1238 varnumber_T n;
1239 int error = FALSE;
1240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001241 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242 if (error)
1243 rettv->vval.v_number = -1;
1244 else if (n > 0)
1245 rettv->vval.v_number = n;
1246 else
1247 rettv->vval.v_number = -n;
1248 }
1249}
1250
1251/*
1252 * "acos()" function
1253 */
1254 static void
1255f_acos(typval_T *argvars, typval_T *rettv)
1256{
1257 float_T f = 0.0;
1258
1259 rettv->v_type = VAR_FLOAT;
1260 if (get_float_arg(argvars, &f) == OK)
1261 rettv->vval.v_float = acos(f);
1262 else
1263 rettv->vval.v_float = 0.0;
1264}
1265#endif
1266
1267/*
1268 * "add(list, item)" function
1269 */
1270 static void
1271f_add(typval_T *argvars, typval_T *rettv)
1272{
1273 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001274 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001275
1276 rettv->vval.v_number = 1; /* Default: Failed */
1277 if (argvars[0].v_type == VAR_LIST)
1278 {
1279 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001280 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001281 (char_u *)N_("add() argument"), TRUE)
1282 && list_append_tv(l, &argvars[1]) == OK)
1283 copy_tv(&argvars[0], rettv);
1284 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 else if (argvars[0].v_type == VAR_BLOB)
1286 {
1287 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001288 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 (char_u *)N_("add() argument"), TRUE))
1290 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001291 int error = FALSE;
1292 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1293
1294 if (!error)
1295 {
1296 ga_append(&b->bv_ga, (int)n);
1297 copy_tv(&argvars[0], rettv);
1298 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 }
1300 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001302 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303}
1304
1305/*
1306 * "and(expr, expr)" function
1307 */
1308 static void
1309f_and(typval_T *argvars, typval_T *rettv)
1310{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1312 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001313}
1314
1315/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001316 * If there is a window for "curbuf", make it the current window.
1317 */
1318 static void
1319find_win_for_curbuf(void)
1320{
1321 wininfo_T *wip;
1322
1323 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1324 {
1325 if (wip->wi_win != NULL)
1326 {
1327 curwin = wip->wi_win;
1328 break;
1329 }
1330 }
1331}
1332
1333/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001334 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335 */
1336 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001337set_buffer_lines(
1338 buf_T *buf,
1339 linenr_T lnum_arg,
1340 int append,
1341 typval_T *lines,
1342 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343{
Bram Moolenaarca851592018-06-06 21:04:07 +02001344 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1345 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001346 list_T *l = NULL;
1347 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001349 linenr_T append_lnum;
1350 buf_T *curbuf_save = NULL;
1351 win_T *curwin_save = NULL;
1352 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 /* When using the current buffer ml_mfp will be set if needed. Useful when
1355 * setline() is used on startup. For other buffers the buffer must be
1356 * loaded. */
1357 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 rettv->vval.v_number = 1; /* FAIL */
1360 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361 }
1362
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001365 curbuf_save = curbuf;
1366 curwin_save = curwin;
1367 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001368 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 }
1370
1371 if (append)
1372 // appendbufline() uses the line number below which we insert
1373 append_lnum = lnum - 1;
1374 else
1375 // setbufline() uses the line number above which we insert, we only
1376 // append if it's below the last line
1377 append_lnum = curbuf->b_ml.ml_line_count;
1378
1379 if (lines->v_type == VAR_LIST)
1380 {
1381 l = lines->vval.v_list;
1382 li = l->lv_first;
1383 }
1384 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001385 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001386
1387 /* default result is zero == OK */
1388 for (;;)
1389 {
1390 if (l != NULL)
1391 {
1392 /* list argument, get next string */
1393 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001394 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001395 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001396 li = li->li_next;
1397 }
1398
Bram Moolenaarca851592018-06-06 21:04:07 +02001399 rettv->vval.v_number = 1; /* FAIL */
1400 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1401 break;
1402
1403 /* When coming here from Insert mode, sync undo, so that this can be
1404 * undone separately from what was previously inserted. */
1405 if (u_sync_once == 2)
1406 {
1407 u_sync_once = 1; /* notify that u_sync() was called */
1408 u_sync(TRUE);
1409 }
1410
1411 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1412 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001413 // Existing line, replace it.
1414 // Removes any existing text properties.
1415 if (u_savesub(lnum) == OK && ml_replace_len(
1416 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001417 {
1418 changed_bytes(lnum, 0);
1419 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1420 check_cursor_col();
1421 rettv->vval.v_number = 0; /* OK */
1422 }
1423 }
1424 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1425 {
1426 /* append the line */
1427 ++added;
1428 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1429 rettv->vval.v_number = 0; /* OK */
1430 }
1431
1432 if (l == NULL) /* only one string argument */
1433 break;
1434 ++lnum;
1435 }
1436
1437 if (added > 0)
1438 {
1439 win_T *wp;
1440 tabpage_T *tp;
1441
1442 appended_lines_mark(append_lnum, added);
1443 FOR_ALL_TAB_WINDOWS(tp, wp)
1444 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1445 wp->w_cursor.lnum += added;
1446 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001447 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001448 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001449
1450 if (!is_curbuf)
1451 {
1452 curbuf = curbuf_save;
1453 curwin = curwin_save;
1454 }
1455}
1456
1457/*
1458 * "append(lnum, string/list)" function
1459 */
1460 static void
1461f_append(typval_T *argvars, typval_T *rettv)
1462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001463 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001464
1465 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1466}
1467
1468/*
1469 * "appendbufline(buf, lnum, string/list)" function
1470 */
1471 static void
1472f_appendbufline(typval_T *argvars, typval_T *rettv)
1473{
1474 linenr_T lnum;
1475 buf_T *buf;
1476
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001477 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001478 if (buf == NULL)
1479 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001481 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001482 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001483 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1484 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001485}
1486
1487/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001488 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489 */
1490 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001491f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001493 win_T *wp;
1494
1495 if (argvars[0].v_type == VAR_UNKNOWN)
1496 // use the current window
1497 rettv->vval.v_number = ARGCOUNT;
1498 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001499 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001500 // use the global argument list
1501 rettv->vval.v_number = GARGCOUNT;
1502 else
1503 {
1504 // use the argument list of the specified window
1505 wp = find_win_by_nr_or_id(&argvars[0]);
1506 if (wp != NULL)
1507 rettv->vval.v_number = WARGCOUNT(wp);
1508 else
1509 rettv->vval.v_number = -1;
1510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511}
1512
1513/*
1514 * "argidx()" function
1515 */
1516 static void
1517f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1518{
1519 rettv->vval.v_number = curwin->w_arg_idx;
1520}
1521
1522/*
1523 * "arglistid()" function
1524 */
1525 static void
1526f_arglistid(typval_T *argvars, typval_T *rettv)
1527{
1528 win_T *wp;
1529
1530 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001531 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001532 if (wp != NULL)
1533 rettv->vval.v_number = wp->w_alist->id;
1534}
1535
1536/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 * Get the argument list for a given window
1538 */
1539 static void
1540get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1541{
1542 int idx;
1543
1544 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1545 for (idx = 0; idx < argcount; ++idx)
1546 list_append_string(rettv->vval.v_list,
1547 alist_name(&arglist[idx]), -1);
1548}
1549
1550/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001551 * "argv(nr)" function
1552 */
1553 static void
1554f_argv(typval_T *argvars, typval_T *rettv)
1555{
1556 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001557 aentry_T *arglist = NULL;
1558 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001559
1560 if (argvars[0].v_type != VAR_UNKNOWN)
1561 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001562 if (argvars[1].v_type == VAR_UNKNOWN)
1563 {
1564 arglist = ARGLIST;
1565 argcount = ARGCOUNT;
1566 }
1567 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001568 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 {
1570 arglist = GARGLIST;
1571 argcount = GARGCOUNT;
1572 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001574 {
1575 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1576
1577 if (wp != NULL)
1578 {
1579 /* Use the argument list of the specified window */
1580 arglist = WARGLIST(wp);
1581 argcount = WARGCOUNT(wp);
1582 }
1583 }
1584
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001586 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001587 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001588 if (arglist != NULL && idx >= 0 && idx < argcount)
1589 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1590 else if (idx == -1)
1591 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001593 else
1594 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595}
1596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597#ifdef FEAT_FLOAT
1598/*
1599 * "asin()" function
1600 */
1601 static void
1602f_asin(typval_T *argvars, typval_T *rettv)
1603{
1604 float_T f = 0.0;
1605
1606 rettv->v_type = VAR_FLOAT;
1607 if (get_float_arg(argvars, &f) == OK)
1608 rettv->vval.v_float = asin(f);
1609 else
1610 rettv->vval.v_float = 0.0;
1611}
1612
1613/*
1614 * "atan()" function
1615 */
1616 static void
1617f_atan(typval_T *argvars, typval_T *rettv)
1618{
1619 float_T f = 0.0;
1620
1621 rettv->v_type = VAR_FLOAT;
1622 if (get_float_arg(argvars, &f) == OK)
1623 rettv->vval.v_float = atan(f);
1624 else
1625 rettv->vval.v_float = 0.0;
1626}
1627
1628/*
1629 * "atan2()" function
1630 */
1631 static void
1632f_atan2(typval_T *argvars, typval_T *rettv)
1633{
1634 float_T fx = 0.0, fy = 0.0;
1635
1636 rettv->v_type = VAR_FLOAT;
1637 if (get_float_arg(argvars, &fx) == OK
1638 && get_float_arg(&argvars[1], &fy) == OK)
1639 rettv->vval.v_float = atan2(fx, fy);
1640 else
1641 rettv->vval.v_float = 0.0;
1642}
1643#endif
1644
1645/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001646 * "balloon_show()" function
1647 */
1648#ifdef FEAT_BEVAL
1649 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001650f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1651{
1652 rettv->v_type = VAR_STRING;
1653 if (balloonEval != NULL)
1654 {
1655 if (balloonEval->msg == NULL)
1656 rettv->vval.v_string = NULL;
1657 else
1658 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1659 }
1660}
1661
1662 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001663f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1664{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001665 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001666 {
1667 if (argvars[0].v_type == VAR_LIST
1668# ifdef FEAT_GUI
1669 && !gui.in_use
1670# endif
1671 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001672 {
1673 list_T *l = argvars[0].vval.v_list;
1674
1675 // empty list removes the balloon
1676 post_balloon(balloonEval, NULL,
1677 l == NULL || l->lv_len == 0 ? NULL : l);
1678 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001679 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001680 {
1681 char_u *mesg = tv_get_string_chk(&argvars[0]);
1682
1683 if (mesg != NULL)
1684 // empty string removes the balloon
1685 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1686 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001687 }
1688}
1689
Bram Moolenaar669a8282017-11-19 20:13:05 +01001690# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001691 static void
1692f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1693{
1694 if (rettv_list_alloc(rettv) == OK)
1695 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001696 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001697
1698 if (msg != NULL)
1699 {
1700 pumitem_T *array;
1701 int size = split_message(msg, &array);
1702 int i;
1703
1704 /* Skip the first and last item, they are always empty. */
1705 for (i = 1; i < size - 1; ++i)
1706 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001707 while (size > 0)
1708 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001709 vim_free(array);
1710 }
1711 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001712}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001713# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001714#endif
1715
1716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 * "browse(save, title, initdir, default)" function
1718 */
1719 static void
1720f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1721{
1722#ifdef FEAT_BROWSE
1723 int save;
1724 char_u *title;
1725 char_u *initdir;
1726 char_u *defname;
1727 char_u buf[NUMBUFLEN];
1728 char_u buf2[NUMBUFLEN];
1729 int error = FALSE;
1730
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001731 save = (int)tv_get_number_chk(&argvars[0], &error);
1732 title = tv_get_string_chk(&argvars[1]);
1733 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1734 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735
1736 if (error || title == NULL || initdir == NULL || defname == NULL)
1737 rettv->vval.v_string = NULL;
1738 else
1739 rettv->vval.v_string =
1740 do_browse(save ? BROWSE_SAVE : 0,
1741 title, defname, NULL, initdir, NULL, curbuf);
1742#else
1743 rettv->vval.v_string = NULL;
1744#endif
1745 rettv->v_type = VAR_STRING;
1746}
1747
1748/*
1749 * "browsedir(title, initdir)" function
1750 */
1751 static void
1752f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1753{
1754#ifdef FEAT_BROWSE
1755 char_u *title;
1756 char_u *initdir;
1757 char_u buf[NUMBUFLEN];
1758
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 title = tv_get_string_chk(&argvars[0]);
1760 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001761
1762 if (title == NULL || initdir == NULL)
1763 rettv->vval.v_string = NULL;
1764 else
1765 rettv->vval.v_string = do_browse(BROWSE_DIR,
1766 title, NULL, NULL, initdir, NULL, curbuf);
1767#else
1768 rettv->vval.v_string = NULL;
1769#endif
1770 rettv->v_type = VAR_STRING;
1771}
1772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001773/*
1774 * Find a buffer by number or exact name.
1775 */
1776 static buf_T *
1777find_buffer(typval_T *avar)
1778{
1779 buf_T *buf = NULL;
1780
1781 if (avar->v_type == VAR_NUMBER)
1782 buf = buflist_findnr((int)avar->vval.v_number);
1783 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1784 {
1785 buf = buflist_findname_exp(avar->vval.v_string);
1786 if (buf == NULL)
1787 {
1788 /* No full path name match, try a match with a URL or a "nofile"
1789 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001790 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001791 if (buf->b_fname != NULL
1792 && (path_with_url(buf->b_fname)
1793#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001794 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001795#endif
1796 )
1797 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1798 break;
1799 }
1800 }
1801 return buf;
1802}
1803
1804/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001805 * "bufadd(expr)" function
1806 */
1807 static void
1808f_bufadd(typval_T *argvars, typval_T *rettv)
1809{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001810 char_u *name = tv_get_string(&argvars[0]);
1811
1812 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001813}
1814
1815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001816 * "bufexists(expr)" function
1817 */
1818 static void
1819f_bufexists(typval_T *argvars, typval_T *rettv)
1820{
1821 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1822}
1823
1824/*
1825 * "buflisted(expr)" function
1826 */
1827 static void
1828f_buflisted(typval_T *argvars, typval_T *rettv)
1829{
1830 buf_T *buf;
1831
1832 buf = find_buffer(&argvars[0]);
1833 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1834}
1835
1836/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001837 * "bufload(expr)" function
1838 */
1839 static void
1840f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1841{
1842 buf_T *buf = get_buf_arg(&argvars[0]);
1843
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001844 if (buf != NULL)
1845 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001846}
1847
1848/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 * "bufloaded(expr)" function
1850 */
1851 static void
1852f_bufloaded(typval_T *argvars, typval_T *rettv)
1853{
1854 buf_T *buf;
1855
1856 buf = find_buffer(&argvars[0]);
1857 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1858}
1859
1860 buf_T *
1861buflist_find_by_name(char_u *name, int curtab_only)
1862{
1863 int save_magic;
1864 char_u *save_cpo;
1865 buf_T *buf;
1866
1867 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1868 save_magic = p_magic;
1869 p_magic = TRUE;
1870 save_cpo = p_cpo;
1871 p_cpo = (char_u *)"";
1872
1873 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1874 TRUE, FALSE, curtab_only));
1875
1876 p_magic = save_magic;
1877 p_cpo = save_cpo;
1878 return buf;
1879}
1880
1881/*
1882 * Get buffer by number or pattern.
1883 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001884 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001885tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886{
1887 char_u *name = tv->vval.v_string;
1888 buf_T *buf;
1889
1890 if (tv->v_type == VAR_NUMBER)
1891 return buflist_findnr((int)tv->vval.v_number);
1892 if (tv->v_type != VAR_STRING)
1893 return NULL;
1894 if (name == NULL || *name == NUL)
1895 return curbuf;
1896 if (name[0] == '$' && name[1] == NUL)
1897 return lastbuf;
1898
1899 buf = buflist_find_by_name(name, curtab_only);
1900
1901 /* If not found, try expanding the name, like done for bufexists(). */
1902 if (buf == NULL)
1903 buf = find_buffer(tv);
1904
1905 return buf;
1906}
1907
1908/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001909 * Get the buffer from "arg" and give an error and return NULL if it is not
1910 * valid.
1911 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001912 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001913get_buf_arg(typval_T *arg)
1914{
1915 buf_T *buf;
1916
1917 ++emsg_off;
1918 buf = tv_get_buf(arg, FALSE);
1919 --emsg_off;
1920 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001922 return buf;
1923}
1924
1925/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 * "bufname(expr)" function
1927 */
1928 static void
1929f_bufname(typval_T *argvars, typval_T *rettv)
1930{
1931 buf_T *buf;
1932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001933 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001935 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936 rettv->v_type = VAR_STRING;
1937 if (buf != NULL && buf->b_fname != NULL)
1938 rettv->vval.v_string = vim_strsave(buf->b_fname);
1939 else
1940 rettv->vval.v_string = NULL;
1941 --emsg_off;
1942}
1943
1944/*
1945 * "bufnr(expr)" function
1946 */
1947 static void
1948f_bufnr(typval_T *argvars, typval_T *rettv)
1949{
1950 buf_T *buf;
1951 int error = FALSE;
1952 char_u *name;
1953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001954 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001956 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001957 --emsg_off;
1958
1959 /* If the buffer isn't found and the second argument is not zero create a
1960 * new buffer. */
1961 if (buf == NULL
1962 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001963 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001965 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001966 && !error)
1967 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1968
1969 if (buf != NULL)
1970 rettv->vval.v_number = buf->b_fnum;
1971 else
1972 rettv->vval.v_number = -1;
1973}
1974
1975 static void
1976buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1977{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978 win_T *wp;
1979 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001980 buf_T *buf;
1981
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001982 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001983 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001984 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001985 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 {
1987 ++winnr;
1988 if (wp->w_buffer == buf)
1989 break;
1990 }
1991 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001992 --emsg_off;
1993}
1994
1995/*
1996 * "bufwinid(nr)" function
1997 */
1998 static void
1999f_bufwinid(typval_T *argvars, typval_T *rettv)
2000{
2001 buf_win_common(argvars, rettv, FALSE);
2002}
2003
2004/*
2005 * "bufwinnr(nr)" function
2006 */
2007 static void
2008f_bufwinnr(typval_T *argvars, typval_T *rettv)
2009{
2010 buf_win_common(argvars, rettv, TRUE);
2011}
2012
2013/*
2014 * "byte2line(byte)" function
2015 */
2016 static void
2017f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2018{
2019#ifndef FEAT_BYTEOFF
2020 rettv->vval.v_number = -1;
2021#else
2022 long boff = 0;
2023
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002024 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002025 if (boff < 0)
2026 rettv->vval.v_number = -1;
2027 else
2028 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2029 (linenr_T)0, &boff);
2030#endif
2031}
2032
2033 static void
2034byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2035{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 char_u *str;
2038 varnumber_T idx;
2039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002040 str = tv_get_string_chk(&argvars[0]);
2041 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 rettv->vval.v_number = -1;
2043 if (str == NULL || idx < 0)
2044 return;
2045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 t = str;
2047 for ( ; idx > 0; idx--)
2048 {
2049 if (*t == NUL) /* EOL reached */
2050 return;
2051 if (enc_utf8 && comp)
2052 t += utf_ptr2len(t);
2053 else
2054 t += (*mb_ptr2len)(t);
2055 }
2056 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057}
2058
2059/*
2060 * "byteidx()" function
2061 */
2062 static void
2063f_byteidx(typval_T *argvars, typval_T *rettv)
2064{
2065 byteidx(argvars, rettv, FALSE);
2066}
2067
2068/*
2069 * "byteidxcomp()" function
2070 */
2071 static void
2072f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2073{
2074 byteidx(argvars, rettv, TRUE);
2075}
2076
2077/*
2078 * "call(func, arglist [, dict])" function
2079 */
2080 static void
2081f_call(typval_T *argvars, typval_T *rettv)
2082{
2083 char_u *func;
2084 partial_T *partial = NULL;
2085 dict_T *selfdict = NULL;
2086
2087 if (argvars[1].v_type != VAR_LIST)
2088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002089 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002090 return;
2091 }
2092 if (argvars[1].vval.v_list == NULL)
2093 return;
2094
2095 if (argvars[0].v_type == VAR_FUNC)
2096 func = argvars[0].vval.v_string;
2097 else if (argvars[0].v_type == VAR_PARTIAL)
2098 {
2099 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002100 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 }
2102 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 if (*func == NUL)
2105 return; /* type error or empty name */
2106
2107 if (argvars[2].v_type != VAR_UNKNOWN)
2108 {
2109 if (argvars[2].v_type != VAR_DICT)
2110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 return;
2113 }
2114 selfdict = argvars[2].vval.v_dict;
2115 }
2116
2117 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2118}
2119
2120#ifdef FEAT_FLOAT
2121/*
2122 * "ceil({float})" function
2123 */
2124 static void
2125f_ceil(typval_T *argvars, typval_T *rettv)
2126{
2127 float_T f = 0.0;
2128
2129 rettv->v_type = VAR_FLOAT;
2130 if (get_float_arg(argvars, &f) == OK)
2131 rettv->vval.v_float = ceil(f);
2132 else
2133 rettv->vval.v_float = 0.0;
2134}
2135#endif
2136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002137/*
2138 * "changenr()" function
2139 */
2140 static void
2141f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2142{
2143 rettv->vval.v_number = curbuf->b_u_seq_cur;
2144}
2145
2146/*
2147 * "char2nr(string)" function
2148 */
2149 static void
2150f_char2nr(typval_T *argvars, typval_T *rettv)
2151{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152 if (has_mbyte)
2153 {
2154 int utf8 = 0;
2155
2156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002157 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002158
2159 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002160 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002162 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 }
2164 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002165 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166}
2167
2168/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002169 * "chdir(dir)" function
2170 */
2171 static void
2172f_chdir(typval_T *argvars, typval_T *rettv)
2173{
2174 char_u *cwd;
2175 cdscope_T scope = CDSCOPE_GLOBAL;
2176
2177 rettv->v_type = VAR_STRING;
2178 rettv->vval.v_string = NULL;
2179
2180 if (argvars[0].v_type != VAR_STRING)
2181 return;
2182
2183 // Return the current directory
2184 cwd = alloc(MAXPATHL);
2185 if (cwd != NULL)
2186 {
2187 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2188 {
2189#ifdef BACKSLASH_IN_FILENAME
2190 slash_adjust(cwd);
2191#endif
2192 rettv->vval.v_string = vim_strsave(cwd);
2193 }
2194 vim_free(cwd);
2195 }
2196
2197 if (curwin->w_localdir != NULL)
2198 scope = CDSCOPE_WINDOW;
2199 else if (curtab->tp_localdir != NULL)
2200 scope = CDSCOPE_TABPAGE;
2201
2202 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2203 // Directory change failed
2204 VIM_CLEAR(rettv->vval.v_string);
2205}
2206
2207/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 * "cindent(lnum)" function
2209 */
2210 static void
2211f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2212{
2213#ifdef FEAT_CINDENT
2214 pos_T pos;
2215 linenr_T lnum;
2216
2217 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2220 {
2221 curwin->w_cursor.lnum = lnum;
2222 rettv->vval.v_number = get_c_indent();
2223 curwin->w_cursor = pos;
2224 }
2225 else
2226#endif
2227 rettv->vval.v_number = -1;
2228}
2229
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002230 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002231get_optional_window(typval_T *argvars, int idx)
2232{
2233 win_T *win = curwin;
2234
2235 if (argvars[idx].v_type != VAR_UNKNOWN)
2236 {
2237 win = find_win_by_nr_or_id(&argvars[idx]);
2238 if (win == NULL)
2239 {
2240 emsg(_(e_invalwindow));
2241 return NULL;
2242 }
2243 }
2244 return win;
2245}
2246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002248 * "col(string)" function
2249 */
2250 static void
2251f_col(typval_T *argvars, typval_T *rettv)
2252{
2253 colnr_T col = 0;
2254 pos_T *fp;
2255 int fnum = curbuf->b_fnum;
2256
2257 fp = var2fpos(&argvars[0], FALSE, &fnum);
2258 if (fp != NULL && fnum == curbuf->b_fnum)
2259 {
2260 if (fp->col == MAXCOL)
2261 {
2262 /* '> can be MAXCOL, get the length of the line then */
2263 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2264 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2265 else
2266 col = MAXCOL;
2267 }
2268 else
2269 {
2270 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 /* col(".") when the cursor is on the NUL at the end of the line
2272 * because of "coladd" can be seen as an extra column. */
2273 if (virtual_active() && fp == &curwin->w_cursor)
2274 {
2275 char_u *p = ml_get_cursor();
2276
2277 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2278 curwin->w_virtcol - curwin->w_cursor.coladd))
2279 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 int l;
2281
2282 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2283 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284 }
2285 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 }
2287 }
2288 rettv->vval.v_number = col;
2289}
2290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002291/*
2292 * "confirm(message, buttons[, default [, type]])" function
2293 */
2294 static void
2295f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2296{
2297#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2298 char_u *message;
2299 char_u *buttons = NULL;
2300 char_u buf[NUMBUFLEN];
2301 char_u buf2[NUMBUFLEN];
2302 int def = 1;
2303 int type = VIM_GENERIC;
2304 char_u *typestr;
2305 int error = FALSE;
2306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002307 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 if (message == NULL)
2309 error = TRUE;
2310 if (argvars[1].v_type != VAR_UNKNOWN)
2311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002312 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 if (buttons == NULL)
2314 error = TRUE;
2315 if (argvars[2].v_type != VAR_UNKNOWN)
2316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002317 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002318 if (argvars[3].v_type != VAR_UNKNOWN)
2319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002320 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002321 if (typestr == NULL)
2322 error = TRUE;
2323 else
2324 {
2325 switch (TOUPPER_ASC(*typestr))
2326 {
2327 case 'E': type = VIM_ERROR; break;
2328 case 'Q': type = VIM_QUESTION; break;
2329 case 'I': type = VIM_INFO; break;
2330 case 'W': type = VIM_WARNING; break;
2331 case 'G': type = VIM_GENERIC; break;
2332 }
2333 }
2334 }
2335 }
2336 }
2337
2338 if (buttons == NULL || *buttons == NUL)
2339 buttons = (char_u *)_("&Ok");
2340
2341 if (!error)
2342 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2343 def, NULL, FALSE);
2344#endif
2345}
2346
2347/*
2348 * "copy()" function
2349 */
2350 static void
2351f_copy(typval_T *argvars, typval_T *rettv)
2352{
2353 item_copy(&argvars[0], rettv, FALSE, 0);
2354}
2355
2356#ifdef FEAT_FLOAT
2357/*
2358 * "cos()" function
2359 */
2360 static void
2361f_cos(typval_T *argvars, typval_T *rettv)
2362{
2363 float_T f = 0.0;
2364
2365 rettv->v_type = VAR_FLOAT;
2366 if (get_float_arg(argvars, &f) == OK)
2367 rettv->vval.v_float = cos(f);
2368 else
2369 rettv->vval.v_float = 0.0;
2370}
2371
2372/*
2373 * "cosh()" function
2374 */
2375 static void
2376f_cosh(typval_T *argvars, typval_T *rettv)
2377{
2378 float_T f = 0.0;
2379
2380 rettv->v_type = VAR_FLOAT;
2381 if (get_float_arg(argvars, &f) == OK)
2382 rettv->vval.v_float = cosh(f);
2383 else
2384 rettv->vval.v_float = 0.0;
2385}
2386#endif
2387
2388/*
2389 * "count()" function
2390 */
2391 static void
2392f_count(typval_T *argvars, typval_T *rettv)
2393{
2394 long n = 0;
2395 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002396 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397
Bram Moolenaar9966b212017-07-28 16:46:57 +02002398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002399 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002400
2401 if (argvars[0].v_type == VAR_STRING)
2402 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002403 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002404 char_u *p = argvars[0].vval.v_string;
2405 char_u *next;
2406
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002407 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002408 {
2409 if (ic)
2410 {
2411 size_t len = STRLEN(expr);
2412
2413 while (*p != NUL)
2414 {
2415 if (MB_STRNICMP(p, expr, len) == 0)
2416 {
2417 ++n;
2418 p += len;
2419 }
2420 else
2421 MB_PTR_ADV(p);
2422 }
2423 }
2424 else
2425 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2426 != NULL)
2427 {
2428 ++n;
2429 p = next + STRLEN(expr);
2430 }
2431 }
2432
2433 }
2434 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 {
2436 listitem_T *li;
2437 list_T *l;
2438 long idx;
2439
2440 if ((l = argvars[0].vval.v_list) != NULL)
2441 {
2442 li = l->lv_first;
2443 if (argvars[2].v_type != VAR_UNKNOWN)
2444 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445 if (argvars[3].v_type != VAR_UNKNOWN)
2446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002447 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (!error)
2449 {
2450 li = list_find(l, idx);
2451 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002452 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453 }
2454 }
2455 if (error)
2456 li = NULL;
2457 }
2458
2459 for ( ; li != NULL; li = li->li_next)
2460 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2461 ++n;
2462 }
2463 }
2464 else if (argvars[0].v_type == VAR_DICT)
2465 {
2466 int todo;
2467 dict_T *d;
2468 hashitem_T *hi;
2469
2470 if ((d = argvars[0].vval.v_dict) != NULL)
2471 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 if (argvars[2].v_type != VAR_UNKNOWN)
2473 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002475 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 }
2477
2478 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2479 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2480 {
2481 if (!HASHITEM_EMPTY(hi))
2482 {
2483 --todo;
2484 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2485 ++n;
2486 }
2487 }
2488 }
2489 }
2490 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002491 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002492 rettv->vval.v_number = n;
2493}
2494
2495/*
2496 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2497 *
2498 * Checks the existence of a cscope connection.
2499 */
2500 static void
2501f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2502{
2503#ifdef FEAT_CSCOPE
2504 int num = 0;
2505 char_u *dbpath = NULL;
2506 char_u *prepend = NULL;
2507 char_u buf[NUMBUFLEN];
2508
2509 if (argvars[0].v_type != VAR_UNKNOWN
2510 && argvars[1].v_type != VAR_UNKNOWN)
2511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002512 num = (int)tv_get_number(&argvars[0]);
2513 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002515 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002516 }
2517
2518 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2519#endif
2520}
2521
2522/*
2523 * "cursor(lnum, col)" function, or
2524 * "cursor(list)"
2525 *
2526 * Moves the cursor to the specified line and column.
2527 * Returns 0 when the position could be set, -1 otherwise.
2528 */
2529 static void
2530f_cursor(typval_T *argvars, typval_T *rettv)
2531{
2532 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 int set_curswant = TRUE;
2535
2536 rettv->vval.v_number = -1;
2537 if (argvars[1].v_type == VAR_UNKNOWN)
2538 {
2539 pos_T pos;
2540 colnr_T curswant = -1;
2541
2542 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002544 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 return;
2546 }
2547 line = pos.lnum;
2548 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 if (curswant >= 0)
2551 {
2552 curwin->w_curswant = curswant - 1;
2553 set_curswant = FALSE;
2554 }
2555 }
2556 else
2557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002558 line = tv_get_lnum(argvars);
2559 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002563 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002564 return; /* type error; errmsg already given */
2565 if (line > 0)
2566 curwin->w_cursor.lnum = line;
2567 if (col > 0)
2568 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002570
2571 /* Make sure the cursor is in a valid position. */
2572 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002573 /* Correct cursor for multi-byte character. */
2574 if (has_mbyte)
2575 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576
2577 curwin->w_set_curswant = set_curswant;
2578 rettv->vval.v_number = 0;
2579}
2580
Bram Moolenaar4f974752019-02-17 17:44:42 +01002581#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002582/*
2583 * "debugbreak()" function
2584 */
2585 static void
2586f_debugbreak(typval_T *argvars, typval_T *rettv)
2587{
2588 int pid;
2589
2590 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002591 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002592 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002594 else
2595 {
2596 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2597
2598 if (hProcess != NULL)
2599 {
2600 DebugBreakProcess(hProcess);
2601 CloseHandle(hProcess);
2602 rettv->vval.v_number = OK;
2603 }
2604 }
2605}
2606#endif
2607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608/*
2609 * "deepcopy()" function
2610 */
2611 static void
2612f_deepcopy(typval_T *argvars, typval_T *rettv)
2613{
2614 int noref = 0;
2615 int copyID;
2616
2617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002618 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002620 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 else
2622 {
2623 copyID = get_copyID();
2624 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2625 }
2626}
2627
2628/*
2629 * "delete()" function
2630 */
2631 static void
2632f_delete(typval_T *argvars, typval_T *rettv)
2633{
2634 char_u nbuf[NUMBUFLEN];
2635 char_u *name;
2636 char_u *flags;
2637
2638 rettv->vval.v_number = -1;
2639 if (check_restricted() || check_secure())
2640 return;
2641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002642 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002643 if (name == NULL || *name == NUL)
2644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002645 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646 return;
2647 }
2648
2649 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002650 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 else
2652 flags = (char_u *)"";
2653
2654 if (*flags == NUL)
2655 /* delete a file */
2656 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2657 else if (STRCMP(flags, "d") == 0)
2658 /* delete an empty directory */
2659 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2660 else if (STRCMP(flags, "rf") == 0)
2661 /* delete a directory recursively */
2662 rettv->vval.v_number = delete_recursive(name);
2663 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665}
2666
2667/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002668 * "deletebufline()" function
2669 */
2670 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002671f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002672{
2673 buf_T *buf;
2674 linenr_T first, last;
2675 linenr_T lnum;
2676 long count;
2677 int is_curbuf;
2678 buf_T *curbuf_save = NULL;
2679 win_T *curwin_save = NULL;
2680 tabpage_T *tp;
2681 win_T *wp;
2682
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002683 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002684 if (buf == NULL)
2685 {
2686 rettv->vval.v_number = 1; /* FAIL */
2687 return;
2688 }
2689 is_curbuf = buf == curbuf;
2690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002693 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002694 else
2695 last = first;
2696
2697 if (buf->b_ml.ml_mfp == NULL || first < 1
2698 || first > buf->b_ml.ml_line_count || last < first)
2699 {
2700 rettv->vval.v_number = 1; /* FAIL */
2701 return;
2702 }
2703
2704 if (!is_curbuf)
2705 {
2706 curbuf_save = curbuf;
2707 curwin_save = curwin;
2708 curbuf = buf;
2709 find_win_for_curbuf();
2710 }
2711 if (last > curbuf->b_ml.ml_line_count)
2712 last = curbuf->b_ml.ml_line_count;
2713 count = last - first + 1;
2714
2715 // When coming here from Insert mode, sync undo, so that this can be
2716 // undone separately from what was previously inserted.
2717 if (u_sync_once == 2)
2718 {
2719 u_sync_once = 1; // notify that u_sync() was called
2720 u_sync(TRUE);
2721 }
2722
2723 if (u_save(first - 1, last + 1) == FAIL)
2724 {
2725 rettv->vval.v_number = 1; /* FAIL */
2726 return;
2727 }
2728
2729 for (lnum = first; lnum <= last; ++lnum)
2730 ml_delete(first, TRUE);
2731
2732 FOR_ALL_TAB_WINDOWS(tp, wp)
2733 if (wp->w_buffer == buf)
2734 {
2735 if (wp->w_cursor.lnum > last)
2736 wp->w_cursor.lnum -= count;
2737 else if (wp->w_cursor.lnum> first)
2738 wp->w_cursor.lnum = first;
2739 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2740 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2741 }
2742 check_cursor_col();
2743 deleted_lines_mark(first, count);
2744
2745 if (!is_curbuf)
2746 {
2747 curbuf = curbuf_save;
2748 curwin = curwin_save;
2749 }
2750}
2751
2752/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 * "did_filetype()" function
2754 */
2755 static void
2756f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2757{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759}
2760
2761/*
2762 * "diff_filler()" function
2763 */
2764 static void
2765f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2766{
2767#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002768 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769#endif
2770}
2771
2772/*
2773 * "diff_hlID()" function
2774 */
2775 static void
2776f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2777{
2778#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002779 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002781 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 static int fnum = 0;
2783 static int change_start = 0;
2784 static int change_end = 0;
2785 static hlf_T hlID = (hlf_T)0;
2786 int filler_lines;
2787 int col;
2788
2789 if (lnum < 0) /* ignore type error in {lnum} arg */
2790 lnum = 0;
2791 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002792 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 || fnum != curbuf->b_fnum)
2794 {
2795 /* New line, buffer, change: need to get the values. */
2796 filler_lines = diff_check(curwin, lnum);
2797 if (filler_lines < 0)
2798 {
2799 if (filler_lines == -1)
2800 {
2801 change_start = MAXCOL;
2802 change_end = -1;
2803 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2804 hlID = HLF_ADD; /* added line */
2805 else
2806 hlID = HLF_CHD; /* changed line */
2807 }
2808 else
2809 hlID = HLF_ADD; /* added line */
2810 }
2811 else
2812 hlID = (hlf_T)0;
2813 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002814 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 fnum = curbuf->b_fnum;
2816 }
2817
2818 if (hlID == HLF_CHD || hlID == HLF_TXD)
2819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002820 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 if (col >= change_start && col <= change_end)
2822 hlID = HLF_TXD; /* changed text */
2823 else
2824 hlID = HLF_CHD; /* changed line */
2825 }
2826 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2827#endif
2828}
2829
2830/*
2831 * "empty({expr})" function
2832 */
2833 static void
2834f_empty(typval_T *argvars, typval_T *rettv)
2835{
2836 int n = FALSE;
2837
2838 switch (argvars[0].v_type)
2839 {
2840 case VAR_STRING:
2841 case VAR_FUNC:
2842 n = argvars[0].vval.v_string == NULL
2843 || *argvars[0].vval.v_string == NUL;
2844 break;
2845 case VAR_PARTIAL:
2846 n = FALSE;
2847 break;
2848 case VAR_NUMBER:
2849 n = argvars[0].vval.v_number == 0;
2850 break;
2851 case VAR_FLOAT:
2852#ifdef FEAT_FLOAT
2853 n = argvars[0].vval.v_float == 0.0;
2854 break;
2855#endif
2856 case VAR_LIST:
2857 n = argvars[0].vval.v_list == NULL
2858 || argvars[0].vval.v_list->lv_first == NULL;
2859 break;
2860 case VAR_DICT:
2861 n = argvars[0].vval.v_dict == NULL
2862 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2863 break;
2864 case VAR_SPECIAL:
2865 n = argvars[0].vval.v_number != VVAL_TRUE;
2866 break;
2867
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002868 case VAR_BLOB:
2869 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002870 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2871 break;
2872
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002873 case VAR_JOB:
2874#ifdef FEAT_JOB_CHANNEL
2875 n = argvars[0].vval.v_job == NULL
2876 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2877 break;
2878#endif
2879 case VAR_CHANNEL:
2880#ifdef FEAT_JOB_CHANNEL
2881 n = argvars[0].vval.v_channel == NULL
2882 || !channel_is_open(argvars[0].vval.v_channel);
2883 break;
2884#endif
2885 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002886 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 n = TRUE;
2888 break;
2889 }
2890
2891 rettv->vval.v_number = n;
2892}
2893
2894/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002895 * "environ()" function
2896 */
2897 static void
2898f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2899{
2900#if !defined(AMIGA)
2901 int i = 0;
2902 char_u *entry, *value;
2903# ifdef MSWIN
2904 extern wchar_t **_wenviron;
2905# else
2906 extern char **environ;
2907# endif
2908
2909 if (rettv_dict_alloc(rettv) != OK)
2910 return;
2911
2912# ifdef MSWIN
2913 if (*_wenviron == NULL)
2914 return;
2915# else
2916 if (*environ == NULL)
2917 return;
2918# endif
2919
2920 for (i = 0; ; ++i)
2921 {
2922# ifdef MSWIN
2923 short_u *p;
2924
2925 if ((p = (short_u *)_wenviron[i]) == NULL)
2926 return;
2927 entry = utf16_to_enc(p, NULL);
2928# else
2929 if ((entry = (char_u *)environ[i]) == NULL)
2930 return;
2931 entry = vim_strsave(entry);
2932# endif
2933 if (entry == NULL) // out of memory
2934 return;
2935 if ((value = vim_strchr(entry, '=')) == NULL)
2936 {
2937 vim_free(entry);
2938 continue;
2939 }
2940 *value++ = NUL;
2941 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2942 vim_free(entry);
2943 }
2944#endif
2945}
2946
2947/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948 * "escape({string}, {chars})" function
2949 */
2950 static void
2951f_escape(typval_T *argvars, typval_T *rettv)
2952{
2953 char_u buf[NUMBUFLEN];
2954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002955 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2956 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 rettv->v_type = VAR_STRING;
2958}
2959
2960/*
2961 * "eval()" function
2962 */
2963 static void
2964f_eval(typval_T *argvars, typval_T *rettv)
2965{
2966 char_u *s, *p;
2967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002968 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 if (s != NULL)
2970 s = skipwhite(s);
2971
2972 p = s;
2973 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2974 {
2975 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 need_clr_eos = FALSE;
2978 rettv->v_type = VAR_NUMBER;
2979 rettv->vval.v_number = 0;
2980 }
2981 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002982 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983}
2984
2985/*
2986 * "eventhandler()" function
2987 */
2988 static void
2989f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2990{
2991 rettv->vval.v_number = vgetc_busy;
2992}
2993
2994/*
2995 * "executable()" function
2996 */
2997 static void
2998f_executable(typval_T *argvars, typval_T *rettv)
2999{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003000 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001
3002 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003003 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004}
3005
3006static garray_T redir_execute_ga;
3007
3008/*
3009 * Append "value[value_len]" to the execute() output.
3010 */
3011 void
3012execute_redir_str(char_u *value, int value_len)
3013{
3014 int len;
3015
3016 if (value_len == -1)
3017 len = (int)STRLEN(value); /* Append the entire string */
3018 else
3019 len = value_len; /* Append only "value_len" characters */
3020 if (ga_grow(&redir_execute_ga, len) == OK)
3021 {
3022 mch_memmove((char *)redir_execute_ga.ga_data
3023 + redir_execute_ga.ga_len, value, len);
3024 redir_execute_ga.ga_len += len;
3025 }
3026}
3027
3028/*
3029 * Get next line from a list.
3030 * Called by do_cmdline() to get the next line.
3031 * Returns allocated string, or NULL for end of function.
3032 */
3033
3034 static char_u *
3035get_list_line(
3036 int c UNUSED,
3037 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003038 int indent UNUSED,
3039 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040{
3041 listitem_T **p = (listitem_T **)cookie;
3042 listitem_T *item = *p;
3043 char_u buf[NUMBUFLEN];
3044 char_u *s;
3045
3046 if (item == NULL)
3047 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003048 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 *p = item->li_next;
3050 return s == NULL ? NULL : vim_strsave(s);
3051}
3052
3053/*
3054 * "execute()" function
3055 */
3056 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003057execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058{
3059 char_u *cmd = NULL;
3060 list_T *list = NULL;
3061 int save_msg_silent = msg_silent;
3062 int save_emsg_silent = emsg_silent;
3063 int save_emsg_noredir = emsg_noredir;
3064 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003065 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003067 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003068 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069
3070 rettv->vval.v_string = NULL;
3071 rettv->v_type = VAR_STRING;
3072
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003073 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003074 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003075 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 if (list == NULL || list->lv_first == NULL)
3077 /* empty list, no commands, empty output */
3078 return;
3079 ++list->lv_refcount;
3080 }
3081 else
3082 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003083 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084 if (cmd == NULL)
3085 return;
3086 }
3087
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003088 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 {
3090 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003091 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092
3093 if (s == NULL)
3094 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003095 if (*s == NUL)
3096 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 if (STRNCMP(s, "silent", 6) == 0)
3098 ++msg_silent;
3099 if (STRCMP(s, "silent!") == 0)
3100 {
3101 emsg_silent = TRUE;
3102 emsg_noredir = TRUE;
3103 }
3104 }
3105 else
3106 ++msg_silent;
3107
3108 if (redir_execute)
3109 save_ga = redir_execute_ga;
3110 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3111 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003112 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003113 if (!echo_output)
3114 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115
3116 if (cmd != NULL)
3117 do_cmdline_cmd(cmd);
3118 else
3119 {
3120 listitem_T *item = list->lv_first;
3121
3122 do_cmdline(NULL, get_list_line, (void *)&item,
3123 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3124 --list->lv_refcount;
3125 }
3126
Bram Moolenaard297f352017-01-29 20:31:21 +01003127 /* Need to append a NUL to the result. */
3128 if (ga_grow(&redir_execute_ga, 1) == OK)
3129 {
3130 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3131 rettv->vval.v_string = redir_execute_ga.ga_data;
3132 }
3133 else
3134 {
3135 ga_clear(&redir_execute_ga);
3136 rettv->vval.v_string = NULL;
3137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 msg_silent = save_msg_silent;
3139 emsg_silent = save_emsg_silent;
3140 emsg_noredir = save_emsg_noredir;
3141
3142 redir_execute = save_redir_execute;
3143 if (redir_execute)
3144 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003145 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003147 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003148 if (echo_output)
3149 // When not working silently: put it in column zero. A following
3150 // "echon" will overwrite the message, unavoidably.
3151 msg_col = 0;
3152 else
3153 // When working silently: Put it back where it was, since nothing
3154 // should have been written.
3155 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156}
3157
3158/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003159 * "execute()" function
3160 */
3161 static void
3162f_execute(typval_T *argvars, typval_T *rettv)
3163{
3164 execute_common(argvars, rettv, 0);
3165}
3166
3167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168 * "exepath()" function
3169 */
3170 static void
3171f_exepath(typval_T *argvars, typval_T *rettv)
3172{
3173 char_u *p = NULL;
3174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003175 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003176 rettv->v_type = VAR_STRING;
3177 rettv->vval.v_string = p;
3178}
3179
3180/*
3181 * "exists()" function
3182 */
3183 static void
3184f_exists(typval_T *argvars, typval_T *rettv)
3185{
3186 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190 if (*p == '$') /* environment variable */
3191 {
3192 /* first try "normal" environment variables (fast) */
3193 if (mch_getenv(p + 1) != NULL)
3194 n = TRUE;
3195 else
3196 {
3197 /* try expanding things like $VIM and ${HOME} */
3198 p = expand_env_save(p);
3199 if (p != NULL && *p != '$')
3200 n = TRUE;
3201 vim_free(p);
3202 }
3203 }
3204 else if (*p == '&' || *p == '+') /* option */
3205 {
3206 n = (get_option_tv(&p, NULL, TRUE) == OK);
3207 if (*skipwhite(p) != NUL)
3208 n = FALSE; /* trailing garbage */
3209 }
3210 else if (*p == '*') /* internal or user defined function */
3211 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003212 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 }
3214 else if (*p == ':')
3215 {
3216 n = cmd_exists(p + 1);
3217 }
3218 else if (*p == '#')
3219 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003220 if (p[1] == '#')
3221 n = autocmd_supported(p + 2);
3222 else
3223 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 }
3225 else /* internal variable */
3226 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003227 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003228 }
3229
3230 rettv->vval.v_number = n;
3231}
3232
3233#ifdef FEAT_FLOAT
3234/*
3235 * "exp()" function
3236 */
3237 static void
3238f_exp(typval_T *argvars, typval_T *rettv)
3239{
3240 float_T f = 0.0;
3241
3242 rettv->v_type = VAR_FLOAT;
3243 if (get_float_arg(argvars, &f) == OK)
3244 rettv->vval.v_float = exp(f);
3245 else
3246 rettv->vval.v_float = 0.0;
3247}
3248#endif
3249
3250/*
3251 * "expand()" function
3252 */
3253 static void
3254f_expand(typval_T *argvars, typval_T *rettv)
3255{
3256 char_u *s;
3257 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003258 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3260 expand_T xpc;
3261 int error = FALSE;
3262 char_u *result;
3263
3264 rettv->v_type = VAR_STRING;
3265 if (argvars[1].v_type != VAR_UNKNOWN
3266 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003267 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003269 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003271 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 if (*s == '%' || *s == '#' || *s == '<')
3273 {
3274 ++emsg_off;
3275 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3276 --emsg_off;
3277 if (rettv->v_type == VAR_LIST)
3278 {
3279 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3280 list_append_string(rettv->vval.v_list, result, -1);
3281 else
3282 vim_free(result);
3283 }
3284 else
3285 rettv->vval.v_string = result;
3286 }
3287 else
3288 {
3289 /* When the optional second argument is non-zero, don't remove matches
3290 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3291 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003292 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 options |= WILD_KEEP_ALL;
3294 if (!error)
3295 {
3296 ExpandInit(&xpc);
3297 xpc.xp_context = EXPAND_FILES;
3298 if (p_wic)
3299 options += WILD_ICASE;
3300 if (rettv->v_type == VAR_STRING)
3301 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3302 options, WILD_ALL);
3303 else if (rettv_list_alloc(rettv) != FAIL)
3304 {
3305 int i;
3306
3307 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3308 for (i = 0; i < xpc.xp_numfiles; i++)
3309 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3310 ExpandCleanup(&xpc);
3311 }
3312 }
3313 else
3314 rettv->vval.v_string = NULL;
3315 }
3316}
3317
3318/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003319 * "expandcmd()" function
3320 * Expand all the special characters in a command string.
3321 */
3322 static void
3323f_expandcmd(typval_T *argvars, typval_T *rettv)
3324{
3325 exarg_T eap;
3326 char_u *cmdstr;
3327 char *errormsg = NULL;
3328
3329 rettv->v_type = VAR_STRING;
3330 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3331
3332 memset(&eap, 0, sizeof(eap));
3333 eap.cmd = cmdstr;
3334 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003335 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003336 eap.usefilter = FALSE;
3337 eap.nextcmd = NULL;
3338 eap.cmdidx = CMD_USER;
3339
3340 expand_filename(&eap, &cmdstr, &errormsg);
3341 if (errormsg != NULL && *errormsg != NUL)
3342 emsg(errormsg);
3343
3344 rettv->vval.v_string = cmdstr;
3345}
3346
3347/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 * "extend(list, list [, idx])" function
3349 * "extend(dict, dict [, action])" function
3350 */
3351 static void
3352f_extend(typval_T *argvars, typval_T *rettv)
3353{
3354 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3355
3356 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3357 {
3358 list_T *l1, *l2;
3359 listitem_T *item;
3360 long before;
3361 int error = FALSE;
3362
3363 l1 = argvars[0].vval.v_list;
3364 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003365 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 && l2 != NULL)
3367 {
3368 if (argvars[2].v_type != VAR_UNKNOWN)
3369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003370 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 if (error)
3372 return; /* type error; errmsg already given */
3373
3374 if (before == l1->lv_len)
3375 item = NULL;
3376 else
3377 {
3378 item = list_find(l1, before);
3379 if (item == NULL)
3380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003381 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382 return;
3383 }
3384 }
3385 }
3386 else
3387 item = NULL;
3388 list_extend(l1, l2, item);
3389
3390 copy_tv(&argvars[0], rettv);
3391 }
3392 }
3393 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3394 {
3395 dict_T *d1, *d2;
3396 char_u *action;
3397 int i;
3398
3399 d1 = argvars[0].vval.v_dict;
3400 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003401 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 && d2 != NULL)
3403 {
3404 /* Check the third argument. */
3405 if (argvars[2].v_type != VAR_UNKNOWN)
3406 {
3407 static char *(av[]) = {"keep", "force", "error"};
3408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003409 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410 if (action == NULL)
3411 return; /* type error; errmsg already given */
3412 for (i = 0; i < 3; ++i)
3413 if (STRCMP(action, av[i]) == 0)
3414 break;
3415 if (i == 3)
3416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003417 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 return;
3419 }
3420 }
3421 else
3422 action = (char_u *)"force";
3423
3424 dict_extend(d1, d2, action);
3425
3426 copy_tv(&argvars[0], rettv);
3427 }
3428 }
3429 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003430 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431}
3432
3433/*
3434 * "feedkeys()" function
3435 */
3436 static void
3437f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3438{
3439 int remap = TRUE;
3440 int insert = FALSE;
3441 char_u *keys, *flags;
3442 char_u nbuf[NUMBUFLEN];
3443 int typed = FALSE;
3444 int execute = FALSE;
3445 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003446 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447 char_u *keys_esc;
3448
3449 /* This is not allowed in the sandbox. If the commands would still be
3450 * executed in the sandbox it would be OK, but it probably happens later,
3451 * when "sandbox" is no longer set. */
3452 if (check_secure())
3453 return;
3454
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456
3457 if (argvars[1].v_type != VAR_UNKNOWN)
3458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003459 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 for ( ; *flags != NUL; ++flags)
3461 {
3462 switch (*flags)
3463 {
3464 case 'n': remap = FALSE; break;
3465 case 'm': remap = TRUE; break;
3466 case 't': typed = TRUE; break;
3467 case 'i': insert = TRUE; break;
3468 case 'x': execute = TRUE; break;
3469 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003470 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 }
3472 }
3473 }
3474
3475 if (*keys != NUL || execute)
3476 {
3477 /* Need to escape K_SPECIAL and CSI before putting the string in the
3478 * typeahead buffer. */
3479 keys_esc = vim_strsave_escape_csi(keys);
3480 if (keys_esc != NULL)
3481 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003482 if (lowlevel)
3483 {
3484#ifdef USE_INPUT_BUF
3485 add_to_input_buf(keys, (int)STRLEN(keys));
3486#else
3487 emsg(_("E980: lowlevel input not supported"));
3488#endif
3489 }
3490 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003491 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003492 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003494 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003495#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003496 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003497#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003498 )
3499 typebuf_was_filled = TRUE;
3500 }
3501 vim_free(keys_esc);
3502
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003503 if (execute)
3504 {
3505 int save_msg_scroll = msg_scroll;
3506
3507 /* Avoid a 1 second delay when the keys start Insert mode. */
3508 msg_scroll = FALSE;
3509
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003510 if (!dangerous)
3511 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003512 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003513 if (!dangerous)
3514 --ex_normal_busy;
3515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003516 msg_scroll |= save_msg_scroll;
3517 }
3518 }
3519 }
3520}
3521
3522/*
3523 * "filereadable()" function
3524 */
3525 static void
3526f_filereadable(typval_T *argvars, typval_T *rettv)
3527{
3528 int fd;
3529 char_u *p;
3530 int n;
3531
3532#ifndef O_NONBLOCK
3533# define O_NONBLOCK 0
3534#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003535 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3537 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3538 {
3539 n = TRUE;
3540 close(fd);
3541 }
3542 else
3543 n = FALSE;
3544
3545 rettv->vval.v_number = n;
3546}
3547
3548/*
3549 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3550 * rights to write into.
3551 */
3552 static void
3553f_filewritable(typval_T *argvars, typval_T *rettv)
3554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003555 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556}
3557
3558 static void
3559findfilendir(
3560 typval_T *argvars UNUSED,
3561 typval_T *rettv,
3562 int find_what UNUSED)
3563{
3564#ifdef FEAT_SEARCHPATH
3565 char_u *fname;
3566 char_u *fresult = NULL;
3567 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3568 char_u *p;
3569 char_u pathbuf[NUMBUFLEN];
3570 int count = 1;
3571 int first = TRUE;
3572 int error = FALSE;
3573#endif
3574
3575 rettv->vval.v_string = NULL;
3576 rettv->v_type = VAR_STRING;
3577
3578#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003579 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580
3581 if (argvars[1].v_type != VAR_UNKNOWN)
3582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 if (p == NULL)
3585 error = TRUE;
3586 else
3587 {
3588 if (*p != NUL)
3589 path = p;
3590
3591 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003592 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 }
3594 }
3595
3596 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3597 error = TRUE;
3598
3599 if (*fname != NUL && !error)
3600 {
3601 do
3602 {
3603 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3604 vim_free(fresult);
3605 fresult = find_file_in_path_option(first ? fname : NULL,
3606 first ? (int)STRLEN(fname) : 0,
3607 0, first, path,
3608 find_what,
3609 curbuf->b_ffname,
3610 find_what == FINDFILE_DIR
3611 ? (char_u *)"" : curbuf->b_p_sua);
3612 first = FALSE;
3613
3614 if (fresult != NULL && rettv->v_type == VAR_LIST)
3615 list_append_string(rettv->vval.v_list, fresult, -1);
3616
3617 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3618 }
3619
3620 if (rettv->v_type == VAR_STRING)
3621 rettv->vval.v_string = fresult;
3622#endif
3623}
3624
3625/*
3626 * "filter()" function
3627 */
3628 static void
3629f_filter(typval_T *argvars, typval_T *rettv)
3630{
3631 filter_map(argvars, rettv, FALSE);
3632}
3633
3634/*
3635 * "finddir({fname}[, {path}[, {count}]])" function
3636 */
3637 static void
3638f_finddir(typval_T *argvars, typval_T *rettv)
3639{
3640 findfilendir(argvars, rettv, FINDFILE_DIR);
3641}
3642
3643/*
3644 * "findfile({fname}[, {path}[, {count}]])" function
3645 */
3646 static void
3647f_findfile(typval_T *argvars, typval_T *rettv)
3648{
3649 findfilendir(argvars, rettv, FINDFILE_FILE);
3650}
3651
3652#ifdef FEAT_FLOAT
3653/*
3654 * "float2nr({float})" function
3655 */
3656 static void
3657f_float2nr(typval_T *argvars, typval_T *rettv)
3658{
3659 float_T f = 0.0;
3660
3661 if (get_float_arg(argvars, &f) == OK)
3662 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003663 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003664 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003665 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003666 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667 else
3668 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 }
3670}
3671
3672/*
3673 * "floor({float})" function
3674 */
3675 static void
3676f_floor(typval_T *argvars, typval_T *rettv)
3677{
3678 float_T f = 0.0;
3679
3680 rettv->v_type = VAR_FLOAT;
3681 if (get_float_arg(argvars, &f) == OK)
3682 rettv->vval.v_float = floor(f);
3683 else
3684 rettv->vval.v_float = 0.0;
3685}
3686
3687/*
3688 * "fmod()" function
3689 */
3690 static void
3691f_fmod(typval_T *argvars, typval_T *rettv)
3692{
3693 float_T fx = 0.0, fy = 0.0;
3694
3695 rettv->v_type = VAR_FLOAT;
3696 if (get_float_arg(argvars, &fx) == OK
3697 && get_float_arg(&argvars[1], &fy) == OK)
3698 rettv->vval.v_float = fmod(fx, fy);
3699 else
3700 rettv->vval.v_float = 0.0;
3701}
3702#endif
3703
3704/*
3705 * "fnameescape({string})" function
3706 */
3707 static void
3708f_fnameescape(typval_T *argvars, typval_T *rettv)
3709{
3710 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003711 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 rettv->v_type = VAR_STRING;
3713}
3714
3715/*
3716 * "fnamemodify({fname}, {mods})" function
3717 */
3718 static void
3719f_fnamemodify(typval_T *argvars, typval_T *rettv)
3720{
3721 char_u *fname;
3722 char_u *mods;
3723 int usedlen = 0;
3724 int len;
3725 char_u *fbuf = NULL;
3726 char_u buf[NUMBUFLEN];
3727
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003728 fname = tv_get_string_chk(&argvars[0]);
3729 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 if (fname == NULL || mods == NULL)
3731 fname = NULL;
3732 else
3733 {
3734 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003735 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 }
3737
3738 rettv->v_type = VAR_STRING;
3739 if (fname == NULL)
3740 rettv->vval.v_string = NULL;
3741 else
3742 rettv->vval.v_string = vim_strnsave(fname, len);
3743 vim_free(fbuf);
3744}
3745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746/*
3747 * "foldclosed()" function
3748 */
3749 static void
3750foldclosed_both(
3751 typval_T *argvars UNUSED,
3752 typval_T *rettv,
3753 int end UNUSED)
3754{
3755#ifdef FEAT_FOLDING
3756 linenr_T lnum;
3757 linenr_T first, last;
3758
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003759 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3761 {
3762 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3763 {
3764 if (end)
3765 rettv->vval.v_number = (varnumber_T)last;
3766 else
3767 rettv->vval.v_number = (varnumber_T)first;
3768 return;
3769 }
3770 }
3771#endif
3772 rettv->vval.v_number = -1;
3773}
3774
3775/*
3776 * "foldclosed()" function
3777 */
3778 static void
3779f_foldclosed(typval_T *argvars, typval_T *rettv)
3780{
3781 foldclosed_both(argvars, rettv, FALSE);
3782}
3783
3784/*
3785 * "foldclosedend()" function
3786 */
3787 static void
3788f_foldclosedend(typval_T *argvars, typval_T *rettv)
3789{
3790 foldclosed_both(argvars, rettv, TRUE);
3791}
3792
3793/*
3794 * "foldlevel()" function
3795 */
3796 static void
3797f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3798{
3799#ifdef FEAT_FOLDING
3800 linenr_T lnum;
3801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003802 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3804 rettv->vval.v_number = foldLevel(lnum);
3805#endif
3806}
3807
3808/*
3809 * "foldtext()" function
3810 */
3811 static void
3812f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3813{
3814#ifdef FEAT_FOLDING
3815 linenr_T foldstart;
3816 linenr_T foldend;
3817 char_u *dashes;
3818 linenr_T lnum;
3819 char_u *s;
3820 char_u *r;
3821 int len;
3822 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003823 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824#endif
3825
3826 rettv->v_type = VAR_STRING;
3827 rettv->vval.v_string = NULL;
3828#ifdef FEAT_FOLDING
3829 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3830 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3831 dashes = get_vim_var_str(VV_FOLDDASHES);
3832 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3833 && dashes != NULL)
3834 {
3835 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003836 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 if (!linewhite(lnum))
3838 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839
3840 /* Find interesting text in this line. */
3841 s = skipwhite(ml_get(lnum));
3842 /* skip C comment-start */
3843 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3844 {
3845 s = skipwhite(s + 2);
3846 if (*skipwhite(s) == NUL
3847 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3848 {
3849 s = skipwhite(ml_get(lnum + 1));
3850 if (*s == '*')
3851 s = skipwhite(s + 1);
3852 }
3853 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003854 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003855 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003856 r = alloc(STRLEN(txt)
3857 + STRLEN(dashes) // for %s
3858 + 20 // for %3ld
3859 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003860 if (r != NULL)
3861 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003862 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863 len = (int)STRLEN(r);
3864 STRCAT(r, s);
3865 /* remove 'foldmarker' and 'commentstring' */
3866 foldtext_cleanup(r + len);
3867 rettv->vval.v_string = r;
3868 }
3869 }
3870#endif
3871}
3872
3873/*
3874 * "foldtextresult(lnum)" function
3875 */
3876 static void
3877f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3878{
3879#ifdef FEAT_FOLDING
3880 linenr_T lnum;
3881 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003882 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883 foldinfo_T foldinfo;
3884 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003885 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886#endif
3887
3888 rettv->v_type = VAR_STRING;
3889 rettv->vval.v_string = NULL;
3890#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003891 if (entered)
3892 return; /* reject recursive use */
3893 entered = TRUE;
3894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 /* treat illegal types and illegal string values for {lnum} the same */
3897 if (lnum < 0)
3898 lnum = 0;
3899 fold_count = foldedCount(curwin, lnum, &foldinfo);
3900 if (fold_count > 0)
3901 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003902 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3903 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003904 if (text == buf)
3905 text = vim_strsave(text);
3906 rettv->vval.v_string = text;
3907 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003908
3909 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910#endif
3911}
3912
3913/*
3914 * "foreground()" function
3915 */
3916 static void
3917f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3918{
3919#ifdef FEAT_GUI
3920 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003921 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003923 return;
3924 }
3925#endif
3926#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928#endif
3929}
3930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003932common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933{
3934 char_u *s;
3935 char_u *name;
3936 int use_string = FALSE;
3937 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003938 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939
3940 if (argvars[0].v_type == VAR_FUNC)
3941 {
3942 /* function(MyFunc, [arg], dict) */
3943 s = argvars[0].vval.v_string;
3944 }
3945 else if (argvars[0].v_type == VAR_PARTIAL
3946 && argvars[0].vval.v_partial != NULL)
3947 {
3948 /* function(dict.MyFunc, [arg]) */
3949 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003950 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 }
3952 else
3953 {
3954 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003955 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 use_string = TRUE;
3957 }
3958
Bram Moolenaar843b8842016-08-21 14:36:15 +02003959 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003960 {
3961 name = s;
3962 trans_name = trans_function_name(&name, FALSE,
3963 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3964 if (*name != NUL)
3965 s = NULL;
3966 }
3967
Bram Moolenaar843b8842016-08-21 14:36:15 +02003968 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3969 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003970 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003972 else if (trans_name != NULL && (is_funcref
3973 ? find_func(trans_name) == NULL
3974 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003975 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976 else
3977 {
3978 int dict_idx = 0;
3979 int arg_idx = 0;
3980 list_T *list = NULL;
3981
3982 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3983 {
3984 char sid_buf[25];
3985 int off = *s == 's' ? 2 : 5;
3986
3987 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3988 * also be called from another script. Using trans_function_name()
3989 * would also work, but some plugins depend on the name being
3990 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003991 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003992 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 if (name != NULL)
3994 {
3995 STRCPY(name, sid_buf);
3996 STRCAT(name, s + off);
3997 }
3998 }
3999 else
4000 name = vim_strsave(s);
4001
4002 if (argvars[1].v_type != VAR_UNKNOWN)
4003 {
4004 if (argvars[2].v_type != VAR_UNKNOWN)
4005 {
4006 /* function(name, [args], dict) */
4007 arg_idx = 1;
4008 dict_idx = 2;
4009 }
4010 else if (argvars[1].v_type == VAR_DICT)
4011 /* function(name, dict) */
4012 dict_idx = 1;
4013 else
4014 /* function(name, [args]) */
4015 arg_idx = 1;
4016 if (dict_idx > 0)
4017 {
4018 if (argvars[dict_idx].v_type != VAR_DICT)
4019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004020 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004023 }
4024 if (argvars[dict_idx].vval.v_dict == NULL)
4025 dict_idx = 0;
4026 }
4027 if (arg_idx > 0)
4028 {
4029 if (argvars[arg_idx].v_type != VAR_LIST)
4030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004031 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004033 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034 }
4035 list = argvars[arg_idx].vval.v_list;
4036 if (list == NULL || list->lv_len == 0)
4037 arg_idx = 0;
4038 }
4039 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004040 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004042 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004043
4044 /* result is a VAR_PARTIAL */
4045 if (pt == NULL)
4046 vim_free(name);
4047 else
4048 {
4049 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4050 {
4051 listitem_T *li;
4052 int i = 0;
4053 int arg_len = 0;
4054 int lv_len = 0;
4055
4056 if (arg_pt != NULL)
4057 arg_len = arg_pt->pt_argc;
4058 if (list != NULL)
4059 lv_len = list->lv_len;
4060 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004061 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004062 if (pt->pt_argv == NULL)
4063 {
4064 vim_free(pt);
4065 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004066 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004068 for (i = 0; i < arg_len; i++)
4069 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4070 if (lv_len > 0)
4071 for (li = list->lv_first; li != NULL;
4072 li = li->li_next)
4073 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004074 }
4075
4076 /* For "function(dict.func, [], dict)" and "func" is a partial
4077 * use "dict". That is backwards compatible. */
4078 if (dict_idx > 0)
4079 {
4080 /* The dict is bound explicitly, pt_auto is FALSE. */
4081 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4082 ++pt->pt_dict->dv_refcount;
4083 }
4084 else if (arg_pt != NULL)
4085 {
4086 /* If the dict was bound automatically the result is also
4087 * bound automatically. */
4088 pt->pt_dict = arg_pt->pt_dict;
4089 pt->pt_auto = arg_pt->pt_auto;
4090 if (pt->pt_dict != NULL)
4091 ++pt->pt_dict->dv_refcount;
4092 }
4093
4094 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004095 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4096 {
4097 pt->pt_func = arg_pt->pt_func;
4098 func_ptr_ref(pt->pt_func);
4099 vim_free(name);
4100 }
4101 else if (is_funcref)
4102 {
4103 pt->pt_func = find_func(trans_name);
4104 func_ptr_ref(pt->pt_func);
4105 vim_free(name);
4106 }
4107 else
4108 {
4109 pt->pt_name = name;
4110 func_ref(name);
4111 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112 }
4113 rettv->v_type = VAR_PARTIAL;
4114 rettv->vval.v_partial = pt;
4115 }
4116 else
4117 {
4118 /* result is a VAR_FUNC */
4119 rettv->v_type = VAR_FUNC;
4120 rettv->vval.v_string = name;
4121 func_ref(name);
4122 }
4123 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004124theend:
4125 vim_free(trans_name);
4126}
4127
4128/*
4129 * "funcref()" function
4130 */
4131 static void
4132f_funcref(typval_T *argvars, typval_T *rettv)
4133{
4134 common_function(argvars, rettv, TRUE);
4135}
4136
4137/*
4138 * "function()" function
4139 */
4140 static void
4141f_function(typval_T *argvars, typval_T *rettv)
4142{
4143 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144}
4145
4146/*
4147 * "garbagecollect()" function
4148 */
4149 static void
4150f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4151{
4152 /* This is postponed until we are back at the toplevel, because we may be
4153 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4154 want_garbage_collect = TRUE;
4155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004156 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157 garbage_collect_at_exit = TRUE;
4158}
4159
4160/*
4161 * "get()" function
4162 */
4163 static void
4164f_get(typval_T *argvars, typval_T *rettv)
4165{
4166 listitem_T *li;
4167 list_T *l;
4168 dictitem_T *di;
4169 dict_T *d;
4170 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004171 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004173 if (argvars[0].v_type == VAR_BLOB)
4174 {
4175 int error = FALSE;
4176 int idx = tv_get_number_chk(&argvars[1], &error);
4177
4178 if (!error)
4179 {
4180 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004181 if (idx < 0)
4182 idx = blob_len(argvars[0].vval.v_blob) + idx;
4183 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4184 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004185 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004186 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004187 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004188 tv = rettv;
4189 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004190 }
4191 }
4192 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 {
4194 if ((l = argvars[0].vval.v_list) != NULL)
4195 {
4196 int error = FALSE;
4197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004198 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004199 if (!error && li != NULL)
4200 tv = &li->li_tv;
4201 }
4202 }
4203 else if (argvars[0].v_type == VAR_DICT)
4204 {
4205 if ((d = argvars[0].vval.v_dict) != NULL)
4206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004207 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 if (di != NULL)
4209 tv = &di->di_tv;
4210 }
4211 }
4212 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4213 {
4214 partial_T *pt;
4215 partial_T fref_pt;
4216
4217 if (argvars[0].v_type == VAR_PARTIAL)
4218 pt = argvars[0].vval.v_partial;
4219 else
4220 {
4221 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4222 fref_pt.pt_name = argvars[0].vval.v_string;
4223 pt = &fref_pt;
4224 }
4225
4226 if (pt != NULL)
4227 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004228 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004229 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230
4231 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4232 {
4233 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004234 n = partial_name(pt);
4235 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 rettv->vval.v_string = NULL;
4237 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004238 {
4239 rettv->vval.v_string = vim_strsave(n);
4240 if (rettv->v_type == VAR_FUNC)
4241 func_ref(rettv->vval.v_string);
4242 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243 }
4244 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004245 {
4246 what_is_dict = TRUE;
4247 if (pt->pt_dict != NULL)
4248 rettv_dict_set(rettv, pt->pt_dict);
4249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250 else if (STRCMP(what, "args") == 0)
4251 {
4252 rettv->v_type = VAR_LIST;
4253 if (rettv_list_alloc(rettv) == OK)
4254 {
4255 int i;
4256
4257 for (i = 0; i < pt->pt_argc; ++i)
4258 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4259 }
4260 }
4261 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004262 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004263
4264 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4265 // third argument
4266 if (!what_is_dict)
4267 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004268 }
4269 }
4270 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004271 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272
4273 if (tv == NULL)
4274 {
4275 if (argvars[2].v_type != VAR_UNKNOWN)
4276 copy_tv(&argvars[2], rettv);
4277 }
4278 else
4279 copy_tv(tv, rettv);
4280}
4281
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004282/*
4283 * Returns buffer options, variables and other attributes in a dictionary.
4284 */
4285 static dict_T *
4286get_buffer_info(buf_T *buf)
4287{
4288 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004289 tabpage_T *tp;
4290 win_T *wp;
4291 list_T *windows;
4292
4293 dict = dict_alloc();
4294 if (dict == NULL)
4295 return NULL;
4296
Bram Moolenaare0be1672018-07-08 16:50:37 +02004297 dict_add_number(dict, "bufnr", buf->b_fnum);
4298 dict_add_string(dict, "name", buf->b_ffname);
4299 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4300 : buflist_findlnum(buf));
4301 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4302 dict_add_number(dict, "listed", buf->b_p_bl);
4303 dict_add_number(dict, "changed", bufIsChanged(buf));
4304 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4305 dict_add_number(dict, "hidden",
4306 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004307
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004308 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004309 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004310
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004311 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004312 windows = list_alloc();
4313 if (windows != NULL)
4314 {
4315 FOR_ALL_TAB_WINDOWS(tp, wp)
4316 if (wp->w_buffer == buf)
4317 list_append_number(windows, (varnumber_T)wp->w_id);
4318 dict_add_list(dict, "windows", windows);
4319 }
4320
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004321#ifdef FEAT_TEXT_PROP
4322 // List of popup windows displaying this buffer
4323 windows = list_alloc();
4324 if (windows != NULL)
4325 {
4326 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4327 if (wp->w_buffer == buf)
4328 list_append_number(windows, (varnumber_T)wp->w_id);
4329 FOR_ALL_TABPAGES(tp)
4330 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4331 if (wp->w_buffer == buf)
4332 list_append_number(windows, (varnumber_T)wp->w_id);
4333
4334 dict_add_list(dict, "popups", windows);
4335 }
4336#endif
4337
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004338#ifdef FEAT_SIGNS
4339 if (buf->b_signlist != NULL)
4340 {
4341 /* List of signs placed in this buffer */
4342 list_T *signs = list_alloc();
4343 if (signs != NULL)
4344 {
4345 get_buffer_signs(buf, signs);
4346 dict_add_list(dict, "signs", signs);
4347 }
4348 }
4349#endif
4350
4351 return dict;
4352}
4353
4354/*
4355 * "getbufinfo()" function
4356 */
4357 static void
4358f_getbufinfo(typval_T *argvars, typval_T *rettv)
4359{
4360 buf_T *buf = NULL;
4361 buf_T *argbuf = NULL;
4362 dict_T *d;
4363 int filtered = FALSE;
4364 int sel_buflisted = FALSE;
4365 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004366 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004367
4368 if (rettv_list_alloc(rettv) != OK)
4369 return;
4370
4371 /* List of all the buffers or selected buffers */
4372 if (argvars[0].v_type == VAR_DICT)
4373 {
4374 dict_T *sel_d = argvars[0].vval.v_dict;
4375
4376 if (sel_d != NULL)
4377 {
4378 dictitem_T *di;
4379
4380 filtered = TRUE;
4381
4382 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004383 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004384 sel_buflisted = TRUE;
4385
4386 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004387 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004388 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004389
4390 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004391 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004392 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004393 }
4394 }
4395 else if (argvars[0].v_type != VAR_UNKNOWN)
4396 {
4397 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004398 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004399 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004400 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004401 --emsg_off;
4402 if (argbuf == NULL)
4403 return;
4404 }
4405
4406 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004407 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004408 {
4409 if (argbuf != NULL && argbuf != buf)
4410 continue;
4411 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004412 || (sel_buflisted && !buf->b_p_bl)
4413 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004414 continue;
4415
4416 d = get_buffer_info(buf);
4417 if (d != NULL)
4418 list_append_dict(rettv->vval.v_list, d);
4419 if (argbuf != NULL)
4420 return;
4421 }
4422}
4423
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424/*
4425 * Get line or list of lines from buffer "buf" into "rettv".
4426 * Return a range (from start to end) of lines in rettv from the specified
4427 * buffer.
4428 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4429 */
4430 static void
4431get_buffer_lines(
4432 buf_T *buf,
4433 linenr_T start,
4434 linenr_T end,
4435 int retlist,
4436 typval_T *rettv)
4437{
4438 char_u *p;
4439
4440 rettv->v_type = VAR_STRING;
4441 rettv->vval.v_string = NULL;
4442 if (retlist && rettv_list_alloc(rettv) == FAIL)
4443 return;
4444
4445 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4446 return;
4447
4448 if (!retlist)
4449 {
4450 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4451 p = ml_get_buf(buf, start, FALSE);
4452 else
4453 p = (char_u *)"";
4454 rettv->vval.v_string = vim_strsave(p);
4455 }
4456 else
4457 {
4458 if (end < start)
4459 return;
4460
4461 if (start < 1)
4462 start = 1;
4463 if (end > buf->b_ml.ml_line_count)
4464 end = buf->b_ml.ml_line_count;
4465 while (start <= end)
4466 if (list_append_string(rettv->vval.v_list,
4467 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4468 break;
4469 }
4470}
4471
4472/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004473 * "getbufline()" function
4474 */
4475 static void
4476f_getbufline(typval_T *argvars, typval_T *rettv)
4477{
4478 linenr_T lnum;
4479 linenr_T end;
4480 buf_T *buf;
4481
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004482 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004483 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004484 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485 --emsg_off;
4486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004487 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 if (argvars[2].v_type == VAR_UNKNOWN)
4489 end = lnum;
4490 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004491 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004492
4493 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4494}
4495
4496/*
4497 * "getbufvar()" function
4498 */
4499 static void
4500f_getbufvar(typval_T *argvars, typval_T *rettv)
4501{
4502 buf_T *buf;
4503 buf_T *save_curbuf;
4504 char_u *varname;
4505 dictitem_T *v;
4506 int done = FALSE;
4507
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004508 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4509 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004511 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512
4513 rettv->v_type = VAR_STRING;
4514 rettv->vval.v_string = NULL;
4515
4516 if (buf != NULL && varname != NULL)
4517 {
4518 /* set curbuf to be our buf, temporarily */
4519 save_curbuf = curbuf;
4520 curbuf = buf;
4521
Bram Moolenaar30567352016-08-27 21:25:44 +02004522 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004524 if (varname[1] == NUL)
4525 {
4526 /* get all buffer-local options in a dict */
4527 dict_T *opts = get_winbuf_options(TRUE);
4528
4529 if (opts != NULL)
4530 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004531 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004532 done = TRUE;
4533 }
4534 }
4535 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4536 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004537 done = TRUE;
4538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539 else
4540 {
4541 /* Look up the variable. */
4542 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4543 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4544 'b', varname, FALSE);
4545 if (v != NULL)
4546 {
4547 copy_tv(&v->di_tv, rettv);
4548 done = TRUE;
4549 }
4550 }
4551
4552 /* restore previous notion of curbuf */
4553 curbuf = save_curbuf;
4554 }
4555
4556 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4557 /* use the default value */
4558 copy_tv(&argvars[2], rettv);
4559
4560 --emsg_off;
4561}
4562
4563/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004564 * "getchangelist()" function
4565 */
4566 static void
4567f_getchangelist(typval_T *argvars, typval_T *rettv)
4568{
4569#ifdef FEAT_JUMPLIST
4570 buf_T *buf;
4571 int i;
4572 list_T *l;
4573 dict_T *d;
4574#endif
4575
4576 if (rettv_list_alloc(rettv) != OK)
4577 return;
4578
4579#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004580 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004581 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004582 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004583 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004584 if (buf == NULL)
4585 return;
4586
4587 l = list_alloc();
4588 if (l == NULL)
4589 return;
4590
4591 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4592 return;
4593 /*
4594 * The current window change list index tracks only the position in the
4595 * current buffer change list. For other buffers, use the change list
4596 * length as the current index.
4597 */
4598 list_append_number(rettv->vval.v_list,
4599 (varnumber_T)((buf == curwin->w_buffer)
4600 ? curwin->w_changelistidx : buf->b_changelistlen));
4601
4602 for (i = 0; i < buf->b_changelistlen; ++i)
4603 {
4604 if (buf->b_changelist[i].lnum == 0)
4605 continue;
4606 if ((d = dict_alloc()) == NULL)
4607 return;
4608 if (list_append_dict(l, d) == FAIL)
4609 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004610 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4611 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004612 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004613 }
4614#endif
4615}
4616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617 * "getchar()" function
4618 */
4619 static void
4620f_getchar(typval_T *argvars, typval_T *rettv)
4621{
4622 varnumber_T n;
4623 int error = FALSE;
4624
Bram Moolenaar84d93902018-09-11 20:10:20 +02004625#ifdef MESSAGE_QUEUE
4626 // vpeekc() used to check for messages, but that caused problems, invoking
4627 // a callback where it was not expected. Some plugins use getchar(1) in a
4628 // loop to await a message, therefore make sure we check for messages here.
4629 parse_queued_messages();
4630#endif
4631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632 /* Position the cursor. Needed after a message that ends in a space. */
4633 windgoto(msg_row, msg_col);
4634
4635 ++no_mapping;
4636 ++allow_keys;
4637 for (;;)
4638 {
4639 if (argvars[0].v_type == VAR_UNKNOWN)
4640 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004641 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004643 /* getchar(1): only check if char avail */
4644 n = vpeekc_any();
4645 else if (error || vpeekc_any() == NUL)
4646 /* illegal argument or getchar(0) and no char avail: return zero */
4647 n = 0;
4648 else
4649 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004650 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004651
4652 if (n == K_IGNORE)
4653 continue;
4654 break;
4655 }
4656 --no_mapping;
4657 --allow_keys;
4658
4659 set_vim_var_nr(VV_MOUSE_WIN, 0);
4660 set_vim_var_nr(VV_MOUSE_WINID, 0);
4661 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4662 set_vim_var_nr(VV_MOUSE_COL, 0);
4663
4664 rettv->vval.v_number = n;
4665 if (IS_SPECIAL(n) || mod_mask != 0)
4666 {
4667 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4668 int i = 0;
4669
4670 /* Turn a special key into three bytes, plus modifier. */
4671 if (mod_mask != 0)
4672 {
4673 temp[i++] = K_SPECIAL;
4674 temp[i++] = KS_MODIFIER;
4675 temp[i++] = mod_mask;
4676 }
4677 if (IS_SPECIAL(n))
4678 {
4679 temp[i++] = K_SPECIAL;
4680 temp[i++] = K_SECOND(n);
4681 temp[i++] = K_THIRD(n);
4682 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 else if (has_mbyte)
4684 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 else
4686 temp[i++] = n;
4687 temp[i++] = NUL;
4688 rettv->v_type = VAR_STRING;
4689 rettv->vval.v_string = vim_strsave(temp);
4690
4691#ifdef FEAT_MOUSE
4692 if (is_mouse_key(n))
4693 {
4694 int row = mouse_row;
4695 int col = mouse_col;
4696 win_T *win;
4697 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699 int winnr = 1;
4700
4701 if (row >= 0 && col >= 0)
4702 {
4703 /* Find the window at the mouse coordinates and compute the
4704 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004705 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004706 if (win == NULL)
4707 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004708 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004709# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004710 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004711 winnr = 0;
4712 else
4713# endif
4714 for (wp = firstwin; wp != win && wp != NULL;
4715 wp = wp->w_next)
4716 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4718 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4719 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4720 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4721 }
4722 }
4723#endif
4724 }
4725}
4726
4727/*
4728 * "getcharmod()" function
4729 */
4730 static void
4731f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4732{
4733 rettv->vval.v_number = mod_mask;
4734}
4735
4736/*
4737 * "getcharsearch()" function
4738 */
4739 static void
4740f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4741{
4742 if (rettv_dict_alloc(rettv) != FAIL)
4743 {
4744 dict_T *dict = rettv->vval.v_dict;
4745
Bram Moolenaare0be1672018-07-08 16:50:37 +02004746 dict_add_string(dict, "char", last_csearch());
4747 dict_add_number(dict, "forward", last_csearch_forward());
4748 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 }
4750}
4751
4752/*
4753 * "getcmdline()" function
4754 */
4755 static void
4756f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4757{
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = get_cmdline_str();
4760}
4761
4762/*
4763 * "getcmdpos()" function
4764 */
4765 static void
4766f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4767{
4768 rettv->vval.v_number = get_cmdline_pos() + 1;
4769}
4770
4771/*
4772 * "getcmdtype()" function
4773 */
4774 static void
4775f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4776{
4777 rettv->v_type = VAR_STRING;
4778 rettv->vval.v_string = alloc(2);
4779 if (rettv->vval.v_string != NULL)
4780 {
4781 rettv->vval.v_string[0] = get_cmdline_type();
4782 rettv->vval.v_string[1] = NUL;
4783 }
4784}
4785
4786/*
4787 * "getcmdwintype()" function
4788 */
4789 static void
4790f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4791{
4792 rettv->v_type = VAR_STRING;
4793 rettv->vval.v_string = NULL;
4794#ifdef FEAT_CMDWIN
4795 rettv->vval.v_string = alloc(2);
4796 if (rettv->vval.v_string != NULL)
4797 {
4798 rettv->vval.v_string[0] = cmdwin_type;
4799 rettv->vval.v_string[1] = NUL;
4800 }
4801#endif
4802}
4803
4804#if defined(FEAT_CMDL_COMPL)
4805/*
4806 * "getcompletion()" function
4807 */
4808 static void
4809f_getcompletion(typval_T *argvars, typval_T *rettv)
4810{
4811 char_u *pat;
4812 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004813 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004814 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4815 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004816
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004817 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004818 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004820 if (p_wic)
4821 options |= WILD_ICASE;
4822
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004823 /* For filtered results, 'wildignore' is used */
4824 if (!filtered)
4825 options |= WILD_KEEP_ALL;
4826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004827 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004828 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004830 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004831 if (xpc.xp_context == EXPAND_NOTHING)
4832 {
4833 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004834 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004835 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837 return;
4838 }
4839
4840# if defined(FEAT_MENU)
4841 if (xpc.xp_context == EXPAND_MENUS)
4842 {
4843 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4844 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4845 }
4846# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004847#ifdef FEAT_CSCOPE
4848 if (xpc.xp_context == EXPAND_CSCOPE)
4849 {
4850 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4851 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4852 }
4853#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004854#ifdef FEAT_SIGNS
4855 if (xpc.xp_context == EXPAND_SIGN)
4856 {
4857 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4858 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4859 }
4860#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861
4862 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4863 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4864 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004865 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004866
4867 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4868
4869 for (i = 0; i < xpc.xp_numfiles; i++)
4870 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4871 }
4872 vim_free(pat);
4873 ExpandCleanup(&xpc);
4874}
4875#endif
4876
4877/*
4878 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004879 *
4880 * Return the current working directory of a window in a tab page.
4881 * First optional argument 'winnr' is the window number or -1 and the second
4882 * optional argument 'tabnr' is the tab page number.
4883 *
4884 * If no arguments are supplied, then return the directory of the current
4885 * window.
4886 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4887 * the specified window.
4888 * If 'winnr' is 0 then return the directory of the current window.
4889 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4890 * directory of the specified tab page. Otherwise return the directory of the
4891 * specified window in the specified tab page.
4892 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893 */
4894 static void
4895f_getcwd(typval_T *argvars, typval_T *rettv)
4896{
4897 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004898 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004900 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901
4902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = NULL;
4904
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004905 if (argvars[0].v_type == VAR_NUMBER
4906 && argvars[0].vval.v_number == -1
4907 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004908 global = TRUE;
4909 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004910 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004911
4912 if (wp != NULL && wp->w_localdir != NULL)
4913 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004914 else if (tp != NULL && tp->tp_localdir != NULL)
4915 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4916 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004918 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004919 rettv->vval.v_string = vim_strsave(globaldir);
4920 else
4921 {
4922 cwd = alloc(MAXPATHL);
4923 if (cwd != NULL)
4924 {
4925 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4926 rettv->vval.v_string = vim_strsave(cwd);
4927 vim_free(cwd);
4928 }
4929 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004930 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004931#ifdef BACKSLASH_IN_FILENAME
4932 if (rettv->vval.v_string != NULL)
4933 slash_adjust(rettv->vval.v_string);
4934#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935}
4936
4937/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004938 * "getenv()" function
4939 */
4940 static void
4941f_getenv(typval_T *argvars, typval_T *rettv)
4942{
4943 int mustfree = FALSE;
4944 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4945
4946 if (p == NULL)
4947 {
4948 rettv->v_type = VAR_SPECIAL;
4949 rettv->vval.v_number = VVAL_NULL;
4950 return;
4951 }
4952 if (!mustfree)
4953 p = vim_strsave(p);
4954 rettv->vval.v_string = p;
4955 rettv->v_type = VAR_STRING;
4956}
4957
4958/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004959 * "getfontname()" function
4960 */
4961 static void
4962f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4963{
4964 rettv->v_type = VAR_STRING;
4965 rettv->vval.v_string = NULL;
4966#ifdef FEAT_GUI
4967 if (gui.in_use)
4968 {
4969 GuiFont font;
4970 char_u *name = NULL;
4971
4972 if (argvars[0].v_type == VAR_UNKNOWN)
4973 {
4974 /* Get the "Normal" font. Either the name saved by
4975 * hl_set_font_name() or from the font ID. */
4976 font = gui.norm_font;
4977 name = hl_get_font_name();
4978 }
4979 else
4980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004982 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4983 return;
4984 font = gui_mch_get_font(name, FALSE);
4985 if (font == NOFONT)
4986 return; /* Invalid font name, return empty string. */
4987 }
4988 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4989 if (argvars[0].v_type != VAR_UNKNOWN)
4990 gui_mch_free_font(font);
4991 }
4992#endif
4993}
4994
4995/*
4996 * "getfperm({fname})" function
4997 */
4998 static void
4999f_getfperm(typval_T *argvars, typval_T *rettv)
5000{
5001 char_u *fname;
5002 stat_T st;
5003 char_u *perm = NULL;
5004 char_u flags[] = "rwx";
5005 int i;
5006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005007 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008
5009 rettv->v_type = VAR_STRING;
5010 if (mch_stat((char *)fname, &st) >= 0)
5011 {
5012 perm = vim_strsave((char_u *)"---------");
5013 if (perm != NULL)
5014 {
5015 for (i = 0; i < 9; i++)
5016 {
5017 if (st.st_mode & (1 << (8 - i)))
5018 perm[i] = flags[i % 3];
5019 }
5020 }
5021 }
5022 rettv->vval.v_string = perm;
5023}
5024
5025/*
5026 * "getfsize({fname})" function
5027 */
5028 static void
5029f_getfsize(typval_T *argvars, typval_T *rettv)
5030{
5031 char_u *fname;
5032 stat_T st;
5033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035
5036 rettv->v_type = VAR_NUMBER;
5037
5038 if (mch_stat((char *)fname, &st) >= 0)
5039 {
5040 if (mch_isdir(fname))
5041 rettv->vval.v_number = 0;
5042 else
5043 {
5044 rettv->vval.v_number = (varnumber_T)st.st_size;
5045
5046 /* non-perfect check for overflow */
5047 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5048 rettv->vval.v_number = -2;
5049 }
5050 }
5051 else
5052 rettv->vval.v_number = -1;
5053}
5054
5055/*
5056 * "getftime({fname})" function
5057 */
5058 static void
5059f_getftime(typval_T *argvars, typval_T *rettv)
5060{
5061 char_u *fname;
5062 stat_T st;
5063
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005064 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 if (mch_stat((char *)fname, &st) >= 0)
5067 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5068 else
5069 rettv->vval.v_number = -1;
5070}
5071
5072/*
5073 * "getftype({fname})" function
5074 */
5075 static void
5076f_getftype(typval_T *argvars, typval_T *rettv)
5077{
5078 char_u *fname;
5079 stat_T st;
5080 char_u *type = NULL;
5081 char *t;
5082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005083 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084
5085 rettv->v_type = VAR_STRING;
5086 if (mch_lstat((char *)fname, &st) >= 0)
5087 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005088 if (S_ISREG(st.st_mode))
5089 t = "file";
5090 else if (S_ISDIR(st.st_mode))
5091 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 else if (S_ISLNK(st.st_mode))
5093 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005094 else if (S_ISBLK(st.st_mode))
5095 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096 else if (S_ISCHR(st.st_mode))
5097 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098 else if (S_ISFIFO(st.st_mode))
5099 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005101 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102 else
5103 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 type = vim_strsave((char_u *)t);
5105 }
5106 rettv->vval.v_string = type;
5107}
5108
5109/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005110 * "getjumplist()" function
5111 */
5112 static void
5113f_getjumplist(typval_T *argvars, typval_T *rettv)
5114{
5115#ifdef FEAT_JUMPLIST
5116 win_T *wp;
5117 int i;
5118 list_T *l;
5119 dict_T *d;
5120#endif
5121
5122 if (rettv_list_alloc(rettv) != OK)
5123 return;
5124
5125#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005126 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005127 if (wp == NULL)
5128 return;
5129
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005130 cleanup_jumplist(wp, TRUE);
5131
Bram Moolenaar4f505882018-02-10 21:06:32 +01005132 l = list_alloc();
5133 if (l == NULL)
5134 return;
5135
5136 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5137 return;
5138 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5139
5140 for (i = 0; i < wp->w_jumplistlen; ++i)
5141 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005142 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5143 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005144 if ((d = dict_alloc()) == NULL)
5145 return;
5146 if (list_append_dict(l, d) == FAIL)
5147 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005148 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5149 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005150 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005151 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005152 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005153 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005154 }
5155#endif
5156}
5157
5158/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159 * "getline(lnum, [end])" function
5160 */
5161 static void
5162f_getline(typval_T *argvars, typval_T *rettv)
5163{
5164 linenr_T lnum;
5165 linenr_T end;
5166 int retlist;
5167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 if (argvars[1].v_type == VAR_UNKNOWN)
5170 {
5171 end = 0;
5172 retlist = FALSE;
5173 }
5174 else
5175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005176 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 retlist = TRUE;
5178 }
5179
5180 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5181}
5182
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005183#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005184 static void
5185get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5186{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005187 if (what_arg->v_type == VAR_UNKNOWN)
5188 {
5189 if (rettv_list_alloc(rettv) == OK)
5190 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005191 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005192 }
5193 else
5194 {
5195 if (rettv_dict_alloc(rettv) == OK)
5196 if (is_qf || (wp != NULL))
5197 {
5198 if (what_arg->v_type == VAR_DICT)
5199 {
5200 dict_T *d = what_arg->vval.v_dict;
5201
5202 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005203 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005204 }
5205 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005206 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005207 }
5208 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005209}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005210#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005211
5212/*
5213 * "getloclist()" function
5214 */
5215 static void
5216f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5217{
5218#ifdef FEAT_QUICKFIX
5219 win_T *wp;
5220
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005221 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005222 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5223#endif
5224}
5225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005227 * "getpid()" function
5228 */
5229 static void
5230f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5231{
5232 rettv->vval.v_number = mch_get_pid();
5233}
5234
5235 static void
5236getpos_both(
5237 typval_T *argvars,
5238 typval_T *rettv,
5239 int getcurpos)
5240{
5241 pos_T *fp;
5242 list_T *l;
5243 int fnum = -1;
5244
5245 if (rettv_list_alloc(rettv) == OK)
5246 {
5247 l = rettv->vval.v_list;
5248 if (getcurpos)
5249 fp = &curwin->w_cursor;
5250 else
5251 fp = var2fpos(&argvars[0], TRUE, &fnum);
5252 if (fnum != -1)
5253 list_append_number(l, (varnumber_T)fnum);
5254 else
5255 list_append_number(l, (varnumber_T)0);
5256 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5257 : (varnumber_T)0);
5258 list_append_number(l, (fp != NULL)
5259 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5260 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005261 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262 (varnumber_T)0);
5263 if (getcurpos)
5264 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005265 int save_set_curswant = curwin->w_set_curswant;
5266 colnr_T save_curswant = curwin->w_curswant;
5267 colnr_T save_virtcol = curwin->w_virtcol;
5268
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269 update_curswant();
5270 list_append_number(l, curwin->w_curswant == MAXCOL ?
5271 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005272
5273 // Do not change "curswant", as it is unexpected that a get
5274 // function has a side effect.
5275 if (save_set_curswant)
5276 {
5277 curwin->w_set_curswant = save_set_curswant;
5278 curwin->w_curswant = save_curswant;
5279 curwin->w_virtcol = save_virtcol;
5280 curwin->w_valid &= ~VALID_VIRTCOL;
5281 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 }
5283 }
5284 else
5285 rettv->vval.v_number = FALSE;
5286}
5287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288/*
5289 * "getcurpos()" function
5290 */
5291 static void
5292f_getcurpos(typval_T *argvars, typval_T *rettv)
5293{
5294 getpos_both(argvars, rettv, TRUE);
5295}
5296
5297/*
5298 * "getpos(string)" function
5299 */
5300 static void
5301f_getpos(typval_T *argvars, typval_T *rettv)
5302{
5303 getpos_both(argvars, rettv, FALSE);
5304}
5305
5306/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005307 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 */
5309 static void
5310f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5311{
5312#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005313 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314#endif
5315}
5316
5317/*
5318 * "getreg()" function
5319 */
5320 static void
5321f_getreg(typval_T *argvars, typval_T *rettv)
5322{
5323 char_u *strregname;
5324 int regname;
5325 int arg2 = FALSE;
5326 int return_list = FALSE;
5327 int error = FALSE;
5328
5329 if (argvars[0].v_type != VAR_UNKNOWN)
5330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005331 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332 error = strregname == NULL;
5333 if (argvars[1].v_type != VAR_UNKNOWN)
5334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005335 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005337 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338 }
5339 }
5340 else
5341 strregname = get_vim_var_str(VV_REG);
5342
5343 if (error)
5344 return;
5345
5346 regname = (strregname == NULL ? '"' : *strregname);
5347 if (regname == 0)
5348 regname = '"';
5349
5350 if (return_list)
5351 {
5352 rettv->v_type = VAR_LIST;
5353 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5354 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5355 if (rettv->vval.v_list == NULL)
5356 (void)rettv_list_alloc(rettv);
5357 else
5358 ++rettv->vval.v_list->lv_refcount;
5359 }
5360 else
5361 {
5362 rettv->v_type = VAR_STRING;
5363 rettv->vval.v_string = get_reg_contents(regname,
5364 arg2 ? GREG_EXPR_SRC : 0);
5365 }
5366}
5367
5368/*
5369 * "getregtype()" function
5370 */
5371 static void
5372f_getregtype(typval_T *argvars, typval_T *rettv)
5373{
5374 char_u *strregname;
5375 int regname;
5376 char_u buf[NUMBUFLEN + 2];
5377 long reglen = 0;
5378
5379 if (argvars[0].v_type != VAR_UNKNOWN)
5380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005381 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 if (strregname == NULL) /* type error; errmsg already given */
5383 {
5384 rettv->v_type = VAR_STRING;
5385 rettv->vval.v_string = NULL;
5386 return;
5387 }
5388 }
5389 else
5390 /* Default to v:register */
5391 strregname = get_vim_var_str(VV_REG);
5392
5393 regname = (strregname == NULL ? '"' : *strregname);
5394 if (regname == 0)
5395 regname = '"';
5396
5397 buf[0] = NUL;
5398 buf[1] = NUL;
5399 switch (get_reg_type(regname, &reglen))
5400 {
5401 case MLINE: buf[0] = 'V'; break;
5402 case MCHAR: buf[0] = 'v'; break;
5403 case MBLOCK:
5404 buf[0] = Ctrl_V;
5405 sprintf((char *)buf + 1, "%ld", reglen + 1);
5406 break;
5407 }
5408 rettv->v_type = VAR_STRING;
5409 rettv->vval.v_string = vim_strsave(buf);
5410}
5411
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005412/*
5413 * Returns information (variables, options, etc.) about a tab page
5414 * as a dictionary.
5415 */
5416 static dict_T *
5417get_tabpage_info(tabpage_T *tp, int tp_idx)
5418{
5419 win_T *wp;
5420 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005421 list_T *l;
5422
5423 dict = dict_alloc();
5424 if (dict == NULL)
5425 return NULL;
5426
Bram Moolenaare0be1672018-07-08 16:50:37 +02005427 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005428
5429 l = list_alloc();
5430 if (l != NULL)
5431 {
5432 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005433 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005434 list_append_number(l, (varnumber_T)wp->w_id);
5435 dict_add_list(dict, "windows", l);
5436 }
5437
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005438 /* Make a reference to tabpage variables */
5439 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005440
5441 return dict;
5442}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005443
5444/*
5445 * "gettabinfo()" function
5446 */
5447 static void
5448f_gettabinfo(typval_T *argvars, typval_T *rettv)
5449{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005450 tabpage_T *tp, *tparg = NULL;
5451 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005452 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005453
5454 if (rettv_list_alloc(rettv) != OK)
5455 return;
5456
5457 if (argvars[0].v_type != VAR_UNKNOWN)
5458 {
5459 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005460 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005461 if (tparg == NULL)
5462 return;
5463 }
5464
5465 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005466 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005467 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005468 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005469 if (tparg != NULL && tp != tparg)
5470 continue;
5471 d = get_tabpage_info(tp, tpnr);
5472 if (d != NULL)
5473 list_append_dict(rettv->vval.v_list, d);
5474 if (tparg != NULL)
5475 return;
5476 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005477}
5478
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479/*
5480 * "gettabvar()" function
5481 */
5482 static void
5483f_gettabvar(typval_T *argvars, typval_T *rettv)
5484{
5485 win_T *oldcurwin;
5486 tabpage_T *tp, *oldtabpage;
5487 dictitem_T *v;
5488 char_u *varname;
5489 int done = FALSE;
5490
5491 rettv->v_type = VAR_STRING;
5492 rettv->vval.v_string = NULL;
5493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005494 varname = tv_get_string_chk(&argvars[1]);
5495 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 if (tp != NULL && varname != NULL)
5497 {
5498 /* Set tp to be our tabpage, temporarily. Also set the window to the
5499 * first window in the tabpage, otherwise the window is not valid. */
5500 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005501 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5502 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503 {
5504 /* look up the variable */
5505 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5506 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5507 if (v != NULL)
5508 {
5509 copy_tv(&v->di_tv, rettv);
5510 done = TRUE;
5511 }
5512 }
5513
5514 /* restore previous notion of curwin */
5515 restore_win(oldcurwin, oldtabpage, TRUE);
5516 }
5517
5518 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5519 copy_tv(&argvars[2], rettv);
5520}
5521
5522/*
5523 * "gettabwinvar()" function
5524 */
5525 static void
5526f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5527{
5528 getwinvar(argvars, rettv, 1);
5529}
5530
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005531/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005532 * "gettagstack()" function
5533 */
5534 static void
5535f_gettagstack(typval_T *argvars, typval_T *rettv)
5536{
5537 win_T *wp = curwin; // default is current window
5538
5539 if (rettv_dict_alloc(rettv) != OK)
5540 return;
5541
5542 if (argvars[0].v_type != VAR_UNKNOWN)
5543 {
5544 wp = find_win_by_nr_or_id(&argvars[0]);
5545 if (wp == NULL)
5546 return;
5547 }
5548
5549 get_tagstack(wp, rettv->vval.v_dict);
5550}
5551
5552/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005553 * Returns information about a window as a dictionary.
5554 */
5555 static dict_T *
5556get_win_info(win_T *wp, short tpnr, short winnr)
5557{
5558 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005559
5560 dict = dict_alloc();
5561 if (dict == NULL)
5562 return NULL;
5563
Bram Moolenaare0be1672018-07-08 16:50:37 +02005564 dict_add_number(dict, "tabnr", tpnr);
5565 dict_add_number(dict, "winnr", winnr);
5566 dict_add_number(dict, "winid", wp->w_id);
5567 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005568 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005569 dict_add_number(dict, "topline", wp->w_topline);
5570 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005571#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005572 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005573#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005574 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005575 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005576 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005577
Bram Moolenaar69905d12017-08-13 18:14:47 +02005578#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005579 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005580#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005581#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005582 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5583 dict_add_number(dict, "loclist",
5584 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005585#endif
5586
Bram Moolenaar30567352016-08-27 21:25:44 +02005587 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005588 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005589
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005590 return dict;
5591}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005592
5593/*
5594 * "getwininfo()" function
5595 */
5596 static void
5597f_getwininfo(typval_T *argvars, typval_T *rettv)
5598{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005599 tabpage_T *tp;
5600 win_T *wp = NULL, *wparg = NULL;
5601 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005602 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005603
5604 if (rettv_list_alloc(rettv) != OK)
5605 return;
5606
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005607 if (argvars[0].v_type != VAR_UNKNOWN)
5608 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005609 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005610 if (wparg == NULL)
5611 return;
5612 }
5613
5614 /* Collect information about either all the windows across all the tab
5615 * pages or one particular window.
5616 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005617 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005618 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005619 tabnr++;
5620 winnr = 0;
5621 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005623 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005624 if (wparg != NULL && wp != wparg)
5625 continue;
5626 d = get_win_info(wp, tabnr, winnr);
5627 if (d != NULL)
5628 list_append_dict(rettv->vval.v_list, d);
5629 if (wparg != NULL)
5630 /* found information about a specific window */
5631 return;
5632 }
5633 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005634}
5635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005637 * "win_execute()" function
5638 */
5639 static void
5640f_win_execute(typval_T *argvars, typval_T *rettv)
5641{
5642 int id = (int)tv_get_number(argvars);
Bram Moolenaar820680b2019-08-09 14:56:22 +02005643 tabpage_T *tp;
5644 win_T *wp = win_id2wp_tp(id, &tp);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005645 win_T *save_curwin;
5646 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005647
Bram Moolenaar820680b2019-08-09 14:56:22 +02005648 if (wp != NULL && tp != NULL)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005649 {
Bram Moolenaar820680b2019-08-09 14:56:22 +02005650 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005651 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005652 check_cursor();
5653 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005654 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005655 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005656 }
5657}
5658
5659/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660 * "win_findbuf()" function
5661 */
5662 static void
5663f_win_findbuf(typval_T *argvars, typval_T *rettv)
5664{
5665 if (rettv_list_alloc(rettv) != FAIL)
5666 win_findbuf(argvars, rettv->vval.v_list);
5667}
5668
5669/*
5670 * "win_getid()" function
5671 */
5672 static void
5673f_win_getid(typval_T *argvars, typval_T *rettv)
5674{
5675 rettv->vval.v_number = win_getid(argvars);
5676}
5677
5678/*
5679 * "win_gotoid()" function
5680 */
5681 static void
5682f_win_gotoid(typval_T *argvars, typval_T *rettv)
5683{
5684 rettv->vval.v_number = win_gotoid(argvars);
5685}
5686
5687/*
5688 * "win_id2tabwin()" function
5689 */
5690 static void
5691f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5692{
5693 if (rettv_list_alloc(rettv) != FAIL)
5694 win_id2tabwin(argvars, rettv->vval.v_list);
5695}
5696
5697/*
5698 * "win_id2win()" function
5699 */
5700 static void
5701f_win_id2win(typval_T *argvars, typval_T *rettv)
5702{
5703 rettv->vval.v_number = win_id2win(argvars);
5704}
5705
5706/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005707 * "win_screenpos()" function
5708 */
5709 static void
5710f_win_screenpos(typval_T *argvars, typval_T *rettv)
5711{
5712 win_T *wp;
5713
5714 if (rettv_list_alloc(rettv) == FAIL)
5715 return;
5716
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005717 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005718 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5719 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5720}
5721
5722/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005723 * "getwinpos({timeout})" function
5724 */
5725 static void
5726f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5727{
5728 int x = -1;
5729 int y = -1;
5730
5731 if (rettv_list_alloc(rettv) == FAIL)
5732 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005733#if defined(FEAT_GUI) \
5734 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5735 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005736 {
5737 varnumber_T timeout = 100;
5738
5739 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005740 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005741
5742 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005743 }
5744#endif
5745 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5746 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5747}
5748
5749
5750/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005751 * "getwinposx()" function
5752 */
5753 static void
5754f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005757#if defined(FEAT_GUI) \
5758 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5759 || defined(MSWIN)
5760
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005761 {
5762 int x, y;
5763
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005764 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005765 rettv->vval.v_number = x;
5766 }
5767#endif
5768}
5769
5770/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 * "getwinposy()" function
5772 */
5773 static void
5774f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5775{
5776 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005777#if defined(FEAT_GUI) \
5778 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5779 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 {
5781 int x, y;
5782
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005783 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005784 rettv->vval.v_number = y;
5785 }
5786#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005787}
5788
5789/*
5790 * "getwinvar()" function
5791 */
5792 static void
5793f_getwinvar(typval_T *argvars, typval_T *rettv)
5794{
5795 getwinvar(argvars, rettv, 0);
5796}
5797
5798/*
5799 * "glob()" function
5800 */
5801 static void
5802f_glob(typval_T *argvars, typval_T *rettv)
5803{
5804 int options = WILD_SILENT|WILD_USE_NL;
5805 expand_T xpc;
5806 int error = FALSE;
5807
5808 /* When the optional second argument is non-zero, don't remove matches
5809 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5810 rettv->v_type = VAR_STRING;
5811 if (argvars[1].v_type != VAR_UNKNOWN)
5812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005813 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 options |= WILD_KEEP_ALL;
5815 if (argvars[2].v_type != VAR_UNKNOWN)
5816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005817 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005818 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005819 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005820 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 options |= WILD_ALLLINKS;
5822 }
5823 }
5824 if (!error)
5825 {
5826 ExpandInit(&xpc);
5827 xpc.xp_context = EXPAND_FILES;
5828 if (p_wic)
5829 options += WILD_ICASE;
5830 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 NULL, options, WILD_ALL);
5833 else if (rettv_list_alloc(rettv) != FAIL)
5834 {
5835 int i;
5836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 NULL, options, WILD_ALL_KEEP);
5839 for (i = 0; i < xpc.xp_numfiles; i++)
5840 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5841
5842 ExpandCleanup(&xpc);
5843 }
5844 }
5845 else
5846 rettv->vval.v_string = NULL;
5847}
5848
5849/*
5850 * "globpath()" function
5851 */
5852 static void
5853f_globpath(typval_T *argvars, typval_T *rettv)
5854{
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005855 int flags = WILD_IGNORE_COMPLETESLASH;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 int error = FALSE;
5859 garray_T ga;
5860 int i;
5861
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005862 // When the optional second argument is non-zero, don't remove matches
5863 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 rettv->v_type = VAR_STRING;
5865 if (argvars[2].v_type != VAR_UNKNOWN)
5866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005867 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 flags |= WILD_KEEP_ALL;
5869 if (argvars[3].v_type != VAR_UNKNOWN)
5870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005871 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005872 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005874 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005875 flags |= WILD_ALLLINKS;
5876 }
5877 }
5878 if (file != NULL && !error)
5879 {
5880 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 if (rettv->v_type == VAR_STRING)
5883 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5884 else if (rettv_list_alloc(rettv) != FAIL)
5885 for (i = 0; i < ga.ga_len; ++i)
5886 list_append_string(rettv->vval.v_list,
5887 ((char_u **)(ga.ga_data))[i], -1);
5888 ga_clear_strings(&ga);
5889 }
5890 else
5891 rettv->vval.v_string = NULL;
5892}
5893
5894/*
5895 * "glob2regpat()" function
5896 */
5897 static void
5898f_glob2regpat(typval_T *argvars, typval_T *rettv)
5899{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005900 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901
5902 rettv->v_type = VAR_STRING;
5903 rettv->vval.v_string = (pat == NULL)
5904 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5905}
5906
5907/* for VIM_VERSION_ defines */
5908#include "version.h"
5909
5910/*
5911 * "has()" function
5912 */
5913 static void
5914f_has(typval_T *argvars, typval_T *rettv)
5915{
5916 int i;
5917 char_u *name;
5918 int n = FALSE;
5919 static char *(has_list[]) =
5920 {
5921#ifdef AMIGA
5922 "amiga",
5923# ifdef FEAT_ARP
5924 "arp",
5925# endif
5926#endif
5927#ifdef __BEOS__
5928 "beos",
5929#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005930#if defined(BSD) && !defined(MACOS_X)
5931 "bsd",
5932#endif
5933#ifdef hpux
5934 "hpux",
5935#endif
5936#ifdef __linux__
5937 "linux",
5938#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005939#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005940 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5941 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005942# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005943 "macunix", /* Mac OS X, with the darwin feature */
5944 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005945# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946#endif
5947#ifdef __QNX__
5948 "qnx",
5949#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005950#ifdef SUN_SYSTEM
5951 "sun",
5952#else
5953 "moon",
5954#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955#ifdef UNIX
5956 "unix",
5957#endif
5958#ifdef VMS
5959 "vms",
5960#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005961#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005962 "win32",
5963#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005964#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 "win32unix",
5966#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005967#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968 "win64",
5969#endif
5970#ifdef EBCDIC
5971 "ebcdic",
5972#endif
5973#ifndef CASE_INSENSITIVE_FILENAME
5974 "fname_case",
5975#endif
5976#ifdef HAVE_ACL
5977 "acl",
5978#endif
5979#ifdef FEAT_ARABIC
5980 "arabic",
5981#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005983#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005984 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005985#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005986#ifdef FEAT_AUTOSERVERNAME
5987 "autoservername",
5988#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005989#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01005991# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005992 "balloon_multiline",
5993# endif
5994#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005995#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005996 "balloon_eval_term",
5997#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005998#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5999 "builtin_terms",
6000# ifdef ALL_BUILTIN_TCAPS
6001 "all_builtin_terms",
6002# endif
6003#endif
6004#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006005 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 || defined(FEAT_GUI_MOTIF))
6007 "browsefilter",
6008#endif
6009#ifdef FEAT_BYTEOFF
6010 "byte_offset",
6011#endif
6012#ifdef FEAT_JOB_CHANNEL
6013 "channel",
6014#endif
6015#ifdef FEAT_CINDENT
6016 "cindent",
6017#endif
6018#ifdef FEAT_CLIENTSERVER
6019 "clientserver",
6020#endif
6021#ifdef FEAT_CLIPBOARD
6022 "clipboard",
6023#endif
6024#ifdef FEAT_CMDL_COMPL
6025 "cmdline_compl",
6026#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 "cmdline_hist",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028#ifdef FEAT_COMMENTS
6029 "comments",
6030#endif
6031#ifdef FEAT_CONCEAL
6032 "conceal",
6033#endif
6034#ifdef FEAT_CRYPT
6035 "cryptv",
6036 "crypt-blowfish",
6037 "crypt-blowfish2",
6038#endif
6039#ifdef FEAT_CSCOPE
6040 "cscope",
6041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043#ifdef CURSOR_SHAPE
6044 "cursorshape",
6045#endif
6046#ifdef DEBUG
6047 "debug",
6048#endif
6049#ifdef FEAT_CON_DIALOG
6050 "dialog_con",
6051#endif
6052#ifdef FEAT_GUI_DIALOG
6053 "dialog_gui",
6054#endif
6055#ifdef FEAT_DIFF
6056 "diff",
6057#endif
6058#ifdef FEAT_DIGRAPHS
6059 "digraphs",
6060#endif
6061#ifdef FEAT_DIRECTX
6062 "directx",
6063#endif
6064#ifdef FEAT_DND
6065 "dnd",
6066#endif
6067#ifdef FEAT_EMACS_TAGS
6068 "emacs_tags",
6069#endif
6070 "eval", /* always present, of course! */
6071 "ex_extra", /* graduated feature */
6072#ifdef FEAT_SEARCH_EXTRA
6073 "extra_search",
6074#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075#ifdef FEAT_SEARCHPATH
6076 "file_in_path",
6077#endif
6078#ifdef FEAT_FILTERPIPE
6079 "filterpipe",
6080#endif
6081#ifdef FEAT_FIND_ID
6082 "find_in_path",
6083#endif
6084#ifdef FEAT_FLOAT
6085 "float",
6086#endif
6087#ifdef FEAT_FOLDING
6088 "folding",
6089#endif
6090#ifdef FEAT_FOOTER
6091 "footer",
6092#endif
6093#if !defined(USE_SYSTEM) && defined(UNIX)
6094 "fork",
6095#endif
6096#ifdef FEAT_GETTEXT
6097 "gettext",
6098#endif
6099#ifdef FEAT_GUI
6100 "gui",
6101#endif
6102#ifdef FEAT_GUI_ATHENA
6103# ifdef FEAT_GUI_NEXTAW
6104 "gui_neXtaw",
6105# else
6106 "gui_athena",
6107# endif
6108#endif
6109#ifdef FEAT_GUI_GTK
6110 "gui_gtk",
6111# ifdef USE_GTK3
6112 "gui_gtk3",
6113# else
6114 "gui_gtk2",
6115# endif
6116#endif
6117#ifdef FEAT_GUI_GNOME
6118 "gui_gnome",
6119#endif
6120#ifdef FEAT_GUI_MAC
6121 "gui_mac",
6122#endif
6123#ifdef FEAT_GUI_MOTIF
6124 "gui_motif",
6125#endif
6126#ifdef FEAT_GUI_PHOTON
6127 "gui_photon",
6128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006129#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 "gui_win32",
6131#endif
6132#ifdef FEAT_HANGULIN
6133 "hangul_input",
6134#endif
6135#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6136 "iconv",
6137#endif
6138#ifdef FEAT_INS_EXPAND
6139 "insert_expand",
6140#endif
6141#ifdef FEAT_JOB_CHANNEL
6142 "job",
6143#endif
6144#ifdef FEAT_JUMPLIST
6145 "jumplist",
6146#endif
6147#ifdef FEAT_KEYMAP
6148 "keymap",
6149#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006150 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006151#ifdef FEAT_LANGMAP
6152 "langmap",
6153#endif
6154#ifdef FEAT_LIBCALL
6155 "libcall",
6156#endif
6157#ifdef FEAT_LINEBREAK
6158 "linebreak",
6159#endif
6160#ifdef FEAT_LISP
6161 "lispindent",
6162#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006163 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 "localmap",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165#ifdef FEAT_LUA
6166# ifndef DYNAMIC_LUA
6167 "lua",
6168# endif
6169#endif
6170#ifdef FEAT_MENU
6171 "menu",
6172#endif
6173#ifdef FEAT_SESSION
6174 "mksession",
6175#endif
6176#ifdef FEAT_MODIFY_FNAME
6177 "modify_fname",
6178#endif
6179#ifdef FEAT_MOUSE
6180 "mouse",
6181#endif
6182#ifdef FEAT_MOUSESHAPE
6183 "mouseshape",
6184#endif
6185#if defined(UNIX) || defined(VMS)
6186# ifdef FEAT_MOUSE_DEC
6187 "mouse_dec",
6188# endif
6189# ifdef FEAT_MOUSE_GPM
6190 "mouse_gpm",
6191# endif
6192# ifdef FEAT_MOUSE_JSB
6193 "mouse_jsbterm",
6194# endif
6195# ifdef FEAT_MOUSE_NET
6196 "mouse_netterm",
6197# endif
6198# ifdef FEAT_MOUSE_PTERM
6199 "mouse_pterm",
6200# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006201# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202 "mouse_sgr",
6203# endif
6204# ifdef FEAT_SYSMOUSE
6205 "mouse_sysmouse",
6206# endif
6207# ifdef FEAT_MOUSE_URXVT
6208 "mouse_urxvt",
6209# endif
6210# ifdef FEAT_MOUSE_XTERM
6211 "mouse_xterm",
6212# endif
6213#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215#ifdef FEAT_MBYTE_IME
6216 "multi_byte_ime",
6217#endif
6218#ifdef FEAT_MULTI_LANG
6219 "multi_lang",
6220#endif
6221#ifdef FEAT_MZSCHEME
6222#ifndef DYNAMIC_MZSCHEME
6223 "mzscheme",
6224#endif
6225#endif
6226#ifdef FEAT_NUM64
6227 "num64",
6228#endif
6229#ifdef FEAT_OLE
6230 "ole",
6231#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006232#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006234#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235#ifdef FEAT_PATH_EXTRA
6236 "path_extra",
6237#endif
6238#ifdef FEAT_PERL
6239#ifndef DYNAMIC_PERL
6240 "perl",
6241#endif
6242#endif
6243#ifdef FEAT_PERSISTENT_UNDO
6244 "persistent_undo",
6245#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006246#if defined(FEAT_PYTHON)
6247 "python_compiled",
6248# if defined(DYNAMIC_PYTHON)
6249 "python_dynamic",
6250# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006252 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006253# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006255#if defined(FEAT_PYTHON3)
6256 "python3_compiled",
6257# if defined(DYNAMIC_PYTHON3)
6258 "python3_dynamic",
6259# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006261 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006262# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263#endif
6264#ifdef FEAT_POSTSCRIPT
6265 "postscript",
6266#endif
6267#ifdef FEAT_PRINTER
6268 "printer",
6269#endif
6270#ifdef FEAT_PROFILE
6271 "profile",
6272#endif
6273#ifdef FEAT_RELTIME
6274 "reltime",
6275#endif
6276#ifdef FEAT_QUICKFIX
6277 "quickfix",
6278#endif
6279#ifdef FEAT_RIGHTLEFT
6280 "rightleft",
6281#endif
6282#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6283 "ruby",
6284#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286#ifdef FEAT_CMDL_INFO
6287 "showcmd",
6288 "cmdline_info",
6289#endif
6290#ifdef FEAT_SIGNS
6291 "signs",
6292#endif
6293#ifdef FEAT_SMARTINDENT
6294 "smartindent",
6295#endif
6296#ifdef STARTUPTIME
6297 "startuptime",
6298#endif
6299#ifdef FEAT_STL_OPT
6300 "statusline",
6301#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006302#ifdef FEAT_NETBEANS_INTG
6303 "netbeans_intg",
6304#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006305#ifdef FEAT_SOUND
6306 "sound",
6307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308#ifdef FEAT_SPELL
6309 "spell",
6310#endif
6311#ifdef FEAT_SYN_HL
6312 "syntax",
6313#endif
6314#if defined(USE_SYSTEM) || !defined(UNIX)
6315 "system",
6316#endif
6317#ifdef FEAT_TAG_BINS
6318 "tag_binary",
6319#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320#ifdef FEAT_TCL
6321# ifndef DYNAMIC_TCL
6322 "tcl",
6323# endif
6324#endif
6325#ifdef FEAT_TERMGUICOLORS
6326 "termguicolors",
6327#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006328#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006329 "terminal",
6330#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006331#ifdef TERMINFO
6332 "terminfo",
6333#endif
6334#ifdef FEAT_TERMRESPONSE
6335 "termresponse",
6336#endif
6337#ifdef FEAT_TEXTOBJ
6338 "textobjects",
6339#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006340#ifdef FEAT_TEXT_PROP
6341 "textprop",
6342#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343#ifdef HAVE_TGETENT
6344 "tgetent",
6345#endif
6346#ifdef FEAT_TIMERS
6347 "timers",
6348#endif
6349#ifdef FEAT_TITLE
6350 "title",
6351#endif
6352#ifdef FEAT_TOOLBAR
6353 "toolbar",
6354#endif
6355#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6356 "unnamedplus",
6357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 "user-commands", /* was accidentally included in 5.4 */
6359 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006360#ifdef FEAT_VARTABS
6361 "vartabs",
6362#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006363 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364#ifdef FEAT_VIMINFO
6365 "viminfo",
6366#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006367 "vimscript-1",
6368 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006369 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006374#ifdef FEAT_VTP
6375 "vtp",
6376#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377#ifdef FEAT_WILDIGN
6378 "wildignore",
6379#endif
6380#ifdef FEAT_WILDMENU
6381 "wildmenu",
6382#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384#ifdef FEAT_WAK
6385 "winaltkeys",
6386#endif
6387#ifdef FEAT_WRITEBACKUP
6388 "writebackup",
6389#endif
6390#ifdef FEAT_XIM
6391 "xim",
6392#endif
6393#ifdef FEAT_XFONTSET
6394 "xfontset",
6395#endif
6396#ifdef FEAT_XPM_W32
6397 "xpm",
6398 "xpm_w32", /* for backward compatibility */
6399#else
6400# if defined(HAVE_XPM)
6401 "xpm",
6402# endif
6403#endif
6404#ifdef USE_XSMP
6405 "xsmp",
6406#endif
6407#ifdef USE_XSMP_INTERACT
6408 "xsmp_interact",
6409#endif
6410#ifdef FEAT_XCLIPBOARD
6411 "xterm_clipboard",
6412#endif
6413#ifdef FEAT_XTERM_SAVE
6414 "xterm_save",
6415#endif
6416#if defined(UNIX) && defined(FEAT_X11)
6417 "X11",
6418#endif
6419 NULL
6420 };
6421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006422 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 for (i = 0; has_list[i] != NULL; ++i)
6424 if (STRICMP(name, has_list[i]) == 0)
6425 {
6426 n = TRUE;
6427 break;
6428 }
6429
6430 if (n == FALSE)
6431 {
6432 if (STRNICMP(name, "patch", 5) == 0)
6433 {
6434 if (name[5] == '-'
6435 && STRLEN(name) >= 11
6436 && vim_isdigit(name[6])
6437 && vim_isdigit(name[8])
6438 && vim_isdigit(name[10]))
6439 {
6440 int major = atoi((char *)name + 6);
6441 int minor = atoi((char *)name + 8);
6442
6443 /* Expect "patch-9.9.01234". */
6444 n = (major < VIM_VERSION_MAJOR
6445 || (major == VIM_VERSION_MAJOR
6446 && (minor < VIM_VERSION_MINOR
6447 || (minor == VIM_VERSION_MINOR
6448 && has_patch(atoi((char *)name + 10))))));
6449 }
6450 else
6451 n = has_patch(atoi((char *)name + 5));
6452 }
6453 else if (STRICMP(name, "vim_starting") == 0)
6454 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006455 else if (STRICMP(name, "ttyin") == 0)
6456 n = mch_input_isatty();
6457 else if (STRICMP(name, "ttyout") == 0)
6458 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459 else if (STRICMP(name, "multi_byte_encoding") == 0)
6460 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006461#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462 else if (STRICMP(name, "balloon_multiline") == 0)
6463 n = multiline_balloon_available();
6464#endif
6465#ifdef DYNAMIC_TCL
6466 else if (STRICMP(name, "tcl") == 0)
6467 n = tcl_enabled(FALSE);
6468#endif
6469#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6470 else if (STRICMP(name, "iconv") == 0)
6471 n = iconv_enabled(FALSE);
6472#endif
6473#ifdef DYNAMIC_LUA
6474 else if (STRICMP(name, "lua") == 0)
6475 n = lua_enabled(FALSE);
6476#endif
6477#ifdef DYNAMIC_MZSCHEME
6478 else if (STRICMP(name, "mzscheme") == 0)
6479 n = mzscheme_enabled(FALSE);
6480#endif
6481#ifdef DYNAMIC_RUBY
6482 else if (STRICMP(name, "ruby") == 0)
6483 n = ruby_enabled(FALSE);
6484#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485#ifdef DYNAMIC_PYTHON
6486 else if (STRICMP(name, "python") == 0)
6487 n = python_enabled(FALSE);
6488#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489#ifdef DYNAMIC_PYTHON3
6490 else if (STRICMP(name, "python3") == 0)
6491 n = python3_enabled(FALSE);
6492#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006493#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6494 else if (STRICMP(name, "pythonx") == 0)
6495 {
6496# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6497 if (p_pyx == 0)
6498 n = python3_enabled(FALSE) || python_enabled(FALSE);
6499 else if (p_pyx == 3)
6500 n = python3_enabled(FALSE);
6501 else if (p_pyx == 2)
6502 n = python_enabled(FALSE);
6503# elif defined(DYNAMIC_PYTHON)
6504 n = python_enabled(FALSE);
6505# elif defined(DYNAMIC_PYTHON3)
6506 n = python3_enabled(FALSE);
6507# endif
6508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509#endif
6510#ifdef DYNAMIC_PERL
6511 else if (STRICMP(name, "perl") == 0)
6512 n = perl_enabled(FALSE);
6513#endif
6514#ifdef FEAT_GUI
6515 else if (STRICMP(name, "gui_running") == 0)
6516 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517# ifdef FEAT_BROWSE
6518 else if (STRICMP(name, "browse") == 0)
6519 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6520# endif
6521#endif
6522#ifdef FEAT_SYN_HL
6523 else if (STRICMP(name, "syntax_items") == 0)
6524 n = syntax_present(curwin);
6525#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006526#ifdef FEAT_VTP
6527 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006528 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529#endif
6530#ifdef FEAT_NETBEANS_INTG
6531 else if (STRICMP(name, "netbeans_enabled") == 0)
6532 n = netbeans_active();
6533#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006534#ifdef FEAT_MOUSE_GPM
6535 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6536 n = gpm_enabled();
6537#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006538#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006539 else if (STRICMP(name, "terminal") == 0)
6540 n = terminal_enabled();
6541#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006542#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006543 else if (STRICMP(name, "conpty") == 0)
6544 n = use_conpty();
6545#endif
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02006546#ifdef FEAT_CLIPBOARD
6547 else if (STRICMP(name, "clipboard_working") == 0)
6548 n = clip_star.available;
6549#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550 }
6551
6552 rettv->vval.v_number = n;
6553}
6554
6555/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 * "haslocaldir()" function
6557 */
6558 static void
6559f_haslocaldir(typval_T *argvars, typval_T *rettv)
6560{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006561 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 win_T *wp = NULL;
6563
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006564 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6565
6566 // Check for window-local and tab-local directories
6567 if (wp != NULL && wp->w_localdir != NULL)
6568 rettv->vval.v_number = 1;
6569 else if (tp != NULL && tp->tp_localdir != NULL)
6570 rettv->vval.v_number = 2;
6571 else
6572 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573}
6574
6575/*
6576 * "hasmapto()" function
6577 */
6578 static void
6579f_hasmapto(typval_T *argvars, typval_T *rettv)
6580{
6581 char_u *name;
6582 char_u *mode;
6583 char_u buf[NUMBUFLEN];
6584 int abbr = FALSE;
6585
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006586 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 if (argvars[1].v_type == VAR_UNKNOWN)
6588 mode = (char_u *)"nvo";
6589 else
6590 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006591 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006593 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594 }
6595
6596 if (map_to_exists(name, mode, abbr))
6597 rettv->vval.v_number = TRUE;
6598 else
6599 rettv->vval.v_number = FALSE;
6600}
6601
6602/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006603 * "highlightID(name)" function
6604 */
6605 static void
6606f_hlID(typval_T *argvars, typval_T *rettv)
6607{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006608 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609}
6610
6611/*
6612 * "highlight_exists()" function
6613 */
6614 static void
6615f_hlexists(typval_T *argvars, typval_T *rettv)
6616{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006617 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618}
6619
6620/*
6621 * "hostname()" function
6622 */
6623 static void
6624f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6625{
6626 char_u hostname[256];
6627
6628 mch_get_host_name(hostname, 256);
6629 rettv->v_type = VAR_STRING;
6630 rettv->vval.v_string = vim_strsave(hostname);
6631}
6632
6633/*
6634 * iconv() function
6635 */
6636 static void
6637f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6638{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 char_u buf1[NUMBUFLEN];
6640 char_u buf2[NUMBUFLEN];
6641 char_u *from, *to, *str;
6642 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643
6644 rettv->v_type = VAR_STRING;
6645 rettv->vval.v_string = NULL;
6646
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006647 str = tv_get_string(&argvars[0]);
6648 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6649 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 vimconv.vc_type = CONV_NONE;
6651 convert_setup(&vimconv, from, to);
6652
6653 /* If the encodings are equal, no conversion needed. */
6654 if (vimconv.vc_type == CONV_NONE)
6655 rettv->vval.v_string = vim_strsave(str);
6656 else
6657 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6658
6659 convert_setup(&vimconv, NULL, NULL);
6660 vim_free(from);
6661 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662}
6663
6664/*
6665 * "indent()" function
6666 */
6667 static void
6668f_indent(typval_T *argvars, typval_T *rettv)
6669{
6670 linenr_T lnum;
6671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006672 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6674 rettv->vval.v_number = get_indent_lnum(lnum);
6675 else
6676 rettv->vval.v_number = -1;
6677}
6678
6679/*
6680 * "index()" function
6681 */
6682 static void
6683f_index(typval_T *argvars, typval_T *rettv)
6684{
6685 list_T *l;
6686 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006687 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 long idx = 0;
6689 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006690 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691
6692 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006693 if (argvars[0].v_type == VAR_BLOB)
6694 {
6695 typval_T tv;
6696 int start = 0;
6697
6698 if (argvars[2].v_type != VAR_UNKNOWN)
6699 {
6700 start = tv_get_number_chk(&argvars[2], &error);
6701 if (error)
6702 return;
6703 }
6704 b = argvars[0].vval.v_blob;
6705 if (b == NULL)
6706 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006707 if (start < 0)
6708 {
6709 start = blob_len(b) + start;
6710 if (start < 0)
6711 start = 0;
6712 }
6713
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006714 for (idx = start; idx < blob_len(b); ++idx)
6715 {
6716 tv.v_type = VAR_NUMBER;
6717 tv.vval.v_number = blob_get(b, idx);
6718 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6719 {
6720 rettv->vval.v_number = idx;
6721 return;
6722 }
6723 }
6724 return;
6725 }
6726 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006728 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 return;
6730 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 l = argvars[0].vval.v_list;
6733 if (l != NULL)
6734 {
6735 item = l->lv_first;
6736 if (argvars[2].v_type != VAR_UNKNOWN)
6737 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 /* Start at specified item. Use the cached index that list_find()
6739 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006740 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 idx = l->lv_idx;
6742 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006743 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744 if (error)
6745 item = NULL;
6746 }
6747
6748 for ( ; item != NULL; item = item->li_next, ++idx)
6749 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6750 {
6751 rettv->vval.v_number = idx;
6752 break;
6753 }
6754 }
6755}
6756
6757static int inputsecret_flag = 0;
6758
6759/*
6760 * "input()" function
6761 * Also handles inputsecret() when inputsecret is set.
6762 */
6763 static void
6764f_input(typval_T *argvars, typval_T *rettv)
6765{
6766 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6767}
6768
6769/*
6770 * "inputdialog()" function
6771 */
6772 static void
6773f_inputdialog(typval_T *argvars, typval_T *rettv)
6774{
6775#if defined(FEAT_GUI_TEXTDIALOG)
6776 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6777 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6778 {
6779 char_u *message;
6780 char_u buf[NUMBUFLEN];
6781 char_u *defstr = (char_u *)"";
6782
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006783 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006785 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6787 else
6788 IObuff[0] = NUL;
6789 if (message != NULL && defstr != NULL
6790 && do_dialog(VIM_QUESTION, NULL, message,
6791 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6792 rettv->vval.v_string = vim_strsave(IObuff);
6793 else
6794 {
6795 if (message != NULL && defstr != NULL
6796 && argvars[1].v_type != VAR_UNKNOWN
6797 && argvars[2].v_type != VAR_UNKNOWN)
6798 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006799 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006800 else
6801 rettv->vval.v_string = NULL;
6802 }
6803 rettv->v_type = VAR_STRING;
6804 }
6805 else
6806#endif
6807 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6808}
6809
6810/*
6811 * "inputlist()" function
6812 */
6813 static void
6814f_inputlist(typval_T *argvars, typval_T *rettv)
6815{
6816 listitem_T *li;
6817 int selected;
6818 int mouse_used;
6819
6820#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006821 /* While starting up, there is no place to enter text. When running tests
6822 * with --not-a-term we assume feedkeys() will be used. */
6823 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 return;
6825#endif
6826 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006828 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006829 return;
6830 }
6831
6832 msg_start();
6833 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6834 lines_left = Rows; /* avoid more prompt */
6835 msg_scroll = TRUE;
6836 msg_clr_eos();
6837
6838 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6839 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006840 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841 msg_putchar('\n');
6842 }
6843
6844 /* Ask for choice. */
6845 selected = prompt_for_number(&mouse_used);
6846 if (mouse_used)
6847 selected -= lines_left;
6848
6849 rettv->vval.v_number = selected;
6850}
6851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6853
6854/*
6855 * "inputrestore()" function
6856 */
6857 static void
6858f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6859{
6860 if (ga_userinput.ga_len > 0)
6861 {
6862 --ga_userinput.ga_len;
6863 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6864 + ga_userinput.ga_len);
6865 /* default return is zero == OK */
6866 }
6867 else if (p_verbose > 1)
6868 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006869 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 rettv->vval.v_number = 1; /* Failed */
6871 }
6872}
6873
6874/*
6875 * "inputsave()" function
6876 */
6877 static void
6878f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6879{
6880 /* Add an entry to the stack of typeahead storage. */
6881 if (ga_grow(&ga_userinput, 1) == OK)
6882 {
6883 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6884 + ga_userinput.ga_len);
6885 ++ga_userinput.ga_len;
6886 /* default return is zero == OK */
6887 }
6888 else
6889 rettv->vval.v_number = 1; /* Failed */
6890}
6891
6892/*
6893 * "inputsecret()" function
6894 */
6895 static void
6896f_inputsecret(typval_T *argvars, typval_T *rettv)
6897{
6898 ++cmdline_star;
6899 ++inputsecret_flag;
6900 f_input(argvars, rettv);
6901 --cmdline_star;
6902 --inputsecret_flag;
6903}
6904
6905/*
6906 * "insert()" function
6907 */
6908 static void
6909f_insert(typval_T *argvars, typval_T *rettv)
6910{
6911 long before = 0;
6912 listitem_T *item;
6913 list_T *l;
6914 int error = FALSE;
6915
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006916 if (argvars[0].v_type == VAR_BLOB)
6917 {
6918 int val, len;
6919 char_u *p;
6920
6921 len = blob_len(argvars[0].vval.v_blob);
6922 if (argvars[2].v_type != VAR_UNKNOWN)
6923 {
6924 before = (long)tv_get_number_chk(&argvars[2], &error);
6925 if (error)
6926 return; // type error; errmsg already given
6927 if (before < 0 || before > len)
6928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006929 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006930 return;
6931 }
6932 }
6933 val = tv_get_number_chk(&argvars[1], &error);
6934 if (error)
6935 return;
6936 if (val < 0 || val > 255)
6937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006938 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006939 return;
6940 }
6941
6942 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
6943 return;
6944 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
6945 mch_memmove(p + before + 1, p + before, (size_t)len - before);
6946 *(p + before) = val;
6947 ++argvars[0].vval.v_blob->bv_ga.ga_len;
6948
6949 copy_tv(&argvars[0], rettv);
6950 }
6951 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006952 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01006953 else if ((l = argvars[0].vval.v_list) != NULL
6954 && !var_check_lock(l->lv_lock,
6955 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 {
6957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006958 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959 if (error)
6960 return; /* type error; errmsg already given */
6961
6962 if (before == l->lv_len)
6963 item = NULL;
6964 else
6965 {
6966 item = list_find(l, before);
6967 if (item == NULL)
6968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006969 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970 l = NULL;
6971 }
6972 }
6973 if (l != NULL)
6974 {
6975 list_insert_tv(l, &argvars[1], item);
6976 copy_tv(&argvars[0], rettv);
6977 }
6978 }
6979}
6980
6981/*
6982 * "invert(expr)" function
6983 */
6984 static void
6985f_invert(typval_T *argvars, typval_T *rettv)
6986{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006987 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006988}
6989
6990/*
6991 * "isdirectory()" function
6992 */
6993 static void
6994f_isdirectory(typval_T *argvars, typval_T *rettv)
6995{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997}
6998
6999/*
7000 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7001 * or it refers to a List or Dictionary that is locked.
7002 */
7003 static int
7004tv_islocked(typval_T *tv)
7005{
7006 return (tv->v_lock & VAR_LOCKED)
7007 || (tv->v_type == VAR_LIST
7008 && tv->vval.v_list != NULL
7009 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7010 || (tv->v_type == VAR_DICT
7011 && tv->vval.v_dict != NULL
7012 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7013}
7014
7015/*
7016 * "islocked()" function
7017 */
7018 static void
7019f_islocked(typval_T *argvars, typval_T *rettv)
7020{
7021 lval_T lv;
7022 char_u *end;
7023 dictitem_T *di;
7024
7025 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007026 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007027 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (end != NULL && lv.ll_name != NULL)
7029 {
7030 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007031 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 else
7033 {
7034 if (lv.ll_tv == NULL)
7035 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007036 di = find_var(lv.ll_name, NULL, TRUE);
7037 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007039 /* Consider a variable locked when:
7040 * 1. the variable itself is locked
7041 * 2. the value of the variable is locked.
7042 * 3. the List or Dict value is locked.
7043 */
7044 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7045 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 }
7047 }
7048 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007049 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007051 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 else if (lv.ll_list != NULL)
7053 /* List item. */
7054 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7055 else
7056 /* Dictionary item. */
7057 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7058 }
7059 }
7060
7061 clear_lval(&lv);
7062}
7063
7064#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7065/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007066 * "isinf()" function
7067 */
7068 static void
7069f_isinf(typval_T *argvars, typval_T *rettv)
7070{
7071 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7072 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7073}
7074
7075/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 * "isnan()" function
7077 */
7078 static void
7079f_isnan(typval_T *argvars, typval_T *rettv)
7080{
7081 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7082 && isnan(argvars[0].vval.v_float);
7083}
7084#endif
7085
7086/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007087 * "last_buffer_nr()" function.
7088 */
7089 static void
7090f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7091{
7092 int n = 0;
7093 buf_T *buf;
7094
Bram Moolenaar29323592016-07-24 22:04:11 +02007095 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 if (n < buf->b_fnum)
7097 n = buf->b_fnum;
7098
7099 rettv->vval.v_number = n;
7100}
7101
7102/*
7103 * "len()" function
7104 */
7105 static void
7106f_len(typval_T *argvars, typval_T *rettv)
7107{
7108 switch (argvars[0].v_type)
7109 {
7110 case VAR_STRING:
7111 case VAR_NUMBER:
7112 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007115 case VAR_BLOB:
7116 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7117 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 case VAR_LIST:
7119 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7120 break;
7121 case VAR_DICT:
7122 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7123 break;
7124 case VAR_UNKNOWN:
7125 case VAR_SPECIAL:
7126 case VAR_FLOAT:
7127 case VAR_FUNC:
7128 case VAR_PARTIAL:
7129 case VAR_JOB:
7130 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007131 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 break;
7133 }
7134}
7135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007137libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138{
7139#ifdef FEAT_LIBCALL
7140 char_u *string_in;
7141 char_u **string_result;
7142 int nr_result;
7143#endif
7144
7145 rettv->v_type = type;
7146 if (type != VAR_NUMBER)
7147 rettv->vval.v_string = NULL;
7148
7149 if (check_restricted() || check_secure())
7150 return;
7151
7152#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007153 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7155 {
7156 string_in = NULL;
7157 if (argvars[2].v_type == VAR_STRING)
7158 string_in = argvars[2].vval.v_string;
7159 if (type == VAR_NUMBER)
7160 string_result = NULL;
7161 else
7162 string_result = &rettv->vval.v_string;
7163 if (mch_libcall(argvars[0].vval.v_string,
7164 argvars[1].vval.v_string,
7165 string_in,
7166 argvars[2].vval.v_number,
7167 string_result,
7168 &nr_result) == OK
7169 && type == VAR_NUMBER)
7170 rettv->vval.v_number = nr_result;
7171 }
7172#endif
7173}
7174
7175/*
7176 * "libcall()" function
7177 */
7178 static void
7179f_libcall(typval_T *argvars, typval_T *rettv)
7180{
7181 libcall_common(argvars, rettv, VAR_STRING);
7182}
7183
7184/*
7185 * "libcallnr()" function
7186 */
7187 static void
7188f_libcallnr(typval_T *argvars, typval_T *rettv)
7189{
7190 libcall_common(argvars, rettv, VAR_NUMBER);
7191}
7192
7193/*
7194 * "line(string)" function
7195 */
7196 static void
7197f_line(typval_T *argvars, typval_T *rettv)
7198{
7199 linenr_T lnum = 0;
7200 pos_T *fp;
7201 int fnum;
7202
7203 fp = var2fpos(&argvars[0], TRUE, &fnum);
7204 if (fp != NULL)
7205 lnum = fp->lnum;
7206 rettv->vval.v_number = lnum;
7207}
7208
7209/*
7210 * "line2byte(lnum)" function
7211 */
7212 static void
7213f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7214{
7215#ifndef FEAT_BYTEOFF
7216 rettv->vval.v_number = -1;
7217#else
7218 linenr_T lnum;
7219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007220 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7222 rettv->vval.v_number = -1;
7223 else
7224 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7225 if (rettv->vval.v_number >= 0)
7226 ++rettv->vval.v_number;
7227#endif
7228}
7229
7230/*
7231 * "lispindent(lnum)" function
7232 */
7233 static void
7234f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7235{
7236#ifdef FEAT_LISP
7237 pos_T pos;
7238 linenr_T lnum;
7239
7240 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007241 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7243 {
7244 curwin->w_cursor.lnum = lnum;
7245 rettv->vval.v_number = get_lisp_indent();
7246 curwin->w_cursor = pos;
7247 }
7248 else
7249#endif
7250 rettv->vval.v_number = -1;
7251}
7252
7253/*
7254 * "localtime()" function
7255 */
7256 static void
7257f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7258{
7259 rettv->vval.v_number = (varnumber_T)time(NULL);
7260}
7261
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007262#ifdef FEAT_FLOAT
7263/*
7264 * "log()" function
7265 */
7266 static void
7267f_log(typval_T *argvars, typval_T *rettv)
7268{
7269 float_T f = 0.0;
7270
7271 rettv->v_type = VAR_FLOAT;
7272 if (get_float_arg(argvars, &f) == OK)
7273 rettv->vval.v_float = log(f);
7274 else
7275 rettv->vval.v_float = 0.0;
7276}
7277
7278/*
7279 * "log10()" function
7280 */
7281 static void
7282f_log10(typval_T *argvars, typval_T *rettv)
7283{
7284 float_T f = 0.0;
7285
7286 rettv->v_type = VAR_FLOAT;
7287 if (get_float_arg(argvars, &f) == OK)
7288 rettv->vval.v_float = log10(f);
7289 else
7290 rettv->vval.v_float = 0.0;
7291}
7292#endif
7293
7294#ifdef FEAT_LUA
7295/*
7296 * "luaeval()" function
7297 */
7298 static void
7299f_luaeval(typval_T *argvars, typval_T *rettv)
7300{
7301 char_u *str;
7302 char_u buf[NUMBUFLEN];
7303
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007304 if (check_restricted() || check_secure())
7305 return;
7306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007307 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007308 do_luaeval(str, argvars + 1, rettv);
7309}
7310#endif
7311
7312/*
7313 * "map()" function
7314 */
7315 static void
7316f_map(typval_T *argvars, typval_T *rettv)
7317{
7318 filter_map(argvars, rettv, TRUE);
7319}
7320
7321/*
7322 * "maparg()" function
7323 */
7324 static void
7325f_maparg(typval_T *argvars, typval_T *rettv)
7326{
7327 get_maparg(argvars, rettv, TRUE);
7328}
7329
7330/*
7331 * "mapcheck()" function
7332 */
7333 static void
7334f_mapcheck(typval_T *argvars, typval_T *rettv)
7335{
7336 get_maparg(argvars, rettv, FALSE);
7337}
7338
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007339typedef enum
7340{
7341 MATCH_END, /* matchend() */
7342 MATCH_MATCH, /* match() */
7343 MATCH_STR, /* matchstr() */
7344 MATCH_LIST, /* matchlist() */
7345 MATCH_POS /* matchstrpos() */
7346} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347
7348 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007349find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350{
7351 char_u *str = NULL;
7352 long len = 0;
7353 char_u *expr = NULL;
7354 char_u *pat;
7355 regmatch_T regmatch;
7356 char_u patbuf[NUMBUFLEN];
7357 char_u strbuf[NUMBUFLEN];
7358 char_u *save_cpo;
7359 long start = 0;
7360 long nth = 1;
7361 colnr_T startcol = 0;
7362 int match = 0;
7363 list_T *l = NULL;
7364 listitem_T *li = NULL;
7365 long idx = 0;
7366 char_u *tofree = NULL;
7367
7368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7369 save_cpo = p_cpo;
7370 p_cpo = (char_u *)"";
7371
7372 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007373 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007374 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007375 /* type MATCH_LIST: return empty list when there are no matches.
7376 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 if (rettv_list_alloc(rettv) == FAIL)
7378 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007379 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 && (list_append_string(rettv->vval.v_list,
7381 (char_u *)"", 0) == FAIL
7382 || list_append_number(rettv->vval.v_list,
7383 (varnumber_T)-1) == FAIL
7384 || list_append_number(rettv->vval.v_list,
7385 (varnumber_T)-1) == FAIL
7386 || list_append_number(rettv->vval.v_list,
7387 (varnumber_T)-1) == FAIL))
7388 {
7389 list_free(rettv->vval.v_list);
7390 rettv->vval.v_list = NULL;
7391 goto theend;
7392 }
7393 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007394 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 {
7396 rettv->v_type = VAR_STRING;
7397 rettv->vval.v_string = NULL;
7398 }
7399
7400 if (argvars[0].v_type == VAR_LIST)
7401 {
7402 if ((l = argvars[0].vval.v_list) == NULL)
7403 goto theend;
7404 li = l->lv_first;
7405 }
7406 else
7407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409 len = (long)STRLEN(str);
7410 }
7411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007412 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 if (pat == NULL)
7414 goto theend;
7415
7416 if (argvars[2].v_type != VAR_UNKNOWN)
7417 {
7418 int error = FALSE;
7419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 if (error)
7422 goto theend;
7423 if (l != NULL)
7424 {
7425 li = list_find(l, start);
7426 if (li == NULL)
7427 goto theend;
7428 idx = l->lv_idx; /* use the cached index */
7429 }
7430 else
7431 {
7432 if (start < 0)
7433 start = 0;
7434 if (start > len)
7435 goto theend;
7436 /* When "count" argument is there ignore matches before "start",
7437 * otherwise skip part of the string. Differs when pattern is "^"
7438 * or "\<". */
7439 if (argvars[3].v_type != VAR_UNKNOWN)
7440 startcol = start;
7441 else
7442 {
7443 str += start;
7444 len -= start;
7445 }
7446 }
7447
7448 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007449 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 if (error)
7451 goto theend;
7452 }
7453
7454 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7455 if (regmatch.regprog != NULL)
7456 {
7457 regmatch.rm_ic = p_ic;
7458
7459 for (;;)
7460 {
7461 if (l != NULL)
7462 {
7463 if (li == NULL)
7464 {
7465 match = FALSE;
7466 break;
7467 }
7468 vim_free(tofree);
7469 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7470 if (str == NULL)
7471 break;
7472 }
7473
7474 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7475
7476 if (match && --nth <= 0)
7477 break;
7478 if (l == NULL && !match)
7479 break;
7480
7481 /* Advance to just after the match. */
7482 if (l != NULL)
7483 {
7484 li = li->li_next;
7485 ++idx;
7486 }
7487 else
7488 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 startcol = (colnr_T)(regmatch.startp[0]
7490 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 if (startcol > (colnr_T)len
7492 || str + startcol <= regmatch.startp[0])
7493 {
7494 match = FALSE;
7495 break;
7496 }
7497 }
7498 }
7499
7500 if (match)
7501 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007502 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503 {
7504 listitem_T *li1 = rettv->vval.v_list->lv_first;
7505 listitem_T *li2 = li1->li_next;
7506 listitem_T *li3 = li2->li_next;
7507 listitem_T *li4 = li3->li_next;
7508
7509 vim_free(li1->li_tv.vval.v_string);
7510 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7511 (int)(regmatch.endp[0] - regmatch.startp[0]));
7512 li3->li_tv.vval.v_number =
7513 (varnumber_T)(regmatch.startp[0] - expr);
7514 li4->li_tv.vval.v_number =
7515 (varnumber_T)(regmatch.endp[0] - expr);
7516 if (l != NULL)
7517 li2->li_tv.vval.v_number = (varnumber_T)idx;
7518 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007519 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 {
7521 int i;
7522
7523 /* return list with matched string and submatches */
7524 for (i = 0; i < NSUBEXP; ++i)
7525 {
7526 if (regmatch.endp[i] == NULL)
7527 {
7528 if (list_append_string(rettv->vval.v_list,
7529 (char_u *)"", 0) == FAIL)
7530 break;
7531 }
7532 else if (list_append_string(rettv->vval.v_list,
7533 regmatch.startp[i],
7534 (int)(regmatch.endp[i] - regmatch.startp[i]))
7535 == FAIL)
7536 break;
7537 }
7538 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007539 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007540 {
7541 /* return matched string */
7542 if (l != NULL)
7543 copy_tv(&li->li_tv, rettv);
7544 else
7545 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7546 (int)(regmatch.endp[0] - regmatch.startp[0]));
7547 }
7548 else if (l != NULL)
7549 rettv->vval.v_number = idx;
7550 else
7551 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007552 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 rettv->vval.v_number =
7554 (varnumber_T)(regmatch.startp[0] - str);
7555 else
7556 rettv->vval.v_number =
7557 (varnumber_T)(regmatch.endp[0] - str);
7558 rettv->vval.v_number += (varnumber_T)(str - expr);
7559 }
7560 }
7561 vim_regfree(regmatch.regprog);
7562 }
7563
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007564theend:
7565 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007566 /* matchstrpos() without a list: drop the second item. */
7567 listitem_remove(rettv->vval.v_list,
7568 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569 vim_free(tofree);
7570 p_cpo = save_cpo;
7571}
7572
7573/*
7574 * "match()" function
7575 */
7576 static void
7577f_match(typval_T *argvars, typval_T *rettv)
7578{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007579 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580}
7581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582/*
7583 * "matchend()" function
7584 */
7585 static void
7586f_matchend(typval_T *argvars, typval_T *rettv)
7587{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007588 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589}
7590
7591/*
7592 * "matchlist()" function
7593 */
7594 static void
7595f_matchlist(typval_T *argvars, typval_T *rettv)
7596{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007597 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598}
7599
7600/*
7601 * "matchstr()" function
7602 */
7603 static void
7604f_matchstr(typval_T *argvars, typval_T *rettv)
7605{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007606 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607}
7608
7609/*
7610 * "matchstrpos()" function
7611 */
7612 static void
7613f_matchstrpos(typval_T *argvars, typval_T *rettv)
7614{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007615 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616}
7617
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 static void
7619max_min(typval_T *argvars, typval_T *rettv, int domax)
7620{
7621 varnumber_T n = 0;
7622 varnumber_T i;
7623 int error = FALSE;
7624
7625 if (argvars[0].v_type == VAR_LIST)
7626 {
7627 list_T *l;
7628 listitem_T *li;
7629
7630 l = argvars[0].vval.v_list;
7631 if (l != NULL)
7632 {
7633 li = l->lv_first;
7634 if (li != NULL)
7635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007636 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 for (;;)
7638 {
7639 li = li->li_next;
7640 if (li == NULL)
7641 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007642 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 if (domax ? i > n : i < n)
7644 n = i;
7645 }
7646 }
7647 }
7648 }
7649 else if (argvars[0].v_type == VAR_DICT)
7650 {
7651 dict_T *d;
7652 int first = TRUE;
7653 hashitem_T *hi;
7654 int todo;
7655
7656 d = argvars[0].vval.v_dict;
7657 if (d != NULL)
7658 {
7659 todo = (int)d->dv_hashtab.ht_used;
7660 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7661 {
7662 if (!HASHITEM_EMPTY(hi))
7663 {
7664 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007665 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 if (first)
7667 {
7668 n = i;
7669 first = FALSE;
7670 }
7671 else if (domax ? i > n : i < n)
7672 n = i;
7673 }
7674 }
7675 }
7676 }
7677 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 rettv->vval.v_number = error ? 0 : n;
7680}
7681
7682/*
7683 * "max()" function
7684 */
7685 static void
7686f_max(typval_T *argvars, typval_T *rettv)
7687{
7688 max_min(argvars, rettv, TRUE);
7689}
7690
7691/*
7692 * "min()" function
7693 */
7694 static void
7695f_min(typval_T *argvars, typval_T *rettv)
7696{
7697 max_min(argvars, rettv, FALSE);
7698}
7699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700/*
7701 * Create the directory in which "dir" is located, and higher levels when
7702 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007703 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 */
7705 static int
7706mkdir_recurse(char_u *dir, int prot)
7707{
7708 char_u *p;
7709 char_u *updir;
7710 int r = FAIL;
7711
7712 /* Get end of directory name in "dir".
7713 * We're done when it's "/" or "c:/". */
7714 p = gettail_sep(dir);
7715 if (p <= get_past_head(dir))
7716 return OK;
7717
7718 /* If the directory exists we're done. Otherwise: create it.*/
7719 updir = vim_strnsave(dir, (int)(p - dir));
7720 if (updir == NULL)
7721 return FAIL;
7722 if (mch_isdir(updir))
7723 r = OK;
7724 else if (mkdir_recurse(updir, prot) == OK)
7725 r = vim_mkdir_emsg(updir, prot);
7726 vim_free(updir);
7727 return r;
7728}
7729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730/*
7731 * "mkdir()" function
7732 */
7733 static void
7734f_mkdir(typval_T *argvars, typval_T *rettv)
7735{
7736 char_u *dir;
7737 char_u buf[NUMBUFLEN];
7738 int prot = 0755;
7739
7740 rettv->vval.v_number = FAIL;
7741 if (check_restricted() || check_secure())
7742 return;
7743
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007744 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007746 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007748 if (*gettail(dir) == NUL)
7749 /* remove trailing slashes */
7750 *gettail_sep(dir) = NUL;
7751
7752 if (argvars[1].v_type != VAR_UNKNOWN)
7753 {
7754 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007756 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007757 if (prot == -1)
7758 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007760 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007761 {
7762 if (mch_isdir(dir))
7763 {
7764 /* With the "p" flag it's OK if the dir already exists. */
7765 rettv->vval.v_number = OK;
7766 return;
7767 }
7768 mkdir_recurse(dir, prot);
7769 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007771 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773
7774/*
7775 * "mode()" function
7776 */
7777 static void
7778f_mode(typval_T *argvars, typval_T *rettv)
7779{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007780 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781
Bram Moolenaar612cc382018-07-29 15:34:26 +02007782 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783
7784 if (time_for_testing == 93784)
7785 {
7786 /* Testing the two-character code. */
7787 buf[0] = 'x';
7788 buf[1] = '!';
7789 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007790#ifdef FEAT_TERMINAL
7791 else if (term_use_loop())
7792 buf[0] = 't';
7793#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 else if (VIsual_active)
7795 {
7796 if (VIsual_select)
7797 buf[0] = VIsual_mode + 's' - 'v';
7798 else
7799 buf[0] = VIsual_mode;
7800 }
7801 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7802 || State == CONFIRM)
7803 {
7804 buf[0] = 'r';
7805 if (State == ASKMORE)
7806 buf[1] = 'm';
7807 else if (State == CONFIRM)
7808 buf[1] = '?';
7809 }
7810 else if (State == EXTERNCMD)
7811 buf[0] = '!';
7812 else if (State & INSERT)
7813 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (State & VREPLACE_FLAG)
7815 {
7816 buf[0] = 'R';
7817 buf[1] = 'v';
7818 }
7819 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01007820 {
7821 if (State & REPLACE_FLAG)
7822 buf[0] = 'R';
7823 else
7824 buf[0] = 'i';
7825#ifdef FEAT_INS_EXPAND
7826 if (ins_compl_active())
7827 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01007828 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01007829 buf[1] = 'x';
7830#endif
7831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007833 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 {
7835 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007836 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007838 else if (exmode_active == EXMODE_NORMAL)
7839 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840 }
7841 else
7842 {
7843 buf[0] = 'n';
7844 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007847 // to be able to detect force-linewise/blockwise/characterwise operations
7848 buf[2] = motion_force;
7849 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02007850 else if (restart_edit == 'I' || restart_edit == 'R'
7851 || restart_edit == 'V')
7852 {
7853 buf[1] = 'i';
7854 buf[2] = restart_edit;
7855 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007856 }
7857
7858 /* Clear out the minor mode when the argument is not a non-zero number or
7859 * non-empty string. */
7860 if (!non_zero_arg(&argvars[0]))
7861 buf[1] = NUL;
7862
7863 rettv->vval.v_string = vim_strsave(buf);
7864 rettv->v_type = VAR_STRING;
7865}
7866
7867#if defined(FEAT_MZSCHEME) || defined(PROTO)
7868/*
7869 * "mzeval()" function
7870 */
7871 static void
7872f_mzeval(typval_T *argvars, typval_T *rettv)
7873{
7874 char_u *str;
7875 char_u buf[NUMBUFLEN];
7876
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007877 if (check_restricted() || check_secure())
7878 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007879 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880 do_mzeval(str, rettv);
7881}
7882
7883 void
7884mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7885{
7886 typval_T argvars[3];
7887
7888 argvars[0].v_type = VAR_STRING;
7889 argvars[0].vval.v_string = name;
7890 copy_tv(args, &argvars[1]);
7891 argvars[2].v_type = VAR_UNKNOWN;
7892 f_call(argvars, rettv);
7893 clear_tv(&argvars[1]);
7894}
7895#endif
7896
7897/*
7898 * "nextnonblank()" function
7899 */
7900 static void
7901f_nextnonblank(typval_T *argvars, typval_T *rettv)
7902{
7903 linenr_T lnum;
7904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 {
7907 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7908 {
7909 lnum = 0;
7910 break;
7911 }
7912 if (*skipwhite(ml_get(lnum)) != NUL)
7913 break;
7914 }
7915 rettv->vval.v_number = lnum;
7916}
7917
7918/*
7919 * "nr2char()" function
7920 */
7921 static void
7922f_nr2char(typval_T *argvars, typval_T *rettv)
7923{
7924 char_u buf[NUMBUFLEN];
7925
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 if (has_mbyte)
7927 {
7928 int utf8 = 0;
7929
7930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007931 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01007933 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007935 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936 }
7937 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007939 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940 buf[1] = NUL;
7941 }
7942 rettv->v_type = VAR_STRING;
7943 rettv->vval.v_string = vim_strsave(buf);
7944}
7945
7946/*
7947 * "or(expr, expr)" function
7948 */
7949 static void
7950f_or(typval_T *argvars, typval_T *rettv)
7951{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007952 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
7953 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954}
7955
7956/*
7957 * "pathshorten()" function
7958 */
7959 static void
7960f_pathshorten(typval_T *argvars, typval_T *rettv)
7961{
7962 char_u *p;
7963
7964 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007965 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966 if (p == NULL)
7967 rettv->vval.v_string = NULL;
7968 else
7969 {
7970 p = vim_strsave(p);
7971 rettv->vval.v_string = p;
7972 if (p != NULL)
7973 shorten_dir(p);
7974 }
7975}
7976
7977#ifdef FEAT_PERL
7978/*
7979 * "perleval()" function
7980 */
7981 static void
7982f_perleval(typval_T *argvars, typval_T *rettv)
7983{
7984 char_u *str;
7985 char_u buf[NUMBUFLEN];
7986
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007987 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 do_perleval(str, rettv);
7989}
7990#endif
7991
7992#ifdef FEAT_FLOAT
7993/*
7994 * "pow()" function
7995 */
7996 static void
7997f_pow(typval_T *argvars, typval_T *rettv)
7998{
7999 float_T fx = 0.0, fy = 0.0;
8000
8001 rettv->v_type = VAR_FLOAT;
8002 if (get_float_arg(argvars, &fx) == OK
8003 && get_float_arg(&argvars[1], &fy) == OK)
8004 rettv->vval.v_float = pow(fx, fy);
8005 else
8006 rettv->vval.v_float = 0.0;
8007}
8008#endif
8009
8010/*
8011 * "prevnonblank()" function
8012 */
8013 static void
8014f_prevnonblank(typval_T *argvars, typval_T *rettv)
8015{
8016 linenr_T lnum;
8017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008018 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8020 lnum = 0;
8021 else
8022 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8023 --lnum;
8024 rettv->vval.v_number = lnum;
8025}
8026
8027/* This dummy va_list is here because:
8028 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8029 * - locally in the function results in a "used before set" warning
8030 * - using va_start() to initialize it gives "function with fixed args" error */
8031static va_list ap;
8032
8033/*
8034 * "printf()" function
8035 */
8036 static void
8037f_printf(typval_T *argvars, typval_T *rettv)
8038{
8039 char_u buf[NUMBUFLEN];
8040 int len;
8041 char_u *s;
8042 int saved_did_emsg = did_emsg;
8043 char *fmt;
8044
8045 rettv->v_type = VAR_STRING;
8046 rettv->vval.v_string = NULL;
8047
8048 /* Get the required length, allocate the buffer and do it for real. */
8049 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008050 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008051 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 if (!did_emsg)
8053 {
8054 s = alloc(len + 1);
8055 if (s != NULL)
8056 {
8057 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008058 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8059 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060 }
8061 }
8062 did_emsg |= saved_did_emsg;
8063}
8064
8065/*
8066 * "pumvisible()" function
8067 */
8068 static void
8069f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8070{
8071#ifdef FEAT_INS_EXPAND
8072 if (pum_visible())
8073 rettv->vval.v_number = 1;
8074#endif
8075}
8076
8077#ifdef FEAT_PYTHON3
8078/*
8079 * "py3eval()" function
8080 */
8081 static void
8082f_py3eval(typval_T *argvars, typval_T *rettv)
8083{
8084 char_u *str;
8085 char_u buf[NUMBUFLEN];
8086
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008087 if (check_restricted() || check_secure())
8088 return;
8089
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008090 if (p_pyx == 0)
8091 p_pyx = 3;
8092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008093 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 do_py3eval(str, rettv);
8095}
8096#endif
8097
8098#ifdef FEAT_PYTHON
8099/*
8100 * "pyeval()" function
8101 */
8102 static void
8103f_pyeval(typval_T *argvars, typval_T *rettv)
8104{
8105 char_u *str;
8106 char_u buf[NUMBUFLEN];
8107
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008108 if (check_restricted() || check_secure())
8109 return;
8110
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008111 if (p_pyx == 0)
8112 p_pyx = 2;
8113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 do_pyeval(str, rettv);
8116}
8117#endif
8118
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008119#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8120/*
8121 * "pyxeval()" function
8122 */
8123 static void
8124f_pyxeval(typval_T *argvars, typval_T *rettv)
8125{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008126 if (check_restricted() || check_secure())
8127 return;
8128
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008129# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8130 init_pyxversion();
8131 if (p_pyx == 2)
8132 f_pyeval(argvars, rettv);
8133 else
8134 f_py3eval(argvars, rettv);
8135# elif defined(FEAT_PYTHON)
8136 f_pyeval(argvars, rettv);
8137# elif defined(FEAT_PYTHON3)
8138 f_py3eval(argvars, rettv);
8139# endif
8140}
8141#endif
8142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143/*
8144 * "range()" function
8145 */
8146 static void
8147f_range(typval_T *argvars, typval_T *rettv)
8148{
8149 varnumber_T start;
8150 varnumber_T end;
8151 varnumber_T stride = 1;
8152 varnumber_T i;
8153 int error = FALSE;
8154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008155 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 if (argvars[1].v_type == VAR_UNKNOWN)
8157 {
8158 end = start - 1;
8159 start = 0;
8160 }
8161 else
8162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008163 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008165 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 }
8167
8168 if (error)
8169 return; /* type error; errmsg already given */
8170 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008171 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008173 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 else
8175 {
8176 if (rettv_list_alloc(rettv) == OK)
8177 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8178 if (list_append_number(rettv->vval.v_list,
8179 (varnumber_T)i) == FAIL)
8180 break;
8181 }
8182}
8183
8184/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008185 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008186 */
8187 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008188readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008189{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008190 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008191 typval_T save_val;
8192 typval_T rettv;
8193 typval_T argv[2];
8194 int retval = 0;
8195 int error = FALSE;
8196
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008197 if (expr->v_type == VAR_UNKNOWN)
8198 return 1;
8199
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008200 prepare_vimvar(VV_VAL, &save_val);
8201 set_vim_var_string(VV_VAL, name, -1);
8202 argv[0].v_type = VAR_STRING;
8203 argv[0].vval.v_string = name;
8204
8205 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8206 goto theend;
8207
8208 retval = tv_get_number_chk(&rettv, &error);
8209 if (error)
8210 retval = -1;
8211 clear_tv(&rettv);
8212
8213theend:
8214 set_vim_var_string(VV_VAL, NULL, 0);
8215 restore_vimvar(VV_VAL, &save_val);
8216 return retval;
8217}
8218
8219/*
8220 * "readdir()" function
8221 */
8222 static void
8223f_readdir(typval_T *argvars, typval_T *rettv)
8224{
8225 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008226 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008227 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008228 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008229 garray_T ga;
8230 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008231
8232 if (rettv_list_alloc(rettv) == FAIL)
8233 return;
8234 path = tv_get_string(&argvars[0]);
8235 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008236
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008237 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8238 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008239 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008240 for (i = 0; i < ga.ga_len; i++)
8241 {
8242 p = ((char_u **)ga.ga_data)[i];
8243 list_append_string(rettv->vval.v_list, p, -1);
8244 }
8245 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008246 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008247}
8248
8249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 * "readfile()" function
8251 */
8252 static void
8253f_readfile(typval_T *argvars, typval_T *rettv)
8254{
8255 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008256 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 int failed = FALSE;
8258 char_u *fname;
8259 FILE *fd;
8260 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8261 int io_size = sizeof(buf);
8262 int readlen; /* size of last fread() */
8263 char_u *prev = NULL; /* previously read bytes, if any */
8264 long prevlen = 0; /* length of data in prev */
8265 long prevsize = 0; /* size of prev buffer */
8266 long maxline = MAXLNUM;
8267 long cnt = 0;
8268 char_u *p; /* position in buf */
8269 char_u *start; /* start of current line */
8270
8271 if (argvars[1].v_type != VAR_UNKNOWN)
8272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008273 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008275 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8276 blob = TRUE;
8277
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008279 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 }
8281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008282 if (blob)
8283 {
8284 if (rettv_blob_alloc(rettv) == FAIL)
8285 return;
8286 }
8287 else
8288 {
8289 if (rettv_list_alloc(rettv) == FAIL)
8290 return;
8291 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292
8293 /* Always open the file in binary mode, library functions have a mind of
8294 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008295 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008298 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 return;
8300 }
8301
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008302 if (blob)
8303 {
8304 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008307 blob_free(rettv->vval.v_blob);
8308 }
8309 fclose(fd);
8310 return;
8311 }
8312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 while (cnt < maxline || maxline < 0)
8314 {
8315 readlen = (int)fread(buf, 1, io_size, fd);
8316
8317 /* This for loop processes what was read, but is also entered at end
8318 * of file so that either:
8319 * - an incomplete line gets written
8320 * - a "binary" file gets an empty line at the end if it ends in a
8321 * newline. */
8322 for (p = buf, start = buf;
8323 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8324 ++p)
8325 {
8326 if (*p == '\n' || readlen <= 0)
8327 {
8328 listitem_T *li;
8329 char_u *s = NULL;
8330 long_u len = p - start;
8331
8332 /* Finished a line. Remove CRs before NL. */
8333 if (readlen > 0 && !binary)
8334 {
8335 while (len > 0 && start[len - 1] == '\r')
8336 --len;
8337 /* removal may cross back to the "prev" string */
8338 if (len == 0)
8339 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8340 --prevlen;
8341 }
8342 if (prevlen == 0)
8343 s = vim_strnsave(start, (int)len);
8344 else
8345 {
8346 /* Change "prev" buffer to be the right size. This way
8347 * the bytes are only copied once, and very long lines are
8348 * allocated only once. */
8349 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8350 {
8351 mch_memmove(s + prevlen, start, len);
8352 s[prevlen + len] = NUL;
8353 prev = NULL; /* the list will own the string */
8354 prevlen = prevsize = 0;
8355 }
8356 }
8357 if (s == NULL)
8358 {
8359 do_outofmem_msg((long_u) prevlen + len + 1);
8360 failed = TRUE;
8361 break;
8362 }
8363
8364 if ((li = listitem_alloc()) == NULL)
8365 {
8366 vim_free(s);
8367 failed = TRUE;
8368 break;
8369 }
8370 li->li_tv.v_type = VAR_STRING;
8371 li->li_tv.v_lock = 0;
8372 li->li_tv.vval.v_string = s;
8373 list_append(rettv->vval.v_list, li);
8374
8375 start = p + 1; /* step over newline */
8376 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8377 break;
8378 }
8379 else if (*p == NUL)
8380 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8382 * when finding the BF and check the previous two bytes. */
8383 else if (*p == 0xbf && enc_utf8 && !binary)
8384 {
8385 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8386 * + 1, these may be in the "prev" string. */
8387 char_u back1 = p >= buf + 1 ? p[-1]
8388 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8389 char_u back2 = p >= buf + 2 ? p[-2]
8390 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8391 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8392
8393 if (back2 == 0xef && back1 == 0xbb)
8394 {
8395 char_u *dest = p - 2;
8396
8397 /* Usually a BOM is at the beginning of a file, and so at
8398 * the beginning of a line; then we can just step over it.
8399 */
8400 if (start == dest)
8401 start = p + 1;
8402 else
8403 {
8404 /* have to shuffle buf to close gap */
8405 int adjust_prevlen = 0;
8406
8407 if (dest < buf)
8408 {
8409 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8410 dest = buf;
8411 }
8412 if (readlen > p - buf + 1)
8413 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8414 readlen -= 3 - adjust_prevlen;
8415 prevlen -= adjust_prevlen;
8416 p = dest - 1;
8417 }
8418 }
8419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 } /* for */
8421
8422 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8423 break;
8424 if (start < p)
8425 {
8426 /* There's part of a line in buf, store it in "prev". */
8427 if (p - start + prevlen >= prevsize)
8428 {
8429 /* need bigger "prev" buffer */
8430 char_u *newprev;
8431
8432 /* A common use case is ordinary text files and "prev" gets a
8433 * fragment of a line, so the first allocation is made
8434 * small, to avoid repeatedly 'allocing' large and
8435 * 'reallocing' small. */
8436 if (prevsize == 0)
8437 prevsize = (long)(p - start);
8438 else
8439 {
8440 long grow50pc = (prevsize * 3) / 2;
8441 long growmin = (long)((p - start) * 2 + prevlen);
8442 prevsize = grow50pc > growmin ? grow50pc : growmin;
8443 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008444 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 if (newprev == NULL)
8446 {
8447 do_outofmem_msg((long_u)prevsize);
8448 failed = TRUE;
8449 break;
8450 }
8451 prev = newprev;
8452 }
8453 /* Add the line part to end of "prev". */
8454 mch_memmove(prev + prevlen, start, p - start);
8455 prevlen += (long)(p - start);
8456 }
8457 } /* while */
8458
8459 /*
8460 * For a negative line count use only the lines at the end of the file,
8461 * free the rest.
8462 */
8463 if (!failed && maxline < 0)
8464 while (cnt > -maxline)
8465 {
8466 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8467 --cnt;
8468 }
8469
8470 if (failed)
8471 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008472 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008474 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 }
8476
8477 vim_free(prev);
8478 fclose(fd);
8479}
8480
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008481 static void
8482return_register(int regname, typval_T *rettv)
8483{
8484 char_u buf[2] = {0, 0};
8485
8486 buf[0] = (char_u)regname;
8487 rettv->v_type = VAR_STRING;
8488 rettv->vval.v_string = vim_strsave(buf);
8489}
8490
8491/*
8492 * "reg_executing()" function
8493 */
8494 static void
8495f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8496{
8497 return_register(reg_executing, rettv);
8498}
8499
8500/*
8501 * "reg_recording()" function
8502 */
8503 static void
8504f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8505{
8506 return_register(reg_recording, rettv);
8507}
8508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510/*
8511 * Convert a List to proftime_T.
8512 * Return FAIL when there is something wrong.
8513 */
8514 static int
8515list2proftime(typval_T *arg, proftime_T *tm)
8516{
8517 long n1, n2;
8518 int error = FALSE;
8519
8520 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8521 || arg->vval.v_list->lv_len != 2)
8522 return FAIL;
8523 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8524 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008525# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 tm->HighPart = n1;
8527 tm->LowPart = n2;
8528# else
8529 tm->tv_sec = n1;
8530 tm->tv_usec = n2;
8531# endif
8532 return error ? FAIL : OK;
8533}
8534#endif /* FEAT_RELTIME */
8535
8536/*
8537 * "reltime()" function
8538 */
8539 static void
8540f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8541{
8542#ifdef FEAT_RELTIME
8543 proftime_T res;
8544 proftime_T start;
8545
8546 if (argvars[0].v_type == VAR_UNKNOWN)
8547 {
8548 /* No arguments: get current time. */
8549 profile_start(&res);
8550 }
8551 else if (argvars[1].v_type == VAR_UNKNOWN)
8552 {
8553 if (list2proftime(&argvars[0], &res) == FAIL)
8554 return;
8555 profile_end(&res);
8556 }
8557 else
8558 {
8559 /* Two arguments: compute the difference. */
8560 if (list2proftime(&argvars[0], &start) == FAIL
8561 || list2proftime(&argvars[1], &res) == FAIL)
8562 return;
8563 profile_sub(&res, &start);
8564 }
8565
8566 if (rettv_list_alloc(rettv) == OK)
8567 {
8568 long n1, n2;
8569
Bram Moolenaar4f974752019-02-17 17:44:42 +01008570# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 n1 = res.HighPart;
8572 n2 = res.LowPart;
8573# else
8574 n1 = res.tv_sec;
8575 n2 = res.tv_usec;
8576# endif
8577 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8578 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8579 }
8580#endif
8581}
8582
8583#ifdef FEAT_FLOAT
8584/*
8585 * "reltimefloat()" function
8586 */
8587 static void
8588f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8589{
8590# ifdef FEAT_RELTIME
8591 proftime_T tm;
8592# endif
8593
8594 rettv->v_type = VAR_FLOAT;
8595 rettv->vval.v_float = 0;
8596# ifdef FEAT_RELTIME
8597 if (list2proftime(&argvars[0], &tm) == OK)
8598 rettv->vval.v_float = profile_float(&tm);
8599# endif
8600}
8601#endif
8602
8603/*
8604 * "reltimestr()" function
8605 */
8606 static void
8607f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8608{
8609#ifdef FEAT_RELTIME
8610 proftime_T tm;
8611#endif
8612
8613 rettv->v_type = VAR_STRING;
8614 rettv->vval.v_string = NULL;
8615#ifdef FEAT_RELTIME
8616 if (list2proftime(&argvars[0], &tm) == OK)
8617 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8618#endif
8619}
8620
8621#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 static void
8623make_connection(void)
8624{
8625 if (X_DISPLAY == NULL
8626# ifdef FEAT_GUI
8627 && !gui.in_use
8628# endif
8629 )
8630 {
8631 x_force_connect = TRUE;
8632 setup_term_clip();
8633 x_force_connect = FALSE;
8634 }
8635}
8636
8637 static int
8638check_connection(void)
8639{
8640 make_connection();
8641 if (X_DISPLAY == NULL)
8642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008643 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 return FAIL;
8645 }
8646 return OK;
8647}
8648#endif
8649
8650#ifdef FEAT_CLIENTSERVER
8651 static void
8652remote_common(typval_T *argvars, typval_T *rettv, int expr)
8653{
8654 char_u *server_name;
8655 char_u *keys;
8656 char_u *r = NULL;
8657 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008658 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008659# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 HWND w;
8661# else
8662 Window w;
8663# endif
8664
8665 if (check_restricted() || check_secure())
8666 return;
8667
8668# ifdef FEAT_X11
8669 if (check_connection() == FAIL)
8670 return;
8671# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008672 if (argvars[2].v_type != VAR_UNKNOWN
8673 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008674 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008676 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 if (server_name == NULL)
8678 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008679 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008680# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008681 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008683 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8684 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685# endif
8686 {
8687 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008689 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008690 vim_free(r);
8691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008693 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 return;
8695 }
8696
8697 rettv->vval.v_string = r;
8698
8699 if (argvars[2].v_type != VAR_UNKNOWN)
8700 {
8701 dictitem_T v;
8702 char_u str[30];
8703 char_u *idvar;
8704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008705 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008706 if (idvar != NULL && *idvar != NUL)
8707 {
8708 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8709 v.di_tv.v_type = VAR_STRING;
8710 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008712 vim_free(v.di_tv.vval.v_string);
8713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 }
8715}
8716#endif
8717
8718/*
8719 * "remote_expr()" function
8720 */
8721 static void
8722f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8723{
8724 rettv->v_type = VAR_STRING;
8725 rettv->vval.v_string = NULL;
8726#ifdef FEAT_CLIENTSERVER
8727 remote_common(argvars, rettv, TRUE);
8728#endif
8729}
8730
8731/*
8732 * "remote_foreground()" function
8733 */
8734 static void
8735f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8736{
8737#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008738# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 /* On Win32 it's done in this application. */
8740 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008741 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742
8743 if (server_name != NULL)
8744 serverForeground(server_name);
8745 }
8746# else
8747 /* Send a foreground() expression to the server. */
8748 argvars[1].v_type = VAR_STRING;
8749 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8750 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008751 rettv->v_type = VAR_STRING;
8752 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 remote_common(argvars, rettv, TRUE);
8754 vim_free(argvars[1].vval.v_string);
8755# endif
8756#endif
8757}
8758
8759 static void
8760f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8761{
8762#ifdef FEAT_CLIENTSERVER
8763 dictitem_T v;
8764 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008765# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 long_u n = 0;
8767# endif
8768 char_u *serverid;
8769
8770 if (check_restricted() || check_secure())
8771 {
8772 rettv->vval.v_number = -1;
8773 return;
8774 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008775 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 if (serverid == NULL)
8777 {
8778 rettv->vval.v_number = -1;
8779 return; /* type error; errmsg already given */
8780 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008781# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8783 if (n == 0)
8784 rettv->vval.v_number = -1;
8785 else
8786 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008787 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788 rettv->vval.v_number = (s != NULL);
8789 }
8790# else
8791 if (check_connection() == FAIL)
8792 return;
8793
8794 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8795 serverStrToWin(serverid), &s);
8796# endif
8797
8798 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8799 {
8800 char_u *retvar;
8801
8802 v.di_tv.v_type = VAR_STRING;
8803 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008804 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 if (retvar != NULL)
8806 set_var(retvar, &v.di_tv, FALSE);
8807 vim_free(v.di_tv.vval.v_string);
8808 }
8809#else
8810 rettv->vval.v_number = -1;
8811#endif
8812}
8813
8814 static void
8815f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8816{
8817 char_u *r = NULL;
8818
8819#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008820 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821
8822 if (serverid != NULL && !check_restricted() && !check_secure())
8823 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008824 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008825# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008826 /* The server's HWND is encoded in the 'id' parameter */
8827 long_u n = 0;
8828# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008829
8830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008831 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008832
Bram Moolenaar4f974752019-02-17 17:44:42 +01008833# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8835 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008836 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 if (r == NULL)
8838# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008839 if (check_connection() == FAIL
8840 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8841 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008843 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 }
8845#endif
8846 rettv->v_type = VAR_STRING;
8847 rettv->vval.v_string = r;
8848}
8849
8850/*
8851 * "remote_send()" function
8852 */
8853 static void
8854f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8855{
8856 rettv->v_type = VAR_STRING;
8857 rettv->vval.v_string = NULL;
8858#ifdef FEAT_CLIENTSERVER
8859 remote_common(argvars, rettv, FALSE);
8860#endif
8861}
8862
8863/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008864 * "remote_startserver()" function
8865 */
8866 static void
8867f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8868{
8869#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008870 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008871
8872 if (server == NULL)
8873 return; /* type error; errmsg already given */
8874 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008875 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008876 else
8877 {
8878# ifdef FEAT_X11
8879 if (check_connection() == OK)
8880 serverRegisterName(X_DISPLAY, server);
8881# else
8882 serverSetName(server);
8883# endif
8884 }
8885#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008886 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008887#endif
8888}
8889
8890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 * "remove()" function
8892 */
8893 static void
8894f_remove(typval_T *argvars, typval_T *rettv)
8895{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8897
8898 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008899 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008900 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008901 blob_remove(argvars, rettv);
8902 else if (argvars[0].v_type == VAR_LIST)
8903 list_remove(argvars, rettv, arg_errmsg);
8904 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01008905 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906}
8907
8908/*
8909 * "rename({from}, {to})" function
8910 */
8911 static void
8912f_rename(typval_T *argvars, typval_T *rettv)
8913{
8914 char_u buf[NUMBUFLEN];
8915
8916 if (check_restricted() || check_secure())
8917 rettv->vval.v_number = -1;
8918 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008919 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
8920 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921}
8922
8923/*
8924 * "repeat()" function
8925 */
8926 static void
8927f_repeat(typval_T *argvars, typval_T *rettv)
8928{
8929 char_u *p;
8930 int n;
8931 int slen;
8932 int len;
8933 char_u *r;
8934 int i;
8935
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008936 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 if (argvars[0].v_type == VAR_LIST)
8938 {
8939 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8940 while (n-- > 0)
8941 if (list_extend(rettv->vval.v_list,
8942 argvars[0].vval.v_list, NULL) == FAIL)
8943 break;
8944 }
8945 else
8946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008947 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 rettv->v_type = VAR_STRING;
8949 rettv->vval.v_string = NULL;
8950
8951 slen = (int)STRLEN(p);
8952 len = slen * n;
8953 if (len <= 0)
8954 return;
8955
8956 r = alloc(len + 1);
8957 if (r != NULL)
8958 {
8959 for (i = 0; i < n; i++)
8960 mch_memmove(r + i * slen, p, (size_t)slen);
8961 r[len] = NUL;
8962 }
8963
8964 rettv->vval.v_string = r;
8965 }
8966}
8967
8968/*
8969 * "resolve()" function
8970 */
8971 static void
8972f_resolve(typval_T *argvars, typval_T *rettv)
8973{
8974 char_u *p;
8975#ifdef HAVE_READLINK
8976 char_u *buf = NULL;
8977#endif
8978
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008979 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980#ifdef FEAT_SHORTCUT
8981 {
8982 char_u *v = NULL;
8983
Bram Moolenaardce1e892019-02-10 23:18:53 +01008984 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 if (v != NULL)
8986 rettv->vval.v_string = v;
8987 else
8988 rettv->vval.v_string = vim_strsave(p);
8989 }
8990#else
8991# ifdef HAVE_READLINK
8992 {
8993 char_u *cpy;
8994 int len;
8995 char_u *remain = NULL;
8996 char_u *q;
8997 int is_relative_to_current = FALSE;
8998 int has_trailing_pathsep = FALSE;
8999 int limit = 100;
9000
9001 p = vim_strsave(p);
9002
9003 if (p[0] == '.' && (vim_ispathsep(p[1])
9004 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9005 is_relative_to_current = TRUE;
9006
9007 len = STRLEN(p);
9008 if (len > 0 && after_pathsep(p, p + len))
9009 {
9010 has_trailing_pathsep = TRUE;
9011 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9012 }
9013
9014 q = getnextcomp(p);
9015 if (*q != NUL)
9016 {
9017 /* Separate the first path component in "p", and keep the
9018 * remainder (beginning with the path separator). */
9019 remain = vim_strsave(q - 1);
9020 q[-1] = NUL;
9021 }
9022
9023 buf = alloc(MAXPATHL + 1);
9024 if (buf == NULL)
9025 goto fail;
9026
9027 for (;;)
9028 {
9029 for (;;)
9030 {
9031 len = readlink((char *)p, (char *)buf, MAXPATHL);
9032 if (len <= 0)
9033 break;
9034 buf[len] = NUL;
9035
9036 if (limit-- == 0)
9037 {
9038 vim_free(p);
9039 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009040 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 rettv->vval.v_string = NULL;
9042 goto fail;
9043 }
9044
9045 /* Ensure that the result will have a trailing path separator
9046 * if the argument has one. */
9047 if (remain == NULL && has_trailing_pathsep)
9048 add_pathsep(buf);
9049
9050 /* Separate the first path component in the link value and
9051 * concatenate the remainders. */
9052 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9053 if (*q != NUL)
9054 {
9055 if (remain == NULL)
9056 remain = vim_strsave(q - 1);
9057 else
9058 {
9059 cpy = concat_str(q - 1, remain);
9060 if (cpy != NULL)
9061 {
9062 vim_free(remain);
9063 remain = cpy;
9064 }
9065 }
9066 q[-1] = NUL;
9067 }
9068
9069 q = gettail(p);
9070 if (q > p && *q == NUL)
9071 {
9072 /* Ignore trailing path separator. */
9073 q[-1] = NUL;
9074 q = gettail(p);
9075 }
9076 if (q > p && !mch_isFullName(buf))
9077 {
9078 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009079 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 if (cpy != NULL)
9081 {
9082 STRCPY(cpy, p);
9083 STRCPY(gettail(cpy), buf);
9084 vim_free(p);
9085 p = cpy;
9086 }
9087 }
9088 else
9089 {
9090 vim_free(p);
9091 p = vim_strsave(buf);
9092 }
9093 }
9094
9095 if (remain == NULL)
9096 break;
9097
9098 /* Append the first path component of "remain" to "p". */
9099 q = getnextcomp(remain + 1);
9100 len = q - remain - (*q != NUL);
9101 cpy = vim_strnsave(p, STRLEN(p) + len);
9102 if (cpy != NULL)
9103 {
9104 STRNCAT(cpy, remain, len);
9105 vim_free(p);
9106 p = cpy;
9107 }
9108 /* Shorten "remain". */
9109 if (*q != NUL)
9110 STRMOVE(remain, q - 1);
9111 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009112 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 }
9114
9115 /* If the result is a relative path name, make it explicitly relative to
9116 * the current directory if and only if the argument had this form. */
9117 if (!vim_ispathsep(*p))
9118 {
9119 if (is_relative_to_current
9120 && *p != NUL
9121 && !(p[0] == '.'
9122 && (p[1] == NUL
9123 || vim_ispathsep(p[1])
9124 || (p[1] == '.'
9125 && (p[2] == NUL
9126 || vim_ispathsep(p[2]))))))
9127 {
9128 /* Prepend "./". */
9129 cpy = concat_str((char_u *)"./", p);
9130 if (cpy != NULL)
9131 {
9132 vim_free(p);
9133 p = cpy;
9134 }
9135 }
9136 else if (!is_relative_to_current)
9137 {
9138 /* Strip leading "./". */
9139 q = p;
9140 while (q[0] == '.' && vim_ispathsep(q[1]))
9141 q += 2;
9142 if (q > p)
9143 STRMOVE(p, p + 2);
9144 }
9145 }
9146
9147 /* Ensure that the result will have no trailing path separator
9148 * if the argument had none. But keep "/" or "//". */
9149 if (!has_trailing_pathsep)
9150 {
9151 q = p + STRLEN(p);
9152 if (after_pathsep(p, q))
9153 *gettail_sep(p) = NUL;
9154 }
9155
9156 rettv->vval.v_string = p;
9157 }
9158# else
9159 rettv->vval.v_string = vim_strsave(p);
9160# endif
9161#endif
9162
9163 simplify_filename(rettv->vval.v_string);
9164
9165#ifdef HAVE_READLINK
9166fail:
9167 vim_free(buf);
9168#endif
9169 rettv->v_type = VAR_STRING;
9170}
9171
9172/*
9173 * "reverse({list})" function
9174 */
9175 static void
9176f_reverse(typval_T *argvars, typval_T *rettv)
9177{
9178 list_T *l;
9179 listitem_T *li, *ni;
9180
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009181 if (argvars[0].v_type == VAR_BLOB)
9182 {
9183 blob_T *b = argvars[0].vval.v_blob;
9184 int i, len = blob_len(b);
9185
9186 for (i = 0; i < len / 2; i++)
9187 {
9188 int tmp = blob_get(b, i);
9189
9190 blob_set(b, i, blob_get(b, len - i - 1));
9191 blob_set(b, len - i - 1, tmp);
9192 }
9193 rettv_blob_set(rettv, b);
9194 return;
9195 }
9196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009198 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009200 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 (char_u *)N_("reverse() argument"), TRUE))
9202 {
9203 li = l->lv_last;
9204 l->lv_first = l->lv_last = NULL;
9205 l->lv_len = 0;
9206 while (li != NULL)
9207 {
9208 ni = li->li_prev;
9209 list_append(l, li);
9210 li = ni;
9211 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009212 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 l->lv_idx = l->lv_len - l->lv_idx - 1;
9214 }
9215}
9216
9217#define SP_NOMOVE 0x01 /* don't move cursor */
9218#define SP_REPEAT 0x02 /* repeat to find outer pair */
9219#define SP_RETCOUNT 0x04 /* return matchcount */
9220#define SP_SETPCMARK 0x08 /* set previous context mark */
9221#define SP_START 0x10 /* accept match at start position */
9222#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9223#define SP_END 0x40 /* leave cursor at end of match */
9224#define SP_COLUMN 0x80 /* start at cursor column */
9225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226/*
9227 * Get flags for a search function.
9228 * Possibly sets "p_ws".
9229 * Returns BACKWARD, FORWARD or zero (for an error).
9230 */
9231 static int
9232get_search_arg(typval_T *varp, int *flagsp)
9233{
9234 int dir = FORWARD;
9235 char_u *flags;
9236 char_u nbuf[NUMBUFLEN];
9237 int mask;
9238
9239 if (varp->v_type != VAR_UNKNOWN)
9240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009241 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242 if (flags == NULL)
9243 return 0; /* type error; errmsg already given */
9244 while (*flags != NUL)
9245 {
9246 switch (*flags)
9247 {
9248 case 'b': dir = BACKWARD; break;
9249 case 'w': p_ws = TRUE; break;
9250 case 'W': p_ws = FALSE; break;
9251 default: mask = 0;
9252 if (flagsp != NULL)
9253 switch (*flags)
9254 {
9255 case 'c': mask = SP_START; break;
9256 case 'e': mask = SP_END; break;
9257 case 'm': mask = SP_RETCOUNT; break;
9258 case 'n': mask = SP_NOMOVE; break;
9259 case 'p': mask = SP_SUBPAT; break;
9260 case 'r': mask = SP_REPEAT; break;
9261 case 's': mask = SP_SETPCMARK; break;
9262 case 'z': mask = SP_COLUMN; break;
9263 }
9264 if (mask == 0)
9265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009266 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 dir = 0;
9268 }
9269 else
9270 *flagsp |= mask;
9271 }
9272 if (dir == 0)
9273 break;
9274 ++flags;
9275 }
9276 }
9277 return dir;
9278}
9279
9280/*
9281 * Shared by search() and searchpos() functions.
9282 */
9283 static int
9284search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9285{
9286 int flags;
9287 char_u *pat;
9288 pos_T pos;
9289 pos_T save_cursor;
9290 int save_p_ws = p_ws;
9291 int dir;
9292 int retval = 0; /* default: FAIL */
9293 long lnum_stop = 0;
9294 proftime_T tm;
9295#ifdef FEAT_RELTIME
9296 long time_limit = 0;
9297#endif
9298 int options = SEARCH_KEEP;
9299 int subpatnum;
9300
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009301 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9303 if (dir == 0)
9304 goto theend;
9305 flags = *flagsp;
9306 if (flags & SP_START)
9307 options |= SEARCH_START;
9308 if (flags & SP_END)
9309 options |= SEARCH_END;
9310 if (flags & SP_COLUMN)
9311 options |= SEARCH_COL;
9312
9313 /* Optional arguments: line number to stop searching and timeout. */
9314 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9315 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009316 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317 if (lnum_stop < 0)
9318 goto theend;
9319#ifdef FEAT_RELTIME
9320 if (argvars[3].v_type != VAR_UNKNOWN)
9321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 if (time_limit < 0)
9324 goto theend;
9325 }
9326#endif
9327 }
9328
9329#ifdef FEAT_RELTIME
9330 /* Set the time limit, if there is one. */
9331 profile_setlimit(time_limit, &tm);
9332#endif
9333
9334 /*
9335 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9336 * Check to make sure only those flags are set.
9337 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9338 * flags cannot be set. Check for that condition also.
9339 */
9340 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9341 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009343 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 goto theend;
9345 }
9346
9347 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009348 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009349 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 if (subpatnum != FAIL)
9351 {
9352 if (flags & SP_SUBPAT)
9353 retval = subpatnum;
9354 else
9355 retval = pos.lnum;
9356 if (flags & SP_SETPCMARK)
9357 setpcmark();
9358 curwin->w_cursor = pos;
9359 if (match_pos != NULL)
9360 {
9361 /* Store the match cursor position */
9362 match_pos->lnum = pos.lnum;
9363 match_pos->col = pos.col + 1;
9364 }
9365 /* "/$" will put the cursor after the end of the line, may need to
9366 * correct that here */
9367 check_cursor();
9368 }
9369
9370 /* If 'n' flag is used: restore cursor position. */
9371 if (flags & SP_NOMOVE)
9372 curwin->w_cursor = save_cursor;
9373 else
9374 curwin->w_set_curswant = TRUE;
9375theend:
9376 p_ws = save_p_ws;
9377
9378 return retval;
9379}
9380
9381#ifdef FEAT_FLOAT
9382
9383/*
9384 * round() is not in C90, use ceil() or floor() instead.
9385 */
9386 float_T
9387vim_round(float_T f)
9388{
9389 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9390}
9391
9392/*
9393 * "round({float})" function
9394 */
9395 static void
9396f_round(typval_T *argvars, typval_T *rettv)
9397{
9398 float_T f = 0.0;
9399
9400 rettv->v_type = VAR_FLOAT;
9401 if (get_float_arg(argvars, &f) == OK)
9402 rettv->vval.v_float = vim_round(f);
9403 else
9404 rettv->vval.v_float = 0.0;
9405}
9406#endif
9407
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009408#ifdef FEAT_RUBY
9409/*
9410 * "rubyeval()" function
9411 */
9412 static void
9413f_rubyeval(typval_T *argvars, typval_T *rettv)
9414{
9415 char_u *str;
9416 char_u buf[NUMBUFLEN];
9417
9418 str = tv_get_string_buf(&argvars[0], buf);
9419 do_rubyeval(str, rettv);
9420}
9421#endif
9422
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423/*
9424 * "screenattr()" function
9425 */
9426 static void
9427f_screenattr(typval_T *argvars, typval_T *rettv)
9428{
9429 int row;
9430 int col;
9431 int c;
9432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009433 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9434 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009435 if (row < 0 || row >= screen_Rows
9436 || col < 0 || col >= screen_Columns)
9437 c = -1;
9438 else
9439 c = ScreenAttrs[LineOffset[row] + col];
9440 rettv->vval.v_number = c;
9441}
9442
9443/*
9444 * "screenchar()" function
9445 */
9446 static void
9447f_screenchar(typval_T *argvars, typval_T *rettv)
9448{
9449 int row;
9450 int col;
9451 int off;
9452 int c;
9453
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009454 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9455 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009456 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457 c = -1;
9458 else
9459 {
9460 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461 if (enc_utf8 && ScreenLinesUC[off] != 0)
9462 c = ScreenLinesUC[off];
9463 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464 c = ScreenLines[off];
9465 }
9466 rettv->vval.v_number = c;
9467}
9468
9469/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009470 * "screenchars()" function
9471 */
9472 static void
9473f_screenchars(typval_T *argvars, typval_T *rettv)
9474{
9475 int row;
9476 int col;
9477 int off;
9478 int c;
9479 int i;
9480
9481 if (rettv_list_alloc(rettv) == FAIL)
9482 return;
9483 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9484 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9485 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9486 return;
9487
9488 off = LineOffset[row] + col;
9489 if (enc_utf8 && ScreenLinesUC[off] != 0)
9490 c = ScreenLinesUC[off];
9491 else
9492 c = ScreenLines[off];
9493 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9494
9495 if (enc_utf8)
9496
9497 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9498 list_append_number(rettv->vval.v_list,
9499 (varnumber_T)ScreenLinesC[i][off]);
9500}
9501
9502/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503 * "screencol()" function
9504 *
9505 * First column is 1 to be consistent with virtcol().
9506 */
9507 static void
9508f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9509{
9510 rettv->vval.v_number = screen_screencol() + 1;
9511}
9512
9513/*
9514 * "screenrow()" function
9515 */
9516 static void
9517f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9518{
9519 rettv->vval.v_number = screen_screenrow() + 1;
9520}
9521
9522/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009523 * "screenstring()" function
9524 */
9525 static void
9526f_screenstring(typval_T *argvars, typval_T *rettv)
9527{
9528 int row;
9529 int col;
9530 int off;
9531 int c;
9532 int i;
9533 char_u buf[MB_MAXBYTES + 1];
9534 int buflen = 0;
9535
9536 rettv->vval.v_string = NULL;
9537 rettv->v_type = VAR_STRING;
9538
9539 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9540 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9541 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9542 return;
9543
9544 off = LineOffset[row] + col;
9545 if (enc_utf8 && ScreenLinesUC[off] != 0)
9546 c = ScreenLinesUC[off];
9547 else
9548 c = ScreenLines[off];
9549 buflen += mb_char2bytes(c, buf);
9550
9551 if (enc_utf8)
9552 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9553 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9554
9555 buf[buflen] = NUL;
9556 rettv->vval.v_string = vim_strsave(buf);
9557}
9558
9559/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 * "search()" function
9561 */
9562 static void
9563f_search(typval_T *argvars, typval_T *rettv)
9564{
9565 int flags = 0;
9566
9567 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9568}
9569
9570/*
9571 * "searchdecl()" function
9572 */
9573 static void
9574f_searchdecl(typval_T *argvars, typval_T *rettv)
9575{
9576 int locally = 1;
9577 int thisblock = 0;
9578 int error = FALSE;
9579 char_u *name;
9580
9581 rettv->vval.v_number = 1; /* default: FAIL */
9582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009583 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 if (argvars[1].v_type != VAR_UNKNOWN)
9585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009586 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009588 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 }
9590 if (!error && name != NULL)
9591 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9592 locally, thisblock, SEARCH_KEEP) == FAIL;
9593}
9594
9595/*
9596 * Used by searchpair() and searchpairpos()
9597 */
9598 static int
9599searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9600{
9601 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009602 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 int save_p_ws = p_ws;
9604 int dir;
9605 int flags = 0;
9606 char_u nbuf1[NUMBUFLEN];
9607 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 int retval = 0; /* default: FAIL */
9609 long lnum_stop = 0;
9610 long time_limit = 0;
9611
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009612 /* Get the three pattern arguments: start, middle, end. Will result in an
9613 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009614 spat = tv_get_string_chk(&argvars[0]);
9615 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9616 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009617 if (spat == NULL || mpat == NULL || epat == NULL)
9618 goto theend; /* type error */
9619
9620 /* Handle the optional fourth argument: flags */
9621 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9622 if (dir == 0)
9623 goto theend;
9624
9625 /* Don't accept SP_END or SP_SUBPAT.
9626 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9627 */
9628 if ((flags & (SP_END | SP_SUBPAT)) != 0
9629 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009631 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632 goto theend;
9633 }
9634
9635 /* Using 'r' implies 'W', otherwise it doesn't work. */
9636 if (flags & SP_REPEAT)
9637 p_ws = FALSE;
9638
9639 /* Optional fifth argument: skip expression */
9640 if (argvars[3].v_type == VAR_UNKNOWN
9641 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009642 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 else
9644 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009645 skip = &argvars[4];
9646 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9647 && skip->v_type != VAR_STRING)
9648 {
9649 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009650 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009651 goto theend;
9652 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 if (argvars[5].v_type != VAR_UNKNOWN)
9654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009655 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009656 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009658 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009660 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661#ifdef FEAT_RELTIME
9662 if (argvars[6].v_type != VAR_UNKNOWN)
9663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009664 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009667 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009669 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009670 }
9671#endif
9672 }
9673 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009674
9675 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9676 match_pos, lnum_stop, time_limit);
9677
9678theend:
9679 p_ws = save_p_ws;
9680
9681 return retval;
9682}
9683
9684/*
9685 * "searchpair()" function
9686 */
9687 static void
9688f_searchpair(typval_T *argvars, typval_T *rettv)
9689{
9690 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9691}
9692
9693/*
9694 * "searchpairpos()" function
9695 */
9696 static void
9697f_searchpairpos(typval_T *argvars, typval_T *rettv)
9698{
9699 pos_T match_pos;
9700 int lnum = 0;
9701 int col = 0;
9702
9703 if (rettv_list_alloc(rettv) == FAIL)
9704 return;
9705
9706 if (searchpair_cmn(argvars, &match_pos) > 0)
9707 {
9708 lnum = match_pos.lnum;
9709 col = match_pos.col;
9710 }
9711
9712 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9713 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9714}
9715
9716/*
9717 * Search for a start/middle/end thing.
9718 * Used by searchpair(), see its documentation for the details.
9719 * Returns 0 or -1 for no match,
9720 */
9721 long
9722do_searchpair(
9723 char_u *spat, /* start pattern */
9724 char_u *mpat, /* middle pattern */
9725 char_u *epat, /* end pattern */
9726 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009727 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009728 int flags, /* SP_SETPCMARK and other SP_ values */
9729 pos_T *match_pos,
9730 linenr_T lnum_stop, /* stop at this line if not zero */
9731 long time_limit UNUSED) /* stop after this many msec */
9732{
9733 char_u *save_cpo;
9734 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9735 long retval = 0;
9736 pos_T pos;
9737 pos_T firstpos;
9738 pos_T foundpos;
9739 pos_T save_cursor;
9740 pos_T save_pos;
9741 int n;
9742 int r;
9743 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009744 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745 int err;
9746 int options = SEARCH_KEEP;
9747 proftime_T tm;
9748
9749 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9750 save_cpo = p_cpo;
9751 p_cpo = empty_option;
9752
9753#ifdef FEAT_RELTIME
9754 /* Set the time limit, if there is one. */
9755 profile_setlimit(time_limit, &tm);
9756#endif
9757
9758 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9759 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009760 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9761 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762 if (pat2 == NULL || pat3 == NULL)
9763 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009764 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 if (*mpat == NUL)
9766 STRCPY(pat3, pat2);
9767 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009768 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 spat, epat, mpat);
9770 if (flags & SP_START)
9771 options |= SEARCH_START;
9772
Bram Moolenaar48570482017-10-30 21:48:41 +01009773 if (skip != NULL)
9774 {
9775 /* Empty string means to not use the skip expression. */
9776 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9777 use_skip = skip->vval.v_string != NULL
9778 && *skip->vval.v_string != NUL;
9779 }
9780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 save_cursor = curwin->w_cursor;
9782 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009783 CLEAR_POS(&firstpos);
9784 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 pat = pat3;
9786 for (;;)
9787 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009788 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009789 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009790 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 /* didn't find it or found the first match again: FAIL */
9792 break;
9793
9794 if (firstpos.lnum == 0)
9795 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009796 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 {
9798 /* Found the same position again. Can happen with a pattern that
9799 * has "\zs" at the end and searching backwards. Advance one
9800 * character and try again. */
9801 if (dir == BACKWARD)
9802 decl(&pos);
9803 else
9804 incl(&pos);
9805 }
9806 foundpos = pos;
9807
9808 /* clear the start flag to avoid getting stuck here */
9809 options &= ~SEARCH_START;
9810
9811 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009812 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 {
9814 save_pos = curwin->w_cursor;
9815 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009816 err = FALSE;
9817 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 curwin->w_cursor = save_pos;
9819 if (err)
9820 {
9821 /* Evaluating {skip} caused an error, break here. */
9822 curwin->w_cursor = save_cursor;
9823 retval = -1;
9824 break;
9825 }
9826 if (r)
9827 continue;
9828 }
9829
9830 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9831 {
9832 /* Found end when searching backwards or start when searching
9833 * forward: nested pair. */
9834 ++nest;
9835 pat = pat2; /* nested, don't search for middle */
9836 }
9837 else
9838 {
9839 /* Found end when searching forward or start when searching
9840 * backward: end of (nested) pair; or found middle in outer pair. */
9841 if (--nest == 1)
9842 pat = pat3; /* outer level, search for middle */
9843 }
9844
9845 if (nest == 0)
9846 {
9847 /* Found the match: return matchcount or line number. */
9848 if (flags & SP_RETCOUNT)
9849 ++retval;
9850 else
9851 retval = pos.lnum;
9852 if (flags & SP_SETPCMARK)
9853 setpcmark();
9854 curwin->w_cursor = pos;
9855 if (!(flags & SP_REPEAT))
9856 break;
9857 nest = 1; /* search for next unmatched */
9858 }
9859 }
9860
9861 if (match_pos != NULL)
9862 {
9863 /* Store the match cursor position */
9864 match_pos->lnum = curwin->w_cursor.lnum;
9865 match_pos->col = curwin->w_cursor.col + 1;
9866 }
9867
9868 /* If 'n' flag is used or search failed: restore cursor position. */
9869 if ((flags & SP_NOMOVE) || retval == 0)
9870 curwin->w_cursor = save_cursor;
9871
9872theend:
9873 vim_free(pat2);
9874 vim_free(pat3);
9875 if (p_cpo == empty_option)
9876 p_cpo = save_cpo;
9877 else
9878 /* Darn, evaluating the {skip} expression changed the value. */
9879 free_string_option(save_cpo);
9880
9881 return retval;
9882}
9883
9884/*
9885 * "searchpos()" function
9886 */
9887 static void
9888f_searchpos(typval_T *argvars, typval_T *rettv)
9889{
9890 pos_T match_pos;
9891 int lnum = 0;
9892 int col = 0;
9893 int n;
9894 int flags = 0;
9895
9896 if (rettv_list_alloc(rettv) == FAIL)
9897 return;
9898
9899 n = search_cmn(argvars, &match_pos, &flags);
9900 if (n > 0)
9901 {
9902 lnum = match_pos.lnum;
9903 col = match_pos.col;
9904 }
9905
9906 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9907 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9908 if (flags & SP_SUBPAT)
9909 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9910}
9911
9912 static void
9913f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9914{
9915#ifdef FEAT_CLIENTSERVER
9916 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009917 char_u *server = tv_get_string_chk(&argvars[0]);
9918 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919
9920 rettv->vval.v_number = -1;
9921 if (server == NULL || reply == NULL)
9922 return;
9923 if (check_restricted() || check_secure())
9924 return;
9925# ifdef FEAT_X11
9926 if (check_connection() == FAIL)
9927 return;
9928# endif
9929
9930 if (serverSendReply(server, reply) < 0)
9931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009932 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 return;
9934 }
9935 rettv->vval.v_number = 0;
9936#else
9937 rettv->vval.v_number = -1;
9938#endif
9939}
9940
9941 static void
9942f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9943{
9944 char_u *r = NULL;
9945
9946#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009947# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 r = serverGetVimNames();
9949# else
9950 make_connection();
9951 if (X_DISPLAY != NULL)
9952 r = serverGetVimNames(X_DISPLAY);
9953# endif
9954#endif
9955 rettv->v_type = VAR_STRING;
9956 rettv->vval.v_string = r;
9957}
9958
9959/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009960 * "setbufline()" function
9961 */
9962 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009963f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009964{
9965 linenr_T lnum;
9966 buf_T *buf;
9967
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009968 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009969 if (buf == NULL)
9970 rettv->vval.v_number = 1; /* FAIL */
9971 else
9972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009973 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02009974 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009975 }
9976}
9977
9978/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979 * "setbufvar()" function
9980 */
9981 static void
9982f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9983{
9984 buf_T *buf;
9985 char_u *varname, *bufvarname;
9986 typval_T *varp;
9987 char_u nbuf[NUMBUFLEN];
9988
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009989 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009991 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
9992 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009993 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 varp = &argvars[2];
9995
9996 if (buf != NULL && varname != NULL && varp != NULL)
9997 {
9998 if (*varname == '&')
9999 {
10000 long numval;
10001 char_u *strval;
10002 int error = FALSE;
10003 aco_save_T aco;
10004
10005 /* set curbuf to be our buf, temporarily */
10006 aucmd_prepbuf(&aco, buf);
10007
10008 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010009 numval = (long)tv_get_number_chk(varp, &error);
10010 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 if (!error && strval != NULL)
10012 set_option_value(varname, numval, strval, OPT_LOCAL);
10013
10014 /* reset notion of buffer */
10015 aucmd_restbuf(&aco);
10016 }
10017 else
10018 {
10019 buf_T *save_curbuf = curbuf;
10020
Bram Moolenaar964b3742019-05-24 18:54:09 +020010021 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 if (bufvarname != NULL)
10023 {
10024 curbuf = buf;
10025 STRCPY(bufvarname, "b:");
10026 STRCPY(bufvarname + 2, varname);
10027 set_var(bufvarname, varp, TRUE);
10028 vim_free(bufvarname);
10029 curbuf = save_curbuf;
10030 }
10031 }
10032 }
10033}
10034
10035 static void
10036f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10037{
10038 dict_T *d;
10039 dictitem_T *di;
10040 char_u *csearch;
10041
10042 if (argvars[0].v_type != VAR_DICT)
10043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010044 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010045 return;
10046 }
10047
10048 if ((d = argvars[0].vval.v_dict) != NULL)
10049 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010050 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 if (csearch != NULL)
10052 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 if (enc_utf8)
10054 {
10055 int pcc[MAX_MCO];
10056 int c = utfc_ptr2char(csearch, pcc);
10057
10058 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10059 }
10060 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010061 set_last_csearch(PTR2CHAR(csearch),
10062 csearch, MB_PTR2LEN(csearch));
10063 }
10064
10065 di = dict_find(d, (char_u *)"forward", -1);
10066 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010067 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010068 ? FORWARD : BACKWARD);
10069
10070 di = dict_find(d, (char_u *)"until", -1);
10071 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010072 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 }
10074}
10075
10076/*
10077 * "setcmdpos()" function
10078 */
10079 static void
10080f_setcmdpos(typval_T *argvars, typval_T *rettv)
10081{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010082 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083
10084 if (pos >= 0)
10085 rettv->vval.v_number = set_cmdline_pos(pos);
10086}
10087
10088/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010089 * "setenv()" function
10090 */
10091 static void
10092f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10093{
10094 char_u namebuf[NUMBUFLEN];
10095 char_u valbuf[NUMBUFLEN];
10096 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10097
10098 if (argvars[1].v_type == VAR_SPECIAL
10099 && argvars[1].vval.v_number == VVAL_NULL)
10100 vim_unsetenv(name);
10101 else
10102 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10103}
10104
10105/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010106 * "setfperm({fname}, {mode})" function
10107 */
10108 static void
10109f_setfperm(typval_T *argvars, typval_T *rettv)
10110{
10111 char_u *fname;
10112 char_u modebuf[NUMBUFLEN];
10113 char_u *mode_str;
10114 int i;
10115 int mask;
10116 int mode = 0;
10117
10118 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010119 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120 if (fname == NULL)
10121 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010122 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 if (mode_str == NULL)
10124 return;
10125 if (STRLEN(mode_str) != 9)
10126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010127 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010128 return;
10129 }
10130
10131 mask = 1;
10132 for (i = 8; i >= 0; --i)
10133 {
10134 if (mode_str[i] != '-')
10135 mode |= mask;
10136 mask = mask << 1;
10137 }
10138 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10139}
10140
10141/*
10142 * "setline()" function
10143 */
10144 static void
10145f_setline(typval_T *argvars, typval_T *rettv)
10146{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010147 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010148
Bram Moolenaarca851592018-06-06 21:04:07 +020010149 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150}
10151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152/*
10153 * Used by "setqflist()" and "setloclist()" functions
10154 */
10155 static void
10156set_qf_ll_list(
10157 win_T *wp UNUSED,
10158 typval_T *list_arg UNUSED,
10159 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010160 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 typval_T *rettv)
10162{
10163#ifdef FEAT_QUICKFIX
10164 static char *e_invact = N_("E927: Invalid action: '%s'");
10165 char_u *act;
10166 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010167 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168#endif
10169
10170 rettv->vval.v_number = -1;
10171
10172#ifdef FEAT_QUICKFIX
10173 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010174 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010175 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010176 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010177 else
10178 {
10179 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010180 dict_T *d = NULL;
10181 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182
10183 if (action_arg->v_type == VAR_STRING)
10184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010185 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 if (act == NULL)
10187 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010188 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10189 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 action = *act;
10191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010192 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 }
10194 else if (action_arg->v_type == VAR_UNKNOWN)
10195 action = ' ';
10196 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010197 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198
Bram Moolenaard823fa92016-08-12 16:29:27 +020010199 if (action_arg->v_type != VAR_UNKNOWN
10200 && what_arg->v_type != VAR_UNKNOWN)
10201 {
10202 if (what_arg->v_type == VAR_DICT)
10203 d = what_arg->vval.v_dict;
10204 else
10205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010206 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010207 valid_dict = FALSE;
10208 }
10209 }
10210
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010211 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010212 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010213 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10214 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010215 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010216 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 }
10218#endif
10219}
10220
10221/*
10222 * "setloclist()" function
10223 */
10224 static void
10225f_setloclist(typval_T *argvars, typval_T *rettv)
10226{
10227 win_T *win;
10228
10229 rettv->vval.v_number = -1;
10230
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010231 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010233 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234}
10235
10236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237 * "setpos()" function
10238 */
10239 static void
10240f_setpos(typval_T *argvars, typval_T *rettv)
10241{
10242 pos_T pos;
10243 int fnum;
10244 char_u *name;
10245 colnr_T curswant = -1;
10246
10247 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 if (name != NULL)
10250 {
10251 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10252 {
10253 if (--pos.col < 0)
10254 pos.col = 0;
10255 if (name[0] == '.' && name[1] == NUL)
10256 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010257 /* set cursor; "fnum" is ignored */
10258 curwin->w_cursor = pos;
10259 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010261 curwin->w_curswant = curswant - 1;
10262 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010264 check_cursor();
10265 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 }
10267 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10268 {
10269 /* set mark */
10270 if (setmark_pos(name[1], &pos, fnum) == OK)
10271 rettv->vval.v_number = 0;
10272 }
10273 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010274 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275 }
10276 }
10277}
10278
10279/*
10280 * "setqflist()" function
10281 */
10282 static void
10283f_setqflist(typval_T *argvars, typval_T *rettv)
10284{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010285 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010286}
10287
10288/*
10289 * "setreg()" function
10290 */
10291 static void
10292f_setreg(typval_T *argvars, typval_T *rettv)
10293{
10294 int regname;
10295 char_u *strregname;
10296 char_u *stropt;
10297 char_u *strval;
10298 int append;
10299 char_u yank_type;
10300 long block_len;
10301
10302 block_len = -1;
10303 yank_type = MAUTO;
10304 append = FALSE;
10305
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010306 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 rettv->vval.v_number = 1; /* FAIL is default */
10308
10309 if (strregname == NULL)
10310 return; /* type error; errmsg already given */
10311 regname = *strregname;
10312 if (regname == 0 || regname == '@')
10313 regname = '"';
10314
10315 if (argvars[2].v_type != VAR_UNKNOWN)
10316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010317 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010318 if (stropt == NULL)
10319 return; /* type error */
10320 for (; *stropt != NUL; ++stropt)
10321 switch (*stropt)
10322 {
10323 case 'a': case 'A': /* append */
10324 append = TRUE;
10325 break;
10326 case 'v': case 'c': /* character-wise selection */
10327 yank_type = MCHAR;
10328 break;
10329 case 'V': case 'l': /* line-wise selection */
10330 yank_type = MLINE;
10331 break;
10332 case 'b': case Ctrl_V: /* block-wise selection */
10333 yank_type = MBLOCK;
10334 if (VIM_ISDIGIT(stropt[1]))
10335 {
10336 ++stropt;
10337 block_len = getdigits(&stropt) - 1;
10338 --stropt;
10339 }
10340 break;
10341 }
10342 }
10343
10344 if (argvars[1].v_type == VAR_LIST)
10345 {
10346 char_u **lstval;
10347 char_u **allocval;
10348 char_u buf[NUMBUFLEN];
10349 char_u **curval;
10350 char_u **curallocval;
10351 list_T *ll = argvars[1].vval.v_list;
10352 listitem_T *li;
10353 int len;
10354
10355 /* If the list is NULL handle like an empty list. */
10356 len = ll == NULL ? 0 : ll->lv_len;
10357
10358 /* First half: use for pointers to result lines; second half: use for
10359 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010360 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361 if (lstval == NULL)
10362 return;
10363 curval = lstval;
10364 allocval = lstval + len + 2;
10365 curallocval = allocval;
10366
10367 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10368 li = li->li_next)
10369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010370 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010371 if (strval == NULL)
10372 goto free_lstval;
10373 if (strval == buf)
10374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010375 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 * overwrite the string. */
10377 strval = vim_strsave(buf);
10378 if (strval == NULL)
10379 goto free_lstval;
10380 *curallocval++ = strval;
10381 }
10382 *curval++ = strval;
10383 }
10384 *curval++ = NULL;
10385
10386 write_reg_contents_lst(regname, lstval, -1,
10387 append, yank_type, block_len);
10388free_lstval:
10389 while (curallocval > allocval)
10390 vim_free(*--curallocval);
10391 vim_free(lstval);
10392 }
10393 else
10394 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010395 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396 if (strval == NULL)
10397 return;
10398 write_reg_contents_ex(regname, strval, -1,
10399 append, yank_type, block_len);
10400 }
10401 rettv->vval.v_number = 0;
10402}
10403
10404/*
10405 * "settabvar()" function
10406 */
10407 static void
10408f_settabvar(typval_T *argvars, typval_T *rettv)
10409{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 tabpage_T *save_curtab;
10411 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 char_u *varname, *tabvarname;
10413 typval_T *varp;
10414
10415 rettv->vval.v_number = 0;
10416
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010417 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 return;
10419
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010420 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10421 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010422 varp = &argvars[2];
10423
Bram Moolenaar4033c552017-09-16 20:54:51 +020010424 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010426 save_curtab = curtab;
10427 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428
Bram Moolenaar964b3742019-05-24 18:54:09 +020010429 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430 if (tabvarname != NULL)
10431 {
10432 STRCPY(tabvarname, "t:");
10433 STRCPY(tabvarname + 2, varname);
10434 set_var(tabvarname, varp, TRUE);
10435 vim_free(tabvarname);
10436 }
10437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010438 /* Restore current tabpage */
10439 if (valid_tabpage(save_curtab))
10440 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010441 }
10442}
10443
10444/*
10445 * "settabwinvar()" function
10446 */
10447 static void
10448f_settabwinvar(typval_T *argvars, typval_T *rettv)
10449{
10450 setwinvar(argvars, rettv, 1);
10451}
10452
10453/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010454 * "settagstack()" function
10455 */
10456 static void
10457f_settagstack(typval_T *argvars, typval_T *rettv)
10458{
10459 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10460 win_T *wp;
10461 dict_T *d;
10462 int action = 'r';
10463
10464 rettv->vval.v_number = -1;
10465
10466 // first argument: window number or id
10467 wp = find_win_by_nr_or_id(&argvars[0]);
10468 if (wp == NULL)
10469 return;
10470
10471 // second argument: dict with items to set in the tag stack
10472 if (argvars[1].v_type != VAR_DICT)
10473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010474 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010475 return;
10476 }
10477 d = argvars[1].vval.v_dict;
10478 if (d == NULL)
10479 return;
10480
10481 // third argument: action - 'a' for append and 'r' for replace.
10482 // default is to replace the stack.
10483 if (argvars[2].v_type == VAR_UNKNOWN)
10484 action = 'r';
10485 else if (argvars[2].v_type == VAR_STRING)
10486 {
10487 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010488 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010489 if (actstr == NULL)
10490 return;
10491 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10492 action = *actstr;
10493 else
10494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010495 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010496 return;
10497 }
10498 }
10499 else
10500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010501 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010502 return;
10503 }
10504
10505 if (set_tagstack(wp, d, action) == OK)
10506 rettv->vval.v_number = 0;
10507}
10508
10509/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010510 * "setwinvar()" function
10511 */
10512 static void
10513f_setwinvar(typval_T *argvars, typval_T *rettv)
10514{
10515 setwinvar(argvars, rettv, 0);
10516}
10517
10518#ifdef FEAT_CRYPT
10519/*
10520 * "sha256({string})" function
10521 */
10522 static void
10523f_sha256(typval_T *argvars, typval_T *rettv)
10524{
10525 char_u *p;
10526
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010527 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 rettv->vval.v_string = vim_strsave(
10529 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10530 rettv->v_type = VAR_STRING;
10531}
10532#endif /* FEAT_CRYPT */
10533
10534/*
10535 * "shellescape({string})" function
10536 */
10537 static void
10538f_shellescape(typval_T *argvars, typval_T *rettv)
10539{
Bram Moolenaar20615522017-06-05 18:46:26 +020010540 int do_special = non_zero_arg(&argvars[1]);
10541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010543 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010544 rettv->v_type = VAR_STRING;
10545}
10546
10547/*
10548 * shiftwidth() function
10549 */
10550 static void
10551f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10552{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010553 rettv->vval.v_number = 0;
10554
10555 if (argvars[0].v_type != VAR_UNKNOWN)
10556 {
10557 long col;
10558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010559 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010560 if (col < 0)
10561 return; // type error; errmsg already given
10562#ifdef FEAT_VARTABS
10563 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10564 return;
10565#endif
10566 }
10567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010568 rettv->vval.v_number = get_sw_value(curbuf);
10569}
10570
10571/*
10572 * "simplify()" function
10573 */
10574 static void
10575f_simplify(typval_T *argvars, typval_T *rettv)
10576{
10577 char_u *p;
10578
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010579 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 rettv->vval.v_string = vim_strsave(p);
10581 simplify_filename(rettv->vval.v_string); /* simplify in place */
10582 rettv->v_type = VAR_STRING;
10583}
10584
10585#ifdef FEAT_FLOAT
10586/*
10587 * "sin()" function
10588 */
10589 static void
10590f_sin(typval_T *argvars, typval_T *rettv)
10591{
10592 float_T f = 0.0;
10593
10594 rettv->v_type = VAR_FLOAT;
10595 if (get_float_arg(argvars, &f) == OK)
10596 rettv->vval.v_float = sin(f);
10597 else
10598 rettv->vval.v_float = 0.0;
10599}
10600
10601/*
10602 * "sinh()" function
10603 */
10604 static void
10605f_sinh(typval_T *argvars, typval_T *rettv)
10606{
10607 float_T f = 0.0;
10608
10609 rettv->v_type = VAR_FLOAT;
10610 if (get_float_arg(argvars, &f) == OK)
10611 rettv->vval.v_float = sinh(f);
10612 else
10613 rettv->vval.v_float = 0.0;
10614}
10615#endif
10616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617/*
10618 * "soundfold({word})" function
10619 */
10620 static void
10621f_soundfold(typval_T *argvars, typval_T *rettv)
10622{
10623 char_u *s;
10624
10625 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010626 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010627#ifdef FEAT_SPELL
10628 rettv->vval.v_string = eval_soundfold(s);
10629#else
10630 rettv->vval.v_string = vim_strsave(s);
10631#endif
10632}
10633
10634/*
10635 * "spellbadword()" function
10636 */
10637 static void
10638f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10639{
10640 char_u *word = (char_u *)"";
10641 hlf_T attr = HLF_COUNT;
10642 int len = 0;
10643
10644 if (rettv_list_alloc(rettv) == FAIL)
10645 return;
10646
10647#ifdef FEAT_SPELL
10648 if (argvars[0].v_type == VAR_UNKNOWN)
10649 {
10650 /* Find the start and length of the badly spelled word. */
10651 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10652 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010653 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010655 curwin->w_set_curswant = TRUE;
10656 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010657 }
10658 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010660 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010661 int capcol = -1;
10662
10663 if (str != NULL)
10664 {
10665 /* Check the argument for spelling. */
10666 while (*str != NUL)
10667 {
10668 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10669 if (attr != HLF_COUNT)
10670 {
10671 word = str;
10672 break;
10673 }
10674 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010675 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +020010676 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 }
10678 }
10679 }
10680#endif
10681
10682 list_append_string(rettv->vval.v_list, word, len);
10683 list_append_string(rettv->vval.v_list, (char_u *)(
10684 attr == HLF_SPB ? "bad" :
10685 attr == HLF_SPR ? "rare" :
10686 attr == HLF_SPL ? "local" :
10687 attr == HLF_SPC ? "caps" :
10688 ""), -1);
10689}
10690
10691/*
10692 * "spellsuggest()" function
10693 */
10694 static void
10695f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10696{
10697#ifdef FEAT_SPELL
10698 char_u *str;
10699 int typeerr = FALSE;
10700 int maxcount;
10701 garray_T ga;
10702 int i;
10703 listitem_T *li;
10704 int need_capital = FALSE;
10705#endif
10706
10707 if (rettv_list_alloc(rettv) == FAIL)
10708 return;
10709
10710#ifdef FEAT_SPELL
10711 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010713 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010714 if (argvars[1].v_type != VAR_UNKNOWN)
10715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010716 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010717 if (maxcount <= 0)
10718 return;
10719 if (argvars[2].v_type != VAR_UNKNOWN)
10720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010721 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 if (typeerr)
10723 return;
10724 }
10725 }
10726 else
10727 maxcount = 25;
10728
10729 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10730
10731 for (i = 0; i < ga.ga_len; ++i)
10732 {
10733 str = ((char_u **)ga.ga_data)[i];
10734
10735 li = listitem_alloc();
10736 if (li == NULL)
10737 vim_free(str);
10738 else
10739 {
10740 li->li_tv.v_type = VAR_STRING;
10741 li->li_tv.v_lock = 0;
10742 li->li_tv.vval.v_string = str;
10743 list_append(rettv->vval.v_list, li);
10744 }
10745 }
10746 ga_clear(&ga);
10747 }
10748#endif
10749}
10750
10751 static void
10752f_split(typval_T *argvars, typval_T *rettv)
10753{
10754 char_u *str;
10755 char_u *end;
10756 char_u *pat = NULL;
10757 regmatch_T regmatch;
10758 char_u patbuf[NUMBUFLEN];
10759 char_u *save_cpo;
10760 int match;
10761 colnr_T col = 0;
10762 int keepempty = FALSE;
10763 int typeerr = FALSE;
10764
10765 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10766 save_cpo = p_cpo;
10767 p_cpo = (char_u *)"";
10768
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010769 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 if (argvars[1].v_type != VAR_UNKNOWN)
10771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010772 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 if (pat == NULL)
10774 typeerr = TRUE;
10775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010776 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 }
10778 if (pat == NULL || *pat == NUL)
10779 pat = (char_u *)"[\\x01- ]\\+";
10780
10781 if (rettv_list_alloc(rettv) == FAIL)
10782 return;
10783 if (typeerr)
10784 return;
10785
10786 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10787 if (regmatch.regprog != NULL)
10788 {
10789 regmatch.rm_ic = FALSE;
10790 while (*str != NUL || keepempty)
10791 {
10792 if (*str == NUL)
10793 match = FALSE; /* empty item at the end */
10794 else
10795 match = vim_regexec_nl(&regmatch, str, col);
10796 if (match)
10797 end = regmatch.startp[0];
10798 else
10799 end = str + STRLEN(str);
10800 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10801 && *str != NUL && match && end < regmatch.endp[0]))
10802 {
10803 if (list_append_string(rettv->vval.v_list, str,
10804 (int)(end - str)) == FAIL)
10805 break;
10806 }
10807 if (!match)
10808 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010010809 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010810 if (regmatch.endp[0] > str)
10811 col = 0;
10812 else
Bram Moolenaar13505972019-01-24 15:04:48 +010010813 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 str = regmatch.endp[0];
10816 }
10817
10818 vim_regfree(regmatch.regprog);
10819 }
10820
10821 p_cpo = save_cpo;
10822}
10823
10824#ifdef FEAT_FLOAT
10825/*
10826 * "sqrt()" function
10827 */
10828 static void
10829f_sqrt(typval_T *argvars, typval_T *rettv)
10830{
10831 float_T f = 0.0;
10832
10833 rettv->v_type = VAR_FLOAT;
10834 if (get_float_arg(argvars, &f) == OK)
10835 rettv->vval.v_float = sqrt(f);
10836 else
10837 rettv->vval.v_float = 0.0;
10838}
10839
10840/*
10841 * "str2float()" function
10842 */
10843 static void
10844f_str2float(typval_T *argvars, typval_T *rettv)
10845{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010846 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010847 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848
Bram Moolenaar08243d22017-01-10 16:12:29 +010010849 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 p = skipwhite(p + 1);
10851 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010010852 if (isneg)
10853 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 rettv->v_type = VAR_FLOAT;
10855}
10856#endif
10857
10858/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020010859 * "str2list()" function
10860 */
10861 static void
10862f_str2list(typval_T *argvars, typval_T *rettv)
10863{
10864 char_u *p;
10865 int utf8 = FALSE;
10866
10867 if (rettv_list_alloc(rettv) == FAIL)
10868 return;
10869
10870 if (argvars[1].v_type != VAR_UNKNOWN)
10871 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
10872
10873 p = tv_get_string(&argvars[0]);
10874
10875 if (has_mbyte || utf8)
10876 {
10877 int (*ptr2len)(char_u *);
10878 int (*ptr2char)(char_u *);
10879
10880 if (utf8 || enc_utf8)
10881 {
10882 ptr2len = utf_ptr2len;
10883 ptr2char = utf_ptr2char;
10884 }
10885 else
10886 {
10887 ptr2len = mb_ptr2len;
10888 ptr2char = mb_ptr2char;
10889 }
10890
10891 for ( ; *p != NUL; p += (*ptr2len)(p))
10892 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
10893 }
10894 else
10895 for ( ; *p != NUL; ++p)
10896 list_append_number(rettv->vval.v_list, *p);
10897}
10898
10899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 * "str2nr()" function
10901 */
10902 static void
10903f_str2nr(typval_T *argvars, typval_T *rettv)
10904{
10905 int base = 10;
10906 char_u *p;
10907 varnumber_T n;
10908 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010010909 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010910
10911 if (argvars[1].v_type != VAR_UNKNOWN)
10912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010913 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 if (base != 2 && base != 8 && base != 10 && base != 16)
10915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010916 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010917 return;
10918 }
10919 }
10920
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010921 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010922 isneg = (*p == '-');
10923 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 p = skipwhite(p + 1);
10925 switch (base)
10926 {
10927 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
10928 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
10929 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
10930 default: what = 0;
10931 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020010932 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
10933 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010010934 if (isneg)
10935 rettv->vval.v_number = -n;
10936 else
10937 rettv->vval.v_number = n;
10938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010939}
10940
10941#ifdef HAVE_STRFTIME
10942/*
10943 * "strftime({format}[, {time}])" function
10944 */
10945 static void
10946f_strftime(typval_T *argvars, typval_T *rettv)
10947{
10948 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020010949 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010950 struct tm *curtime;
10951 time_t seconds;
10952 char_u *p;
10953
10954 rettv->v_type = VAR_STRING;
10955
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010956 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957 if (argvars[1].v_type == VAR_UNKNOWN)
10958 seconds = time(NULL);
10959 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010960 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020010961 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 /* MSVC returns NULL for an invalid value of seconds. */
10963 if (curtime == NULL)
10964 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
10965 else
10966 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967 vimconv_T conv;
10968 char_u *enc;
10969
10970 conv.vc_type = CONV_NONE;
10971 enc = enc_locale();
10972 convert_setup(&conv, p_enc, enc);
10973 if (conv.vc_type != CONV_NONE)
10974 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 if (p != NULL)
10976 (void)strftime((char *)result_buf, sizeof(result_buf),
10977 (char *)p, curtime);
10978 else
10979 result_buf[0] = NUL;
10980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010981 if (conv.vc_type != CONV_NONE)
10982 vim_free(p);
10983 convert_setup(&conv, enc, p_enc);
10984 if (conv.vc_type != CONV_NONE)
10985 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
10986 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987 rettv->vval.v_string = vim_strsave(result_buf);
10988
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989 /* Release conversion descriptors */
10990 convert_setup(&conv, NULL, NULL);
10991 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 }
10993}
10994#endif
10995
10996/*
10997 * "strgetchar()" function
10998 */
10999 static void
11000f_strgetchar(typval_T *argvars, typval_T *rettv)
11001{
11002 char_u *str;
11003 int len;
11004 int error = FALSE;
11005 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011006 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007
11008 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011009 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011010 if (str == NULL)
11011 return;
11012 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011013 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014 if (error)
11015 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011016
Bram Moolenaar13505972019-01-24 15:04:48 +010011017 while (charidx >= 0 && byteidx < len)
11018 {
11019 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011021 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11022 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011024 --charidx;
11025 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027}
11028
11029/*
11030 * "stridx()" function
11031 */
11032 static void
11033f_stridx(typval_T *argvars, typval_T *rettv)
11034{
11035 char_u buf[NUMBUFLEN];
11036 char_u *needle;
11037 char_u *haystack;
11038 char_u *save_haystack;
11039 char_u *pos;
11040 int start_idx;
11041
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011042 needle = tv_get_string_chk(&argvars[1]);
11043 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 rettv->vval.v_number = -1;
11045 if (needle == NULL || haystack == NULL)
11046 return; /* type error; errmsg already given */
11047
11048 if (argvars[2].v_type != VAR_UNKNOWN)
11049 {
11050 int error = FALSE;
11051
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011052 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011053 if (error || start_idx >= (int)STRLEN(haystack))
11054 return;
11055 if (start_idx >= 0)
11056 haystack += start_idx;
11057 }
11058
11059 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11060 if (pos != NULL)
11061 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11062}
11063
11064/*
11065 * "string()" function
11066 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011067 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068f_string(typval_T *argvars, typval_T *rettv)
11069{
11070 char_u *tofree;
11071 char_u numbuf[NUMBUFLEN];
11072
11073 rettv->v_type = VAR_STRING;
11074 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11075 get_copyID());
11076 /* Make a copy if we have a value but it's not in allocated memory. */
11077 if (rettv->vval.v_string != NULL && tofree == NULL)
11078 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11079}
11080
11081/*
11082 * "strlen()" function
11083 */
11084 static void
11085f_strlen(typval_T *argvars, typval_T *rettv)
11086{
11087 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011088 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011089}
11090
11091/*
11092 * "strchars()" function
11093 */
11094 static void
11095f_strchars(typval_T *argvars, typval_T *rettv)
11096{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011097 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011098 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011099 varnumber_T len = 0;
11100 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101
11102 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011103 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011104 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011105 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 else
11107 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11109 while (*s != NUL)
11110 {
11111 func_mb_ptr2char_adv(&s);
11112 ++len;
11113 }
11114 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 }
11116}
11117
11118/*
11119 * "strdisplaywidth()" function
11120 */
11121 static void
11122f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11123{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011124 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 int col = 0;
11126
11127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011128 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129
11130 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11131}
11132
11133/*
11134 * "strwidth()" function
11135 */
11136 static void
11137f_strwidth(typval_T *argvars, typval_T *rettv)
11138{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011139 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140
Bram Moolenaar13505972019-01-24 15:04:48 +010011141 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142}
11143
11144/*
11145 * "strcharpart()" function
11146 */
11147 static void
11148f_strcharpart(typval_T *argvars, typval_T *rettv)
11149{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 char_u *p;
11151 int nchar;
11152 int nbyte = 0;
11153 int charlen;
11154 int len = 0;
11155 int slen;
11156 int error = FALSE;
11157
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011158 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011159 slen = (int)STRLEN(p);
11160
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011161 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 if (!error)
11163 {
11164 if (nchar > 0)
11165 while (nchar > 0 && nbyte < slen)
11166 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011167 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011168 --nchar;
11169 }
11170 else
11171 nbyte = nchar;
11172 if (argvars[2].v_type != VAR_UNKNOWN)
11173 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011174 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011175 while (charlen > 0 && nbyte + len < slen)
11176 {
11177 int off = nbyte + len;
11178
11179 if (off < 0)
11180 len += 1;
11181 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011182 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011183 --charlen;
11184 }
11185 }
11186 else
11187 len = slen - nbyte; /* default: all bytes that are available. */
11188 }
11189
11190 /*
11191 * Only return the overlap between the specified part and the actual
11192 * string.
11193 */
11194 if (nbyte < 0)
11195 {
11196 len += nbyte;
11197 nbyte = 0;
11198 }
11199 else if (nbyte > slen)
11200 nbyte = slen;
11201 if (len < 0)
11202 len = 0;
11203 else if (nbyte + len > slen)
11204 len = slen - nbyte;
11205
11206 rettv->v_type = VAR_STRING;
11207 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208}
11209
11210/*
11211 * "strpart()" function
11212 */
11213 static void
11214f_strpart(typval_T *argvars, typval_T *rettv)
11215{
11216 char_u *p;
11217 int n;
11218 int len;
11219 int slen;
11220 int error = FALSE;
11221
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011222 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223 slen = (int)STRLEN(p);
11224
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011225 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011226 if (error)
11227 len = 0;
11228 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011229 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 else
11231 len = slen - n; /* default len: all bytes that are available. */
11232
11233 /*
11234 * Only return the overlap between the specified part and the actual
11235 * string.
11236 */
11237 if (n < 0)
11238 {
11239 len += n;
11240 n = 0;
11241 }
11242 else if (n > slen)
11243 n = slen;
11244 if (len < 0)
11245 len = 0;
11246 else if (n + len > slen)
11247 len = slen - n;
11248
11249 rettv->v_type = VAR_STRING;
11250 rettv->vval.v_string = vim_strnsave(p + n, len);
11251}
11252
11253/*
11254 * "strridx()" function
11255 */
11256 static void
11257f_strridx(typval_T *argvars, typval_T *rettv)
11258{
11259 char_u buf[NUMBUFLEN];
11260 char_u *needle;
11261 char_u *haystack;
11262 char_u *rest;
11263 char_u *lastmatch = NULL;
11264 int haystack_len, end_idx;
11265
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011266 needle = tv_get_string_chk(&argvars[1]);
11267 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011268
11269 rettv->vval.v_number = -1;
11270 if (needle == NULL || haystack == NULL)
11271 return; /* type error; errmsg already given */
11272
11273 haystack_len = (int)STRLEN(haystack);
11274 if (argvars[2].v_type != VAR_UNKNOWN)
11275 {
11276 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011277 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011278 if (end_idx < 0)
11279 return; /* can never find a match */
11280 }
11281 else
11282 end_idx = haystack_len;
11283
11284 if (*needle == NUL)
11285 {
11286 /* Empty string matches past the end. */
11287 lastmatch = haystack + end_idx;
11288 }
11289 else
11290 {
11291 for (rest = haystack; *rest != '\0'; ++rest)
11292 {
11293 rest = (char_u *)strstr((char *)rest, (char *)needle);
11294 if (rest == NULL || rest > haystack + end_idx)
11295 break;
11296 lastmatch = rest;
11297 }
11298 }
11299
11300 if (lastmatch == NULL)
11301 rettv->vval.v_number = -1;
11302 else
11303 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11304}
11305
11306/*
11307 * "strtrans()" function
11308 */
11309 static void
11310f_strtrans(typval_T *argvars, typval_T *rettv)
11311{
11312 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011313 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314}
11315
11316/*
11317 * "submatch()" function
11318 */
11319 static void
11320f_submatch(typval_T *argvars, typval_T *rettv)
11321{
11322 int error = FALSE;
11323 int no;
11324 int retList = 0;
11325
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011326 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 if (error)
11328 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011329 if (no < 0 || no >= NSUBEXP)
11330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011331 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011332 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011333 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011335 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011336 if (error)
11337 return;
11338
11339 if (retList == 0)
11340 {
11341 rettv->v_type = VAR_STRING;
11342 rettv->vval.v_string = reg_submatch(no);
11343 }
11344 else
11345 {
11346 rettv->v_type = VAR_LIST;
11347 rettv->vval.v_list = reg_submatch_list(no);
11348 }
11349}
11350
11351/*
11352 * "substitute()" function
11353 */
11354 static void
11355f_substitute(typval_T *argvars, typval_T *rettv)
11356{
11357 char_u patbuf[NUMBUFLEN];
11358 char_u subbuf[NUMBUFLEN];
11359 char_u flagsbuf[NUMBUFLEN];
11360
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011361 char_u *str = tv_get_string_chk(&argvars[0]);
11362 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011363 char_u *sub = NULL;
11364 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011365 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011367 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11368 expr = &argvars[2];
11369 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011370 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011373 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11374 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011375 rettv->vval.v_string = NULL;
11376 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011377 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378}
11379
11380/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011381 * "swapinfo(swap_filename)" function
11382 */
11383 static void
11384f_swapinfo(typval_T *argvars, typval_T *rettv)
11385{
11386 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011387 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011388}
11389
11390/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011391 * "swapname(expr)" function
11392 */
11393 static void
11394f_swapname(typval_T *argvars, typval_T *rettv)
11395{
11396 buf_T *buf;
11397
11398 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011399 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011400 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11401 || buf->b_ml.ml_mfp->mf_fname == NULL)
11402 rettv->vval.v_string = NULL;
11403 else
11404 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11405}
11406
11407/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011408 * "synID(lnum, col, trans)" function
11409 */
11410 static void
11411f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11412{
11413 int id = 0;
11414#ifdef FEAT_SYN_HL
11415 linenr_T lnum;
11416 colnr_T col;
11417 int trans;
11418 int transerr = FALSE;
11419
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011420 lnum = tv_get_lnum(argvars); /* -1 on type error */
11421 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11422 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011423
11424 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11425 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11426 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11427#endif
11428
11429 rettv->vval.v_number = id;
11430}
11431
11432/*
11433 * "synIDattr(id, what [, mode])" function
11434 */
11435 static void
11436f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11437{
11438 char_u *p = NULL;
11439#ifdef FEAT_SYN_HL
11440 int id;
11441 char_u *what;
11442 char_u *mode;
11443 char_u modebuf[NUMBUFLEN];
11444 int modec;
11445
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011446 id = (int)tv_get_number(&argvars[0]);
11447 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 if (argvars[2].v_type != VAR_UNKNOWN)
11449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011450 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 modec = TOLOWER_ASC(mode[0]);
11452 if (modec != 't' && modec != 'c' && modec != 'g')
11453 modec = 0; /* replace invalid with current */
11454 }
11455 else
11456 {
11457#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11458 if (USE_24BIT)
11459 modec = 'g';
11460 else
11461#endif
11462 if (t_colors > 1)
11463 modec = 'c';
11464 else
11465 modec = 't';
11466 }
11467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 switch (TOLOWER_ASC(what[0]))
11469 {
11470 case 'b':
11471 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11472 p = highlight_color(id, what, modec);
11473 else /* bold */
11474 p = highlight_has_attr(id, HL_BOLD, modec);
11475 break;
11476
11477 case 'f': /* fg[#] or font */
11478 p = highlight_color(id, what, modec);
11479 break;
11480
11481 case 'i':
11482 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11483 p = highlight_has_attr(id, HL_INVERSE, modec);
11484 else /* italic */
11485 p = highlight_has_attr(id, HL_ITALIC, modec);
11486 break;
11487
11488 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011489 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011490 break;
11491
11492 case 'r': /* reverse */
11493 p = highlight_has_attr(id, HL_INVERSE, modec);
11494 break;
11495
11496 case 's':
11497 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11498 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011499 /* strikeout */
11500 else if (TOLOWER_ASC(what[1]) == 't' &&
11501 TOLOWER_ASC(what[2]) == 'r')
11502 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503 else /* standout */
11504 p = highlight_has_attr(id, HL_STANDOUT, modec);
11505 break;
11506
11507 case 'u':
11508 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11509 /* underline */
11510 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11511 else
11512 /* undercurl */
11513 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11514 break;
11515 }
11516
11517 if (p != NULL)
11518 p = vim_strsave(p);
11519#endif
11520 rettv->v_type = VAR_STRING;
11521 rettv->vval.v_string = p;
11522}
11523
11524/*
11525 * "synIDtrans(id)" function
11526 */
11527 static void
11528f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11529{
11530 int id;
11531
11532#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011533 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534
11535 if (id > 0)
11536 id = syn_get_final_id(id);
11537 else
11538#endif
11539 id = 0;
11540
11541 rettv->vval.v_number = id;
11542}
11543
11544/*
11545 * "synconcealed(lnum, col)" function
11546 */
11547 static void
11548f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11549{
11550#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11551 linenr_T lnum;
11552 colnr_T col;
11553 int syntax_flags = 0;
11554 int cchar;
11555 int matchid = 0;
11556 char_u str[NUMBUFLEN];
11557#endif
11558
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011559 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011560
11561#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011562 lnum = tv_get_lnum(argvars); /* -1 on type error */
11563 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564
11565 vim_memset(str, NUL, sizeof(str));
11566
11567 if (rettv_list_alloc(rettv) != FAIL)
11568 {
11569 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11570 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11571 && curwin->w_p_cole > 0)
11572 {
11573 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11574 syntax_flags = get_syntax_info(&matchid);
11575
11576 /* get the conceal character */
11577 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11578 {
11579 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011580 if (cchar == NUL && curwin->w_p_cole == 1)
11581 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011582 if (cchar != NUL)
11583 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 if (has_mbyte)
11585 (*mb_char2bytes)(cchar, str);
11586 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011587 str[0] = cchar;
11588 }
11589 }
11590 }
11591
11592 list_append_number(rettv->vval.v_list,
11593 (syntax_flags & HL_CONCEAL) != 0);
11594 /* -1 to auto-determine strlen */
11595 list_append_string(rettv->vval.v_list, str, -1);
11596 list_append_number(rettv->vval.v_list, matchid);
11597 }
11598#endif
11599}
11600
11601/*
11602 * "synstack(lnum, col)" function
11603 */
11604 static void
11605f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11606{
11607#ifdef FEAT_SYN_HL
11608 linenr_T lnum;
11609 colnr_T col;
11610 int i;
11611 int id;
11612#endif
11613
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011614 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615
11616#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011617 lnum = tv_get_lnum(argvars); /* -1 on type error */
11618 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619
11620 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11621 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11622 && rettv_list_alloc(rettv) != FAIL)
11623 {
11624 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11625 for (i = 0; ; ++i)
11626 {
11627 id = syn_get_stack_item(i);
11628 if (id < 0)
11629 break;
11630 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11631 break;
11632 }
11633 }
11634#endif
11635}
11636
11637 static void
11638get_cmd_output_as_rettv(
11639 typval_T *argvars,
11640 typval_T *rettv,
11641 int retlist)
11642{
11643 char_u *res = NULL;
11644 char_u *p;
11645 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011646 int err = FALSE;
11647 FILE *fd;
11648 list_T *list = NULL;
11649 int flags = SHELL_SILENT;
11650
11651 rettv->v_type = VAR_STRING;
11652 rettv->vval.v_string = NULL;
11653 if (check_restricted() || check_secure())
11654 goto errret;
11655
11656 if (argvars[1].v_type != VAR_UNKNOWN)
11657 {
11658 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011659 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011660 * command.
11661 */
11662 if ((infile = vim_tempname('i', TRUE)) == NULL)
11663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011664 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 goto errret;
11666 }
11667
11668 fd = mch_fopen((char *)infile, WRITEBIN);
11669 if (fd == NULL)
11670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011671 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672 goto errret;
11673 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011674 if (argvars[1].v_type == VAR_NUMBER)
11675 {
11676 linenr_T lnum;
11677 buf_T *buf;
11678
11679 buf = buflist_findnr(argvars[1].vval.v_number);
11680 if (buf == NULL)
11681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011682 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011683 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011684 goto errret;
11685 }
11686
11687 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11688 {
11689 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11690 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11691 {
11692 err = TRUE;
11693 break;
11694 }
11695 if (putc(NL, fd) == EOF)
11696 {
11697 err = TRUE;
11698 break;
11699 }
11700 }
11701 }
11702 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011703 {
11704 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11705 err = TRUE;
11706 }
11707 else
11708 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011709 size_t len;
11710 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011711
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011712 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011713 if (p == NULL)
11714 {
11715 fclose(fd);
11716 goto errret; /* type error; errmsg already given */
11717 }
11718 len = STRLEN(p);
11719 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11720 err = TRUE;
11721 }
11722 if (fclose(fd) != 0)
11723 err = TRUE;
11724 if (err)
11725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011726 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011727 goto errret;
11728 }
11729 }
11730
11731 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11732 * echoes typeahead, that messes up the display. */
11733 if (!msg_silent)
11734 flags += SHELL_COOKED;
11735
11736 if (retlist)
11737 {
11738 int len;
11739 listitem_T *li;
11740 char_u *s = NULL;
11741 char_u *start;
11742 char_u *end;
11743 int i;
11744
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011745 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011746 if (res == NULL)
11747 goto errret;
11748
11749 list = list_alloc();
11750 if (list == NULL)
11751 goto errret;
11752
11753 for (i = 0; i < len; ++i)
11754 {
11755 start = res + i;
11756 while (i < len && res[i] != NL)
11757 ++i;
11758 end = res + i;
11759
Bram Moolenaar964b3742019-05-24 18:54:09 +020011760 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011761 if (s == NULL)
11762 goto errret;
11763
11764 for (p = s; start < end; ++p, ++start)
11765 *p = *start == NUL ? NL : *start;
11766 *p = NUL;
11767
11768 li = listitem_alloc();
11769 if (li == NULL)
11770 {
11771 vim_free(s);
11772 goto errret;
11773 }
11774 li->li_tv.v_type = VAR_STRING;
11775 li->li_tv.v_lock = 0;
11776 li->li_tv.vval.v_string = s;
11777 list_append(list, li);
11778 }
11779
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011780 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011781 list = NULL;
11782 }
11783 else
11784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011785 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011786#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011787 /* translate <CR><NL> into <NL> */
11788 if (res != NULL)
11789 {
11790 char_u *s, *d;
11791
11792 d = res;
11793 for (s = res; *s; ++s)
11794 {
11795 if (s[0] == CAR && s[1] == NL)
11796 ++s;
11797 *d++ = *s;
11798 }
11799 *d = NUL;
11800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011801#endif
11802 rettv->vval.v_string = res;
11803 res = NULL;
11804 }
11805
11806errret:
11807 if (infile != NULL)
11808 {
11809 mch_remove(infile);
11810 vim_free(infile);
11811 }
11812 if (res != NULL)
11813 vim_free(res);
11814 if (list != NULL)
11815 list_free(list);
11816}
11817
11818/*
11819 * "system()" function
11820 */
11821 static void
11822f_system(typval_T *argvars, typval_T *rettv)
11823{
11824 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11825}
11826
11827/*
11828 * "systemlist()" function
11829 */
11830 static void
11831f_systemlist(typval_T *argvars, typval_T *rettv)
11832{
11833 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11834}
11835
11836/*
11837 * "tabpagebuflist()" function
11838 */
11839 static void
11840f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11841{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 tabpage_T *tp;
11843 win_T *wp = NULL;
11844
11845 if (argvars[0].v_type == VAR_UNKNOWN)
11846 wp = firstwin;
11847 else
11848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011849 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 if (tp != NULL)
11851 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11852 }
11853 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11854 {
11855 for (; wp != NULL; wp = wp->w_next)
11856 if (list_append_number(rettv->vval.v_list,
11857 wp->w_buffer->b_fnum) == FAIL)
11858 break;
11859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011860}
11861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862/*
11863 * "tabpagenr()" function
11864 */
11865 static void
11866f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
11867{
11868 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 char_u *arg;
11870
11871 if (argvars[0].v_type != VAR_UNKNOWN)
11872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011873 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011874 nr = 0;
11875 if (arg != NULL)
11876 {
11877 if (STRCMP(arg, "$") == 0)
11878 nr = tabpage_index(NULL) - 1;
11879 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011880 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011881 }
11882 }
11883 else
11884 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011885 rettv->vval.v_number = nr;
11886}
11887
11888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011889/*
11890 * Common code for tabpagewinnr() and winnr().
11891 */
11892 static int
11893get_winnr(tabpage_T *tp, typval_T *argvar)
11894{
11895 win_T *twin;
11896 int nr = 1;
11897 win_T *wp;
11898 char_u *arg;
11899
11900 twin = (tp == curtab) ? curwin : tp->tp_curwin;
11901 if (argvar->v_type != VAR_UNKNOWN)
11902 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011903 int invalid_arg = FALSE;
11904
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011905 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011906 if (arg == NULL)
11907 nr = 0; /* type error; errmsg already given */
11908 else if (STRCMP(arg, "$") == 0)
11909 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
11910 else if (STRCMP(arg, "#") == 0)
11911 {
11912 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
11913 if (twin == NULL)
11914 nr = 0;
11915 }
11916 else
11917 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011918 long count;
11919 char_u *endp;
11920
11921 // Extract the window count (if specified). e.g. winnr('3j')
11922 count = strtol((char *)arg, (char **)&endp, 10);
11923 if (count <= 0)
11924 count = 1; // if count is not specified, default to 1
11925 if (endp != NULL && *endp != '\0')
11926 {
11927 if (STRCMP(endp, "j") == 0)
11928 twin = win_vert_neighbor(tp, twin, FALSE, count);
11929 else if (STRCMP(endp, "k") == 0)
11930 twin = win_vert_neighbor(tp, twin, TRUE, count);
11931 else if (STRCMP(endp, "h") == 0)
11932 twin = win_horz_neighbor(tp, twin, TRUE, count);
11933 else if (STRCMP(endp, "l") == 0)
11934 twin = win_horz_neighbor(tp, twin, FALSE, count);
11935 else
11936 invalid_arg = TRUE;
11937 }
11938 else
11939 invalid_arg = TRUE;
11940 }
11941
11942 if (invalid_arg)
11943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011944 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 nr = 0;
11946 }
11947 }
11948
11949 if (nr > 0)
11950 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11951 wp != twin; wp = wp->w_next)
11952 {
11953 if (wp == NULL)
11954 {
11955 /* didn't find it in this tabpage */
11956 nr = 0;
11957 break;
11958 }
11959 ++nr;
11960 }
11961 return nr;
11962}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011963
11964/*
11965 * "tabpagewinnr()" function
11966 */
11967 static void
11968f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
11969{
11970 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 tabpage_T *tp;
11972
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011973 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974 if (tp == NULL)
11975 nr = 0;
11976 else
11977 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011978 rettv->vval.v_number = nr;
11979}
11980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011981/*
11982 * "tagfiles()" function
11983 */
11984 static void
11985f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
11986{
11987 char_u *fname;
11988 tagname_T tn;
11989 int first;
11990
11991 if (rettv_list_alloc(rettv) == FAIL)
11992 return;
11993 fname = alloc(MAXPATHL);
11994 if (fname == NULL)
11995 return;
11996
11997 for (first = TRUE; ; first = FALSE)
11998 if (get_tagfname(&tn, first, fname) == FAIL
11999 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12000 break;
12001 tagname_free(&tn);
12002 vim_free(fname);
12003}
12004
12005/*
12006 * "taglist()" function
12007 */
12008 static void
12009f_taglist(typval_T *argvars, typval_T *rettv)
12010{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012011 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012012 char_u *tag_pattern;
12013
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012014 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012015
12016 rettv->vval.v_number = FALSE;
12017 if (*tag_pattern == NUL)
12018 return;
12019
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012020 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012021 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012022 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012023 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012024}
12025
12026/*
12027 * "tempname()" function
12028 */
12029 static void
12030f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12031{
12032 static int x = 'A';
12033
12034 rettv->v_type = VAR_STRING;
12035 rettv->vval.v_string = vim_tempname(x, FALSE);
12036
12037 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12038 * names. Skip 'I' and 'O', they are used for shell redirection. */
12039 do
12040 {
12041 if (x == 'Z')
12042 x = '0';
12043 else if (x == '9')
12044 x = 'A';
12045 else
12046 {
12047#ifdef EBCDIC
12048 if (x == 'I')
12049 x = 'J';
12050 else if (x == 'R')
12051 x = 'S';
12052 else
12053#endif
12054 ++x;
12055 }
12056 } while (x == 'I' || x == 'O');
12057}
12058
12059#ifdef FEAT_FLOAT
12060/*
12061 * "tan()" function
12062 */
12063 static void
12064f_tan(typval_T *argvars, typval_T *rettv)
12065{
12066 float_T f = 0.0;
12067
12068 rettv->v_type = VAR_FLOAT;
12069 if (get_float_arg(argvars, &f) == OK)
12070 rettv->vval.v_float = tan(f);
12071 else
12072 rettv->vval.v_float = 0.0;
12073}
12074
12075/*
12076 * "tanh()" function
12077 */
12078 static void
12079f_tanh(typval_T *argvars, typval_T *rettv)
12080{
12081 float_T f = 0.0;
12082
12083 rettv->v_type = VAR_FLOAT;
12084 if (get_float_arg(argvars, &f) == OK)
12085 rettv->vval.v_float = tanh(f);
12086 else
12087 rettv->vval.v_float = 0.0;
12088}
12089#endif
12090
12091/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012092 * Get a callback from "arg". It can be a Funcref or a function name.
12093 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012094 * "cb_name" is not allocated.
12095 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012096 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012097 callback_T
12098get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012099{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012100 callback_T res;
12101
12102 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012103 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12104 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012105 res.cb_partial = arg->vval.v_partial;
12106 ++res.cb_partial->pt_refcount;
12107 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012109 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012110 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012111 res.cb_partial = NULL;
12112 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12113 {
12114 // Note that we don't make a copy of the string.
12115 res.cb_name = arg->vval.v_string;
12116 func_ref(res.cb_name);
12117 }
12118 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12119 {
12120 res.cb_name = (char_u *)"";
12121 }
12122 else
12123 {
12124 emsg(_("E921: Invalid callback argument"));
12125 res.cb_name = NULL;
12126 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012127 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012128 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012129}
12130
12131/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012132 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012133 */
12134 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012135put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012136{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012137 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012138 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012139 tv->v_type = VAR_PARTIAL;
12140 tv->vval.v_partial = cb->cb_partial;
12141 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012142 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012143 else
12144 {
12145 tv->v_type = VAR_FUNC;
12146 tv->vval.v_string = vim_strsave(cb->cb_name);
12147 func_ref(cb->cb_name);
12148 }
12149}
12150
12151/*
12152 * Make a copy of "src" into "dest", allocating the function name if needed,
12153 * without incrementing the refcount.
12154 */
12155 void
12156set_callback(callback_T *dest, callback_T *src)
12157{
12158 if (src->cb_partial == NULL)
12159 {
12160 // just a function name, make a copy
12161 dest->cb_name = vim_strsave(src->cb_name);
12162 dest->cb_free_name = TRUE;
12163 }
12164 else
12165 {
12166 // cb_name is a pointer into cb_partial
12167 dest->cb_name = src->cb_name;
12168 dest->cb_free_name = FALSE;
12169 }
12170 dest->cb_partial = src->cb_partial;
12171}
12172
12173/*
12174 * Unref/free "callback" returned by get_callback() or set_callback().
12175 */
12176 void
12177free_callback(callback_T *callback)
12178{
12179 if (callback->cb_partial != NULL)
12180 {
12181 partial_unref(callback->cb_partial);
12182 callback->cb_partial = NULL;
12183 }
12184 else if (callback->cb_name != NULL)
12185 func_unref(callback->cb_name);
12186 if (callback->cb_free_name)
12187 {
12188 vim_free(callback->cb_name);
12189 callback->cb_free_name = FALSE;
12190 }
12191 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012192}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012193
12194#ifdef FEAT_TIMERS
12195/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012196 * "timer_info([timer])" function
12197 */
12198 static void
12199f_timer_info(typval_T *argvars, typval_T *rettv)
12200{
12201 timer_T *timer = NULL;
12202
12203 if (rettv_list_alloc(rettv) != OK)
12204 return;
12205 if (argvars[0].v_type != VAR_UNKNOWN)
12206 {
12207 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012208 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012209 else
12210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012211 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012212 if (timer != NULL)
12213 add_timer_info(rettv, timer);
12214 }
12215 }
12216 else
12217 add_timer_info_all(rettv);
12218}
12219
12220/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012221 * "timer_pause(timer, paused)" function
12222 */
12223 static void
12224f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12225{
12226 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012227 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012228
12229 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012230 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012231 else
12232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012233 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012234 if (timer != NULL)
12235 timer->tr_paused = paused;
12236 }
12237}
12238
12239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012240 * "timer_start(time, callback [, options])" function
12241 */
12242 static void
12243f_timer_start(typval_T *argvars, typval_T *rettv)
12244{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012245 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012246 timer_T *timer;
12247 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012248 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012249 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250
Bram Moolenaar75537a92016-09-05 22:45:28 +020012251 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012252 if (check_secure())
12253 return;
12254 if (argvars[2].v_type != VAR_UNKNOWN)
12255 {
12256 if (argvars[2].v_type != VAR_DICT
12257 || (dict = argvars[2].vval.v_dict) == NULL)
12258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012259 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260 return;
12261 }
12262 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012263 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264 }
12265
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012266 callback = get_callback(&argvars[1]);
12267 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012268 return;
12269
12270 timer = create_timer(msec, repeat);
12271 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012272 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012273 else
12274 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012275 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012276 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012277 }
12278}
12279
12280/*
12281 * "timer_stop(timer)" function
12282 */
12283 static void
12284f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12285{
12286 timer_T *timer;
12287
12288 if (argvars[0].v_type != VAR_NUMBER)
12289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012290 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012291 return;
12292 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012293 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294 if (timer != NULL)
12295 stop_timer(timer);
12296}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012297
12298/*
12299 * "timer_stopall()" function
12300 */
12301 static void
12302f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12303{
12304 stop_all_timers();
12305}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306#endif
12307
12308/*
12309 * "tolower(string)" function
12310 */
12311 static void
12312f_tolower(typval_T *argvars, typval_T *rettv)
12313{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012314 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012315 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012316}
12317
12318/*
12319 * "toupper(string)" function
12320 */
12321 static void
12322f_toupper(typval_T *argvars, typval_T *rettv)
12323{
12324 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012325 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012326}
12327
12328/*
12329 * "tr(string, fromstr, tostr)" function
12330 */
12331 static void
12332f_tr(typval_T *argvars, typval_T *rettv)
12333{
12334 char_u *in_str;
12335 char_u *fromstr;
12336 char_u *tostr;
12337 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012338 int inlen;
12339 int fromlen;
12340 int tolen;
12341 int idx;
12342 char_u *cpstr;
12343 int cplen;
12344 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012345 char_u buf[NUMBUFLEN];
12346 char_u buf2[NUMBUFLEN];
12347 garray_T ga;
12348
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012349 in_str = tv_get_string(&argvars[0]);
12350 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12351 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352
12353 /* Default return value: empty string. */
12354 rettv->v_type = VAR_STRING;
12355 rettv->vval.v_string = NULL;
12356 if (fromstr == NULL || tostr == NULL)
12357 return; /* type error; errmsg already given */
12358 ga_init2(&ga, (int)sizeof(char), 80);
12359
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012361 /* not multi-byte: fromstr and tostr must be the same length */
12362 if (STRLEN(fromstr) != STRLEN(tostr))
12363 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012365 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012366 ga_clear(&ga);
12367 return;
12368 }
12369
12370 /* fromstr and tostr have to contain the same number of chars */
12371 while (*in_str != NUL)
12372 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012373 if (has_mbyte)
12374 {
12375 inlen = (*mb_ptr2len)(in_str);
12376 cpstr = in_str;
12377 cplen = inlen;
12378 idx = 0;
12379 for (p = fromstr; *p != NUL; p += fromlen)
12380 {
12381 fromlen = (*mb_ptr2len)(p);
12382 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12383 {
12384 for (p = tostr; *p != NUL; p += tolen)
12385 {
12386 tolen = (*mb_ptr2len)(p);
12387 if (idx-- == 0)
12388 {
12389 cplen = tolen;
12390 cpstr = p;
12391 break;
12392 }
12393 }
12394 if (*p == NUL) /* tostr is shorter than fromstr */
12395 goto error;
12396 break;
12397 }
12398 ++idx;
12399 }
12400
12401 if (first && cpstr == in_str)
12402 {
12403 /* Check that fromstr and tostr have the same number of
12404 * (multi-byte) characters. Done only once when a character
12405 * of in_str doesn't appear in fromstr. */
12406 first = FALSE;
12407 for (p = tostr; *p != NUL; p += tolen)
12408 {
12409 tolen = (*mb_ptr2len)(p);
12410 --idx;
12411 }
12412 if (idx != 0)
12413 goto error;
12414 }
12415
12416 (void)ga_grow(&ga, cplen);
12417 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12418 ga.ga_len += cplen;
12419
12420 in_str += inlen;
12421 }
12422 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 {
12424 /* When not using multi-byte chars we can do it faster. */
12425 p = vim_strchr(fromstr, *in_str);
12426 if (p != NULL)
12427 ga_append(&ga, tostr[p - fromstr]);
12428 else
12429 ga_append(&ga, *in_str);
12430 ++in_str;
12431 }
12432 }
12433
12434 /* add a terminating NUL */
12435 (void)ga_grow(&ga, 1);
12436 ga_append(&ga, NUL);
12437
12438 rettv->vval.v_string = ga.ga_data;
12439}
12440
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012441/*
12442 * "trim({expr})" function
12443 */
12444 static void
12445f_trim(typval_T *argvars, typval_T *rettv)
12446{
12447 char_u buf1[NUMBUFLEN];
12448 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012450 char_u *mask = NULL;
12451 char_u *tail;
12452 char_u *prev;
12453 char_u *p;
12454 int c1;
12455
12456 rettv->v_type = VAR_STRING;
12457 if (head == NULL)
12458 {
12459 rettv->vval.v_string = NULL;
12460 return;
12461 }
12462
12463 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012464 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012465
12466 while (*head != NUL)
12467 {
12468 c1 = PTR2CHAR(head);
12469 if (mask == NULL)
12470 {
12471 if (c1 > ' ' && c1 != 0xa0)
12472 break;
12473 }
12474 else
12475 {
12476 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12477 if (c1 == PTR2CHAR(p))
12478 break;
12479 if (*p == NUL)
12480 break;
12481 }
12482 MB_PTR_ADV(head);
12483 }
12484
12485 for (tail = head + STRLEN(head); tail > head; tail = prev)
12486 {
12487 prev = tail;
12488 MB_PTR_BACK(head, prev);
12489 c1 = PTR2CHAR(prev);
12490 if (mask == NULL)
12491 {
12492 if (c1 > ' ' && c1 != 0xa0)
12493 break;
12494 }
12495 else
12496 {
12497 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12498 if (c1 == PTR2CHAR(p))
12499 break;
12500 if (*p == NUL)
12501 break;
12502 }
12503 }
12504 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12505}
12506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507#ifdef FEAT_FLOAT
12508/*
12509 * "trunc({float})" function
12510 */
12511 static void
12512f_trunc(typval_T *argvars, typval_T *rettv)
12513{
12514 float_T f = 0.0;
12515
12516 rettv->v_type = VAR_FLOAT;
12517 if (get_float_arg(argvars, &f) == OK)
12518 /* trunc() is not in C90, use floor() or ceil() instead. */
12519 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12520 else
12521 rettv->vval.v_float = 0.0;
12522}
12523#endif
12524
12525/*
12526 * "type(expr)" function
12527 */
12528 static void
12529f_type(typval_T *argvars, typval_T *rettv)
12530{
12531 int n = -1;
12532
12533 switch (argvars[0].v_type)
12534 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012535 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12536 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012538 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12539 case VAR_LIST: n = VAR_TYPE_LIST; break;
12540 case VAR_DICT: n = VAR_TYPE_DICT; break;
12541 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542 case VAR_SPECIAL:
12543 if (argvars[0].vval.v_number == VVAL_FALSE
12544 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012545 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012547 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012548 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012549 case VAR_JOB: n = VAR_TYPE_JOB; break;
12550 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012551 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012553 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 n = -1;
12555 break;
12556 }
12557 rettv->vval.v_number = n;
12558}
12559
12560/*
12561 * "undofile(name)" function
12562 */
12563 static void
12564f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12565{
12566 rettv->v_type = VAR_STRING;
12567#ifdef FEAT_PERSISTENT_UNDO
12568 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012569 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570
12571 if (*fname == NUL)
12572 {
12573 /* If there is no file name there will be no undo file. */
12574 rettv->vval.v_string = NULL;
12575 }
12576 else
12577 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012578 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012579
12580 if (ffname != NULL)
12581 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12582 vim_free(ffname);
12583 }
12584 }
12585#else
12586 rettv->vval.v_string = NULL;
12587#endif
12588}
12589
12590/*
12591 * "undotree()" function
12592 */
12593 static void
12594f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12595{
12596 if (rettv_dict_alloc(rettv) == OK)
12597 {
12598 dict_T *dict = rettv->vval.v_dict;
12599 list_T *list;
12600
Bram Moolenaare0be1672018-07-08 16:50:37 +020012601 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12602 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12603 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12604 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12605 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12606 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607
12608 list = list_alloc();
12609 if (list != NULL)
12610 {
12611 u_eval_tree(curbuf->b_u_oldhead, list);
12612 dict_add_list(dict, "entries", list);
12613 }
12614 }
12615}
12616
12617/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 * "virtcol(string)" function
12619 */
12620 static void
12621f_virtcol(typval_T *argvars, typval_T *rettv)
12622{
12623 colnr_T vcol = 0;
12624 pos_T *fp;
12625 int fnum = curbuf->b_fnum;
12626
12627 fp = var2fpos(&argvars[0], FALSE, &fnum);
12628 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12629 && fnum == curbuf->b_fnum)
12630 {
12631 getvvcol(curwin, fp, NULL, NULL, &vcol);
12632 ++vcol;
12633 }
12634
12635 rettv->vval.v_number = vcol;
12636}
12637
12638/*
12639 * "visualmode()" function
12640 */
12641 static void
12642f_visualmode(typval_T *argvars, typval_T *rettv)
12643{
12644 char_u str[2];
12645
12646 rettv->v_type = VAR_STRING;
12647 str[0] = curbuf->b_visual_mode_eval;
12648 str[1] = NUL;
12649 rettv->vval.v_string = vim_strsave(str);
12650
12651 /* A non-zero number or non-empty string argument: reset mode. */
12652 if (non_zero_arg(&argvars[0]))
12653 curbuf->b_visual_mode_eval = NUL;
12654}
12655
12656/*
12657 * "wildmenumode()" function
12658 */
12659 static void
12660f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12661{
12662#ifdef FEAT_WILDMENU
12663 if (wild_menu_showing)
12664 rettv->vval.v_number = 1;
12665#endif
12666}
12667
12668/*
12669 * "winbufnr(nr)" function
12670 */
12671 static void
12672f_winbufnr(typval_T *argvars, typval_T *rettv)
12673{
12674 win_T *wp;
12675
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012676 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (wp == NULL)
12678 rettv->vval.v_number = -1;
12679 else
12680 rettv->vval.v_number = wp->w_buffer->b_fnum;
12681}
12682
12683/*
12684 * "wincol()" function
12685 */
12686 static void
12687f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12688{
12689 validate_cursor();
12690 rettv->vval.v_number = curwin->w_wcol + 1;
12691}
12692
12693/*
12694 * "winheight(nr)" function
12695 */
12696 static void
12697f_winheight(typval_T *argvars, typval_T *rettv)
12698{
12699 win_T *wp;
12700
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012701 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012702 if (wp == NULL)
12703 rettv->vval.v_number = -1;
12704 else
12705 rettv->vval.v_number = wp->w_height;
12706}
12707
12708/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012709 * "winlayout()" function
12710 */
12711 static void
12712f_winlayout(typval_T *argvars, typval_T *rettv)
12713{
12714 tabpage_T *tp;
12715
12716 if (rettv_list_alloc(rettv) != OK)
12717 return;
12718
12719 if (argvars[0].v_type == VAR_UNKNOWN)
12720 tp = curtab;
12721 else
12722 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012723 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012724 if (tp == NULL)
12725 return;
12726 }
12727
12728 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12729}
12730
12731/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732 * "winline()" function
12733 */
12734 static void
12735f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12736{
12737 validate_cursor();
12738 rettv->vval.v_number = curwin->w_wrow + 1;
12739}
12740
12741/*
12742 * "winnr()" function
12743 */
12744 static void
12745f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12746{
12747 int nr = 1;
12748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750 rettv->vval.v_number = nr;
12751}
12752
12753/*
12754 * "winrestcmd()" function
12755 */
12756 static void
12757f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12758{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759 win_T *wp;
12760 int winnr = 1;
12761 garray_T ga;
12762 char_u buf[50];
12763
12764 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012765 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 {
12767 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12768 ga_concat(&ga, buf);
12769 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12770 ga_concat(&ga, buf);
12771 ++winnr;
12772 }
12773 ga_append(&ga, NUL);
12774
12775 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 rettv->v_type = VAR_STRING;
12777}
12778
12779/*
12780 * "winrestview()" function
12781 */
12782 static void
12783f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12784{
12785 dict_T *dict;
12786
12787 if (argvars[0].v_type != VAR_DICT
12788 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012789 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 else
12791 {
12792 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012793 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012795 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012796 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012797 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012798 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12799 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010012800 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801 curwin->w_set_curswant = FALSE;
12802 }
12803
12804 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012805 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012806#ifdef FEAT_DIFF
12807 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012808 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809#endif
12810 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012811 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012812 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012813 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814
12815 check_cursor();
12816 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020012817 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 changed_window_setting();
12819
12820 if (curwin->w_topline <= 0)
12821 curwin->w_topline = 1;
12822 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12823 curwin->w_topline = curbuf->b_ml.ml_line_count;
12824#ifdef FEAT_DIFF
12825 check_topfill(curwin, TRUE);
12826#endif
12827 }
12828}
12829
12830/*
12831 * "winsaveview()" function
12832 */
12833 static void
12834f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12835{
12836 dict_T *dict;
12837
12838 if (rettv_dict_alloc(rettv) == FAIL)
12839 return;
12840 dict = rettv->vval.v_dict;
12841
Bram Moolenaare0be1672018-07-08 16:50:37 +020012842 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
12843 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020012844 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012845 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020012846 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012847
Bram Moolenaare0be1672018-07-08 16:50:37 +020012848 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020012850 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020012852 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
12853 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012854}
12855
12856/*
12857 * "winwidth(nr)" function
12858 */
12859 static void
12860f_winwidth(typval_T *argvars, typval_T *rettv)
12861{
12862 win_T *wp;
12863
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012864 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865 if (wp == NULL)
12866 rettv->vval.v_number = -1;
12867 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012869}
12870
12871/*
12872 * "wordcount()" function
12873 */
12874 static void
12875f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
12876{
12877 if (rettv_dict_alloc(rettv) == FAIL)
12878 return;
12879 cursor_pos_info(rettv->vval.v_dict);
12880}
12881
12882/*
12883 * "writefile()" function
12884 */
12885 static void
12886f_writefile(typval_T *argvars, typval_T *rettv)
12887{
12888 int binary = FALSE;
12889 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012890#ifdef HAVE_FSYNC
12891 int do_fsync = p_fs;
12892#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 char_u *fname;
12894 FILE *fd;
12895 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012896 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012897 list_T *list = NULL;
12898 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012899
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012900 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010012901 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902 return;
12903
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012904 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012906 list = argvars[0].vval.v_list;
12907 if (list == NULL)
12908 return;
12909 for (li = list->lv_first; li != NULL; li = li->li_next)
12910 if (tv_get_string_chk(&li->li_tv) == NULL)
12911 return;
12912 }
12913 else if (argvars[0].v_type == VAR_BLOB)
12914 {
12915 blob = argvars[0].vval.v_blob;
12916 if (blob == NULL)
12917 return;
12918 }
12919 else
12920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012921 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 return;
12923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924
12925 if (argvars[2].v_type != VAR_UNKNOWN)
12926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012927 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012928
12929 if (arg2 == NULL)
12930 return;
12931 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012933 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012935#ifdef HAVE_FSYNC
12936 if (vim_strchr(arg2, 's') != NULL)
12937 do_fsync = TRUE;
12938 else if (vim_strchr(arg2, 'S') != NULL)
12939 do_fsync = FALSE;
12940#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012941 }
12942
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012943 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012944 if (fname == NULL)
12945 return;
12946
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 /* Always open the file in binary mode, library functions have a mind of
12948 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949 if (*fname == NUL || (fd = mch_fopen((char *)fname,
12950 append ? APPENDBIN : WRITEBIN)) == NULL)
12951 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012952 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012953 ret = -1;
12954 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012955 else if (blob)
12956 {
12957 if (write_blob(fd, blob) == FAIL)
12958 ret = -1;
12959#ifdef HAVE_FSYNC
12960 else if (do_fsync)
12961 // Ignore the error, the user wouldn't know what to do about it.
12962 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010012963 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012964#endif
12965 fclose(fd);
12966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012967 else
12968 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012969 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012970 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012971#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010012972 else if (do_fsync)
12973 /* Ignore the error, the user wouldn't know what to do about it.
12974 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010012975 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 fclose(fd);
12978 }
12979
12980 rettv->vval.v_number = ret;
12981}
12982
12983/*
12984 * "xor(expr, expr)" function
12985 */
12986 static void
12987f_xor(typval_T *argvars, typval_T *rettv)
12988{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012989 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
12990 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012991}
12992
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012993#endif /* FEAT_EVAL */