blob: 8253df8201f4ef056255e9f8209231ef3ac1ea06 [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);
78#if defined(FEAT_INS_EXPAND)
79static void f_complete(typval_T *argvars, typval_T *rettv);
80static void f_complete_add(typval_T *argvars, typval_T *rettv);
81static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010082static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083#endif
84static void f_confirm(typval_T *argvars, typval_T *rettv);
85static void f_copy(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_cos(typval_T *argvars, typval_T *rettv);
88static void f_cosh(typval_T *argvars, typval_T *rettv);
89#endif
90static void f_count(typval_T *argvars, typval_T *rettv);
91static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
92static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010093#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020094static void f_debugbreak(typval_T *argvars, typval_T *rettv);
95#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_deepcopy(typval_T *argvars, typval_T *rettv);
97static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +020098static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_did_filetype(typval_T *argvars, typval_T *rettv);
100static void f_diff_filler(typval_T *argvars, typval_T *rettv);
101static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
102static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200103static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200104static void f_escape(typval_T *argvars, typval_T *rettv);
105static void f_eval(typval_T *argvars, typval_T *rettv);
106static void f_eventhandler(typval_T *argvars, typval_T *rettv);
107static void f_executable(typval_T *argvars, typval_T *rettv);
108static void f_execute(typval_T *argvars, typval_T *rettv);
109static void f_exepath(typval_T *argvars, typval_T *rettv);
110static void f_exists(typval_T *argvars, typval_T *rettv);
111#ifdef FEAT_FLOAT
112static void f_exp(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200115static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_extend(typval_T *argvars, typval_T *rettv);
117static void f_feedkeys(typval_T *argvars, typval_T *rettv);
118static void f_filereadable(typval_T *argvars, typval_T *rettv);
119static void f_filewritable(typval_T *argvars, typval_T *rettv);
120static void f_filter(typval_T *argvars, typval_T *rettv);
121static void f_finddir(typval_T *argvars, typval_T *rettv);
122static void f_findfile(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_float2nr(typval_T *argvars, typval_T *rettv);
125static void f_floor(typval_T *argvars, typval_T *rettv);
126static void f_fmod(typval_T *argvars, typval_T *rettv);
127#endif
128static void f_fnameescape(typval_T *argvars, typval_T *rettv);
129static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
130static void f_foldclosed(typval_T *argvars, typval_T *rettv);
131static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
132static void f_foldlevel(typval_T *argvars, typval_T *rettv);
133static void f_foldtext(typval_T *argvars, typval_T *rettv);
134static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
135static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200136static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200137static void f_function(typval_T *argvars, typval_T *rettv);
138static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
139static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200140static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_getbufline(typval_T *argvars, typval_T *rettv);
142static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100143static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200144static void f_getchar(typval_T *argvars, typval_T *rettv);
145static void f_getcharmod(typval_T *argvars, typval_T *rettv);
146static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
147static void f_getcmdline(typval_T *argvars, typval_T *rettv);
148#if defined(FEAT_CMDL_COMPL)
149static void f_getcompletion(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
152static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
153static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
154static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200155static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getfontname(typval_T *argvars, typval_T *rettv);
157static void f_getfperm(typval_T *argvars, typval_T *rettv);
158static void f_getfsize(typval_T *argvars, typval_T *rettv);
159static void f_getftime(typval_T *argvars, typval_T *rettv);
160static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100161static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200163static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_getpid(typval_T *argvars, typval_T *rettv);
165static void f_getcurpos(typval_T *argvars, typval_T *rettv);
166static void f_getpos(typval_T *argvars, typval_T *rettv);
167static void f_getqflist(typval_T *argvars, typval_T *rettv);
168static void f_getreg(typval_T *argvars, typval_T *rettv);
169static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200170static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_gettabvar(typval_T *argvars, typval_T *rettv);
172static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100173static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200174static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100175static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200176static void f_getwinposx(typval_T *argvars, typval_T *rettv);
177static void f_getwinposy(typval_T *argvars, typval_T *rettv);
178static void f_getwinvar(typval_T *argvars, typval_T *rettv);
179static void f_glob(typval_T *argvars, typval_T *rettv);
180static void f_globpath(typval_T *argvars, typval_T *rettv);
181static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
182static void f_has(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
184static void f_hasmapto(typval_T *argvars, typval_T *rettv);
185static void f_histadd(typval_T *argvars, typval_T *rettv);
186static void f_histdel(typval_T *argvars, typval_T *rettv);
187static void f_histget(typval_T *argvars, typval_T *rettv);
188static void f_histnr(typval_T *argvars, typval_T *rettv);
189static void f_hlID(typval_T *argvars, typval_T *rettv);
190static void f_hlexists(typval_T *argvars, typval_T *rettv);
191static void f_hostname(typval_T *argvars, typval_T *rettv);
192static void f_iconv(typval_T *argvars, typval_T *rettv);
193static void f_indent(typval_T *argvars, typval_T *rettv);
194static void f_index(typval_T *argvars, typval_T *rettv);
195static void f_input(typval_T *argvars, typval_T *rettv);
196static void f_inputdialog(typval_T *argvars, typval_T *rettv);
197static void f_inputlist(typval_T *argvars, typval_T *rettv);
198static void f_inputrestore(typval_T *argvars, typval_T *rettv);
199static void f_inputsave(typval_T *argvars, typval_T *rettv);
200static void f_inputsecret(typval_T *argvars, typval_T *rettv);
201static void f_insert(typval_T *argvars, typval_T *rettv);
202static void f_invert(typval_T *argvars, typval_T *rettv);
203static void f_isdirectory(typval_T *argvars, typval_T *rettv);
204static void f_islocked(typval_T *argvars, typval_T *rettv);
205#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200206static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_isnan(typval_T *argvars, typval_T *rettv);
208#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
210static void f_len(typval_T *argvars, typval_T *rettv);
211static void f_libcall(typval_T *argvars, typval_T *rettv);
212static void f_libcallnr(typval_T *argvars, typval_T *rettv);
213static void f_line(typval_T *argvars, typval_T *rettv);
214static void f_line2byte(typval_T *argvars, typval_T *rettv);
215static void f_lispindent(typval_T *argvars, typval_T *rettv);
216static void f_localtime(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_log(typval_T *argvars, typval_T *rettv);
219static void f_log10(typval_T *argvars, typval_T *rettv);
220#endif
221#ifdef FEAT_LUA
222static void f_luaeval(typval_T *argvars, typval_T *rettv);
223#endif
224static void f_map(typval_T *argvars, typval_T *rettv);
225static void f_maparg(typval_T *argvars, typval_T *rettv);
226static void f_mapcheck(typval_T *argvars, typval_T *rettv);
227static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_matchend(typval_T *argvars, typval_T *rettv);
229static void f_matchlist(typval_T *argvars, typval_T *rettv);
230static void f_matchstr(typval_T *argvars, typval_T *rettv);
231static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
232static void f_max(typval_T *argvars, typval_T *rettv);
233static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200235static void f_mode(typval_T *argvars, typval_T *rettv);
236#ifdef FEAT_MZSCHEME
237static void f_mzeval(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
240static void f_nr2char(typval_T *argvars, typval_T *rettv);
241static void f_or(typval_T *argvars, typval_T *rettv);
242static void f_pathshorten(typval_T *argvars, typval_T *rettv);
243#ifdef FEAT_PERL
244static void f_perleval(typval_T *argvars, typval_T *rettv);
245#endif
246#ifdef FEAT_FLOAT
247static void f_pow(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
250static void f_printf(typval_T *argvars, typval_T *rettv);
251static void f_pumvisible(typval_T *argvars, typval_T *rettv);
252#ifdef FEAT_PYTHON3
253static void f_py3eval(typval_T *argvars, typval_T *rettv);
254#endif
255#ifdef FEAT_PYTHON
256static void f_pyeval(typval_T *argvars, typval_T *rettv);
257#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100258#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
259static void f_pyxeval(typval_T *argvars, typval_T *rettv);
260#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200262static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200263static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200264static void f_reg_executing(typval_T *argvars, typval_T *rettv);
265static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200266static void f_reltime(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_FLOAT
268static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_reltimestr(typval_T *argvars, typval_T *rettv);
271static void f_remote_expr(typval_T *argvars, typval_T *rettv);
272static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
273static void f_remote_peek(typval_T *argvars, typval_T *rettv);
274static void f_remote_read(typval_T *argvars, typval_T *rettv);
275static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100276static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200277static void f_remove(typval_T *argvars, typval_T *rettv);
278static void f_rename(typval_T *argvars, typval_T *rettv);
279static void f_repeat(typval_T *argvars, typval_T *rettv);
280static void f_resolve(typval_T *argvars, typval_T *rettv);
281static void f_reverse(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_FLOAT
283static void f_round(typval_T *argvars, typval_T *rettv);
284#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100285#ifdef FEAT_RUBY
286static void f_rubyeval(typval_T *argvars, typval_T *rettv);
287#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200288static void f_screenattr(typval_T *argvars, typval_T *rettv);
289static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100290static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200291static void f_screencol(typval_T *argvars, typval_T *rettv);
292static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100293static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_search(typval_T *argvars, typval_T *rettv);
295static void f_searchdecl(typval_T *argvars, typval_T *rettv);
296static void f_searchpair(typval_T *argvars, typval_T *rettv);
297static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
298static void f_searchpos(typval_T *argvars, typval_T *rettv);
299static void f_server2client(typval_T *argvars, typval_T *rettv);
300static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200301static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200302static void f_setbufvar(typval_T *argvars, typval_T *rettv);
303static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
304static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200305static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_setfperm(typval_T *argvars, typval_T *rettv);
307static void f_setline(typval_T *argvars, typval_T *rettv);
308static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200309static void f_setpos(typval_T *argvars, typval_T *rettv);
310static void f_setqflist(typval_T *argvars, typval_T *rettv);
311static void f_setreg(typval_T *argvars, typval_T *rettv);
312static void f_settabvar(typval_T *argvars, typval_T *rettv);
313static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100314static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200315static void f_setwinvar(typval_T *argvars, typval_T *rettv);
316#ifdef FEAT_CRYPT
317static void f_sha256(typval_T *argvars, typval_T *rettv);
318#endif /* FEAT_CRYPT */
319static void f_shellescape(typval_T *argvars, typval_T *rettv);
320static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
321static void f_simplify(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_sin(typval_T *argvars, typval_T *rettv);
324static void f_sinh(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_soundfold(typval_T *argvars, typval_T *rettv);
327static void f_spellbadword(typval_T *argvars, typval_T *rettv);
328static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
329static void f_split(typval_T *argvars, typval_T *rettv);
330#ifdef FEAT_FLOAT
331static void f_sqrt(typval_T *argvars, typval_T *rettv);
332static void f_str2float(typval_T *argvars, typval_T *rettv);
333#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200334static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_str2nr(typval_T *argvars, typval_T *rettv);
336static void f_strchars(typval_T *argvars, typval_T *rettv);
337#ifdef HAVE_STRFTIME
338static void f_strftime(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_strgetchar(typval_T *argvars, typval_T *rettv);
341static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_strlen(typval_T *argvars, typval_T *rettv);
343static void f_strcharpart(typval_T *argvars, typval_T *rettv);
344static void f_strpart(typval_T *argvars, typval_T *rettv);
345static void f_strridx(typval_T *argvars, typval_T *rettv);
346static void f_strtrans(typval_T *argvars, typval_T *rettv);
347static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
348static void f_strwidth(typval_T *argvars, typval_T *rettv);
349static void f_submatch(typval_T *argvars, typval_T *rettv);
350static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200351static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200352static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_synID(typval_T *argvars, typval_T *rettv);
354static void f_synIDattr(typval_T *argvars, typval_T *rettv);
355static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
356static void f_synstack(typval_T *argvars, typval_T *rettv);
357static void f_synconcealed(typval_T *argvars, typval_T *rettv);
358static void f_system(typval_T *argvars, typval_T *rettv);
359static void f_systemlist(typval_T *argvars, typval_T *rettv);
360static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
361static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
362static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
363static void f_taglist(typval_T *argvars, typval_T *rettv);
364static void f_tagfiles(typval_T *argvars, typval_T *rettv);
365static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366#ifdef FEAT_FLOAT
367static void f_tan(typval_T *argvars, typval_T *rettv);
368static void f_tanh(typval_T *argvars, typval_T *rettv);
369#endif
370#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200371static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200372static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200373static void f_timer_start(typval_T *argvars, typval_T *rettv);
374static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200375static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376#endif
377static void f_tolower(typval_T *argvars, typval_T *rettv);
378static void f_toupper(typval_T *argvars, typval_T *rettv);
379static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100380static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381#ifdef FEAT_FLOAT
382static void f_trunc(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_type(typval_T *argvars, typval_T *rettv);
385static void f_undofile(typval_T *argvars, typval_T *rettv);
386static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387static void f_virtcol(typval_T *argvars, typval_T *rettv);
388static void f_visualmode(typval_T *argvars, typval_T *rettv);
389static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200390static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
392static void f_win_getid(typval_T *argvars, typval_T *rettv);
393static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
394static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
395static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100396static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_winbufnr(typval_T *argvars, typval_T *rettv);
398static void f_wincol(typval_T *argvars, typval_T *rettv);
399static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200400static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_winline(typval_T *argvars, typval_T *rettv);
402static void f_winnr(typval_T *argvars, typval_T *rettv);
403static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
404static void f_winrestview(typval_T *argvars, typval_T *rettv);
405static void f_winsaveview(typval_T *argvars, typval_T *rettv);
406static void f_winwidth(typval_T *argvars, typval_T *rettv);
407static void f_writefile(typval_T *argvars, typval_T *rettv);
408static void f_wordcount(typval_T *argvars, typval_T *rettv);
409static void f_xor(typval_T *argvars, typval_T *rettv);
410
411/*
412 * Array with names and number of arguments of all internal functions
413 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
414 */
415static struct fst
416{
417 char *f_name; /* function name */
418 char f_min_argc; /* minimal number of arguments */
419 char f_max_argc; /* maximal number of arguments */
420 void (*f_func)(typval_T *args, typval_T *rvar);
421 /* implementation of function */
422} functions[] =
423{
424#ifdef FEAT_FLOAT
425 {"abs", 1, 1, f_abs},
426 {"acos", 1, 1, f_acos}, /* WJMc */
427#endif
428 {"add", 2, 2, f_add},
429 {"and", 2, 2, f_and},
430 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200431 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200432 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200433 {"argidx", 0, 0, f_argidx},
434 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200435 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436#ifdef FEAT_FLOAT
437 {"asin", 1, 1, f_asin}, /* WJMc */
438#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100439 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200440 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100441 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200442 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200443 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100445 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446 {"assert_match", 2, 3, f_assert_match},
447 {"assert_notequal", 2, 3, f_assert_notequal},
448 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100449 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450 {"assert_true", 1, 2, f_assert_true},
451#ifdef FEAT_FLOAT
452 {"atan", 1, 1, f_atan},
453 {"atan2", 2, 2, f_atan2},
454#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100455#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200456 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100457 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100458# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100459 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100460# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462 {"browse", 4, 4, f_browse},
463 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200464 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200465 {"bufexists", 1, 1, f_bufexists},
466 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
467 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
468 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
469 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200470 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200471 {"bufloaded", 1, 1, f_bufloaded},
472 {"bufname", 1, 1, f_bufname},
473 {"bufnr", 1, 2, f_bufnr},
474 {"bufwinid", 1, 1, f_bufwinid},
475 {"bufwinnr", 1, 1, f_bufwinnr},
476 {"byte2line", 1, 1, f_byte2line},
477 {"byteidx", 2, 2, f_byteidx},
478 {"byteidxcomp", 2, 2, f_byteidxcomp},
479 {"call", 2, 3, f_call},
480#ifdef FEAT_FLOAT
481 {"ceil", 1, 1, f_ceil},
482#endif
483#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100484 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200486 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200487 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
488 {"ch_evalraw", 2, 3, f_ch_evalraw},
489 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
490 {"ch_getjob", 1, 1, f_ch_getjob},
491 {"ch_info", 1, 1, f_ch_info},
492 {"ch_log", 1, 2, f_ch_log},
493 {"ch_logfile", 1, 2, f_ch_logfile},
494 {"ch_open", 1, 2, f_ch_open},
495 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100496 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"ch_readraw", 1, 2, f_ch_readraw},
498 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
499 {"ch_sendraw", 2, 3, f_ch_sendraw},
500 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200501 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502#endif
503 {"changenr", 0, 0, f_changenr},
504 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200505 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100507 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"col", 1, 1, f_col},
509#if defined(FEAT_INS_EXPAND)
510 {"complete", 2, 2, f_complete},
511 {"complete_add", 1, 1, f_complete_add},
512 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100513 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514#endif
515 {"confirm", 1, 4, f_confirm},
516 {"copy", 1, 1, f_copy},
517#ifdef FEAT_FLOAT
518 {"cos", 1, 1, f_cos},
519 {"cosh", 1, 1, f_cosh},
520#endif
521 {"count", 2, 4, f_count},
522 {"cscope_connection",0,3, f_cscope_connection},
523 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100524#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200525 {"debugbreak", 1, 1, f_debugbreak},
526#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"deepcopy", 1, 2, f_deepcopy},
528 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200529 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200530 {"did_filetype", 0, 0, f_did_filetype},
531 {"diff_filler", 1, 1, f_diff_filler},
532 {"diff_hlID", 2, 2, f_diff_hlID},
533 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200534 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"escape", 2, 2, f_escape},
536 {"eval", 1, 1, f_eval},
537 {"eventhandler", 0, 0, f_eventhandler},
538 {"executable", 1, 1, f_executable},
539 {"execute", 1, 2, f_execute},
540 {"exepath", 1, 1, f_exepath},
541 {"exists", 1, 1, f_exists},
542#ifdef FEAT_FLOAT
543 {"exp", 1, 1, f_exp},
544#endif
545 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200546 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"extend", 2, 3, f_extend},
548 {"feedkeys", 1, 2, f_feedkeys},
549 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
550 {"filereadable", 1, 1, f_filereadable},
551 {"filewritable", 1, 1, f_filewritable},
552 {"filter", 2, 2, f_filter},
553 {"finddir", 1, 3, f_finddir},
554 {"findfile", 1, 3, f_findfile},
555#ifdef FEAT_FLOAT
556 {"float2nr", 1, 1, f_float2nr},
557 {"floor", 1, 1, f_floor},
558 {"fmod", 2, 2, f_fmod},
559#endif
560 {"fnameescape", 1, 1, f_fnameescape},
561 {"fnamemodify", 2, 2, f_fnamemodify},
562 {"foldclosed", 1, 1, f_foldclosed},
563 {"foldclosedend", 1, 1, f_foldclosedend},
564 {"foldlevel", 1, 1, f_foldlevel},
565 {"foldtext", 0, 0, f_foldtext},
566 {"foldtextresult", 1, 1, f_foldtextresult},
567 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200568 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569 {"function", 1, 3, f_function},
570 {"garbagecollect", 0, 1, f_garbagecollect},
571 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200572 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200573 {"getbufline", 2, 3, f_getbufline},
574 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100575 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200576 {"getchar", 0, 1, f_getchar},
577 {"getcharmod", 0, 0, f_getcharmod},
578 {"getcharsearch", 0, 0, f_getcharsearch},
579 {"getcmdline", 0, 0, f_getcmdline},
580 {"getcmdpos", 0, 0, f_getcmdpos},
581 {"getcmdtype", 0, 0, f_getcmdtype},
582 {"getcmdwintype", 0, 0, f_getcmdwintype},
583#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200584 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200585#endif
586 {"getcurpos", 0, 0, f_getcurpos},
587 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200588 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"getfontname", 0, 1, f_getfontname},
590 {"getfperm", 1, 1, f_getfperm},
591 {"getfsize", 1, 1, f_getfsize},
592 {"getftime", 1, 1, f_getftime},
593 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100594 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200596 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100597 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"getpid", 0, 0, f_getpid},
599 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200600 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"getreg", 0, 3, f_getreg},
602 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200603 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604 {"gettabvar", 2, 3, f_gettabvar},
605 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100606 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200607 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100608 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"getwinposx", 0, 0, f_getwinposx},
610 {"getwinposy", 0, 0, f_getwinposy},
611 {"getwinvar", 2, 3, f_getwinvar},
612 {"glob", 1, 4, f_glob},
613 {"glob2regpat", 1, 1, f_glob2regpat},
614 {"globpath", 2, 5, f_globpath},
615 {"has", 1, 1, f_has},
616 {"has_key", 2, 2, f_has_key},
617 {"haslocaldir", 0, 2, f_haslocaldir},
618 {"hasmapto", 1, 3, f_hasmapto},
619 {"highlightID", 1, 1, f_hlID}, /* obsolete */
620 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
621 {"histadd", 2, 2, f_histadd},
622 {"histdel", 1, 2, f_histdel},
623 {"histget", 1, 2, f_histget},
624 {"histnr", 1, 1, f_histnr},
625 {"hlID", 1, 1, f_hlID},
626 {"hlexists", 1, 1, f_hlexists},
627 {"hostname", 0, 0, f_hostname},
628 {"iconv", 3, 3, f_iconv},
629 {"indent", 1, 1, f_indent},
630 {"index", 2, 4, f_index},
631 {"input", 1, 3, f_input},
632 {"inputdialog", 1, 3, f_inputdialog},
633 {"inputlist", 1, 1, f_inputlist},
634 {"inputrestore", 0, 0, f_inputrestore},
635 {"inputsave", 0, 0, f_inputsave},
636 {"inputsecret", 1, 2, f_inputsecret},
637 {"insert", 2, 3, f_insert},
638 {"invert", 1, 1, f_invert},
639 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200640#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
641 {"isinf", 1, 1, f_isinf},
642#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643 {"islocked", 1, 1, f_islocked},
644#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
645 {"isnan", 1, 1, f_isnan},
646#endif
647 {"items", 1, 1, f_items},
648#ifdef FEAT_JOB_CHANNEL
649 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200650 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"job_setoptions", 2, 2, f_job_setoptions},
652 {"job_start", 1, 2, f_job_start},
653 {"job_status", 1, 1, f_job_status},
654 {"job_stop", 1, 2, f_job_stop},
655#endif
656 {"join", 1, 2, f_join},
657 {"js_decode", 1, 1, f_js_decode},
658 {"js_encode", 1, 1, f_js_encode},
659 {"json_decode", 1, 1, f_json_decode},
660 {"json_encode", 1, 1, f_json_encode},
661 {"keys", 1, 1, f_keys},
662 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
663 {"len", 1, 1, f_len},
664 {"libcall", 3, 3, f_libcall},
665 {"libcallnr", 3, 3, f_libcallnr},
666 {"line", 1, 1, f_line},
667 {"line2byte", 1, 1, f_line2byte},
668 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200669 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200670 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200671 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200672 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"localtime", 0, 0, f_localtime},
674#ifdef FEAT_FLOAT
675 {"log", 1, 1, f_log},
676 {"log10", 1, 1, f_log10},
677#endif
678#ifdef FEAT_LUA
679 {"luaeval", 1, 2, f_luaeval},
680#endif
681 {"map", 2, 2, f_map},
682 {"maparg", 1, 4, f_maparg},
683 {"mapcheck", 1, 3, f_mapcheck},
684 {"match", 2, 4, f_match},
685 {"matchadd", 2, 5, f_matchadd},
686 {"matchaddpos", 2, 5, f_matchaddpos},
687 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100688 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689 {"matchend", 2, 4, f_matchend},
690 {"matchlist", 2, 4, f_matchlist},
691 {"matchstr", 2, 4, f_matchstr},
692 {"matchstrpos", 2, 4, f_matchstrpos},
693 {"max", 1, 1, f_max},
694 {"min", 1, 1, f_min},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200695 {"mkdir", 1, 3, f_mkdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"mode", 0, 1, f_mode},
697#ifdef FEAT_MZSCHEME
698 {"mzeval", 1, 1, f_mzeval},
699#endif
700 {"nextnonblank", 1, 1, f_nextnonblank},
701 {"nr2char", 1, 2, f_nr2char},
702 {"or", 2, 2, f_or},
703 {"pathshorten", 1, 1, f_pathshorten},
704#ifdef FEAT_PERL
705 {"perleval", 1, 1, f_perleval},
706#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200707#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200708 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200709 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200710 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200711 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200712 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200713 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200714 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200715 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200716 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200717 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar56c0c472019-07-28 17:57:43 +0200718 {"popup_getpreview", 0, 0, f_popup_getpreview},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200719 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200720 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200721 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200722 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200723 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200724 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200725 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200726 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200727#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200728#ifdef FEAT_FLOAT
729 {"pow", 2, 2, f_pow},
730#endif
731 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100732 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200733#ifdef FEAT_JOB_CHANNEL
734 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200735 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200736 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
737#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100738#ifdef FEAT_TEXT_PROP
739 {"prop_add", 3, 3, f_prop_add},
740 {"prop_clear", 1, 3, f_prop_clear},
741 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100742 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100743 {"prop_type_add", 2, 2, f_prop_type_add},
744 {"prop_type_change", 2, 2, f_prop_type_change},
745 {"prop_type_delete", 1, 2, f_prop_type_delete},
746 {"prop_type_get", 1, 2, f_prop_type_get},
747 {"prop_type_list", 0, 1, f_prop_type_list},
748#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200749 {"pumvisible", 0, 0, f_pumvisible},
750#ifdef FEAT_PYTHON3
751 {"py3eval", 1, 1, f_py3eval},
752#endif
753#ifdef FEAT_PYTHON
754 {"pyeval", 1, 1, f_pyeval},
755#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100756#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
757 {"pyxeval", 1, 1, f_pyxeval},
758#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200760 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200762 {"reg_executing", 0, 0, f_reg_executing},
763 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200764 {"reltime", 0, 2, f_reltime},
765#ifdef FEAT_FLOAT
766 {"reltimefloat", 1, 1, f_reltimefloat},
767#endif
768 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100769 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200770 {"remote_foreground", 1, 1, f_remote_foreground},
771 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100772 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100774 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"remove", 2, 3, f_remove},
776 {"rename", 2, 2, f_rename},
777 {"repeat", 2, 2, f_repeat},
778 {"resolve", 1, 1, f_resolve},
779 {"reverse", 1, 1, f_reverse},
780#ifdef FEAT_FLOAT
781 {"round", 1, 1, f_round},
782#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100783#ifdef FEAT_RUBY
784 {"rubyeval", 1, 1, f_rubyeval},
785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"screenattr", 2, 2, f_screenattr},
787 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100788 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200790 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100792 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"search", 1, 4, f_search},
794 {"searchdecl", 1, 3, f_searchdecl},
795 {"searchpair", 3, 7, f_searchpair},
796 {"searchpairpos", 3, 7, f_searchpairpos},
797 {"searchpos", 1, 4, f_searchpos},
798 {"server2client", 2, 2, f_server2client},
799 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200800 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200801 {"setbufvar", 3, 3, f_setbufvar},
802 {"setcharsearch", 1, 1, f_setcharsearch},
803 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200804 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200805 {"setfperm", 2, 2, f_setfperm},
806 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200807 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100808 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200809 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200810 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"setreg", 2, 3, f_setreg},
812 {"settabvar", 3, 3, f_settabvar},
813 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100814 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"setwinvar", 3, 3, f_setwinvar},
816#ifdef FEAT_CRYPT
817 {"sha256", 1, 1, f_sha256},
818#endif
819 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100820 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100821#ifdef FEAT_SIGNS
822 {"sign_define", 1, 2, f_sign_define},
823 {"sign_getdefined", 0, 1, f_sign_getdefined},
824 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100825 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100826 {"sign_place", 4, 5, f_sign_place},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200827 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100828 {"sign_undefine", 0, 1, f_sign_undefine},
829 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200830 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100831#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"simplify", 1, 1, f_simplify},
833#ifdef FEAT_FLOAT
834 {"sin", 1, 1, f_sin},
835 {"sinh", 1, 1, f_sinh},
836#endif
837 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200838#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200839 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200840 {"sound_playevent", 1, 2, f_sound_playevent},
841 {"sound_playfile", 1, 2, f_sound_playfile},
842 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200843#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"soundfold", 1, 1, f_soundfold},
845 {"spellbadword", 0, 1, f_spellbadword},
846 {"spellsuggest", 1, 3, f_spellsuggest},
847 {"split", 1, 3, f_split},
848#ifdef FEAT_FLOAT
849 {"sqrt", 1, 1, f_sqrt},
850 {"str2float", 1, 1, f_str2float},
851#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200852 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200853 {"str2nr", 1, 2, f_str2nr},
854 {"strcharpart", 2, 3, f_strcharpart},
855 {"strchars", 1, 2, f_strchars},
856 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
857#ifdef HAVE_STRFTIME
858 {"strftime", 1, 2, f_strftime},
859#endif
860 {"strgetchar", 2, 2, f_strgetchar},
861 {"stridx", 2, 3, f_stridx},
862 {"string", 1, 1, f_string},
863 {"strlen", 1, 1, f_strlen},
864 {"strpart", 2, 3, f_strpart},
865 {"strridx", 2, 3, f_strridx},
866 {"strtrans", 1, 1, f_strtrans},
867 {"strwidth", 1, 1, f_strwidth},
868 {"submatch", 1, 2, f_submatch},
869 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200870 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200871 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"synID", 3, 3, f_synID},
873 {"synIDattr", 2, 3, f_synIDattr},
874 {"synIDtrans", 1, 1, f_synIDtrans},
875 {"synconcealed", 2, 2, f_synconcealed},
876 {"synstack", 2, 2, f_synstack},
877 {"system", 1, 2, f_system},
878 {"systemlist", 1, 2, f_systemlist},
879 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
880 {"tabpagenr", 0, 1, f_tabpagenr},
881 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
882 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100883 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884#ifdef FEAT_FLOAT
885 {"tan", 1, 1, f_tan},
886 {"tanh", 1, 1, f_tanh},
887#endif
888 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200889#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100890 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
891 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100892 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200893 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200894# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
895 {"term_getansicolors", 1, 1, f_term_getansicolors},
896# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200898 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200899 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200900 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200901 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200903 {"term_getstatus", 1, 1, f_term_getstatus},
904 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200905 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200907 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200909# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
910 {"term_setansicolors", 2, 2, f_term_setansicolors},
911# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100912 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100913 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200914 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200916 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
919 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200920 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200922 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200923 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100924 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100925 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926#ifdef FEAT_JOB_CHANNEL
927 {"test_null_channel", 0, 0, f_test_null_channel},
928#endif
929 {"test_null_dict", 0, 0, f_test_null_dict},
930#ifdef FEAT_JOB_CHANNEL
931 {"test_null_job", 0, 0, f_test_null_job},
932#endif
933 {"test_null_list", 0, 0, f_test_null_list},
934 {"test_null_partial", 0, 0, f_test_null_partial},
935 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200936 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100937 {"test_override", 2, 2, f_test_override},
938 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200939#ifdef FEAT_GUI
940 {"test_scrollbar", 3, 3, f_test_scrollbar},
941#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200942#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200943 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200944#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945 {"test_settime", 1, 1, f_test_settime},
946#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200947 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200948 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949 {"timer_start", 2, 3, f_timer_start},
950 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200951 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952#endif
953 {"tolower", 1, 1, f_tolower},
954 {"toupper", 1, 1, f_toupper},
955 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100956 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#ifdef FEAT_FLOAT
958 {"trunc", 1, 1, f_trunc},
959#endif
960 {"type", 1, 1, f_type},
961 {"undofile", 1, 1, f_undofile},
962 {"undotree", 0, 0, f_undotree},
963 {"uniq", 1, 3, f_uniq},
964 {"values", 1, 1, f_values},
965 {"virtcol", 1, 1, f_virtcol},
966 {"visualmode", 0, 1, f_visualmode},
967 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200968 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969 {"win_findbuf", 1, 1, f_win_findbuf},
970 {"win_getid", 0, 2, f_win_getid},
971 {"win_gotoid", 1, 1, f_win_gotoid},
972 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
973 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100974 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975 {"winbufnr", 1, 1, f_winbufnr},
976 {"wincol", 0, 0, f_wincol},
977 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200978 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200979 {"winline", 0, 0, f_winline},
980 {"winnr", 0, 1, f_winnr},
981 {"winrestcmd", 0, 0, f_winrestcmd},
982 {"winrestview", 1, 1, f_winrestview},
983 {"winsaveview", 0, 0, f_winsaveview},
984 {"winwidth", 1, 1, f_winwidth},
985 {"wordcount", 0, 0, f_wordcount},
986 {"writefile", 2, 3, f_writefile},
987 {"xor", 2, 2, f_xor},
988};
989
990#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
991
992/*
993 * Function given to ExpandGeneric() to obtain the list of internal
994 * or user defined function names.
995 */
996 char_u *
997get_function_name(expand_T *xp, int idx)
998{
999 static int intidx = -1;
1000 char_u *name;
1001
1002 if (idx == 0)
1003 intidx = -1;
1004 if (intidx < 0)
1005 {
1006 name = get_user_func_name(xp, idx);
1007 if (name != NULL)
1008 return name;
1009 }
1010 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1011 {
1012 STRCPY(IObuff, functions[intidx].f_name);
1013 STRCAT(IObuff, "(");
1014 if (functions[intidx].f_max_argc == 0)
1015 STRCAT(IObuff, ")");
1016 return IObuff;
1017 }
1018
1019 return NULL;
1020}
1021
1022/*
1023 * Function given to ExpandGeneric() to obtain the list of internal or
1024 * user defined variable or function names.
1025 */
1026 char_u *
1027get_expr_name(expand_T *xp, int idx)
1028{
1029 static int intidx = -1;
1030 char_u *name;
1031
1032 if (idx == 0)
1033 intidx = -1;
1034 if (intidx < 0)
1035 {
1036 name = get_function_name(xp, idx);
1037 if (name != NULL)
1038 return name;
1039 }
1040 return get_user_var_name(xp, ++intidx);
1041}
1042
1043#endif /* FEAT_CMDL_COMPL */
1044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001045/*
1046 * Find internal function in table above.
1047 * Return index, or -1 if not found
1048 */
1049 int
1050find_internal_func(
1051 char_u *name) /* name of the function */
1052{
1053 int first = 0;
1054 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1055 int cmp;
1056 int x;
1057
1058 /*
1059 * Find the function name in the table. Binary search.
1060 */
1061 while (first <= last)
1062 {
1063 x = first + ((unsigned)(last - first) >> 1);
1064 cmp = STRCMP(name, functions[x].f_name);
1065 if (cmp < 0)
1066 last = x - 1;
1067 else if (cmp > 0)
1068 first = x + 1;
1069 else
1070 return x;
1071 }
1072 return -1;
1073}
1074
1075 int
1076call_internal_func(
1077 char_u *name,
1078 int argcount,
1079 typval_T *argvars,
1080 typval_T *rettv)
1081{
1082 int i;
1083
1084 i = find_internal_func(name);
1085 if (i < 0)
1086 return ERROR_UNKNOWN;
1087 if (argcount < functions[i].f_min_argc)
1088 return ERROR_TOOFEW;
1089 if (argcount > functions[i].f_max_argc)
1090 return ERROR_TOOMANY;
1091 argvars[argcount].v_type = VAR_UNKNOWN;
1092 functions[i].f_func(argvars, rettv);
1093 return ERROR_NONE;
1094}
1095
1096/*
1097 * Return TRUE for a non-zero Number and a non-empty String.
1098 */
1099 static int
1100non_zero_arg(typval_T *argvars)
1101{
1102 return ((argvars[0].v_type == VAR_NUMBER
1103 && argvars[0].vval.v_number != 0)
1104 || (argvars[0].v_type == VAR_SPECIAL
1105 && argvars[0].vval.v_number == VVAL_TRUE)
1106 || (argvars[0].v_type == VAR_STRING
1107 && argvars[0].vval.v_string != NULL
1108 && *argvars[0].vval.v_string != NUL));
1109}
1110
1111/*
1112 * Get the lnum from the first argument.
1113 * Also accepts ".", "$", etc., but that only works for the current buffer.
1114 * Returns -1 on error.
1115 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001116 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001117tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001118{
1119 typval_T rettv;
1120 linenr_T lnum;
1121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001122 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001123 if (lnum == 0) /* no valid number, try using line() */
1124 {
1125 rettv.v_type = VAR_NUMBER;
1126 f_line(argvars, &rettv);
1127 lnum = (linenr_T)rettv.vval.v_number;
1128 clear_tv(&rettv);
1129 }
1130 return lnum;
1131}
1132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001133/*
1134 * Get the lnum from the first argument.
1135 * Also accepts "$", then "buf" is used.
1136 * Returns 0 on error.
1137 */
1138 static linenr_T
1139tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1140{
1141 if (argvars[0].v_type == VAR_STRING
1142 && argvars[0].vval.v_string != NULL
1143 && argvars[0].vval.v_string[0] == '$'
1144 && buf != NULL)
1145 return buf->b_ml.ml_line_count;
1146 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1147}
1148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001149#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001150/*
1151 * Get the float value of "argvars[0]" into "f".
1152 * Returns FAIL when the argument is not a Number or Float.
1153 */
1154 static int
1155get_float_arg(typval_T *argvars, float_T *f)
1156{
1157 if (argvars[0].v_type == VAR_FLOAT)
1158 {
1159 *f = argvars[0].vval.v_float;
1160 return OK;
1161 }
1162 if (argvars[0].v_type == VAR_NUMBER)
1163 {
1164 *f = (float_T)argvars[0].vval.v_number;
1165 return OK;
1166 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001167 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168 return FAIL;
1169}
1170
1171/*
1172 * "abs(expr)" function
1173 */
1174 static void
1175f_abs(typval_T *argvars, typval_T *rettv)
1176{
1177 if (argvars[0].v_type == VAR_FLOAT)
1178 {
1179 rettv->v_type = VAR_FLOAT;
1180 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1181 }
1182 else
1183 {
1184 varnumber_T n;
1185 int error = FALSE;
1186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001187 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001188 if (error)
1189 rettv->vval.v_number = -1;
1190 else if (n > 0)
1191 rettv->vval.v_number = n;
1192 else
1193 rettv->vval.v_number = -n;
1194 }
1195}
1196
1197/*
1198 * "acos()" function
1199 */
1200 static void
1201f_acos(typval_T *argvars, typval_T *rettv)
1202{
1203 float_T f = 0.0;
1204
1205 rettv->v_type = VAR_FLOAT;
1206 if (get_float_arg(argvars, &f) == OK)
1207 rettv->vval.v_float = acos(f);
1208 else
1209 rettv->vval.v_float = 0.0;
1210}
1211#endif
1212
1213/*
1214 * "add(list, item)" function
1215 */
1216 static void
1217f_add(typval_T *argvars, typval_T *rettv)
1218{
1219 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001220 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221
1222 rettv->vval.v_number = 1; /* Default: Failed */
1223 if (argvars[0].v_type == VAR_LIST)
1224 {
1225 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001226 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 (char_u *)N_("add() argument"), TRUE)
1228 && list_append_tv(l, &argvars[1]) == OK)
1229 copy_tv(&argvars[0], rettv);
1230 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 else if (argvars[0].v_type == VAR_BLOB)
1232 {
1233 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001234 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001235 (char_u *)N_("add() argument"), TRUE))
1236 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001237 int error = FALSE;
1238 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1239
1240 if (!error)
1241 {
1242 ga_append(&b->bv_ga, (int)n);
1243 copy_tv(&argvars[0], rettv);
1244 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 }
1246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001248 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249}
1250
1251/*
1252 * "and(expr, expr)" function
1253 */
1254 static void
1255f_and(typval_T *argvars, typval_T *rettv)
1256{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001257 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1258 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001259}
1260
1261/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001262 * If there is a window for "curbuf", make it the current window.
1263 */
1264 static void
1265find_win_for_curbuf(void)
1266{
1267 wininfo_T *wip;
1268
1269 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1270 {
1271 if (wip->wi_win != NULL)
1272 {
1273 curwin = wip->wi_win;
1274 break;
1275 }
1276 }
1277}
1278
1279/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001280 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001281 */
1282 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001283set_buffer_lines(
1284 buf_T *buf,
1285 linenr_T lnum_arg,
1286 int append,
1287 typval_T *lines,
1288 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001289{
Bram Moolenaarca851592018-06-06 21:04:07 +02001290 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1291 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001292 list_T *l = NULL;
1293 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001294 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001295 linenr_T append_lnum;
1296 buf_T *curbuf_save = NULL;
1297 win_T *curwin_save = NULL;
1298 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299
Bram Moolenaarca851592018-06-06 21:04:07 +02001300 /* When using the current buffer ml_mfp will be set if needed. Useful when
1301 * setline() is used on startup. For other buffers the buffer must be
1302 * loaded. */
1303 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001304 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001305 rettv->vval.v_number = 1; /* FAIL */
1306 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001307 }
1308
Bram Moolenaarca851592018-06-06 21:04:07 +02001309 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001311 curbuf_save = curbuf;
1312 curwin_save = curwin;
1313 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001314 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001315 }
1316
1317 if (append)
1318 // appendbufline() uses the line number below which we insert
1319 append_lnum = lnum - 1;
1320 else
1321 // setbufline() uses the line number above which we insert, we only
1322 // append if it's below the last line
1323 append_lnum = curbuf->b_ml.ml_line_count;
1324
1325 if (lines->v_type == VAR_LIST)
1326 {
1327 l = lines->vval.v_list;
1328 li = l->lv_first;
1329 }
1330 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001331 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001332
1333 /* default result is zero == OK */
1334 for (;;)
1335 {
1336 if (l != NULL)
1337 {
1338 /* list argument, get next string */
1339 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001341 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 li = li->li_next;
1343 }
1344
Bram Moolenaarca851592018-06-06 21:04:07 +02001345 rettv->vval.v_number = 1; /* FAIL */
1346 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1347 break;
1348
1349 /* When coming here from Insert mode, sync undo, so that this can be
1350 * undone separately from what was previously inserted. */
1351 if (u_sync_once == 2)
1352 {
1353 u_sync_once = 1; /* notify that u_sync() was called */
1354 u_sync(TRUE);
1355 }
1356
1357 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1358 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001359 // Existing line, replace it.
1360 // Removes any existing text properties.
1361 if (u_savesub(lnum) == OK && ml_replace_len(
1362 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 {
1364 changed_bytes(lnum, 0);
1365 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1366 check_cursor_col();
1367 rettv->vval.v_number = 0; /* OK */
1368 }
1369 }
1370 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1371 {
1372 /* append the line */
1373 ++added;
1374 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1375 rettv->vval.v_number = 0; /* OK */
1376 }
1377
1378 if (l == NULL) /* only one string argument */
1379 break;
1380 ++lnum;
1381 }
1382
1383 if (added > 0)
1384 {
1385 win_T *wp;
1386 tabpage_T *tp;
1387
1388 appended_lines_mark(append_lnum, added);
1389 FOR_ALL_TAB_WINDOWS(tp, wp)
1390 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1391 wp->w_cursor.lnum += added;
1392 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001393 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001394 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001395
1396 if (!is_curbuf)
1397 {
1398 curbuf = curbuf_save;
1399 curwin = curwin_save;
1400 }
1401}
1402
1403/*
1404 * "append(lnum, string/list)" function
1405 */
1406 static void
1407f_append(typval_T *argvars, typval_T *rettv)
1408{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001409 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001410
1411 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1412}
1413
1414/*
1415 * "appendbufline(buf, lnum, string/list)" function
1416 */
1417 static void
1418f_appendbufline(typval_T *argvars, typval_T *rettv)
1419{
1420 linenr_T lnum;
1421 buf_T *buf;
1422
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001423 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001424 if (buf == NULL)
1425 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001426 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001428 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001429 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1430 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001431}
1432
1433/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001434 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435 */
1436 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001437f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001439 win_T *wp;
1440
1441 if (argvars[0].v_type == VAR_UNKNOWN)
1442 // use the current window
1443 rettv->vval.v_number = ARGCOUNT;
1444 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001445 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001446 // use the global argument list
1447 rettv->vval.v_number = GARGCOUNT;
1448 else
1449 {
1450 // use the argument list of the specified window
1451 wp = find_win_by_nr_or_id(&argvars[0]);
1452 if (wp != NULL)
1453 rettv->vval.v_number = WARGCOUNT(wp);
1454 else
1455 rettv->vval.v_number = -1;
1456 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001457}
1458
1459/*
1460 * "argidx()" function
1461 */
1462 static void
1463f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1464{
1465 rettv->vval.v_number = curwin->w_arg_idx;
1466}
1467
1468/*
1469 * "arglistid()" function
1470 */
1471 static void
1472f_arglistid(typval_T *argvars, typval_T *rettv)
1473{
1474 win_T *wp;
1475
1476 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001477 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001478 if (wp != NULL)
1479 rettv->vval.v_number = wp->w_alist->id;
1480}
1481
1482/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001483 * Get the argument list for a given window
1484 */
1485 static void
1486get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1487{
1488 int idx;
1489
1490 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1491 for (idx = 0; idx < argcount; ++idx)
1492 list_append_string(rettv->vval.v_list,
1493 alist_name(&arglist[idx]), -1);
1494}
1495
1496/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001497 * "argv(nr)" function
1498 */
1499 static void
1500f_argv(typval_T *argvars, typval_T *rettv)
1501{
1502 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 aentry_T *arglist = NULL;
1504 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505
1506 if (argvars[0].v_type != VAR_UNKNOWN)
1507 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001508 if (argvars[1].v_type == VAR_UNKNOWN)
1509 {
1510 arglist = ARGLIST;
1511 argcount = ARGCOUNT;
1512 }
1513 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001514 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001515 {
1516 arglist = GARGLIST;
1517 argcount = GARGCOUNT;
1518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001520 {
1521 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1522
1523 if (wp != NULL)
1524 {
1525 /* Use the argument list of the specified window */
1526 arglist = WARGLIST(wp);
1527 argcount = WARGCOUNT(wp);
1528 }
1529 }
1530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001532 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001533 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534 if (arglist != NULL && idx >= 0 && idx < argcount)
1535 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1536 else if (idx == -1)
1537 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001539 else
1540 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541}
1542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001543#ifdef FEAT_FLOAT
1544/*
1545 * "asin()" function
1546 */
1547 static void
1548f_asin(typval_T *argvars, typval_T *rettv)
1549{
1550 float_T f = 0.0;
1551
1552 rettv->v_type = VAR_FLOAT;
1553 if (get_float_arg(argvars, &f) == OK)
1554 rettv->vval.v_float = asin(f);
1555 else
1556 rettv->vval.v_float = 0.0;
1557}
1558
1559/*
1560 * "atan()" function
1561 */
1562 static void
1563f_atan(typval_T *argvars, typval_T *rettv)
1564{
1565 float_T f = 0.0;
1566
1567 rettv->v_type = VAR_FLOAT;
1568 if (get_float_arg(argvars, &f) == OK)
1569 rettv->vval.v_float = atan(f);
1570 else
1571 rettv->vval.v_float = 0.0;
1572}
1573
1574/*
1575 * "atan2()" function
1576 */
1577 static void
1578f_atan2(typval_T *argvars, typval_T *rettv)
1579{
1580 float_T fx = 0.0, fy = 0.0;
1581
1582 rettv->v_type = VAR_FLOAT;
1583 if (get_float_arg(argvars, &fx) == OK
1584 && get_float_arg(&argvars[1], &fy) == OK)
1585 rettv->vval.v_float = atan2(fx, fy);
1586 else
1587 rettv->vval.v_float = 0.0;
1588}
1589#endif
1590
1591/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001592 * "balloon_show()" function
1593 */
1594#ifdef FEAT_BEVAL
1595 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001596f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1597{
1598 rettv->v_type = VAR_STRING;
1599 if (balloonEval != NULL)
1600 {
1601 if (balloonEval->msg == NULL)
1602 rettv->vval.v_string = NULL;
1603 else
1604 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1605 }
1606}
1607
1608 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001609f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1610{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001611 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001612 {
1613 if (argvars[0].v_type == VAR_LIST
1614# ifdef FEAT_GUI
1615 && !gui.in_use
1616# endif
1617 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001618 {
1619 list_T *l = argvars[0].vval.v_list;
1620
1621 // empty list removes the balloon
1622 post_balloon(balloonEval, NULL,
1623 l == NULL || l->lv_len == 0 ? NULL : l);
1624 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001625 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001626 {
1627 char_u *mesg = tv_get_string_chk(&argvars[0]);
1628
1629 if (mesg != NULL)
1630 // empty string removes the balloon
1631 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1632 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001633 }
1634}
1635
Bram Moolenaar669a8282017-11-19 20:13:05 +01001636# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001637 static void
1638f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1639{
1640 if (rettv_list_alloc(rettv) == OK)
1641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001642 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001643
1644 if (msg != NULL)
1645 {
1646 pumitem_T *array;
1647 int size = split_message(msg, &array);
1648 int i;
1649
1650 /* Skip the first and last item, they are always empty. */
1651 for (i = 1; i < size - 1; ++i)
1652 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001653 while (size > 0)
1654 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001655 vim_free(array);
1656 }
1657 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001658}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001659# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001660#endif
1661
1662/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663 * "browse(save, title, initdir, default)" function
1664 */
1665 static void
1666f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1667{
1668#ifdef FEAT_BROWSE
1669 int save;
1670 char_u *title;
1671 char_u *initdir;
1672 char_u *defname;
1673 char_u buf[NUMBUFLEN];
1674 char_u buf2[NUMBUFLEN];
1675 int error = FALSE;
1676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001677 save = (int)tv_get_number_chk(&argvars[0], &error);
1678 title = tv_get_string_chk(&argvars[1]);
1679 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1680 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681
1682 if (error || title == NULL || initdir == NULL || defname == NULL)
1683 rettv->vval.v_string = NULL;
1684 else
1685 rettv->vval.v_string =
1686 do_browse(save ? BROWSE_SAVE : 0,
1687 title, defname, NULL, initdir, NULL, curbuf);
1688#else
1689 rettv->vval.v_string = NULL;
1690#endif
1691 rettv->v_type = VAR_STRING;
1692}
1693
1694/*
1695 * "browsedir(title, initdir)" function
1696 */
1697 static void
1698f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1699{
1700#ifdef FEAT_BROWSE
1701 char_u *title;
1702 char_u *initdir;
1703 char_u buf[NUMBUFLEN];
1704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001705 title = tv_get_string_chk(&argvars[0]);
1706 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707
1708 if (title == NULL || initdir == NULL)
1709 rettv->vval.v_string = NULL;
1710 else
1711 rettv->vval.v_string = do_browse(BROWSE_DIR,
1712 title, NULL, NULL, initdir, NULL, curbuf);
1713#else
1714 rettv->vval.v_string = NULL;
1715#endif
1716 rettv->v_type = VAR_STRING;
1717}
1718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719/*
1720 * Find a buffer by number or exact name.
1721 */
1722 static buf_T *
1723find_buffer(typval_T *avar)
1724{
1725 buf_T *buf = NULL;
1726
1727 if (avar->v_type == VAR_NUMBER)
1728 buf = buflist_findnr((int)avar->vval.v_number);
1729 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1730 {
1731 buf = buflist_findname_exp(avar->vval.v_string);
1732 if (buf == NULL)
1733 {
1734 /* No full path name match, try a match with a URL or a "nofile"
1735 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001736 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737 if (buf->b_fname != NULL
1738 && (path_with_url(buf->b_fname)
1739#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001740 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001741#endif
1742 )
1743 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1744 break;
1745 }
1746 }
1747 return buf;
1748}
1749
1750/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001751 * "bufadd(expr)" function
1752 */
1753 static void
1754f_bufadd(typval_T *argvars, typval_T *rettv)
1755{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001756 char_u *name = tv_get_string(&argvars[0]);
1757
1758 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001759}
1760
1761/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762 * "bufexists(expr)" function
1763 */
1764 static void
1765f_bufexists(typval_T *argvars, typval_T *rettv)
1766{
1767 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1768}
1769
1770/*
1771 * "buflisted(expr)" function
1772 */
1773 static void
1774f_buflisted(typval_T *argvars, typval_T *rettv)
1775{
1776 buf_T *buf;
1777
1778 buf = find_buffer(&argvars[0]);
1779 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1780}
1781
1782/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001783 * "bufload(expr)" function
1784 */
1785 static void
1786f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1787{
1788 buf_T *buf = get_buf_arg(&argvars[0]);
1789
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001790 if (buf != NULL)
1791 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001792}
1793
1794/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001795 * "bufloaded(expr)" function
1796 */
1797 static void
1798f_bufloaded(typval_T *argvars, typval_T *rettv)
1799{
1800 buf_T *buf;
1801
1802 buf = find_buffer(&argvars[0]);
1803 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1804}
1805
1806 buf_T *
1807buflist_find_by_name(char_u *name, int curtab_only)
1808{
1809 int save_magic;
1810 char_u *save_cpo;
1811 buf_T *buf;
1812
1813 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1814 save_magic = p_magic;
1815 p_magic = TRUE;
1816 save_cpo = p_cpo;
1817 p_cpo = (char_u *)"";
1818
1819 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1820 TRUE, FALSE, curtab_only));
1821
1822 p_magic = save_magic;
1823 p_cpo = save_cpo;
1824 return buf;
1825}
1826
1827/*
1828 * Get buffer by number or pattern.
1829 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001830 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001831tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832{
1833 char_u *name = tv->vval.v_string;
1834 buf_T *buf;
1835
1836 if (tv->v_type == VAR_NUMBER)
1837 return buflist_findnr((int)tv->vval.v_number);
1838 if (tv->v_type != VAR_STRING)
1839 return NULL;
1840 if (name == NULL || *name == NUL)
1841 return curbuf;
1842 if (name[0] == '$' && name[1] == NUL)
1843 return lastbuf;
1844
1845 buf = buflist_find_by_name(name, curtab_only);
1846
1847 /* If not found, try expanding the name, like done for bufexists(). */
1848 if (buf == NULL)
1849 buf = find_buffer(tv);
1850
1851 return buf;
1852}
1853
1854/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001855 * Get the buffer from "arg" and give an error and return NULL if it is not
1856 * valid.
1857 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001858 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001859get_buf_arg(typval_T *arg)
1860{
1861 buf_T *buf;
1862
1863 ++emsg_off;
1864 buf = tv_get_buf(arg, FALSE);
1865 --emsg_off;
1866 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001867 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001868 return buf;
1869}
1870
1871/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 * "bufname(expr)" function
1873 */
1874 static void
1875f_bufname(typval_T *argvars, typval_T *rettv)
1876{
1877 buf_T *buf;
1878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001879 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001881 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001882 rettv->v_type = VAR_STRING;
1883 if (buf != NULL && buf->b_fname != NULL)
1884 rettv->vval.v_string = vim_strsave(buf->b_fname);
1885 else
1886 rettv->vval.v_string = NULL;
1887 --emsg_off;
1888}
1889
1890/*
1891 * "bufnr(expr)" function
1892 */
1893 static void
1894f_bufnr(typval_T *argvars, typval_T *rettv)
1895{
1896 buf_T *buf;
1897 int error = FALSE;
1898 char_u *name;
1899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001900 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001902 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903 --emsg_off;
1904
1905 /* If the buffer isn't found and the second argument is not zero create a
1906 * new buffer. */
1907 if (buf == NULL
1908 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001909 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001911 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 && !error)
1913 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1914
1915 if (buf != NULL)
1916 rettv->vval.v_number = buf->b_fnum;
1917 else
1918 rettv->vval.v_number = -1;
1919}
1920
1921 static void
1922buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1923{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 win_T *wp;
1925 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 buf_T *buf;
1927
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001928 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001930 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001931 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 {
1933 ++winnr;
1934 if (wp->w_buffer == buf)
1935 break;
1936 }
1937 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001938 --emsg_off;
1939}
1940
1941/*
1942 * "bufwinid(nr)" function
1943 */
1944 static void
1945f_bufwinid(typval_T *argvars, typval_T *rettv)
1946{
1947 buf_win_common(argvars, rettv, FALSE);
1948}
1949
1950/*
1951 * "bufwinnr(nr)" function
1952 */
1953 static void
1954f_bufwinnr(typval_T *argvars, typval_T *rettv)
1955{
1956 buf_win_common(argvars, rettv, TRUE);
1957}
1958
1959/*
1960 * "byte2line(byte)" function
1961 */
1962 static void
1963f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1964{
1965#ifndef FEAT_BYTEOFF
1966 rettv->vval.v_number = -1;
1967#else
1968 long boff = 0;
1969
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001970 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 if (boff < 0)
1972 rettv->vval.v_number = -1;
1973 else
1974 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1975 (linenr_T)0, &boff);
1976#endif
1977}
1978
1979 static void
1980byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1981{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001982 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001983 char_u *str;
1984 varnumber_T idx;
1985
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001986 str = tv_get_string_chk(&argvars[0]);
1987 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 rettv->vval.v_number = -1;
1989 if (str == NULL || idx < 0)
1990 return;
1991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001992 t = str;
1993 for ( ; idx > 0; idx--)
1994 {
1995 if (*t == NUL) /* EOL reached */
1996 return;
1997 if (enc_utf8 && comp)
1998 t += utf_ptr2len(t);
1999 else
2000 t += (*mb_ptr2len)(t);
2001 }
2002 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003}
2004
2005/*
2006 * "byteidx()" function
2007 */
2008 static void
2009f_byteidx(typval_T *argvars, typval_T *rettv)
2010{
2011 byteidx(argvars, rettv, FALSE);
2012}
2013
2014/*
2015 * "byteidxcomp()" function
2016 */
2017 static void
2018f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2019{
2020 byteidx(argvars, rettv, TRUE);
2021}
2022
2023/*
2024 * "call(func, arglist [, dict])" function
2025 */
2026 static void
2027f_call(typval_T *argvars, typval_T *rettv)
2028{
2029 char_u *func;
2030 partial_T *partial = NULL;
2031 dict_T *selfdict = NULL;
2032
2033 if (argvars[1].v_type != VAR_LIST)
2034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 return;
2037 }
2038 if (argvars[1].vval.v_list == NULL)
2039 return;
2040
2041 if (argvars[0].v_type == VAR_FUNC)
2042 func = argvars[0].vval.v_string;
2043 else if (argvars[0].v_type == VAR_PARTIAL)
2044 {
2045 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002046 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 }
2048 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002049 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 if (*func == NUL)
2051 return; /* type error or empty name */
2052
2053 if (argvars[2].v_type != VAR_UNKNOWN)
2054 {
2055 if (argvars[2].v_type != VAR_DICT)
2056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002057 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 return;
2059 }
2060 selfdict = argvars[2].vval.v_dict;
2061 }
2062
2063 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2064}
2065
2066#ifdef FEAT_FLOAT
2067/*
2068 * "ceil({float})" function
2069 */
2070 static void
2071f_ceil(typval_T *argvars, typval_T *rettv)
2072{
2073 float_T f = 0.0;
2074
2075 rettv->v_type = VAR_FLOAT;
2076 if (get_float_arg(argvars, &f) == OK)
2077 rettv->vval.v_float = ceil(f);
2078 else
2079 rettv->vval.v_float = 0.0;
2080}
2081#endif
2082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083/*
2084 * "changenr()" function
2085 */
2086 static void
2087f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2088{
2089 rettv->vval.v_number = curbuf->b_u_seq_cur;
2090}
2091
2092/*
2093 * "char2nr(string)" function
2094 */
2095 static void
2096f_char2nr(typval_T *argvars, typval_T *rettv)
2097{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 if (has_mbyte)
2099 {
2100 int utf8 = 0;
2101
2102 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104
2105 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002106 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002108 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 }
2110 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002111 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112}
2113
2114/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002115 * "chdir(dir)" function
2116 */
2117 static void
2118f_chdir(typval_T *argvars, typval_T *rettv)
2119{
2120 char_u *cwd;
2121 cdscope_T scope = CDSCOPE_GLOBAL;
2122
2123 rettv->v_type = VAR_STRING;
2124 rettv->vval.v_string = NULL;
2125
2126 if (argvars[0].v_type != VAR_STRING)
2127 return;
2128
2129 // Return the current directory
2130 cwd = alloc(MAXPATHL);
2131 if (cwd != NULL)
2132 {
2133 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2134 {
2135#ifdef BACKSLASH_IN_FILENAME
2136 slash_adjust(cwd);
2137#endif
2138 rettv->vval.v_string = vim_strsave(cwd);
2139 }
2140 vim_free(cwd);
2141 }
2142
2143 if (curwin->w_localdir != NULL)
2144 scope = CDSCOPE_WINDOW;
2145 else if (curtab->tp_localdir != NULL)
2146 scope = CDSCOPE_TABPAGE;
2147
2148 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2149 // Directory change failed
2150 VIM_CLEAR(rettv->vval.v_string);
2151}
2152
2153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 * "cindent(lnum)" function
2155 */
2156 static void
2157f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2158{
2159#ifdef FEAT_CINDENT
2160 pos_T pos;
2161 linenr_T lnum;
2162
2163 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002164 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2166 {
2167 curwin->w_cursor.lnum = lnum;
2168 rettv->vval.v_number = get_c_indent();
2169 curwin->w_cursor = pos;
2170 }
2171 else
2172#endif
2173 rettv->vval.v_number = -1;
2174}
2175
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002176 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002177get_optional_window(typval_T *argvars, int idx)
2178{
2179 win_T *win = curwin;
2180
2181 if (argvars[idx].v_type != VAR_UNKNOWN)
2182 {
2183 win = find_win_by_nr_or_id(&argvars[idx]);
2184 if (win == NULL)
2185 {
2186 emsg(_(e_invalwindow));
2187 return NULL;
2188 }
2189 }
2190 return win;
2191}
2192
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002193/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002194 * "col(string)" function
2195 */
2196 static void
2197f_col(typval_T *argvars, typval_T *rettv)
2198{
2199 colnr_T col = 0;
2200 pos_T *fp;
2201 int fnum = curbuf->b_fnum;
2202
2203 fp = var2fpos(&argvars[0], FALSE, &fnum);
2204 if (fp != NULL && fnum == curbuf->b_fnum)
2205 {
2206 if (fp->col == MAXCOL)
2207 {
2208 /* '> can be MAXCOL, get the length of the line then */
2209 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2210 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2211 else
2212 col = MAXCOL;
2213 }
2214 else
2215 {
2216 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 /* col(".") when the cursor is on the NUL at the end of the line
2218 * because of "coladd" can be seen as an extra column. */
2219 if (virtual_active() && fp == &curwin->w_cursor)
2220 {
2221 char_u *p = ml_get_cursor();
2222
2223 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2224 curwin->w_virtcol - curwin->w_cursor.coladd))
2225 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 int l;
2227
2228 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2229 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 }
2231 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 }
2233 }
2234 rettv->vval.v_number = col;
2235}
2236
2237#if defined(FEAT_INS_EXPAND)
2238/*
2239 * "complete()" function
2240 */
2241 static void
2242f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2243{
2244 int startcol;
2245
2246 if ((State & INSERT) == 0)
2247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002248 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002249 return;
2250 }
2251
2252 /* Check for undo allowed here, because if something was already inserted
2253 * the line was already saved for undo and this check isn't done. */
2254 if (!undo_allowed())
2255 return;
2256
2257 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002259 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002260 return;
2261 }
2262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 if (startcol <= 0)
2265 return;
2266
2267 set_completion(startcol - 1, argvars[1].vval.v_list);
2268}
2269
2270/*
2271 * "complete_add()" function
2272 */
2273 static void
2274f_complete_add(typval_T *argvars, typval_T *rettv)
2275{
2276 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2277}
2278
2279/*
2280 * "complete_check()" function
2281 */
2282 static void
2283f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2284{
2285 int saved = RedrawingDisabled;
2286
2287 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002288 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002289 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002290 RedrawingDisabled = saved;
2291}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002292
2293/*
2294 * "complete_info()" function
2295 */
2296 static void
2297f_complete_info(typval_T *argvars, typval_T *rettv)
2298{
2299 list_T *what_list = NULL;
2300
2301 if (rettv_dict_alloc(rettv) != OK)
2302 return;
2303
2304 if (argvars[0].v_type != VAR_UNKNOWN)
2305 {
2306 if (argvars[0].v_type != VAR_LIST)
2307 {
2308 emsg(_(e_listreq));
2309 return;
2310 }
2311 what_list = argvars[0].vval.v_list;
2312 }
2313 get_complete_info(what_list, rettv->vval.v_dict);
2314}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002315#endif
2316
2317/*
2318 * "confirm(message, buttons[, default [, type]])" function
2319 */
2320 static void
2321f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2322{
2323#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2324 char_u *message;
2325 char_u *buttons = NULL;
2326 char_u buf[NUMBUFLEN];
2327 char_u buf2[NUMBUFLEN];
2328 int def = 1;
2329 int type = VIM_GENERIC;
2330 char_u *typestr;
2331 int error = FALSE;
2332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002333 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002334 if (message == NULL)
2335 error = TRUE;
2336 if (argvars[1].v_type != VAR_UNKNOWN)
2337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002338 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339 if (buttons == NULL)
2340 error = TRUE;
2341 if (argvars[2].v_type != VAR_UNKNOWN)
2342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002343 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002344 if (argvars[3].v_type != VAR_UNKNOWN)
2345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002346 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002347 if (typestr == NULL)
2348 error = TRUE;
2349 else
2350 {
2351 switch (TOUPPER_ASC(*typestr))
2352 {
2353 case 'E': type = VIM_ERROR; break;
2354 case 'Q': type = VIM_QUESTION; break;
2355 case 'I': type = VIM_INFO; break;
2356 case 'W': type = VIM_WARNING; break;
2357 case 'G': type = VIM_GENERIC; break;
2358 }
2359 }
2360 }
2361 }
2362 }
2363
2364 if (buttons == NULL || *buttons == NUL)
2365 buttons = (char_u *)_("&Ok");
2366
2367 if (!error)
2368 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2369 def, NULL, FALSE);
2370#endif
2371}
2372
2373/*
2374 * "copy()" function
2375 */
2376 static void
2377f_copy(typval_T *argvars, typval_T *rettv)
2378{
2379 item_copy(&argvars[0], rettv, FALSE, 0);
2380}
2381
2382#ifdef FEAT_FLOAT
2383/*
2384 * "cos()" function
2385 */
2386 static void
2387f_cos(typval_T *argvars, typval_T *rettv)
2388{
2389 float_T f = 0.0;
2390
2391 rettv->v_type = VAR_FLOAT;
2392 if (get_float_arg(argvars, &f) == OK)
2393 rettv->vval.v_float = cos(f);
2394 else
2395 rettv->vval.v_float = 0.0;
2396}
2397
2398/*
2399 * "cosh()" function
2400 */
2401 static void
2402f_cosh(typval_T *argvars, typval_T *rettv)
2403{
2404 float_T f = 0.0;
2405
2406 rettv->v_type = VAR_FLOAT;
2407 if (get_float_arg(argvars, &f) == OK)
2408 rettv->vval.v_float = cosh(f);
2409 else
2410 rettv->vval.v_float = 0.0;
2411}
2412#endif
2413
2414/*
2415 * "count()" function
2416 */
2417 static void
2418f_count(typval_T *argvars, typval_T *rettv)
2419{
2420 long n = 0;
2421 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002422 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423
Bram Moolenaar9966b212017-07-28 16:46:57 +02002424 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002425 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002426
2427 if (argvars[0].v_type == VAR_STRING)
2428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002429 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002430 char_u *p = argvars[0].vval.v_string;
2431 char_u *next;
2432
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002433 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002434 {
2435 if (ic)
2436 {
2437 size_t len = STRLEN(expr);
2438
2439 while (*p != NUL)
2440 {
2441 if (MB_STRNICMP(p, expr, len) == 0)
2442 {
2443 ++n;
2444 p += len;
2445 }
2446 else
2447 MB_PTR_ADV(p);
2448 }
2449 }
2450 else
2451 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2452 != NULL)
2453 {
2454 ++n;
2455 p = next + STRLEN(expr);
2456 }
2457 }
2458
2459 }
2460 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 {
2462 listitem_T *li;
2463 list_T *l;
2464 long idx;
2465
2466 if ((l = argvars[0].vval.v_list) != NULL)
2467 {
2468 li = l->lv_first;
2469 if (argvars[2].v_type != VAR_UNKNOWN)
2470 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 if (argvars[3].v_type != VAR_UNKNOWN)
2472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002473 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (!error)
2475 {
2476 li = list_find(l, idx);
2477 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002479 }
2480 }
2481 if (error)
2482 li = NULL;
2483 }
2484
2485 for ( ; li != NULL; li = li->li_next)
2486 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2487 ++n;
2488 }
2489 }
2490 else if (argvars[0].v_type == VAR_DICT)
2491 {
2492 int todo;
2493 dict_T *d;
2494 hashitem_T *hi;
2495
2496 if ((d = argvars[0].vval.v_dict) != NULL)
2497 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002498 if (argvars[2].v_type != VAR_UNKNOWN)
2499 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002500 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002501 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502 }
2503
2504 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2505 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2506 {
2507 if (!HASHITEM_EMPTY(hi))
2508 {
2509 --todo;
2510 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2511 ++n;
2512 }
2513 }
2514 }
2515 }
2516 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 rettv->vval.v_number = n;
2519}
2520
2521/*
2522 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2523 *
2524 * Checks the existence of a cscope connection.
2525 */
2526 static void
2527f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2528{
2529#ifdef FEAT_CSCOPE
2530 int num = 0;
2531 char_u *dbpath = NULL;
2532 char_u *prepend = NULL;
2533 char_u buf[NUMBUFLEN];
2534
2535 if (argvars[0].v_type != VAR_UNKNOWN
2536 && argvars[1].v_type != VAR_UNKNOWN)
2537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 num = (int)tv_get_number(&argvars[0]);
2539 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542 }
2543
2544 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2545#endif
2546}
2547
2548/*
2549 * "cursor(lnum, col)" function, or
2550 * "cursor(list)"
2551 *
2552 * Moves the cursor to the specified line and column.
2553 * Returns 0 when the position could be set, -1 otherwise.
2554 */
2555 static void
2556f_cursor(typval_T *argvars, typval_T *rettv)
2557{
2558 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002559 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002560 int set_curswant = TRUE;
2561
2562 rettv->vval.v_number = -1;
2563 if (argvars[1].v_type == VAR_UNKNOWN)
2564 {
2565 pos_T pos;
2566 colnr_T curswant = -1;
2567
2568 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002570 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 return;
2572 }
2573 line = pos.lnum;
2574 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002575 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 if (curswant >= 0)
2577 {
2578 curwin->w_curswant = curswant - 1;
2579 set_curswant = FALSE;
2580 }
2581 }
2582 else
2583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002584 line = tv_get_lnum(argvars);
2585 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002587 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002589 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002590 return; /* type error; errmsg already given */
2591 if (line > 0)
2592 curwin->w_cursor.lnum = line;
2593 if (col > 0)
2594 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002595 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002596
2597 /* Make sure the cursor is in a valid position. */
2598 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002599 /* Correct cursor for multi-byte character. */
2600 if (has_mbyte)
2601 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602
2603 curwin->w_set_curswant = set_curswant;
2604 rettv->vval.v_number = 0;
2605}
2606
Bram Moolenaar4f974752019-02-17 17:44:42 +01002607#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002608/*
2609 * "debugbreak()" function
2610 */
2611 static void
2612f_debugbreak(typval_T *argvars, typval_T *rettv)
2613{
2614 int pid;
2615
2616 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002617 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002618 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002620 else
2621 {
2622 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2623
2624 if (hProcess != NULL)
2625 {
2626 DebugBreakProcess(hProcess);
2627 CloseHandle(hProcess);
2628 rettv->vval.v_number = OK;
2629 }
2630 }
2631}
2632#endif
2633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634/*
2635 * "deepcopy()" function
2636 */
2637 static void
2638f_deepcopy(typval_T *argvars, typval_T *rettv)
2639{
2640 int noref = 0;
2641 int copyID;
2642
2643 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002644 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002645 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 else
2648 {
2649 copyID = get_copyID();
2650 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2651 }
2652}
2653
2654/*
2655 * "delete()" function
2656 */
2657 static void
2658f_delete(typval_T *argvars, typval_T *rettv)
2659{
2660 char_u nbuf[NUMBUFLEN];
2661 char_u *name;
2662 char_u *flags;
2663
2664 rettv->vval.v_number = -1;
2665 if (check_restricted() || check_secure())
2666 return;
2667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002668 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 if (name == NULL || *name == NUL)
2670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 return;
2673 }
2674
2675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 else
2678 flags = (char_u *)"";
2679
2680 if (*flags == NUL)
2681 /* delete a file */
2682 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2683 else if (STRCMP(flags, "d") == 0)
2684 /* delete an empty directory */
2685 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2686 else if (STRCMP(flags, "rf") == 0)
2687 /* delete a directory recursively */
2688 rettv->vval.v_number = delete_recursive(name);
2689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691}
2692
2693/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002694 * "deletebufline()" function
2695 */
2696 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002697f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002698{
2699 buf_T *buf;
2700 linenr_T first, last;
2701 linenr_T lnum;
2702 long count;
2703 int is_curbuf;
2704 buf_T *curbuf_save = NULL;
2705 win_T *curwin_save = NULL;
2706 tabpage_T *tp;
2707 win_T *wp;
2708
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002709 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002710 if (buf == NULL)
2711 {
2712 rettv->vval.v_number = 1; /* FAIL */
2713 return;
2714 }
2715 is_curbuf = buf == curbuf;
2716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002717 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002718 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002719 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002720 else
2721 last = first;
2722
2723 if (buf->b_ml.ml_mfp == NULL || first < 1
2724 || first > buf->b_ml.ml_line_count || last < first)
2725 {
2726 rettv->vval.v_number = 1; /* FAIL */
2727 return;
2728 }
2729
2730 if (!is_curbuf)
2731 {
2732 curbuf_save = curbuf;
2733 curwin_save = curwin;
2734 curbuf = buf;
2735 find_win_for_curbuf();
2736 }
2737 if (last > curbuf->b_ml.ml_line_count)
2738 last = curbuf->b_ml.ml_line_count;
2739 count = last - first + 1;
2740
2741 // When coming here from Insert mode, sync undo, so that this can be
2742 // undone separately from what was previously inserted.
2743 if (u_sync_once == 2)
2744 {
2745 u_sync_once = 1; // notify that u_sync() was called
2746 u_sync(TRUE);
2747 }
2748
2749 if (u_save(first - 1, last + 1) == FAIL)
2750 {
2751 rettv->vval.v_number = 1; /* FAIL */
2752 return;
2753 }
2754
2755 for (lnum = first; lnum <= last; ++lnum)
2756 ml_delete(first, TRUE);
2757
2758 FOR_ALL_TAB_WINDOWS(tp, wp)
2759 if (wp->w_buffer == buf)
2760 {
2761 if (wp->w_cursor.lnum > last)
2762 wp->w_cursor.lnum -= count;
2763 else if (wp->w_cursor.lnum> first)
2764 wp->w_cursor.lnum = first;
2765 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2766 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2767 }
2768 check_cursor_col();
2769 deleted_lines_mark(first, count);
2770
2771 if (!is_curbuf)
2772 {
2773 curbuf = curbuf_save;
2774 curwin = curwin_save;
2775 }
2776}
2777
2778/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 * "did_filetype()" function
2780 */
2781 static void
2782f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2783{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785}
2786
2787/*
2788 * "diff_filler()" function
2789 */
2790 static void
2791f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2792{
2793#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795#endif
2796}
2797
2798/*
2799 * "diff_hlID()" function
2800 */
2801 static void
2802f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2803{
2804#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002805 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002807 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002808 static int fnum = 0;
2809 static int change_start = 0;
2810 static int change_end = 0;
2811 static hlf_T hlID = (hlf_T)0;
2812 int filler_lines;
2813 int col;
2814
2815 if (lnum < 0) /* ignore type error in {lnum} arg */
2816 lnum = 0;
2817 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002818 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 || fnum != curbuf->b_fnum)
2820 {
2821 /* New line, buffer, change: need to get the values. */
2822 filler_lines = diff_check(curwin, lnum);
2823 if (filler_lines < 0)
2824 {
2825 if (filler_lines == -1)
2826 {
2827 change_start = MAXCOL;
2828 change_end = -1;
2829 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2830 hlID = HLF_ADD; /* added line */
2831 else
2832 hlID = HLF_CHD; /* changed line */
2833 }
2834 else
2835 hlID = HLF_ADD; /* added line */
2836 }
2837 else
2838 hlID = (hlf_T)0;
2839 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002840 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002841 fnum = curbuf->b_fnum;
2842 }
2843
2844 if (hlID == HLF_CHD || hlID == HLF_TXD)
2845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002846 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002847 if (col >= change_start && col <= change_end)
2848 hlID = HLF_TXD; /* changed text */
2849 else
2850 hlID = HLF_CHD; /* changed line */
2851 }
2852 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2853#endif
2854}
2855
2856/*
2857 * "empty({expr})" function
2858 */
2859 static void
2860f_empty(typval_T *argvars, typval_T *rettv)
2861{
2862 int n = FALSE;
2863
2864 switch (argvars[0].v_type)
2865 {
2866 case VAR_STRING:
2867 case VAR_FUNC:
2868 n = argvars[0].vval.v_string == NULL
2869 || *argvars[0].vval.v_string == NUL;
2870 break;
2871 case VAR_PARTIAL:
2872 n = FALSE;
2873 break;
2874 case VAR_NUMBER:
2875 n = argvars[0].vval.v_number == 0;
2876 break;
2877 case VAR_FLOAT:
2878#ifdef FEAT_FLOAT
2879 n = argvars[0].vval.v_float == 0.0;
2880 break;
2881#endif
2882 case VAR_LIST:
2883 n = argvars[0].vval.v_list == NULL
2884 || argvars[0].vval.v_list->lv_first == NULL;
2885 break;
2886 case VAR_DICT:
2887 n = argvars[0].vval.v_dict == NULL
2888 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2889 break;
2890 case VAR_SPECIAL:
2891 n = argvars[0].vval.v_number != VVAL_TRUE;
2892 break;
2893
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002894 case VAR_BLOB:
2895 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002896 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2897 break;
2898
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 case VAR_JOB:
2900#ifdef FEAT_JOB_CHANNEL
2901 n = argvars[0].vval.v_job == NULL
2902 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2903 break;
2904#endif
2905 case VAR_CHANNEL:
2906#ifdef FEAT_JOB_CHANNEL
2907 n = argvars[0].vval.v_channel == NULL
2908 || !channel_is_open(argvars[0].vval.v_channel);
2909 break;
2910#endif
2911 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002912 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 n = TRUE;
2914 break;
2915 }
2916
2917 rettv->vval.v_number = n;
2918}
2919
2920/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002921 * "environ()" function
2922 */
2923 static void
2924f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2925{
2926#if !defined(AMIGA)
2927 int i = 0;
2928 char_u *entry, *value;
2929# ifdef MSWIN
2930 extern wchar_t **_wenviron;
2931# else
2932 extern char **environ;
2933# endif
2934
2935 if (rettv_dict_alloc(rettv) != OK)
2936 return;
2937
2938# ifdef MSWIN
2939 if (*_wenviron == NULL)
2940 return;
2941# else
2942 if (*environ == NULL)
2943 return;
2944# endif
2945
2946 for (i = 0; ; ++i)
2947 {
2948# ifdef MSWIN
2949 short_u *p;
2950
2951 if ((p = (short_u *)_wenviron[i]) == NULL)
2952 return;
2953 entry = utf16_to_enc(p, NULL);
2954# else
2955 if ((entry = (char_u *)environ[i]) == NULL)
2956 return;
2957 entry = vim_strsave(entry);
2958# endif
2959 if (entry == NULL) // out of memory
2960 return;
2961 if ((value = vim_strchr(entry, '=')) == NULL)
2962 {
2963 vim_free(entry);
2964 continue;
2965 }
2966 *value++ = NUL;
2967 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2968 vim_free(entry);
2969 }
2970#endif
2971}
2972
2973/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974 * "escape({string}, {chars})" function
2975 */
2976 static void
2977f_escape(typval_T *argvars, typval_T *rettv)
2978{
2979 char_u buf[NUMBUFLEN];
2980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002981 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2982 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 rettv->v_type = VAR_STRING;
2984}
2985
2986/*
2987 * "eval()" function
2988 */
2989 static void
2990f_eval(typval_T *argvars, typval_T *rettv)
2991{
2992 char_u *s, *p;
2993
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002994 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 if (s != NULL)
2996 s = skipwhite(s);
2997
2998 p = s;
2999 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3000 {
3001 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003002 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 need_clr_eos = FALSE;
3004 rettv->v_type = VAR_NUMBER;
3005 rettv->vval.v_number = 0;
3006 }
3007 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009}
3010
3011/*
3012 * "eventhandler()" function
3013 */
3014 static void
3015f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3016{
3017 rettv->vval.v_number = vgetc_busy;
3018}
3019
3020/*
3021 * "executable()" function
3022 */
3023 static void
3024f_executable(typval_T *argvars, typval_T *rettv)
3025{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003026 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027
3028 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003029 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030}
3031
3032static garray_T redir_execute_ga;
3033
3034/*
3035 * Append "value[value_len]" to the execute() output.
3036 */
3037 void
3038execute_redir_str(char_u *value, int value_len)
3039{
3040 int len;
3041
3042 if (value_len == -1)
3043 len = (int)STRLEN(value); /* Append the entire string */
3044 else
3045 len = value_len; /* Append only "value_len" characters */
3046 if (ga_grow(&redir_execute_ga, len) == OK)
3047 {
3048 mch_memmove((char *)redir_execute_ga.ga_data
3049 + redir_execute_ga.ga_len, value, len);
3050 redir_execute_ga.ga_len += len;
3051 }
3052}
3053
3054/*
3055 * Get next line from a list.
3056 * Called by do_cmdline() to get the next line.
3057 * Returns allocated string, or NULL for end of function.
3058 */
3059
3060 static char_u *
3061get_list_line(
3062 int c UNUSED,
3063 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003064 int indent UNUSED,
3065 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066{
3067 listitem_T **p = (listitem_T **)cookie;
3068 listitem_T *item = *p;
3069 char_u buf[NUMBUFLEN];
3070 char_u *s;
3071
3072 if (item == NULL)
3073 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075 *p = item->li_next;
3076 return s == NULL ? NULL : vim_strsave(s);
3077}
3078
3079/*
3080 * "execute()" function
3081 */
3082 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003083execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084{
3085 char_u *cmd = NULL;
3086 list_T *list = NULL;
3087 int save_msg_silent = msg_silent;
3088 int save_emsg_silent = emsg_silent;
3089 int save_emsg_noredir = emsg_noredir;
3090 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003091 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003093 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003094 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095
3096 rettv->vval.v_string = NULL;
3097 rettv->v_type = VAR_STRING;
3098
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003099 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003101 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 if (list == NULL || list->lv_first == NULL)
3103 /* empty list, no commands, empty output */
3104 return;
3105 ++list->lv_refcount;
3106 }
3107 else
3108 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003109 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 if (cmd == NULL)
3111 return;
3112 }
3113
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003114 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 {
3116 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003117 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118
3119 if (s == NULL)
3120 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003121 if (*s == NUL)
3122 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 if (STRNCMP(s, "silent", 6) == 0)
3124 ++msg_silent;
3125 if (STRCMP(s, "silent!") == 0)
3126 {
3127 emsg_silent = TRUE;
3128 emsg_noredir = TRUE;
3129 }
3130 }
3131 else
3132 ++msg_silent;
3133
3134 if (redir_execute)
3135 save_ga = redir_execute_ga;
3136 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3137 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003138 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003139 if (!echo_output)
3140 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141
3142 if (cmd != NULL)
3143 do_cmdline_cmd(cmd);
3144 else
3145 {
3146 listitem_T *item = list->lv_first;
3147
3148 do_cmdline(NULL, get_list_line, (void *)&item,
3149 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3150 --list->lv_refcount;
3151 }
3152
Bram Moolenaard297f352017-01-29 20:31:21 +01003153 /* Need to append a NUL to the result. */
3154 if (ga_grow(&redir_execute_ga, 1) == OK)
3155 {
3156 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3157 rettv->vval.v_string = redir_execute_ga.ga_data;
3158 }
3159 else
3160 {
3161 ga_clear(&redir_execute_ga);
3162 rettv->vval.v_string = NULL;
3163 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 msg_silent = save_msg_silent;
3165 emsg_silent = save_emsg_silent;
3166 emsg_noredir = save_emsg_noredir;
3167
3168 redir_execute = save_redir_execute;
3169 if (redir_execute)
3170 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003171 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003173 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003174 if (echo_output)
3175 // When not working silently: put it in column zero. A following
3176 // "echon" will overwrite the message, unavoidably.
3177 msg_col = 0;
3178 else
3179 // When working silently: Put it back where it was, since nothing
3180 // should have been written.
3181 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182}
3183
3184/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003185 * "execute()" function
3186 */
3187 static void
3188f_execute(typval_T *argvars, typval_T *rettv)
3189{
3190 execute_common(argvars, rettv, 0);
3191}
3192
3193/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194 * "exepath()" function
3195 */
3196 static void
3197f_exepath(typval_T *argvars, typval_T *rettv)
3198{
3199 char_u *p = NULL;
3200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003201 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202 rettv->v_type = VAR_STRING;
3203 rettv->vval.v_string = p;
3204}
3205
3206/*
3207 * "exists()" function
3208 */
3209 static void
3210f_exists(typval_T *argvars, typval_T *rettv)
3211{
3212 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003215 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 if (*p == '$') /* environment variable */
3217 {
3218 /* first try "normal" environment variables (fast) */
3219 if (mch_getenv(p + 1) != NULL)
3220 n = TRUE;
3221 else
3222 {
3223 /* try expanding things like $VIM and ${HOME} */
3224 p = expand_env_save(p);
3225 if (p != NULL && *p != '$')
3226 n = TRUE;
3227 vim_free(p);
3228 }
3229 }
3230 else if (*p == '&' || *p == '+') /* option */
3231 {
3232 n = (get_option_tv(&p, NULL, TRUE) == OK);
3233 if (*skipwhite(p) != NUL)
3234 n = FALSE; /* trailing garbage */
3235 }
3236 else if (*p == '*') /* internal or user defined function */
3237 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003238 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 }
3240 else if (*p == ':')
3241 {
3242 n = cmd_exists(p + 1);
3243 }
3244 else if (*p == '#')
3245 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 if (p[1] == '#')
3247 n = autocmd_supported(p + 2);
3248 else
3249 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 }
3251 else /* internal variable */
3252 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003253 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 }
3255
3256 rettv->vval.v_number = n;
3257}
3258
3259#ifdef FEAT_FLOAT
3260/*
3261 * "exp()" function
3262 */
3263 static void
3264f_exp(typval_T *argvars, typval_T *rettv)
3265{
3266 float_T f = 0.0;
3267
3268 rettv->v_type = VAR_FLOAT;
3269 if (get_float_arg(argvars, &f) == OK)
3270 rettv->vval.v_float = exp(f);
3271 else
3272 rettv->vval.v_float = 0.0;
3273}
3274#endif
3275
3276/*
3277 * "expand()" function
3278 */
3279 static void
3280f_expand(typval_T *argvars, typval_T *rettv)
3281{
3282 char_u *s;
3283 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003284 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3286 expand_T xpc;
3287 int error = FALSE;
3288 char_u *result;
3289
3290 rettv->v_type = VAR_STRING;
3291 if (argvars[1].v_type != VAR_UNKNOWN
3292 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003293 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003295 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003297 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 if (*s == '%' || *s == '#' || *s == '<')
3299 {
3300 ++emsg_off;
3301 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3302 --emsg_off;
3303 if (rettv->v_type == VAR_LIST)
3304 {
3305 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3306 list_append_string(rettv->vval.v_list, result, -1);
3307 else
3308 vim_free(result);
3309 }
3310 else
3311 rettv->vval.v_string = result;
3312 }
3313 else
3314 {
3315 /* When the optional second argument is non-zero, don't remove matches
3316 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3317 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003318 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 options |= WILD_KEEP_ALL;
3320 if (!error)
3321 {
3322 ExpandInit(&xpc);
3323 xpc.xp_context = EXPAND_FILES;
3324 if (p_wic)
3325 options += WILD_ICASE;
3326 if (rettv->v_type == VAR_STRING)
3327 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3328 options, WILD_ALL);
3329 else if (rettv_list_alloc(rettv) != FAIL)
3330 {
3331 int i;
3332
3333 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3334 for (i = 0; i < xpc.xp_numfiles; i++)
3335 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3336 ExpandCleanup(&xpc);
3337 }
3338 }
3339 else
3340 rettv->vval.v_string = NULL;
3341 }
3342}
3343
3344/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003345 * "expandcmd()" function
3346 * Expand all the special characters in a command string.
3347 */
3348 static void
3349f_expandcmd(typval_T *argvars, typval_T *rettv)
3350{
3351 exarg_T eap;
3352 char_u *cmdstr;
3353 char *errormsg = NULL;
3354
3355 rettv->v_type = VAR_STRING;
3356 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3357
3358 memset(&eap, 0, sizeof(eap));
3359 eap.cmd = cmdstr;
3360 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003361 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003362 eap.usefilter = FALSE;
3363 eap.nextcmd = NULL;
3364 eap.cmdidx = CMD_USER;
3365
3366 expand_filename(&eap, &cmdstr, &errormsg);
3367 if (errormsg != NULL && *errormsg != NUL)
3368 emsg(errormsg);
3369
3370 rettv->vval.v_string = cmdstr;
3371}
3372
3373/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374 * "extend(list, list [, idx])" function
3375 * "extend(dict, dict [, action])" function
3376 */
3377 static void
3378f_extend(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3381
3382 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3383 {
3384 list_T *l1, *l2;
3385 listitem_T *item;
3386 long before;
3387 int error = FALSE;
3388
3389 l1 = argvars[0].vval.v_list;
3390 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003391 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 && l2 != NULL)
3393 {
3394 if (argvars[2].v_type != VAR_UNKNOWN)
3395 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003396 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 if (error)
3398 return; /* type error; errmsg already given */
3399
3400 if (before == l1->lv_len)
3401 item = NULL;
3402 else
3403 {
3404 item = list_find(l1, before);
3405 if (item == NULL)
3406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003408 return;
3409 }
3410 }
3411 }
3412 else
3413 item = NULL;
3414 list_extend(l1, l2, item);
3415
3416 copy_tv(&argvars[0], rettv);
3417 }
3418 }
3419 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3420 {
3421 dict_T *d1, *d2;
3422 char_u *action;
3423 int i;
3424
3425 d1 = argvars[0].vval.v_dict;
3426 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003427 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 && d2 != NULL)
3429 {
3430 /* Check the third argument. */
3431 if (argvars[2].v_type != VAR_UNKNOWN)
3432 {
3433 static char *(av[]) = {"keep", "force", "error"};
3434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 if (action == NULL)
3437 return; /* type error; errmsg already given */
3438 for (i = 0; i < 3; ++i)
3439 if (STRCMP(action, av[i]) == 0)
3440 break;
3441 if (i == 3)
3442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003443 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 return;
3445 }
3446 }
3447 else
3448 action = (char_u *)"force";
3449
3450 dict_extend(d1, d2, action);
3451
3452 copy_tv(&argvars[0], rettv);
3453 }
3454 }
3455 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003456 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457}
3458
3459/*
3460 * "feedkeys()" function
3461 */
3462 static void
3463f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3464{
3465 int remap = TRUE;
3466 int insert = FALSE;
3467 char_u *keys, *flags;
3468 char_u nbuf[NUMBUFLEN];
3469 int typed = FALSE;
3470 int execute = FALSE;
3471 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003472 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473 char_u *keys_esc;
3474
3475 /* This is not allowed in the sandbox. If the commands would still be
3476 * executed in the sandbox it would be OK, but it probably happens later,
3477 * when "sandbox" is no longer set. */
3478 if (check_secure())
3479 return;
3480
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003481 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482
3483 if (argvars[1].v_type != VAR_UNKNOWN)
3484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003485 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 for ( ; *flags != NUL; ++flags)
3487 {
3488 switch (*flags)
3489 {
3490 case 'n': remap = FALSE; break;
3491 case 'm': remap = TRUE; break;
3492 case 't': typed = TRUE; break;
3493 case 'i': insert = TRUE; break;
3494 case 'x': execute = TRUE; break;
3495 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003496 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003497 }
3498 }
3499 }
3500
3501 if (*keys != NUL || execute)
3502 {
3503 /* Need to escape K_SPECIAL and CSI before putting the string in the
3504 * typeahead buffer. */
3505 keys_esc = vim_strsave_escape_csi(keys);
3506 if (keys_esc != NULL)
3507 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003508 if (lowlevel)
3509 {
3510#ifdef USE_INPUT_BUF
3511 add_to_input_buf(keys, (int)STRLEN(keys));
3512#else
3513 emsg(_("E980: lowlevel input not supported"));
3514#endif
3515 }
3516 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003517 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003518 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003520 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003521#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003522 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003523#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003524 )
3525 typebuf_was_filled = TRUE;
3526 }
3527 vim_free(keys_esc);
3528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 if (execute)
3530 {
3531 int save_msg_scroll = msg_scroll;
3532
3533 /* Avoid a 1 second delay when the keys start Insert mode. */
3534 msg_scroll = FALSE;
3535
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003536 if (!dangerous)
3537 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003538 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003539 if (!dangerous)
3540 --ex_normal_busy;
3541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542 msg_scroll |= save_msg_scroll;
3543 }
3544 }
3545 }
3546}
3547
3548/*
3549 * "filereadable()" function
3550 */
3551 static void
3552f_filereadable(typval_T *argvars, typval_T *rettv)
3553{
3554 int fd;
3555 char_u *p;
3556 int n;
3557
3558#ifndef O_NONBLOCK
3559# define O_NONBLOCK 0
3560#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003561 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3563 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3564 {
3565 n = TRUE;
3566 close(fd);
3567 }
3568 else
3569 n = FALSE;
3570
3571 rettv->vval.v_number = n;
3572}
3573
3574/*
3575 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3576 * rights to write into.
3577 */
3578 static void
3579f_filewritable(typval_T *argvars, typval_T *rettv)
3580{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003581 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582}
3583
3584 static void
3585findfilendir(
3586 typval_T *argvars UNUSED,
3587 typval_T *rettv,
3588 int find_what UNUSED)
3589{
3590#ifdef FEAT_SEARCHPATH
3591 char_u *fname;
3592 char_u *fresult = NULL;
3593 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3594 char_u *p;
3595 char_u pathbuf[NUMBUFLEN];
3596 int count = 1;
3597 int first = TRUE;
3598 int error = FALSE;
3599#endif
3600
3601 rettv->vval.v_string = NULL;
3602 rettv->v_type = VAR_STRING;
3603
3604#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003605 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606
3607 if (argvars[1].v_type != VAR_UNKNOWN)
3608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003609 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 if (p == NULL)
3611 error = TRUE;
3612 else
3613 {
3614 if (*p != NUL)
3615 path = p;
3616
3617 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003618 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619 }
3620 }
3621
3622 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3623 error = TRUE;
3624
3625 if (*fname != NUL && !error)
3626 {
3627 do
3628 {
3629 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3630 vim_free(fresult);
3631 fresult = find_file_in_path_option(first ? fname : NULL,
3632 first ? (int)STRLEN(fname) : 0,
3633 0, first, path,
3634 find_what,
3635 curbuf->b_ffname,
3636 find_what == FINDFILE_DIR
3637 ? (char_u *)"" : curbuf->b_p_sua);
3638 first = FALSE;
3639
3640 if (fresult != NULL && rettv->v_type == VAR_LIST)
3641 list_append_string(rettv->vval.v_list, fresult, -1);
3642
3643 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3644 }
3645
3646 if (rettv->v_type == VAR_STRING)
3647 rettv->vval.v_string = fresult;
3648#endif
3649}
3650
3651/*
3652 * "filter()" function
3653 */
3654 static void
3655f_filter(typval_T *argvars, typval_T *rettv)
3656{
3657 filter_map(argvars, rettv, FALSE);
3658}
3659
3660/*
3661 * "finddir({fname}[, {path}[, {count}]])" function
3662 */
3663 static void
3664f_finddir(typval_T *argvars, typval_T *rettv)
3665{
3666 findfilendir(argvars, rettv, FINDFILE_DIR);
3667}
3668
3669/*
3670 * "findfile({fname}[, {path}[, {count}]])" function
3671 */
3672 static void
3673f_findfile(typval_T *argvars, typval_T *rettv)
3674{
3675 findfilendir(argvars, rettv, FINDFILE_FILE);
3676}
3677
3678#ifdef FEAT_FLOAT
3679/*
3680 * "float2nr({float})" function
3681 */
3682 static void
3683f_float2nr(typval_T *argvars, typval_T *rettv)
3684{
3685 float_T f = 0.0;
3686
3687 if (get_float_arg(argvars, &f) == OK)
3688 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003689 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003690 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003691 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003692 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 else
3694 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 }
3696}
3697
3698/*
3699 * "floor({float})" function
3700 */
3701 static void
3702f_floor(typval_T *argvars, typval_T *rettv)
3703{
3704 float_T f = 0.0;
3705
3706 rettv->v_type = VAR_FLOAT;
3707 if (get_float_arg(argvars, &f) == OK)
3708 rettv->vval.v_float = floor(f);
3709 else
3710 rettv->vval.v_float = 0.0;
3711}
3712
3713/*
3714 * "fmod()" function
3715 */
3716 static void
3717f_fmod(typval_T *argvars, typval_T *rettv)
3718{
3719 float_T fx = 0.0, fy = 0.0;
3720
3721 rettv->v_type = VAR_FLOAT;
3722 if (get_float_arg(argvars, &fx) == OK
3723 && get_float_arg(&argvars[1], &fy) == OK)
3724 rettv->vval.v_float = fmod(fx, fy);
3725 else
3726 rettv->vval.v_float = 0.0;
3727}
3728#endif
3729
3730/*
3731 * "fnameescape({string})" function
3732 */
3733 static void
3734f_fnameescape(typval_T *argvars, typval_T *rettv)
3735{
3736 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003737 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 rettv->v_type = VAR_STRING;
3739}
3740
3741/*
3742 * "fnamemodify({fname}, {mods})" function
3743 */
3744 static void
3745f_fnamemodify(typval_T *argvars, typval_T *rettv)
3746{
3747 char_u *fname;
3748 char_u *mods;
3749 int usedlen = 0;
3750 int len;
3751 char_u *fbuf = NULL;
3752 char_u buf[NUMBUFLEN];
3753
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003754 fname = tv_get_string_chk(&argvars[0]);
3755 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 if (fname == NULL || mods == NULL)
3757 fname = NULL;
3758 else
3759 {
3760 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003761 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 }
3763
3764 rettv->v_type = VAR_STRING;
3765 if (fname == NULL)
3766 rettv->vval.v_string = NULL;
3767 else
3768 rettv->vval.v_string = vim_strnsave(fname, len);
3769 vim_free(fbuf);
3770}
3771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772/*
3773 * "foldclosed()" function
3774 */
3775 static void
3776foldclosed_both(
3777 typval_T *argvars UNUSED,
3778 typval_T *rettv,
3779 int end UNUSED)
3780{
3781#ifdef FEAT_FOLDING
3782 linenr_T lnum;
3783 linenr_T first, last;
3784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003785 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003786 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3787 {
3788 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3789 {
3790 if (end)
3791 rettv->vval.v_number = (varnumber_T)last;
3792 else
3793 rettv->vval.v_number = (varnumber_T)first;
3794 return;
3795 }
3796 }
3797#endif
3798 rettv->vval.v_number = -1;
3799}
3800
3801/*
3802 * "foldclosed()" function
3803 */
3804 static void
3805f_foldclosed(typval_T *argvars, typval_T *rettv)
3806{
3807 foldclosed_both(argvars, rettv, FALSE);
3808}
3809
3810/*
3811 * "foldclosedend()" function
3812 */
3813 static void
3814f_foldclosedend(typval_T *argvars, typval_T *rettv)
3815{
3816 foldclosed_both(argvars, rettv, TRUE);
3817}
3818
3819/*
3820 * "foldlevel()" function
3821 */
3822 static void
3823f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3824{
3825#ifdef FEAT_FOLDING
3826 linenr_T lnum;
3827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003828 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3830 rettv->vval.v_number = foldLevel(lnum);
3831#endif
3832}
3833
3834/*
3835 * "foldtext()" function
3836 */
3837 static void
3838f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3839{
3840#ifdef FEAT_FOLDING
3841 linenr_T foldstart;
3842 linenr_T foldend;
3843 char_u *dashes;
3844 linenr_T lnum;
3845 char_u *s;
3846 char_u *r;
3847 int len;
3848 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003849 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850#endif
3851
3852 rettv->v_type = VAR_STRING;
3853 rettv->vval.v_string = NULL;
3854#ifdef FEAT_FOLDING
3855 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3856 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3857 dashes = get_vim_var_str(VV_FOLDDASHES);
3858 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3859 && dashes != NULL)
3860 {
3861 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003862 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863 if (!linewhite(lnum))
3864 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003865
3866 /* Find interesting text in this line. */
3867 s = skipwhite(ml_get(lnum));
3868 /* skip C comment-start */
3869 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3870 {
3871 s = skipwhite(s + 2);
3872 if (*skipwhite(s) == NUL
3873 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3874 {
3875 s = skipwhite(ml_get(lnum + 1));
3876 if (*s == '*')
3877 s = skipwhite(s + 1);
3878 }
3879 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003880 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003881 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003882 r = alloc(STRLEN(txt)
3883 + STRLEN(dashes) // for %s
3884 + 20 // for %3ld
3885 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 if (r != NULL)
3887 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003888 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 len = (int)STRLEN(r);
3890 STRCAT(r, s);
3891 /* remove 'foldmarker' and 'commentstring' */
3892 foldtext_cleanup(r + len);
3893 rettv->vval.v_string = r;
3894 }
3895 }
3896#endif
3897}
3898
3899/*
3900 * "foldtextresult(lnum)" function
3901 */
3902 static void
3903f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3904{
3905#ifdef FEAT_FOLDING
3906 linenr_T lnum;
3907 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003908 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909 foldinfo_T foldinfo;
3910 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003911 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912#endif
3913
3914 rettv->v_type = VAR_STRING;
3915 rettv->vval.v_string = NULL;
3916#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003917 if (entered)
3918 return; /* reject recursive use */
3919 entered = TRUE;
3920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003921 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 /* treat illegal types and illegal string values for {lnum} the same */
3923 if (lnum < 0)
3924 lnum = 0;
3925 fold_count = foldedCount(curwin, lnum, &foldinfo);
3926 if (fold_count > 0)
3927 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003928 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3929 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 if (text == buf)
3931 text = vim_strsave(text);
3932 rettv->vval.v_string = text;
3933 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003934
3935 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936#endif
3937}
3938
3939/*
3940 * "foreground()" function
3941 */
3942 static void
3943f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3944{
3945#ifdef FEAT_GUI
3946 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003947 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003949 return;
3950 }
3951#endif
3952#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954#endif
3955}
3956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003958common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959{
3960 char_u *s;
3961 char_u *name;
3962 int use_string = FALSE;
3963 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003964 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965
3966 if (argvars[0].v_type == VAR_FUNC)
3967 {
3968 /* function(MyFunc, [arg], dict) */
3969 s = argvars[0].vval.v_string;
3970 }
3971 else if (argvars[0].v_type == VAR_PARTIAL
3972 && argvars[0].vval.v_partial != NULL)
3973 {
3974 /* function(dict.MyFunc, [arg]) */
3975 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003976 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 }
3978 else
3979 {
3980 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003981 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003982 use_string = TRUE;
3983 }
3984
Bram Moolenaar843b8842016-08-21 14:36:15 +02003985 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003986 {
3987 name = s;
3988 trans_name = trans_function_name(&name, FALSE,
3989 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3990 if (*name != NUL)
3991 s = NULL;
3992 }
3993
Bram Moolenaar843b8842016-08-21 14:36:15 +02003994 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3995 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003996 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003997 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003998 else if (trans_name != NULL && (is_funcref
3999 ? find_func(trans_name) == NULL
4000 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004001 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002 else
4003 {
4004 int dict_idx = 0;
4005 int arg_idx = 0;
4006 list_T *list = NULL;
4007
4008 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4009 {
4010 char sid_buf[25];
4011 int off = *s == 's' ? 2 : 5;
4012
4013 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4014 * also be called from another script. Using trans_function_name()
4015 * would also work, but some plugins depend on the name being
4016 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004017 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004018 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 if (name != NULL)
4020 {
4021 STRCPY(name, sid_buf);
4022 STRCAT(name, s + off);
4023 }
4024 }
4025 else
4026 name = vim_strsave(s);
4027
4028 if (argvars[1].v_type != VAR_UNKNOWN)
4029 {
4030 if (argvars[2].v_type != VAR_UNKNOWN)
4031 {
4032 /* function(name, [args], dict) */
4033 arg_idx = 1;
4034 dict_idx = 2;
4035 }
4036 else if (argvars[1].v_type == VAR_DICT)
4037 /* function(name, dict) */
4038 dict_idx = 1;
4039 else
4040 /* function(name, [args]) */
4041 arg_idx = 1;
4042 if (dict_idx > 0)
4043 {
4044 if (argvars[dict_idx].v_type != VAR_DICT)
4045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004046 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004047 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004048 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004049 }
4050 if (argvars[dict_idx].vval.v_dict == NULL)
4051 dict_idx = 0;
4052 }
4053 if (arg_idx > 0)
4054 {
4055 if (argvars[arg_idx].v_type != VAR_LIST)
4056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004057 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004059 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060 }
4061 list = argvars[arg_idx].vval.v_list;
4062 if (list == NULL || list->lv_len == 0)
4063 arg_idx = 0;
4064 }
4065 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004066 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004068 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069
4070 /* result is a VAR_PARTIAL */
4071 if (pt == NULL)
4072 vim_free(name);
4073 else
4074 {
4075 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4076 {
4077 listitem_T *li;
4078 int i = 0;
4079 int arg_len = 0;
4080 int lv_len = 0;
4081
4082 if (arg_pt != NULL)
4083 arg_len = arg_pt->pt_argc;
4084 if (list != NULL)
4085 lv_len = list->lv_len;
4086 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004087 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 if (pt->pt_argv == NULL)
4089 {
4090 vim_free(pt);
4091 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004092 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004094 for (i = 0; i < arg_len; i++)
4095 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4096 if (lv_len > 0)
4097 for (li = list->lv_first; li != NULL;
4098 li = li->li_next)
4099 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 }
4101
4102 /* For "function(dict.func, [], dict)" and "func" is a partial
4103 * use "dict". That is backwards compatible. */
4104 if (dict_idx > 0)
4105 {
4106 /* The dict is bound explicitly, pt_auto is FALSE. */
4107 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4108 ++pt->pt_dict->dv_refcount;
4109 }
4110 else if (arg_pt != NULL)
4111 {
4112 /* If the dict was bound automatically the result is also
4113 * bound automatically. */
4114 pt->pt_dict = arg_pt->pt_dict;
4115 pt->pt_auto = arg_pt->pt_auto;
4116 if (pt->pt_dict != NULL)
4117 ++pt->pt_dict->dv_refcount;
4118 }
4119
4120 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004121 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4122 {
4123 pt->pt_func = arg_pt->pt_func;
4124 func_ptr_ref(pt->pt_func);
4125 vim_free(name);
4126 }
4127 else if (is_funcref)
4128 {
4129 pt->pt_func = find_func(trans_name);
4130 func_ptr_ref(pt->pt_func);
4131 vim_free(name);
4132 }
4133 else
4134 {
4135 pt->pt_name = name;
4136 func_ref(name);
4137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138 }
4139 rettv->v_type = VAR_PARTIAL;
4140 rettv->vval.v_partial = pt;
4141 }
4142 else
4143 {
4144 /* result is a VAR_FUNC */
4145 rettv->v_type = VAR_FUNC;
4146 rettv->vval.v_string = name;
4147 func_ref(name);
4148 }
4149 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150theend:
4151 vim_free(trans_name);
4152}
4153
4154/*
4155 * "funcref()" function
4156 */
4157 static void
4158f_funcref(typval_T *argvars, typval_T *rettv)
4159{
4160 common_function(argvars, rettv, TRUE);
4161}
4162
4163/*
4164 * "function()" function
4165 */
4166 static void
4167f_function(typval_T *argvars, typval_T *rettv)
4168{
4169 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170}
4171
4172/*
4173 * "garbagecollect()" function
4174 */
4175 static void
4176f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4177{
4178 /* This is postponed until we are back at the toplevel, because we may be
4179 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4180 want_garbage_collect = TRUE;
4181
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004182 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 garbage_collect_at_exit = TRUE;
4184}
4185
4186/*
4187 * "get()" function
4188 */
4189 static void
4190f_get(typval_T *argvars, typval_T *rettv)
4191{
4192 listitem_T *li;
4193 list_T *l;
4194 dictitem_T *di;
4195 dict_T *d;
4196 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004197 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004199 if (argvars[0].v_type == VAR_BLOB)
4200 {
4201 int error = FALSE;
4202 int idx = tv_get_number_chk(&argvars[1], &error);
4203
4204 if (!error)
4205 {
4206 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004207 if (idx < 0)
4208 idx = blob_len(argvars[0].vval.v_blob) + idx;
4209 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4210 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004211 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004212 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004213 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004214 tv = rettv;
4215 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004216 }
4217 }
4218 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004219 {
4220 if ((l = argvars[0].vval.v_list) != NULL)
4221 {
4222 int error = FALSE;
4223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004224 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004225 if (!error && li != NULL)
4226 tv = &li->li_tv;
4227 }
4228 }
4229 else if (argvars[0].v_type == VAR_DICT)
4230 {
4231 if ((d = argvars[0].vval.v_dict) != NULL)
4232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004233 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 if (di != NULL)
4235 tv = &di->di_tv;
4236 }
4237 }
4238 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4239 {
4240 partial_T *pt;
4241 partial_T fref_pt;
4242
4243 if (argvars[0].v_type == VAR_PARTIAL)
4244 pt = argvars[0].vval.v_partial;
4245 else
4246 {
4247 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4248 fref_pt.pt_name = argvars[0].vval.v_string;
4249 pt = &fref_pt;
4250 }
4251
4252 if (pt != NULL)
4253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004254 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256
4257 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4258 {
4259 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004260 n = partial_name(pt);
4261 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 rettv->vval.v_string = NULL;
4263 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004264 {
4265 rettv->vval.v_string = vim_strsave(n);
4266 if (rettv->v_type == VAR_FUNC)
4267 func_ref(rettv->vval.v_string);
4268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269 }
4270 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004271 {
4272 what_is_dict = TRUE;
4273 if (pt->pt_dict != NULL)
4274 rettv_dict_set(rettv, pt->pt_dict);
4275 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004276 else if (STRCMP(what, "args") == 0)
4277 {
4278 rettv->v_type = VAR_LIST;
4279 if (rettv_list_alloc(rettv) == OK)
4280 {
4281 int i;
4282
4283 for (i = 0; i < pt->pt_argc; ++i)
4284 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4285 }
4286 }
4287 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004288 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004289
4290 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4291 // third argument
4292 if (!what_is_dict)
4293 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294 }
4295 }
4296 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004297 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298
4299 if (tv == NULL)
4300 {
4301 if (argvars[2].v_type != VAR_UNKNOWN)
4302 copy_tv(&argvars[2], rettv);
4303 }
4304 else
4305 copy_tv(tv, rettv);
4306}
4307
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004308/*
4309 * Returns buffer options, variables and other attributes in a dictionary.
4310 */
4311 static dict_T *
4312get_buffer_info(buf_T *buf)
4313{
4314 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004315 tabpage_T *tp;
4316 win_T *wp;
4317 list_T *windows;
4318
4319 dict = dict_alloc();
4320 if (dict == NULL)
4321 return NULL;
4322
Bram Moolenaare0be1672018-07-08 16:50:37 +02004323 dict_add_number(dict, "bufnr", buf->b_fnum);
4324 dict_add_string(dict, "name", buf->b_ffname);
4325 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4326 : buflist_findlnum(buf));
4327 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4328 dict_add_number(dict, "listed", buf->b_p_bl);
4329 dict_add_number(dict, "changed", bufIsChanged(buf));
4330 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4331 dict_add_number(dict, "hidden",
4332 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004333
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004334 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004335 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004336
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004337 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004338 windows = list_alloc();
4339 if (windows != NULL)
4340 {
4341 FOR_ALL_TAB_WINDOWS(tp, wp)
4342 if (wp->w_buffer == buf)
4343 list_append_number(windows, (varnumber_T)wp->w_id);
4344 dict_add_list(dict, "windows", windows);
4345 }
4346
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004347#ifdef FEAT_TEXT_PROP
4348 // List of popup windows displaying this buffer
4349 windows = list_alloc();
4350 if (windows != NULL)
4351 {
4352 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4353 if (wp->w_buffer == buf)
4354 list_append_number(windows, (varnumber_T)wp->w_id);
4355 FOR_ALL_TABPAGES(tp)
4356 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4357 if (wp->w_buffer == buf)
4358 list_append_number(windows, (varnumber_T)wp->w_id);
4359
4360 dict_add_list(dict, "popups", windows);
4361 }
4362#endif
4363
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004364#ifdef FEAT_SIGNS
4365 if (buf->b_signlist != NULL)
4366 {
4367 /* List of signs placed in this buffer */
4368 list_T *signs = list_alloc();
4369 if (signs != NULL)
4370 {
4371 get_buffer_signs(buf, signs);
4372 dict_add_list(dict, "signs", signs);
4373 }
4374 }
4375#endif
4376
4377 return dict;
4378}
4379
4380/*
4381 * "getbufinfo()" function
4382 */
4383 static void
4384f_getbufinfo(typval_T *argvars, typval_T *rettv)
4385{
4386 buf_T *buf = NULL;
4387 buf_T *argbuf = NULL;
4388 dict_T *d;
4389 int filtered = FALSE;
4390 int sel_buflisted = FALSE;
4391 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004392 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004393
4394 if (rettv_list_alloc(rettv) != OK)
4395 return;
4396
4397 /* List of all the buffers or selected buffers */
4398 if (argvars[0].v_type == VAR_DICT)
4399 {
4400 dict_T *sel_d = argvars[0].vval.v_dict;
4401
4402 if (sel_d != NULL)
4403 {
4404 dictitem_T *di;
4405
4406 filtered = TRUE;
4407
4408 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004409 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004410 sel_buflisted = TRUE;
4411
4412 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004413 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004414 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004415
4416 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004417 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004418 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004419 }
4420 }
4421 else if (argvars[0].v_type != VAR_UNKNOWN)
4422 {
4423 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004424 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004425 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004426 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004427 --emsg_off;
4428 if (argbuf == NULL)
4429 return;
4430 }
4431
4432 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004433 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004434 {
4435 if (argbuf != NULL && argbuf != buf)
4436 continue;
4437 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004438 || (sel_buflisted && !buf->b_p_bl)
4439 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004440 continue;
4441
4442 d = get_buffer_info(buf);
4443 if (d != NULL)
4444 list_append_dict(rettv->vval.v_list, d);
4445 if (argbuf != NULL)
4446 return;
4447 }
4448}
4449
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450/*
4451 * Get line or list of lines from buffer "buf" into "rettv".
4452 * Return a range (from start to end) of lines in rettv from the specified
4453 * buffer.
4454 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4455 */
4456 static void
4457get_buffer_lines(
4458 buf_T *buf,
4459 linenr_T start,
4460 linenr_T end,
4461 int retlist,
4462 typval_T *rettv)
4463{
4464 char_u *p;
4465
4466 rettv->v_type = VAR_STRING;
4467 rettv->vval.v_string = NULL;
4468 if (retlist && rettv_list_alloc(rettv) == FAIL)
4469 return;
4470
4471 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4472 return;
4473
4474 if (!retlist)
4475 {
4476 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4477 p = ml_get_buf(buf, start, FALSE);
4478 else
4479 p = (char_u *)"";
4480 rettv->vval.v_string = vim_strsave(p);
4481 }
4482 else
4483 {
4484 if (end < start)
4485 return;
4486
4487 if (start < 1)
4488 start = 1;
4489 if (end > buf->b_ml.ml_line_count)
4490 end = buf->b_ml.ml_line_count;
4491 while (start <= end)
4492 if (list_append_string(rettv->vval.v_list,
4493 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4494 break;
4495 }
4496}
4497
4498/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499 * "getbufline()" function
4500 */
4501 static void
4502f_getbufline(typval_T *argvars, typval_T *rettv)
4503{
4504 linenr_T lnum;
4505 linenr_T end;
4506 buf_T *buf;
4507
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004508 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004510 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 --emsg_off;
4512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004513 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514 if (argvars[2].v_type == VAR_UNKNOWN)
4515 end = lnum;
4516 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004517 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518
4519 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4520}
4521
4522/*
4523 * "getbufvar()" function
4524 */
4525 static void
4526f_getbufvar(typval_T *argvars, typval_T *rettv)
4527{
4528 buf_T *buf;
4529 buf_T *save_curbuf;
4530 char_u *varname;
4531 dictitem_T *v;
4532 int done = FALSE;
4533
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004534 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4535 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004537 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538
4539 rettv->v_type = VAR_STRING;
4540 rettv->vval.v_string = NULL;
4541
4542 if (buf != NULL && varname != NULL)
4543 {
4544 /* set curbuf to be our buf, temporarily */
4545 save_curbuf = curbuf;
4546 curbuf = buf;
4547
Bram Moolenaar30567352016-08-27 21:25:44 +02004548 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004550 if (varname[1] == NUL)
4551 {
4552 /* get all buffer-local options in a dict */
4553 dict_T *opts = get_winbuf_options(TRUE);
4554
4555 if (opts != NULL)
4556 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004557 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004558 done = TRUE;
4559 }
4560 }
4561 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4562 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563 done = TRUE;
4564 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565 else
4566 {
4567 /* Look up the variable. */
4568 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4569 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4570 'b', varname, FALSE);
4571 if (v != NULL)
4572 {
4573 copy_tv(&v->di_tv, rettv);
4574 done = TRUE;
4575 }
4576 }
4577
4578 /* restore previous notion of curbuf */
4579 curbuf = save_curbuf;
4580 }
4581
4582 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4583 /* use the default value */
4584 copy_tv(&argvars[2], rettv);
4585
4586 --emsg_off;
4587}
4588
4589/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004590 * "getchangelist()" function
4591 */
4592 static void
4593f_getchangelist(typval_T *argvars, typval_T *rettv)
4594{
4595#ifdef FEAT_JUMPLIST
4596 buf_T *buf;
4597 int i;
4598 list_T *l;
4599 dict_T *d;
4600#endif
4601
4602 if (rettv_list_alloc(rettv) != OK)
4603 return;
4604
4605#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004606 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004607 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004608 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004609 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004610 if (buf == NULL)
4611 return;
4612
4613 l = list_alloc();
4614 if (l == NULL)
4615 return;
4616
4617 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4618 return;
4619 /*
4620 * The current window change list index tracks only the position in the
4621 * current buffer change list. For other buffers, use the change list
4622 * length as the current index.
4623 */
4624 list_append_number(rettv->vval.v_list,
4625 (varnumber_T)((buf == curwin->w_buffer)
4626 ? curwin->w_changelistidx : buf->b_changelistlen));
4627
4628 for (i = 0; i < buf->b_changelistlen; ++i)
4629 {
4630 if (buf->b_changelist[i].lnum == 0)
4631 continue;
4632 if ((d = dict_alloc()) == NULL)
4633 return;
4634 if (list_append_dict(l, d) == FAIL)
4635 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004636 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4637 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004638 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004639 }
4640#endif
4641}
4642/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004643 * "getchar()" function
4644 */
4645 static void
4646f_getchar(typval_T *argvars, typval_T *rettv)
4647{
4648 varnumber_T n;
4649 int error = FALSE;
4650
Bram Moolenaar84d93902018-09-11 20:10:20 +02004651#ifdef MESSAGE_QUEUE
4652 // vpeekc() used to check for messages, but that caused problems, invoking
4653 // a callback where it was not expected. Some plugins use getchar(1) in a
4654 // loop to await a message, therefore make sure we check for messages here.
4655 parse_queued_messages();
4656#endif
4657
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004658 /* Position the cursor. Needed after a message that ends in a space. */
4659 windgoto(msg_row, msg_col);
4660
4661 ++no_mapping;
4662 ++allow_keys;
4663 for (;;)
4664 {
4665 if (argvars[0].v_type == VAR_UNKNOWN)
4666 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004667 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004668 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 /* getchar(1): only check if char avail */
4670 n = vpeekc_any();
4671 else if (error || vpeekc_any() == NUL)
4672 /* illegal argument or getchar(0) and no char avail: return zero */
4673 n = 0;
4674 else
4675 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004676 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677
4678 if (n == K_IGNORE)
4679 continue;
4680 break;
4681 }
4682 --no_mapping;
4683 --allow_keys;
4684
4685 set_vim_var_nr(VV_MOUSE_WIN, 0);
4686 set_vim_var_nr(VV_MOUSE_WINID, 0);
4687 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4688 set_vim_var_nr(VV_MOUSE_COL, 0);
4689
4690 rettv->vval.v_number = n;
4691 if (IS_SPECIAL(n) || mod_mask != 0)
4692 {
4693 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4694 int i = 0;
4695
4696 /* Turn a special key into three bytes, plus modifier. */
4697 if (mod_mask != 0)
4698 {
4699 temp[i++] = K_SPECIAL;
4700 temp[i++] = KS_MODIFIER;
4701 temp[i++] = mod_mask;
4702 }
4703 if (IS_SPECIAL(n))
4704 {
4705 temp[i++] = K_SPECIAL;
4706 temp[i++] = K_SECOND(n);
4707 temp[i++] = K_THIRD(n);
4708 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004709 else if (has_mbyte)
4710 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711 else
4712 temp[i++] = n;
4713 temp[i++] = NUL;
4714 rettv->v_type = VAR_STRING;
4715 rettv->vval.v_string = vim_strsave(temp);
4716
4717#ifdef FEAT_MOUSE
4718 if (is_mouse_key(n))
4719 {
4720 int row = mouse_row;
4721 int col = mouse_col;
4722 win_T *win;
4723 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004725 int winnr = 1;
4726
4727 if (row >= 0 && col >= 0)
4728 {
4729 /* Find the window at the mouse coordinates and compute the
4730 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004731 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004732 if (win == NULL)
4733 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004734 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004735# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004736 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004737 winnr = 0;
4738 else
4739# endif
4740 for (wp = firstwin; wp != win && wp != NULL;
4741 wp = wp->w_next)
4742 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4744 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4745 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4746 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4747 }
4748 }
4749#endif
4750 }
4751}
4752
4753/*
4754 * "getcharmod()" function
4755 */
4756 static void
4757f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4758{
4759 rettv->vval.v_number = mod_mask;
4760}
4761
4762/*
4763 * "getcharsearch()" function
4764 */
4765 static void
4766f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4767{
4768 if (rettv_dict_alloc(rettv) != FAIL)
4769 {
4770 dict_T *dict = rettv->vval.v_dict;
4771
Bram Moolenaare0be1672018-07-08 16:50:37 +02004772 dict_add_string(dict, "char", last_csearch());
4773 dict_add_number(dict, "forward", last_csearch_forward());
4774 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 }
4776}
4777
4778/*
4779 * "getcmdline()" function
4780 */
4781 static void
4782f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4783{
4784 rettv->v_type = VAR_STRING;
4785 rettv->vval.v_string = get_cmdline_str();
4786}
4787
4788/*
4789 * "getcmdpos()" function
4790 */
4791 static void
4792f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4793{
4794 rettv->vval.v_number = get_cmdline_pos() + 1;
4795}
4796
4797/*
4798 * "getcmdtype()" function
4799 */
4800 static void
4801f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4802{
4803 rettv->v_type = VAR_STRING;
4804 rettv->vval.v_string = alloc(2);
4805 if (rettv->vval.v_string != NULL)
4806 {
4807 rettv->vval.v_string[0] = get_cmdline_type();
4808 rettv->vval.v_string[1] = NUL;
4809 }
4810}
4811
4812/*
4813 * "getcmdwintype()" function
4814 */
4815 static void
4816f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4817{
4818 rettv->v_type = VAR_STRING;
4819 rettv->vval.v_string = NULL;
4820#ifdef FEAT_CMDWIN
4821 rettv->vval.v_string = alloc(2);
4822 if (rettv->vval.v_string != NULL)
4823 {
4824 rettv->vval.v_string[0] = cmdwin_type;
4825 rettv->vval.v_string[1] = NUL;
4826 }
4827#endif
4828}
4829
4830#if defined(FEAT_CMDL_COMPL)
4831/*
4832 * "getcompletion()" function
4833 */
4834 static void
4835f_getcompletion(typval_T *argvars, typval_T *rettv)
4836{
4837 char_u *pat;
4838 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004839 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004840 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4841 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004845
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004846 if (p_wic)
4847 options |= WILD_ICASE;
4848
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004849 /* For filtered results, 'wildignore' is used */
4850 if (!filtered)
4851 options |= WILD_KEEP_ALL;
4852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004854 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004855 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004856 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004857 if (xpc.xp_context == EXPAND_NOTHING)
4858 {
4859 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004860 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 return;
4864 }
4865
4866# if defined(FEAT_MENU)
4867 if (xpc.xp_context == EXPAND_MENUS)
4868 {
4869 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4870 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4871 }
4872# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004873#ifdef FEAT_CSCOPE
4874 if (xpc.xp_context == EXPAND_CSCOPE)
4875 {
4876 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4877 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4878 }
4879#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004880#ifdef FEAT_SIGNS
4881 if (xpc.xp_context == EXPAND_SIGN)
4882 {
4883 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4884 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4885 }
4886#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887
4888 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4889 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4890 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004891 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892
4893 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4894
4895 for (i = 0; i < xpc.xp_numfiles; i++)
4896 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4897 }
4898 vim_free(pat);
4899 ExpandCleanup(&xpc);
4900}
4901#endif
4902
4903/*
4904 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004905 *
4906 * Return the current working directory of a window in a tab page.
4907 * First optional argument 'winnr' is the window number or -1 and the second
4908 * optional argument 'tabnr' is the tab page number.
4909 *
4910 * If no arguments are supplied, then return the directory of the current
4911 * window.
4912 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4913 * the specified window.
4914 * If 'winnr' is 0 then return the directory of the current window.
4915 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4916 * directory of the specified tab page. Otherwise return the directory of the
4917 * specified window in the specified tab page.
4918 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004919 */
4920 static void
4921f_getcwd(typval_T *argvars, typval_T *rettv)
4922{
4923 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004924 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004926 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927
4928 rettv->v_type = VAR_STRING;
4929 rettv->vval.v_string = NULL;
4930
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004931 if (argvars[0].v_type == VAR_NUMBER
4932 && argvars[0].vval.v_number == -1
4933 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004934 global = TRUE;
4935 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004936 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004937
4938 if (wp != NULL && wp->w_localdir != NULL)
4939 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004940 else if (tp != NULL && tp->tp_localdir != NULL)
4941 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4942 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004944 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 rettv->vval.v_string = vim_strsave(globaldir);
4946 else
4947 {
4948 cwd = alloc(MAXPATHL);
4949 if (cwd != NULL)
4950 {
4951 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4952 rettv->vval.v_string = vim_strsave(cwd);
4953 vim_free(cwd);
4954 }
4955 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004957#ifdef BACKSLASH_IN_FILENAME
4958 if (rettv->vval.v_string != NULL)
4959 slash_adjust(rettv->vval.v_string);
4960#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004961}
4962
4963/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004964 * "getenv()" function
4965 */
4966 static void
4967f_getenv(typval_T *argvars, typval_T *rettv)
4968{
4969 int mustfree = FALSE;
4970 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4971
4972 if (p == NULL)
4973 {
4974 rettv->v_type = VAR_SPECIAL;
4975 rettv->vval.v_number = VVAL_NULL;
4976 return;
4977 }
4978 if (!mustfree)
4979 p = vim_strsave(p);
4980 rettv->vval.v_string = p;
4981 rettv->v_type = VAR_STRING;
4982}
4983
4984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004985 * "getfontname()" function
4986 */
4987 static void
4988f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4989{
4990 rettv->v_type = VAR_STRING;
4991 rettv->vval.v_string = NULL;
4992#ifdef FEAT_GUI
4993 if (gui.in_use)
4994 {
4995 GuiFont font;
4996 char_u *name = NULL;
4997
4998 if (argvars[0].v_type == VAR_UNKNOWN)
4999 {
5000 /* Get the "Normal" font. Either the name saved by
5001 * hl_set_font_name() or from the font ID. */
5002 font = gui.norm_font;
5003 name = hl_get_font_name();
5004 }
5005 else
5006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005007 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5009 return;
5010 font = gui_mch_get_font(name, FALSE);
5011 if (font == NOFONT)
5012 return; /* Invalid font name, return empty string. */
5013 }
5014 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5015 if (argvars[0].v_type != VAR_UNKNOWN)
5016 gui_mch_free_font(font);
5017 }
5018#endif
5019}
5020
5021/*
5022 * "getfperm({fname})" function
5023 */
5024 static void
5025f_getfperm(typval_T *argvars, typval_T *rettv)
5026{
5027 char_u *fname;
5028 stat_T st;
5029 char_u *perm = NULL;
5030 char_u flags[] = "rwx";
5031 int i;
5032
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005033 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034
5035 rettv->v_type = VAR_STRING;
5036 if (mch_stat((char *)fname, &st) >= 0)
5037 {
5038 perm = vim_strsave((char_u *)"---------");
5039 if (perm != NULL)
5040 {
5041 for (i = 0; i < 9; i++)
5042 {
5043 if (st.st_mode & (1 << (8 - i)))
5044 perm[i] = flags[i % 3];
5045 }
5046 }
5047 }
5048 rettv->vval.v_string = perm;
5049}
5050
5051/*
5052 * "getfsize({fname})" function
5053 */
5054 static void
5055f_getfsize(typval_T *argvars, typval_T *rettv)
5056{
5057 char_u *fname;
5058 stat_T st;
5059
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005060 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005061
5062 rettv->v_type = VAR_NUMBER;
5063
5064 if (mch_stat((char *)fname, &st) >= 0)
5065 {
5066 if (mch_isdir(fname))
5067 rettv->vval.v_number = 0;
5068 else
5069 {
5070 rettv->vval.v_number = (varnumber_T)st.st_size;
5071
5072 /* non-perfect check for overflow */
5073 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5074 rettv->vval.v_number = -2;
5075 }
5076 }
5077 else
5078 rettv->vval.v_number = -1;
5079}
5080
5081/*
5082 * "getftime({fname})" function
5083 */
5084 static void
5085f_getftime(typval_T *argvars, typval_T *rettv)
5086{
5087 char_u *fname;
5088 stat_T st;
5089
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005090 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005091
5092 if (mch_stat((char *)fname, &st) >= 0)
5093 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5094 else
5095 rettv->vval.v_number = -1;
5096}
5097
5098/*
5099 * "getftype({fname})" function
5100 */
5101 static void
5102f_getftype(typval_T *argvars, typval_T *rettv)
5103{
5104 char_u *fname;
5105 stat_T st;
5106 char_u *type = NULL;
5107 char *t;
5108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110
5111 rettv->v_type = VAR_STRING;
5112 if (mch_lstat((char *)fname, &st) >= 0)
5113 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 if (S_ISREG(st.st_mode))
5115 t = "file";
5116 else if (S_ISDIR(st.st_mode))
5117 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else if (S_ISLNK(st.st_mode))
5119 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 else if (S_ISBLK(st.st_mode))
5121 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 else if (S_ISCHR(st.st_mode))
5123 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 else if (S_ISFIFO(st.st_mode))
5125 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005127 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 else
5129 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130 type = vim_strsave((char_u *)t);
5131 }
5132 rettv->vval.v_string = type;
5133}
5134
5135/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005136 * "getjumplist()" function
5137 */
5138 static void
5139f_getjumplist(typval_T *argvars, typval_T *rettv)
5140{
5141#ifdef FEAT_JUMPLIST
5142 win_T *wp;
5143 int i;
5144 list_T *l;
5145 dict_T *d;
5146#endif
5147
5148 if (rettv_list_alloc(rettv) != OK)
5149 return;
5150
5151#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005152 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005153 if (wp == NULL)
5154 return;
5155
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005156 cleanup_jumplist(wp, TRUE);
5157
Bram Moolenaar4f505882018-02-10 21:06:32 +01005158 l = list_alloc();
5159 if (l == NULL)
5160 return;
5161
5162 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5163 return;
5164 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5165
5166 for (i = 0; i < wp->w_jumplistlen; ++i)
5167 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005168 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5169 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005170 if ((d = dict_alloc()) == NULL)
5171 return;
5172 if (list_append_dict(l, d) == FAIL)
5173 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005174 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5175 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005176 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005177 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005178 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005179 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005180 }
5181#endif
5182}
5183
5184/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005185 * "getline(lnum, [end])" function
5186 */
5187 static void
5188f_getline(typval_T *argvars, typval_T *rettv)
5189{
5190 linenr_T lnum;
5191 linenr_T end;
5192 int retlist;
5193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005194 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005195 if (argvars[1].v_type == VAR_UNKNOWN)
5196 {
5197 end = 0;
5198 retlist = FALSE;
5199 }
5200 else
5201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005202 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005203 retlist = TRUE;
5204 }
5205
5206 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5207}
5208
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005209#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005210 static void
5211get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5212{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005213 if (what_arg->v_type == VAR_UNKNOWN)
5214 {
5215 if (rettv_list_alloc(rettv) == OK)
5216 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005217 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005218 }
5219 else
5220 {
5221 if (rettv_dict_alloc(rettv) == OK)
5222 if (is_qf || (wp != NULL))
5223 {
5224 if (what_arg->v_type == VAR_DICT)
5225 {
5226 dict_T *d = what_arg->vval.v_dict;
5227
5228 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005229 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005230 }
5231 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005232 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005233 }
5234 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005236#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237
5238/*
5239 * "getloclist()" function
5240 */
5241 static void
5242f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5243{
5244#ifdef FEAT_QUICKFIX
5245 win_T *wp;
5246
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005247 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005248 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5249#endif
5250}
5251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005253 * "getpid()" function
5254 */
5255 static void
5256f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5257{
5258 rettv->vval.v_number = mch_get_pid();
5259}
5260
5261 static void
5262getpos_both(
5263 typval_T *argvars,
5264 typval_T *rettv,
5265 int getcurpos)
5266{
5267 pos_T *fp;
5268 list_T *l;
5269 int fnum = -1;
5270
5271 if (rettv_list_alloc(rettv) == OK)
5272 {
5273 l = rettv->vval.v_list;
5274 if (getcurpos)
5275 fp = &curwin->w_cursor;
5276 else
5277 fp = var2fpos(&argvars[0], TRUE, &fnum);
5278 if (fnum != -1)
5279 list_append_number(l, (varnumber_T)fnum);
5280 else
5281 list_append_number(l, (varnumber_T)0);
5282 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5283 : (varnumber_T)0);
5284 list_append_number(l, (fp != NULL)
5285 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5286 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005287 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 (varnumber_T)0);
5289 if (getcurpos)
5290 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005291 int save_set_curswant = curwin->w_set_curswant;
5292 colnr_T save_curswant = curwin->w_curswant;
5293 colnr_T save_virtcol = curwin->w_virtcol;
5294
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 update_curswant();
5296 list_append_number(l, curwin->w_curswant == MAXCOL ?
5297 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005298
5299 // Do not change "curswant", as it is unexpected that a get
5300 // function has a side effect.
5301 if (save_set_curswant)
5302 {
5303 curwin->w_set_curswant = save_set_curswant;
5304 curwin->w_curswant = save_curswant;
5305 curwin->w_virtcol = save_virtcol;
5306 curwin->w_valid &= ~VALID_VIRTCOL;
5307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 }
5309 }
5310 else
5311 rettv->vval.v_number = FALSE;
5312}
5313
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314/*
5315 * "getcurpos()" function
5316 */
5317 static void
5318f_getcurpos(typval_T *argvars, typval_T *rettv)
5319{
5320 getpos_both(argvars, rettv, TRUE);
5321}
5322
5323/*
5324 * "getpos(string)" function
5325 */
5326 static void
5327f_getpos(typval_T *argvars, typval_T *rettv)
5328{
5329 getpos_both(argvars, rettv, FALSE);
5330}
5331
5332/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005333 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334 */
5335 static void
5336f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5337{
5338#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005339 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005340#endif
5341}
5342
5343/*
5344 * "getreg()" function
5345 */
5346 static void
5347f_getreg(typval_T *argvars, typval_T *rettv)
5348{
5349 char_u *strregname;
5350 int regname;
5351 int arg2 = FALSE;
5352 int return_list = FALSE;
5353 int error = FALSE;
5354
5355 if (argvars[0].v_type != VAR_UNKNOWN)
5356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005357 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 error = strregname == NULL;
5359 if (argvars[1].v_type != VAR_UNKNOWN)
5360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005361 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005363 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 }
5365 }
5366 else
5367 strregname = get_vim_var_str(VV_REG);
5368
5369 if (error)
5370 return;
5371
5372 regname = (strregname == NULL ? '"' : *strregname);
5373 if (regname == 0)
5374 regname = '"';
5375
5376 if (return_list)
5377 {
5378 rettv->v_type = VAR_LIST;
5379 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5380 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5381 if (rettv->vval.v_list == NULL)
5382 (void)rettv_list_alloc(rettv);
5383 else
5384 ++rettv->vval.v_list->lv_refcount;
5385 }
5386 else
5387 {
5388 rettv->v_type = VAR_STRING;
5389 rettv->vval.v_string = get_reg_contents(regname,
5390 arg2 ? GREG_EXPR_SRC : 0);
5391 }
5392}
5393
5394/*
5395 * "getregtype()" function
5396 */
5397 static void
5398f_getregtype(typval_T *argvars, typval_T *rettv)
5399{
5400 char_u *strregname;
5401 int regname;
5402 char_u buf[NUMBUFLEN + 2];
5403 long reglen = 0;
5404
5405 if (argvars[0].v_type != VAR_UNKNOWN)
5406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005407 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005408 if (strregname == NULL) /* type error; errmsg already given */
5409 {
5410 rettv->v_type = VAR_STRING;
5411 rettv->vval.v_string = NULL;
5412 return;
5413 }
5414 }
5415 else
5416 /* Default to v:register */
5417 strregname = get_vim_var_str(VV_REG);
5418
5419 regname = (strregname == NULL ? '"' : *strregname);
5420 if (regname == 0)
5421 regname = '"';
5422
5423 buf[0] = NUL;
5424 buf[1] = NUL;
5425 switch (get_reg_type(regname, &reglen))
5426 {
5427 case MLINE: buf[0] = 'V'; break;
5428 case MCHAR: buf[0] = 'v'; break;
5429 case MBLOCK:
5430 buf[0] = Ctrl_V;
5431 sprintf((char *)buf + 1, "%ld", reglen + 1);
5432 break;
5433 }
5434 rettv->v_type = VAR_STRING;
5435 rettv->vval.v_string = vim_strsave(buf);
5436}
5437
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005438/*
5439 * Returns information (variables, options, etc.) about a tab page
5440 * as a dictionary.
5441 */
5442 static dict_T *
5443get_tabpage_info(tabpage_T *tp, int tp_idx)
5444{
5445 win_T *wp;
5446 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005447 list_T *l;
5448
5449 dict = dict_alloc();
5450 if (dict == NULL)
5451 return NULL;
5452
Bram Moolenaare0be1672018-07-08 16:50:37 +02005453 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454
5455 l = list_alloc();
5456 if (l != NULL)
5457 {
5458 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005459 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005460 list_append_number(l, (varnumber_T)wp->w_id);
5461 dict_add_list(dict, "windows", l);
5462 }
5463
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005464 /* Make a reference to tabpage variables */
5465 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005466
5467 return dict;
5468}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005469
5470/*
5471 * "gettabinfo()" function
5472 */
5473 static void
5474f_gettabinfo(typval_T *argvars, typval_T *rettv)
5475{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005476 tabpage_T *tp, *tparg = NULL;
5477 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005478 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005479
5480 if (rettv_list_alloc(rettv) != OK)
5481 return;
5482
5483 if (argvars[0].v_type != VAR_UNKNOWN)
5484 {
5485 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005486 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005487 if (tparg == NULL)
5488 return;
5489 }
5490
5491 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005492 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005493 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005494 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005495 if (tparg != NULL && tp != tparg)
5496 continue;
5497 d = get_tabpage_info(tp, tpnr);
5498 if (d != NULL)
5499 list_append_dict(rettv->vval.v_list, d);
5500 if (tparg != NULL)
5501 return;
5502 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005503}
5504
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005505/*
5506 * "gettabvar()" function
5507 */
5508 static void
5509f_gettabvar(typval_T *argvars, typval_T *rettv)
5510{
5511 win_T *oldcurwin;
5512 tabpage_T *tp, *oldtabpage;
5513 dictitem_T *v;
5514 char_u *varname;
5515 int done = FALSE;
5516
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = NULL;
5519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005520 varname = tv_get_string_chk(&argvars[1]);
5521 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522 if (tp != NULL && varname != NULL)
5523 {
5524 /* Set tp to be our tabpage, temporarily. Also set the window to the
5525 * first window in the tabpage, otherwise the window is not valid. */
5526 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005527 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5528 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 {
5530 /* look up the variable */
5531 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5532 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5533 if (v != NULL)
5534 {
5535 copy_tv(&v->di_tv, rettv);
5536 done = TRUE;
5537 }
5538 }
5539
5540 /* restore previous notion of curwin */
5541 restore_win(oldcurwin, oldtabpage, TRUE);
5542 }
5543
5544 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5545 copy_tv(&argvars[2], rettv);
5546}
5547
5548/*
5549 * "gettabwinvar()" function
5550 */
5551 static void
5552f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5553{
5554 getwinvar(argvars, rettv, 1);
5555}
5556
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005558 * "gettagstack()" function
5559 */
5560 static void
5561f_gettagstack(typval_T *argvars, typval_T *rettv)
5562{
5563 win_T *wp = curwin; // default is current window
5564
5565 if (rettv_dict_alloc(rettv) != OK)
5566 return;
5567
5568 if (argvars[0].v_type != VAR_UNKNOWN)
5569 {
5570 wp = find_win_by_nr_or_id(&argvars[0]);
5571 if (wp == NULL)
5572 return;
5573 }
5574
5575 get_tagstack(wp, rettv->vval.v_dict);
5576}
5577
5578/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005579 * Returns information about a window as a dictionary.
5580 */
5581 static dict_T *
5582get_win_info(win_T *wp, short tpnr, short winnr)
5583{
5584 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005585
5586 dict = dict_alloc();
5587 if (dict == NULL)
5588 return NULL;
5589
Bram Moolenaare0be1672018-07-08 16:50:37 +02005590 dict_add_number(dict, "tabnr", tpnr);
5591 dict_add_number(dict, "winnr", winnr);
5592 dict_add_number(dict, "winid", wp->w_id);
5593 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005594 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005595 dict_add_number(dict, "topline", wp->w_topline);
5596 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005597#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005598 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005599#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005600 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005601 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005602 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005603
Bram Moolenaar69905d12017-08-13 18:14:47 +02005604#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005605 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005606#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005607#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005608 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5609 dict_add_number(dict, "loclist",
5610 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005611#endif
5612
Bram Moolenaar30567352016-08-27 21:25:44 +02005613 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005614 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005615
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005616 return dict;
5617}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005618
5619/*
5620 * "getwininfo()" function
5621 */
5622 static void
5623f_getwininfo(typval_T *argvars, typval_T *rettv)
5624{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005625 tabpage_T *tp;
5626 win_T *wp = NULL, *wparg = NULL;
5627 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005628 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005629
5630 if (rettv_list_alloc(rettv) != OK)
5631 return;
5632
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005633 if (argvars[0].v_type != VAR_UNKNOWN)
5634 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005635 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636 if (wparg == NULL)
5637 return;
5638 }
5639
5640 /* Collect information about either all the windows across all the tab
5641 * pages or one particular window.
5642 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005644 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005645 tabnr++;
5646 winnr = 0;
5647 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005648 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005649 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005650 if (wparg != NULL && wp != wparg)
5651 continue;
5652 d = get_win_info(wp, tabnr, winnr);
5653 if (d != NULL)
5654 list_append_dict(rettv->vval.v_list, d);
5655 if (wparg != NULL)
5656 /* found information about a specific window */
5657 return;
5658 }
5659 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660}
5661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005663 * "win_execute()" function
5664 */
5665 static void
5666f_win_execute(typval_T *argvars, typval_T *rettv)
5667{
5668 int id = (int)tv_get_number(argvars);
5669 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005670 win_T *save_curwin;
5671 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005672
5673 if (wp != NULL)
5674 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005675 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5676 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005677 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005678 check_cursor();
5679 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005680 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005681 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005682 }
5683}
5684
5685/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005686 * "win_findbuf()" function
5687 */
5688 static void
5689f_win_findbuf(typval_T *argvars, typval_T *rettv)
5690{
5691 if (rettv_list_alloc(rettv) != FAIL)
5692 win_findbuf(argvars, rettv->vval.v_list);
5693}
5694
5695/*
5696 * "win_getid()" function
5697 */
5698 static void
5699f_win_getid(typval_T *argvars, typval_T *rettv)
5700{
5701 rettv->vval.v_number = win_getid(argvars);
5702}
5703
5704/*
5705 * "win_gotoid()" function
5706 */
5707 static void
5708f_win_gotoid(typval_T *argvars, typval_T *rettv)
5709{
5710 rettv->vval.v_number = win_gotoid(argvars);
5711}
5712
5713/*
5714 * "win_id2tabwin()" function
5715 */
5716 static void
5717f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5718{
5719 if (rettv_list_alloc(rettv) != FAIL)
5720 win_id2tabwin(argvars, rettv->vval.v_list);
5721}
5722
5723/*
5724 * "win_id2win()" function
5725 */
5726 static void
5727f_win_id2win(typval_T *argvars, typval_T *rettv)
5728{
5729 rettv->vval.v_number = win_id2win(argvars);
5730}
5731
5732/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005733 * "win_screenpos()" function
5734 */
5735 static void
5736f_win_screenpos(typval_T *argvars, typval_T *rettv)
5737{
5738 win_T *wp;
5739
5740 if (rettv_list_alloc(rettv) == FAIL)
5741 return;
5742
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005743 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005744 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5745 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5746}
5747
5748/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005749 * "getwinpos({timeout})" function
5750 */
5751 static void
5752f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5753{
5754 int x = -1;
5755 int y = -1;
5756
5757 if (rettv_list_alloc(rettv) == FAIL)
5758 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005759#if defined(FEAT_GUI) \
5760 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5761 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005762 {
5763 varnumber_T timeout = 100;
5764
5765 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005766 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005767
5768 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005769 }
5770#endif
5771 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5772 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5773}
5774
5775
5776/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005777 * "getwinposx()" function
5778 */
5779 static void
5780f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5781{
5782 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005783#if defined(FEAT_GUI) \
5784 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5785 || defined(MSWIN)
5786
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005787 {
5788 int x, y;
5789
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005790 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005791 rettv->vval.v_number = x;
5792 }
5793#endif
5794}
5795
5796/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005797 * "getwinposy()" function
5798 */
5799 static void
5800f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5801{
5802 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005803#if defined(FEAT_GUI) \
5804 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5805 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 {
5807 int x, y;
5808
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005809 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005810 rettv->vval.v_number = y;
5811 }
5812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813}
5814
5815/*
5816 * "getwinvar()" function
5817 */
5818 static void
5819f_getwinvar(typval_T *argvars, typval_T *rettv)
5820{
5821 getwinvar(argvars, rettv, 0);
5822}
5823
5824/*
5825 * "glob()" function
5826 */
5827 static void
5828f_glob(typval_T *argvars, typval_T *rettv)
5829{
5830 int options = WILD_SILENT|WILD_USE_NL;
5831 expand_T xpc;
5832 int error = FALSE;
5833
5834 /* When the optional second argument is non-zero, don't remove matches
5835 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5836 rettv->v_type = VAR_STRING;
5837 if (argvars[1].v_type != VAR_UNKNOWN)
5838 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005839 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840 options |= WILD_KEEP_ALL;
5841 if (argvars[2].v_type != VAR_UNKNOWN)
5842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005844 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005846 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 options |= WILD_ALLLINKS;
5848 }
5849 }
5850 if (!error)
5851 {
5852 ExpandInit(&xpc);
5853 xpc.xp_context = EXPAND_FILES;
5854 if (p_wic)
5855 options += WILD_ICASE;
5856 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 NULL, options, WILD_ALL);
5859 else if (rettv_list_alloc(rettv) != FAIL)
5860 {
5861 int i;
5862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 NULL, options, WILD_ALL_KEEP);
5865 for (i = 0; i < xpc.xp_numfiles; i++)
5866 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5867
5868 ExpandCleanup(&xpc);
5869 }
5870 }
5871 else
5872 rettv->vval.v_string = NULL;
5873}
5874
5875/*
5876 * "globpath()" function
5877 */
5878 static void
5879f_globpath(typval_T *argvars, typval_T *rettv)
5880{
5881 int flags = 0;
5882 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005883 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 int error = FALSE;
5885 garray_T ga;
5886 int i;
5887
5888 /* When the optional second argument is non-zero, don't remove matches
5889 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5890 rettv->v_type = VAR_STRING;
5891 if (argvars[2].v_type != VAR_UNKNOWN)
5892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005893 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 flags |= WILD_KEEP_ALL;
5895 if (argvars[3].v_type != VAR_UNKNOWN)
5896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005897 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005898 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005900 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901 flags |= WILD_ALLLINKS;
5902 }
5903 }
5904 if (file != NULL && !error)
5905 {
5906 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 if (rettv->v_type == VAR_STRING)
5909 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5910 else if (rettv_list_alloc(rettv) != FAIL)
5911 for (i = 0; i < ga.ga_len; ++i)
5912 list_append_string(rettv->vval.v_list,
5913 ((char_u **)(ga.ga_data))[i], -1);
5914 ga_clear_strings(&ga);
5915 }
5916 else
5917 rettv->vval.v_string = NULL;
5918}
5919
5920/*
5921 * "glob2regpat()" function
5922 */
5923 static void
5924f_glob2regpat(typval_T *argvars, typval_T *rettv)
5925{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005926 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005927
5928 rettv->v_type = VAR_STRING;
5929 rettv->vval.v_string = (pat == NULL)
5930 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5931}
5932
5933/* for VIM_VERSION_ defines */
5934#include "version.h"
5935
5936/*
5937 * "has()" function
5938 */
5939 static void
5940f_has(typval_T *argvars, typval_T *rettv)
5941{
5942 int i;
5943 char_u *name;
5944 int n = FALSE;
5945 static char *(has_list[]) =
5946 {
5947#ifdef AMIGA
5948 "amiga",
5949# ifdef FEAT_ARP
5950 "arp",
5951# endif
5952#endif
5953#ifdef __BEOS__
5954 "beos",
5955#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005956#if defined(BSD) && !defined(MACOS_X)
5957 "bsd",
5958#endif
5959#ifdef hpux
5960 "hpux",
5961#endif
5962#ifdef __linux__
5963 "linux",
5964#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005965#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005966 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5967 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005968# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005969 "macunix", /* Mac OS X, with the darwin feature */
5970 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005971# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972#endif
5973#ifdef __QNX__
5974 "qnx",
5975#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005976#ifdef SUN_SYSTEM
5977 "sun",
5978#else
5979 "moon",
5980#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981#ifdef UNIX
5982 "unix",
5983#endif
5984#ifdef VMS
5985 "vms",
5986#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005987#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 "win32",
5989#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005990#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 "win32unix",
5992#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005993#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 "win64",
5995#endif
5996#ifdef EBCDIC
5997 "ebcdic",
5998#endif
5999#ifndef CASE_INSENSITIVE_FILENAME
6000 "fname_case",
6001#endif
6002#ifdef HAVE_ACL
6003 "acl",
6004#endif
6005#ifdef FEAT_ARABIC
6006 "arabic",
6007#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006009#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006010 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006011#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006012#ifdef FEAT_AUTOSERVERNAME
6013 "autoservername",
6014#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006015#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006017# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 "balloon_multiline",
6019# endif
6020#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006021#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006022 "balloon_eval_term",
6023#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6025 "builtin_terms",
6026# ifdef ALL_BUILTIN_TCAPS
6027 "all_builtin_terms",
6028# endif
6029#endif
6030#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006031 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 || defined(FEAT_GUI_MOTIF))
6033 "browsefilter",
6034#endif
6035#ifdef FEAT_BYTEOFF
6036 "byte_offset",
6037#endif
6038#ifdef FEAT_JOB_CHANNEL
6039 "channel",
6040#endif
6041#ifdef FEAT_CINDENT
6042 "cindent",
6043#endif
6044#ifdef FEAT_CLIENTSERVER
6045 "clientserver",
6046#endif
6047#ifdef FEAT_CLIPBOARD
6048 "clipboard",
6049#endif
6050#ifdef FEAT_CMDL_COMPL
6051 "cmdline_compl",
6052#endif
6053#ifdef FEAT_CMDHIST
6054 "cmdline_hist",
6055#endif
6056#ifdef FEAT_COMMENTS
6057 "comments",
6058#endif
6059#ifdef FEAT_CONCEAL
6060 "conceal",
6061#endif
6062#ifdef FEAT_CRYPT
6063 "cryptv",
6064 "crypt-blowfish",
6065 "crypt-blowfish2",
6066#endif
6067#ifdef FEAT_CSCOPE
6068 "cscope",
6069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006071#ifdef CURSOR_SHAPE
6072 "cursorshape",
6073#endif
6074#ifdef DEBUG
6075 "debug",
6076#endif
6077#ifdef FEAT_CON_DIALOG
6078 "dialog_con",
6079#endif
6080#ifdef FEAT_GUI_DIALOG
6081 "dialog_gui",
6082#endif
6083#ifdef FEAT_DIFF
6084 "diff",
6085#endif
6086#ifdef FEAT_DIGRAPHS
6087 "digraphs",
6088#endif
6089#ifdef FEAT_DIRECTX
6090 "directx",
6091#endif
6092#ifdef FEAT_DND
6093 "dnd",
6094#endif
6095#ifdef FEAT_EMACS_TAGS
6096 "emacs_tags",
6097#endif
6098 "eval", /* always present, of course! */
6099 "ex_extra", /* graduated feature */
6100#ifdef FEAT_SEARCH_EXTRA
6101 "extra_search",
6102#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103#ifdef FEAT_SEARCHPATH
6104 "file_in_path",
6105#endif
6106#ifdef FEAT_FILTERPIPE
6107 "filterpipe",
6108#endif
6109#ifdef FEAT_FIND_ID
6110 "find_in_path",
6111#endif
6112#ifdef FEAT_FLOAT
6113 "float",
6114#endif
6115#ifdef FEAT_FOLDING
6116 "folding",
6117#endif
6118#ifdef FEAT_FOOTER
6119 "footer",
6120#endif
6121#if !defined(USE_SYSTEM) && defined(UNIX)
6122 "fork",
6123#endif
6124#ifdef FEAT_GETTEXT
6125 "gettext",
6126#endif
6127#ifdef FEAT_GUI
6128 "gui",
6129#endif
6130#ifdef FEAT_GUI_ATHENA
6131# ifdef FEAT_GUI_NEXTAW
6132 "gui_neXtaw",
6133# else
6134 "gui_athena",
6135# endif
6136#endif
6137#ifdef FEAT_GUI_GTK
6138 "gui_gtk",
6139# ifdef USE_GTK3
6140 "gui_gtk3",
6141# else
6142 "gui_gtk2",
6143# endif
6144#endif
6145#ifdef FEAT_GUI_GNOME
6146 "gui_gnome",
6147#endif
6148#ifdef FEAT_GUI_MAC
6149 "gui_mac",
6150#endif
6151#ifdef FEAT_GUI_MOTIF
6152 "gui_motif",
6153#endif
6154#ifdef FEAT_GUI_PHOTON
6155 "gui_photon",
6156#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006157#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158 "gui_win32",
6159#endif
6160#ifdef FEAT_HANGULIN
6161 "hangul_input",
6162#endif
6163#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6164 "iconv",
6165#endif
6166#ifdef FEAT_INS_EXPAND
6167 "insert_expand",
6168#endif
6169#ifdef FEAT_JOB_CHANNEL
6170 "job",
6171#endif
6172#ifdef FEAT_JUMPLIST
6173 "jumplist",
6174#endif
6175#ifdef FEAT_KEYMAP
6176 "keymap",
6177#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006178 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006179#ifdef FEAT_LANGMAP
6180 "langmap",
6181#endif
6182#ifdef FEAT_LIBCALL
6183 "libcall",
6184#endif
6185#ifdef FEAT_LINEBREAK
6186 "linebreak",
6187#endif
6188#ifdef FEAT_LISP
6189 "lispindent",
6190#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192#ifdef FEAT_LOCALMAP
6193 "localmap",
6194#endif
6195#ifdef FEAT_LUA
6196# ifndef DYNAMIC_LUA
6197 "lua",
6198# endif
6199#endif
6200#ifdef FEAT_MENU
6201 "menu",
6202#endif
6203#ifdef FEAT_SESSION
6204 "mksession",
6205#endif
6206#ifdef FEAT_MODIFY_FNAME
6207 "modify_fname",
6208#endif
6209#ifdef FEAT_MOUSE
6210 "mouse",
6211#endif
6212#ifdef FEAT_MOUSESHAPE
6213 "mouseshape",
6214#endif
6215#if defined(UNIX) || defined(VMS)
6216# ifdef FEAT_MOUSE_DEC
6217 "mouse_dec",
6218# endif
6219# ifdef FEAT_MOUSE_GPM
6220 "mouse_gpm",
6221# endif
6222# ifdef FEAT_MOUSE_JSB
6223 "mouse_jsbterm",
6224# endif
6225# ifdef FEAT_MOUSE_NET
6226 "mouse_netterm",
6227# endif
6228# ifdef FEAT_MOUSE_PTERM
6229 "mouse_pterm",
6230# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006231# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 "mouse_sgr",
6233# endif
6234# ifdef FEAT_SYSMOUSE
6235 "mouse_sysmouse",
6236# endif
6237# ifdef FEAT_MOUSE_URXVT
6238 "mouse_urxvt",
6239# endif
6240# ifdef FEAT_MOUSE_XTERM
6241 "mouse_xterm",
6242# endif
6243#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245#ifdef FEAT_MBYTE_IME
6246 "multi_byte_ime",
6247#endif
6248#ifdef FEAT_MULTI_LANG
6249 "multi_lang",
6250#endif
6251#ifdef FEAT_MZSCHEME
6252#ifndef DYNAMIC_MZSCHEME
6253 "mzscheme",
6254#endif
6255#endif
6256#ifdef FEAT_NUM64
6257 "num64",
6258#endif
6259#ifdef FEAT_OLE
6260 "ole",
6261#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006262#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006264#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265#ifdef FEAT_PATH_EXTRA
6266 "path_extra",
6267#endif
6268#ifdef FEAT_PERL
6269#ifndef DYNAMIC_PERL
6270 "perl",
6271#endif
6272#endif
6273#ifdef FEAT_PERSISTENT_UNDO
6274 "persistent_undo",
6275#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006276#if defined(FEAT_PYTHON)
6277 "python_compiled",
6278# if defined(DYNAMIC_PYTHON)
6279 "python_dynamic",
6280# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006282 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006283# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006285#if defined(FEAT_PYTHON3)
6286 "python3_compiled",
6287# if defined(DYNAMIC_PYTHON3)
6288 "python3_dynamic",
6289# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006291 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006292# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293#endif
6294#ifdef FEAT_POSTSCRIPT
6295 "postscript",
6296#endif
6297#ifdef FEAT_PRINTER
6298 "printer",
6299#endif
6300#ifdef FEAT_PROFILE
6301 "profile",
6302#endif
6303#ifdef FEAT_RELTIME
6304 "reltime",
6305#endif
6306#ifdef FEAT_QUICKFIX
6307 "quickfix",
6308#endif
6309#ifdef FEAT_RIGHTLEFT
6310 "rightleft",
6311#endif
6312#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6313 "ruby",
6314#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006316#ifdef FEAT_CMDL_INFO
6317 "showcmd",
6318 "cmdline_info",
6319#endif
6320#ifdef FEAT_SIGNS
6321 "signs",
6322#endif
6323#ifdef FEAT_SMARTINDENT
6324 "smartindent",
6325#endif
6326#ifdef STARTUPTIME
6327 "startuptime",
6328#endif
6329#ifdef FEAT_STL_OPT
6330 "statusline",
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_NETBEANS_INTG
6333 "netbeans_intg",
6334#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006335#ifdef FEAT_SOUND
6336 "sound",
6337#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338#ifdef FEAT_SPELL
6339 "spell",
6340#endif
6341#ifdef FEAT_SYN_HL
6342 "syntax",
6343#endif
6344#if defined(USE_SYSTEM) || !defined(UNIX)
6345 "system",
6346#endif
6347#ifdef FEAT_TAG_BINS
6348 "tag_binary",
6349#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006350#ifdef FEAT_TCL
6351# ifndef DYNAMIC_TCL
6352 "tcl",
6353# endif
6354#endif
6355#ifdef FEAT_TERMGUICOLORS
6356 "termguicolors",
6357#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006358#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006359 "terminal",
6360#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361#ifdef TERMINFO
6362 "terminfo",
6363#endif
6364#ifdef FEAT_TERMRESPONSE
6365 "termresponse",
6366#endif
6367#ifdef FEAT_TEXTOBJ
6368 "textobjects",
6369#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006370#ifdef FEAT_TEXT_PROP
6371 "textprop",
6372#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373#ifdef HAVE_TGETENT
6374 "tgetent",
6375#endif
6376#ifdef FEAT_TIMERS
6377 "timers",
6378#endif
6379#ifdef FEAT_TITLE
6380 "title",
6381#endif
6382#ifdef FEAT_TOOLBAR
6383 "toolbar",
6384#endif
6385#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6386 "unnamedplus",
6387#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388 "user-commands", /* was accidentally included in 5.4 */
6389 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006390#ifdef FEAT_VARTABS
6391 "vartabs",
6392#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006393 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394#ifdef FEAT_VIMINFO
6395 "viminfo",
6396#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006397 "vimscript-1",
6398 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006399 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006404#ifdef FEAT_VTP
6405 "vtp",
6406#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407#ifdef FEAT_WILDIGN
6408 "wildignore",
6409#endif
6410#ifdef FEAT_WILDMENU
6411 "wildmenu",
6412#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414#ifdef FEAT_WAK
6415 "winaltkeys",
6416#endif
6417#ifdef FEAT_WRITEBACKUP
6418 "writebackup",
6419#endif
6420#ifdef FEAT_XIM
6421 "xim",
6422#endif
6423#ifdef FEAT_XFONTSET
6424 "xfontset",
6425#endif
6426#ifdef FEAT_XPM_W32
6427 "xpm",
6428 "xpm_w32", /* for backward compatibility */
6429#else
6430# if defined(HAVE_XPM)
6431 "xpm",
6432# endif
6433#endif
6434#ifdef USE_XSMP
6435 "xsmp",
6436#endif
6437#ifdef USE_XSMP_INTERACT
6438 "xsmp_interact",
6439#endif
6440#ifdef FEAT_XCLIPBOARD
6441 "xterm_clipboard",
6442#endif
6443#ifdef FEAT_XTERM_SAVE
6444 "xterm_save",
6445#endif
6446#if defined(UNIX) && defined(FEAT_X11)
6447 "X11",
6448#endif
6449 NULL
6450 };
6451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006452 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006453 for (i = 0; has_list[i] != NULL; ++i)
6454 if (STRICMP(name, has_list[i]) == 0)
6455 {
6456 n = TRUE;
6457 break;
6458 }
6459
6460 if (n == FALSE)
6461 {
6462 if (STRNICMP(name, "patch", 5) == 0)
6463 {
6464 if (name[5] == '-'
6465 && STRLEN(name) >= 11
6466 && vim_isdigit(name[6])
6467 && vim_isdigit(name[8])
6468 && vim_isdigit(name[10]))
6469 {
6470 int major = atoi((char *)name + 6);
6471 int minor = atoi((char *)name + 8);
6472
6473 /* Expect "patch-9.9.01234". */
6474 n = (major < VIM_VERSION_MAJOR
6475 || (major == VIM_VERSION_MAJOR
6476 && (minor < VIM_VERSION_MINOR
6477 || (minor == VIM_VERSION_MINOR
6478 && has_patch(atoi((char *)name + 10))))));
6479 }
6480 else
6481 n = has_patch(atoi((char *)name + 5));
6482 }
6483 else if (STRICMP(name, "vim_starting") == 0)
6484 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006485 else if (STRICMP(name, "ttyin") == 0)
6486 n = mch_input_isatty();
6487 else if (STRICMP(name, "ttyout") == 0)
6488 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489 else if (STRICMP(name, "multi_byte_encoding") == 0)
6490 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006491#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 else if (STRICMP(name, "balloon_multiline") == 0)
6493 n = multiline_balloon_available();
6494#endif
6495#ifdef DYNAMIC_TCL
6496 else if (STRICMP(name, "tcl") == 0)
6497 n = tcl_enabled(FALSE);
6498#endif
6499#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6500 else if (STRICMP(name, "iconv") == 0)
6501 n = iconv_enabled(FALSE);
6502#endif
6503#ifdef DYNAMIC_LUA
6504 else if (STRICMP(name, "lua") == 0)
6505 n = lua_enabled(FALSE);
6506#endif
6507#ifdef DYNAMIC_MZSCHEME
6508 else if (STRICMP(name, "mzscheme") == 0)
6509 n = mzscheme_enabled(FALSE);
6510#endif
6511#ifdef DYNAMIC_RUBY
6512 else if (STRICMP(name, "ruby") == 0)
6513 n = ruby_enabled(FALSE);
6514#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#ifdef DYNAMIC_PYTHON
6516 else if (STRICMP(name, "python") == 0)
6517 n = python_enabled(FALSE);
6518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519#ifdef DYNAMIC_PYTHON3
6520 else if (STRICMP(name, "python3") == 0)
6521 n = python3_enabled(FALSE);
6522#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006523#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6524 else if (STRICMP(name, "pythonx") == 0)
6525 {
6526# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6527 if (p_pyx == 0)
6528 n = python3_enabled(FALSE) || python_enabled(FALSE);
6529 else if (p_pyx == 3)
6530 n = python3_enabled(FALSE);
6531 else if (p_pyx == 2)
6532 n = python_enabled(FALSE);
6533# elif defined(DYNAMIC_PYTHON)
6534 n = python_enabled(FALSE);
6535# elif defined(DYNAMIC_PYTHON3)
6536 n = python3_enabled(FALSE);
6537# endif
6538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#endif
6540#ifdef DYNAMIC_PERL
6541 else if (STRICMP(name, "perl") == 0)
6542 n = perl_enabled(FALSE);
6543#endif
6544#ifdef FEAT_GUI
6545 else if (STRICMP(name, "gui_running") == 0)
6546 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547# ifdef FEAT_BROWSE
6548 else if (STRICMP(name, "browse") == 0)
6549 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6550# endif
6551#endif
6552#ifdef FEAT_SYN_HL
6553 else if (STRICMP(name, "syntax_items") == 0)
6554 n = syntax_present(curwin);
6555#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006556#ifdef FEAT_VTP
6557 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006558 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559#endif
6560#ifdef FEAT_NETBEANS_INTG
6561 else if (STRICMP(name, "netbeans_enabled") == 0)
6562 n = netbeans_active();
6563#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006564#ifdef FEAT_MOUSE_GPM
6565 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6566 n = gpm_enabled();
6567#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006568#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006569 else if (STRICMP(name, "terminal") == 0)
6570 n = terminal_enabled();
6571#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006572#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006573 else if (STRICMP(name, "conpty") == 0)
6574 n = use_conpty();
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 }
6577
6578 rettv->vval.v_number = n;
6579}
6580
6581/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 * "haslocaldir()" function
6583 */
6584 static void
6585f_haslocaldir(typval_T *argvars, typval_T *rettv)
6586{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006587 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 win_T *wp = NULL;
6589
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006590 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6591
6592 // Check for window-local and tab-local directories
6593 if (wp != NULL && wp->w_localdir != NULL)
6594 rettv->vval.v_number = 1;
6595 else if (tp != NULL && tp->tp_localdir != NULL)
6596 rettv->vval.v_number = 2;
6597 else
6598 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599}
6600
6601/*
6602 * "hasmapto()" function
6603 */
6604 static void
6605f_hasmapto(typval_T *argvars, typval_T *rettv)
6606{
6607 char_u *name;
6608 char_u *mode;
6609 char_u buf[NUMBUFLEN];
6610 int abbr = FALSE;
6611
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006612 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 if (argvars[1].v_type == VAR_UNKNOWN)
6614 mode = (char_u *)"nvo";
6615 else
6616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006617 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006619 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 }
6621
6622 if (map_to_exists(name, mode, abbr))
6623 rettv->vval.v_number = TRUE;
6624 else
6625 rettv->vval.v_number = FALSE;
6626}
6627
6628/*
6629 * "histadd()" function
6630 */
6631 static void
6632f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6633{
6634#ifdef FEAT_CMDHIST
6635 int histype;
6636 char_u *str;
6637 char_u buf[NUMBUFLEN];
6638#endif
6639
6640 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006641 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 return;
6643#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006644 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 histype = str != NULL ? get_histtype(str) : -1;
6646 if (histype >= 0)
6647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006648 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 if (*str != NUL)
6650 {
6651 init_history();
6652 add_to_history(histype, str, FALSE, NUL);
6653 rettv->vval.v_number = TRUE;
6654 return;
6655 }
6656 }
6657#endif
6658}
6659
6660/*
6661 * "histdel()" function
6662 */
6663 static void
6664f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6665{
6666#ifdef FEAT_CMDHIST
6667 int n;
6668 char_u buf[NUMBUFLEN];
6669 char_u *str;
6670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006671 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672 if (str == NULL)
6673 n = 0;
6674 else if (argvars[1].v_type == VAR_UNKNOWN)
6675 /* only one argument: clear entire history */
6676 n = clr_history(get_histtype(str));
6677 else if (argvars[1].v_type == VAR_NUMBER)
6678 /* index given: remove that entry */
6679 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006680 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 else
6682 /* string given: remove all matching entries */
6683 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006684 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 rettv->vval.v_number = n;
6686#endif
6687}
6688
6689/*
6690 * "histget()" function
6691 */
6692 static void
6693f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6694{
6695#ifdef FEAT_CMDHIST
6696 int type;
6697 int idx;
6698 char_u *str;
6699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006700 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 if (str == NULL)
6702 rettv->vval.v_string = NULL;
6703 else
6704 {
6705 type = get_histtype(str);
6706 if (argvars[1].v_type == VAR_UNKNOWN)
6707 idx = get_history_idx(type);
6708 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006709 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006710 /* -1 on type error */
6711 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6712 }
6713#else
6714 rettv->vval.v_string = NULL;
6715#endif
6716 rettv->v_type = VAR_STRING;
6717}
6718
6719/*
6720 * "histnr()" function
6721 */
6722 static void
6723f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6724{
6725 int i;
6726
6727#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006728 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729
6730 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6731 if (i >= HIST_CMD && i < HIST_COUNT)
6732 i = get_history_idx(i);
6733 else
6734#endif
6735 i = -1;
6736 rettv->vval.v_number = i;
6737}
6738
6739/*
6740 * "highlightID(name)" function
6741 */
6742 static void
6743f_hlID(typval_T *argvars, typval_T *rettv)
6744{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006745 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746}
6747
6748/*
6749 * "highlight_exists()" function
6750 */
6751 static void
6752f_hlexists(typval_T *argvars, typval_T *rettv)
6753{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006754 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755}
6756
6757/*
6758 * "hostname()" function
6759 */
6760 static void
6761f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6762{
6763 char_u hostname[256];
6764
6765 mch_get_host_name(hostname, 256);
6766 rettv->v_type = VAR_STRING;
6767 rettv->vval.v_string = vim_strsave(hostname);
6768}
6769
6770/*
6771 * iconv() function
6772 */
6773 static void
6774f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6775{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006776 char_u buf1[NUMBUFLEN];
6777 char_u buf2[NUMBUFLEN];
6778 char_u *from, *to, *str;
6779 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780
6781 rettv->v_type = VAR_STRING;
6782 rettv->vval.v_string = NULL;
6783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006784 str = tv_get_string(&argvars[0]);
6785 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6786 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 vimconv.vc_type = CONV_NONE;
6788 convert_setup(&vimconv, from, to);
6789
6790 /* If the encodings are equal, no conversion needed. */
6791 if (vimconv.vc_type == CONV_NONE)
6792 rettv->vval.v_string = vim_strsave(str);
6793 else
6794 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6795
6796 convert_setup(&vimconv, NULL, NULL);
6797 vim_free(from);
6798 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799}
6800
6801/*
6802 * "indent()" function
6803 */
6804 static void
6805f_indent(typval_T *argvars, typval_T *rettv)
6806{
6807 linenr_T lnum;
6808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006809 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6811 rettv->vval.v_number = get_indent_lnum(lnum);
6812 else
6813 rettv->vval.v_number = -1;
6814}
6815
6816/*
6817 * "index()" function
6818 */
6819 static void
6820f_index(typval_T *argvars, typval_T *rettv)
6821{
6822 list_T *l;
6823 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006824 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825 long idx = 0;
6826 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006827 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828
6829 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006830 if (argvars[0].v_type == VAR_BLOB)
6831 {
6832 typval_T tv;
6833 int start = 0;
6834
6835 if (argvars[2].v_type != VAR_UNKNOWN)
6836 {
6837 start = tv_get_number_chk(&argvars[2], &error);
6838 if (error)
6839 return;
6840 }
6841 b = argvars[0].vval.v_blob;
6842 if (b == NULL)
6843 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006844 if (start < 0)
6845 {
6846 start = blob_len(b) + start;
6847 if (start < 0)
6848 start = 0;
6849 }
6850
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006851 for (idx = start; idx < blob_len(b); ++idx)
6852 {
6853 tv.v_type = VAR_NUMBER;
6854 tv.vval.v_number = blob_get(b, idx);
6855 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6856 {
6857 rettv->vval.v_number = idx;
6858 return;
6859 }
6860 }
6861 return;
6862 }
6863 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006865 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 return;
6867 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 l = argvars[0].vval.v_list;
6870 if (l != NULL)
6871 {
6872 item = l->lv_first;
6873 if (argvars[2].v_type != VAR_UNKNOWN)
6874 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 /* Start at specified item. Use the cached index that list_find()
6876 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006877 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006878 idx = l->lv_idx;
6879 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006880 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 if (error)
6882 item = NULL;
6883 }
6884
6885 for ( ; item != NULL; item = item->li_next, ++idx)
6886 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6887 {
6888 rettv->vval.v_number = idx;
6889 break;
6890 }
6891 }
6892}
6893
6894static int inputsecret_flag = 0;
6895
6896/*
6897 * "input()" function
6898 * Also handles inputsecret() when inputsecret is set.
6899 */
6900 static void
6901f_input(typval_T *argvars, typval_T *rettv)
6902{
6903 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6904}
6905
6906/*
6907 * "inputdialog()" function
6908 */
6909 static void
6910f_inputdialog(typval_T *argvars, typval_T *rettv)
6911{
6912#if defined(FEAT_GUI_TEXTDIALOG)
6913 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6914 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6915 {
6916 char_u *message;
6917 char_u buf[NUMBUFLEN];
6918 char_u *defstr = (char_u *)"";
6919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006920 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6924 else
6925 IObuff[0] = NUL;
6926 if (message != NULL && defstr != NULL
6927 && do_dialog(VIM_QUESTION, NULL, message,
6928 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6929 rettv->vval.v_string = vim_strsave(IObuff);
6930 else
6931 {
6932 if (message != NULL && defstr != NULL
6933 && argvars[1].v_type != VAR_UNKNOWN
6934 && argvars[2].v_type != VAR_UNKNOWN)
6935 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006936 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 else
6938 rettv->vval.v_string = NULL;
6939 }
6940 rettv->v_type = VAR_STRING;
6941 }
6942 else
6943#endif
6944 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6945}
6946
6947/*
6948 * "inputlist()" function
6949 */
6950 static void
6951f_inputlist(typval_T *argvars, typval_T *rettv)
6952{
6953 listitem_T *li;
6954 int selected;
6955 int mouse_used;
6956
6957#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006958 /* While starting up, there is no place to enter text. When running tests
6959 * with --not-a-term we assume feedkeys() will be used. */
6960 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 return;
6962#endif
6963 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006965 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 return;
6967 }
6968
6969 msg_start();
6970 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6971 lines_left = Rows; /* avoid more prompt */
6972 msg_scroll = TRUE;
6973 msg_clr_eos();
6974
6975 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6976 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006977 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978 msg_putchar('\n');
6979 }
6980
6981 /* Ask for choice. */
6982 selected = prompt_for_number(&mouse_used);
6983 if (mouse_used)
6984 selected -= lines_left;
6985
6986 rettv->vval.v_number = selected;
6987}
6988
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6990
6991/*
6992 * "inputrestore()" function
6993 */
6994 static void
6995f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6996{
6997 if (ga_userinput.ga_len > 0)
6998 {
6999 --ga_userinput.ga_len;
7000 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7001 + ga_userinput.ga_len);
7002 /* default return is zero == OK */
7003 }
7004 else if (p_verbose > 1)
7005 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007006 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 rettv->vval.v_number = 1; /* Failed */
7008 }
7009}
7010
7011/*
7012 * "inputsave()" function
7013 */
7014 static void
7015f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7016{
7017 /* Add an entry to the stack of typeahead storage. */
7018 if (ga_grow(&ga_userinput, 1) == OK)
7019 {
7020 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7021 + ga_userinput.ga_len);
7022 ++ga_userinput.ga_len;
7023 /* default return is zero == OK */
7024 }
7025 else
7026 rettv->vval.v_number = 1; /* Failed */
7027}
7028
7029/*
7030 * "inputsecret()" function
7031 */
7032 static void
7033f_inputsecret(typval_T *argvars, typval_T *rettv)
7034{
7035 ++cmdline_star;
7036 ++inputsecret_flag;
7037 f_input(argvars, rettv);
7038 --cmdline_star;
7039 --inputsecret_flag;
7040}
7041
7042/*
7043 * "insert()" function
7044 */
7045 static void
7046f_insert(typval_T *argvars, typval_T *rettv)
7047{
7048 long before = 0;
7049 listitem_T *item;
7050 list_T *l;
7051 int error = FALSE;
7052
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007053 if (argvars[0].v_type == VAR_BLOB)
7054 {
7055 int val, len;
7056 char_u *p;
7057
7058 len = blob_len(argvars[0].vval.v_blob);
7059 if (argvars[2].v_type != VAR_UNKNOWN)
7060 {
7061 before = (long)tv_get_number_chk(&argvars[2], &error);
7062 if (error)
7063 return; // type error; errmsg already given
7064 if (before < 0 || before > len)
7065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007066 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007067 return;
7068 }
7069 }
7070 val = tv_get_number_chk(&argvars[1], &error);
7071 if (error)
7072 return;
7073 if (val < 0 || val > 255)
7074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007075 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007076 return;
7077 }
7078
7079 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7080 return;
7081 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7082 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7083 *(p + before) = val;
7084 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7085
7086 copy_tv(&argvars[0], rettv);
7087 }
7088 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007089 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007090 else if ((l = argvars[0].vval.v_list) != NULL
7091 && !var_check_lock(l->lv_lock,
7092 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 {
7094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007095 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 if (error)
7097 return; /* type error; errmsg already given */
7098
7099 if (before == l->lv_len)
7100 item = NULL;
7101 else
7102 {
7103 item = list_find(l, before);
7104 if (item == NULL)
7105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007106 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 l = NULL;
7108 }
7109 }
7110 if (l != NULL)
7111 {
7112 list_insert_tv(l, &argvars[1], item);
7113 copy_tv(&argvars[0], rettv);
7114 }
7115 }
7116}
7117
7118/*
7119 * "invert(expr)" function
7120 */
7121 static void
7122f_invert(typval_T *argvars, typval_T *rettv)
7123{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007124 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125}
7126
7127/*
7128 * "isdirectory()" function
7129 */
7130 static void
7131f_isdirectory(typval_T *argvars, typval_T *rettv)
7132{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007133 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134}
7135
7136/*
7137 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7138 * or it refers to a List or Dictionary that is locked.
7139 */
7140 static int
7141tv_islocked(typval_T *tv)
7142{
7143 return (tv->v_lock & VAR_LOCKED)
7144 || (tv->v_type == VAR_LIST
7145 && tv->vval.v_list != NULL
7146 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7147 || (tv->v_type == VAR_DICT
7148 && tv->vval.v_dict != NULL
7149 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7150}
7151
7152/*
7153 * "islocked()" function
7154 */
7155 static void
7156f_islocked(typval_T *argvars, typval_T *rettv)
7157{
7158 lval_T lv;
7159 char_u *end;
7160 dictitem_T *di;
7161
7162 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007163 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007164 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 if (end != NULL && lv.ll_name != NULL)
7166 {
7167 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007168 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 else
7170 {
7171 if (lv.ll_tv == NULL)
7172 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007173 di = find_var(lv.ll_name, NULL, TRUE);
7174 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007176 /* Consider a variable locked when:
7177 * 1. the variable itself is locked
7178 * 2. the value of the variable is locked.
7179 * 3. the List or Dict value is locked.
7180 */
7181 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7182 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007183 }
7184 }
7185 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007186 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007188 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 else if (lv.ll_list != NULL)
7190 /* List item. */
7191 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7192 else
7193 /* Dictionary item. */
7194 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7195 }
7196 }
7197
7198 clear_lval(&lv);
7199}
7200
7201#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7202/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007203 * "isinf()" function
7204 */
7205 static void
7206f_isinf(typval_T *argvars, typval_T *rettv)
7207{
7208 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7209 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7210}
7211
7212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 * "isnan()" function
7214 */
7215 static void
7216f_isnan(typval_T *argvars, typval_T *rettv)
7217{
7218 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7219 && isnan(argvars[0].vval.v_float);
7220}
7221#endif
7222
7223/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 * "last_buffer_nr()" function.
7225 */
7226 static void
7227f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7228{
7229 int n = 0;
7230 buf_T *buf;
7231
Bram Moolenaar29323592016-07-24 22:04:11 +02007232 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 if (n < buf->b_fnum)
7234 n = buf->b_fnum;
7235
7236 rettv->vval.v_number = n;
7237}
7238
7239/*
7240 * "len()" function
7241 */
7242 static void
7243f_len(typval_T *argvars, typval_T *rettv)
7244{
7245 switch (argvars[0].v_type)
7246 {
7247 case VAR_STRING:
7248 case VAR_NUMBER:
7249 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007250 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007252 case VAR_BLOB:
7253 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7254 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255 case VAR_LIST:
7256 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7257 break;
7258 case VAR_DICT:
7259 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7260 break;
7261 case VAR_UNKNOWN:
7262 case VAR_SPECIAL:
7263 case VAR_FLOAT:
7264 case VAR_FUNC:
7265 case VAR_PARTIAL:
7266 case VAR_JOB:
7267 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007268 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 break;
7270 }
7271}
7272
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007274libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275{
7276#ifdef FEAT_LIBCALL
7277 char_u *string_in;
7278 char_u **string_result;
7279 int nr_result;
7280#endif
7281
7282 rettv->v_type = type;
7283 if (type != VAR_NUMBER)
7284 rettv->vval.v_string = NULL;
7285
7286 if (check_restricted() || check_secure())
7287 return;
7288
7289#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007290 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7292 {
7293 string_in = NULL;
7294 if (argvars[2].v_type == VAR_STRING)
7295 string_in = argvars[2].vval.v_string;
7296 if (type == VAR_NUMBER)
7297 string_result = NULL;
7298 else
7299 string_result = &rettv->vval.v_string;
7300 if (mch_libcall(argvars[0].vval.v_string,
7301 argvars[1].vval.v_string,
7302 string_in,
7303 argvars[2].vval.v_number,
7304 string_result,
7305 &nr_result) == OK
7306 && type == VAR_NUMBER)
7307 rettv->vval.v_number = nr_result;
7308 }
7309#endif
7310}
7311
7312/*
7313 * "libcall()" function
7314 */
7315 static void
7316f_libcall(typval_T *argvars, typval_T *rettv)
7317{
7318 libcall_common(argvars, rettv, VAR_STRING);
7319}
7320
7321/*
7322 * "libcallnr()" function
7323 */
7324 static void
7325f_libcallnr(typval_T *argvars, typval_T *rettv)
7326{
7327 libcall_common(argvars, rettv, VAR_NUMBER);
7328}
7329
7330/*
7331 * "line(string)" function
7332 */
7333 static void
7334f_line(typval_T *argvars, typval_T *rettv)
7335{
7336 linenr_T lnum = 0;
7337 pos_T *fp;
7338 int fnum;
7339
7340 fp = var2fpos(&argvars[0], TRUE, &fnum);
7341 if (fp != NULL)
7342 lnum = fp->lnum;
7343 rettv->vval.v_number = lnum;
7344}
7345
7346/*
7347 * "line2byte(lnum)" function
7348 */
7349 static void
7350f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7351{
7352#ifndef FEAT_BYTEOFF
7353 rettv->vval.v_number = -1;
7354#else
7355 linenr_T lnum;
7356
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7359 rettv->vval.v_number = -1;
7360 else
7361 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7362 if (rettv->vval.v_number >= 0)
7363 ++rettv->vval.v_number;
7364#endif
7365}
7366
7367/*
7368 * "lispindent(lnum)" function
7369 */
7370 static void
7371f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7372{
7373#ifdef FEAT_LISP
7374 pos_T pos;
7375 linenr_T lnum;
7376
7377 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007378 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007379 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7380 {
7381 curwin->w_cursor.lnum = lnum;
7382 rettv->vval.v_number = get_lisp_indent();
7383 curwin->w_cursor = pos;
7384 }
7385 else
7386#endif
7387 rettv->vval.v_number = -1;
7388}
7389
7390/*
7391 * "localtime()" function
7392 */
7393 static void
7394f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7395{
7396 rettv->vval.v_number = (varnumber_T)time(NULL);
7397}
7398
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399 static void
7400get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7401{
7402 char_u *keys;
7403 char_u *which;
7404 char_u buf[NUMBUFLEN];
7405 char_u *keys_buf = NULL;
7406 char_u *rhs;
7407 int mode;
7408 int abbr = FALSE;
7409 int get_dict = FALSE;
7410 mapblock_T *mp;
7411 int buffer_local;
7412
7413 /* return empty string for failure */
7414 rettv->v_type = VAR_STRING;
7415 rettv->vval.v_string = NULL;
7416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007417 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418 if (*keys == NUL)
7419 return;
7420
7421 if (argvars[1].v_type != VAR_UNKNOWN)
7422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007423 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007424 if (argvars[2].v_type != VAR_UNKNOWN)
7425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007428 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 }
7430 }
7431 else
7432 which = (char_u *)"";
7433 if (which == NULL)
7434 return;
7435
7436 mode = get_map_mode(&which, 0);
7437
7438 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7439 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7440 vim_free(keys_buf);
7441
7442 if (!get_dict)
7443 {
7444 /* Return a string. */
7445 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007446 {
7447 if (*rhs == NUL)
7448 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7449 else
7450 rettv->vval.v_string = str2special_save(rhs, FALSE);
7451 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452
7453 }
7454 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7455 {
7456 /* Return a dictionary. */
7457 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7458 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7459 dict_T *dict = rettv->vval.v_dict;
7460
Bram Moolenaare0be1672018-07-08 16:50:37 +02007461 dict_add_string(dict, "lhs", lhs);
7462 dict_add_string(dict, "rhs", mp->m_orig_str);
7463 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7464 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7465 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007466 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7467 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007468 dict_add_number(dict, "buffer", (long)buffer_local);
7469 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7470 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471
7472 vim_free(lhs);
7473 vim_free(mapmode);
7474 }
7475}
7476
7477#ifdef FEAT_FLOAT
7478/*
7479 * "log()" function
7480 */
7481 static void
7482f_log(typval_T *argvars, typval_T *rettv)
7483{
7484 float_T f = 0.0;
7485
7486 rettv->v_type = VAR_FLOAT;
7487 if (get_float_arg(argvars, &f) == OK)
7488 rettv->vval.v_float = log(f);
7489 else
7490 rettv->vval.v_float = 0.0;
7491}
7492
7493/*
7494 * "log10()" function
7495 */
7496 static void
7497f_log10(typval_T *argvars, typval_T *rettv)
7498{
7499 float_T f = 0.0;
7500
7501 rettv->v_type = VAR_FLOAT;
7502 if (get_float_arg(argvars, &f) == OK)
7503 rettv->vval.v_float = log10(f);
7504 else
7505 rettv->vval.v_float = 0.0;
7506}
7507#endif
7508
7509#ifdef FEAT_LUA
7510/*
7511 * "luaeval()" function
7512 */
7513 static void
7514f_luaeval(typval_T *argvars, typval_T *rettv)
7515{
7516 char_u *str;
7517 char_u buf[NUMBUFLEN];
7518
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007519 if (check_restricted() || check_secure())
7520 return;
7521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007522 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 do_luaeval(str, argvars + 1, rettv);
7524}
7525#endif
7526
7527/*
7528 * "map()" function
7529 */
7530 static void
7531f_map(typval_T *argvars, typval_T *rettv)
7532{
7533 filter_map(argvars, rettv, TRUE);
7534}
7535
7536/*
7537 * "maparg()" function
7538 */
7539 static void
7540f_maparg(typval_T *argvars, typval_T *rettv)
7541{
7542 get_maparg(argvars, rettv, TRUE);
7543}
7544
7545/*
7546 * "mapcheck()" function
7547 */
7548 static void
7549f_mapcheck(typval_T *argvars, typval_T *rettv)
7550{
7551 get_maparg(argvars, rettv, FALSE);
7552}
7553
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007554typedef enum
7555{
7556 MATCH_END, /* matchend() */
7557 MATCH_MATCH, /* match() */
7558 MATCH_STR, /* matchstr() */
7559 MATCH_LIST, /* matchlist() */
7560 MATCH_POS /* matchstrpos() */
7561} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562
7563 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007564find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007565{
7566 char_u *str = NULL;
7567 long len = 0;
7568 char_u *expr = NULL;
7569 char_u *pat;
7570 regmatch_T regmatch;
7571 char_u patbuf[NUMBUFLEN];
7572 char_u strbuf[NUMBUFLEN];
7573 char_u *save_cpo;
7574 long start = 0;
7575 long nth = 1;
7576 colnr_T startcol = 0;
7577 int match = 0;
7578 list_T *l = NULL;
7579 listitem_T *li = NULL;
7580 long idx = 0;
7581 char_u *tofree = NULL;
7582
7583 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7584 save_cpo = p_cpo;
7585 p_cpo = (char_u *)"";
7586
7587 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007588 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007590 /* type MATCH_LIST: return empty list when there are no matches.
7591 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007592 if (rettv_list_alloc(rettv) == FAIL)
7593 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007594 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007595 && (list_append_string(rettv->vval.v_list,
7596 (char_u *)"", 0) == FAIL
7597 || list_append_number(rettv->vval.v_list,
7598 (varnumber_T)-1) == FAIL
7599 || list_append_number(rettv->vval.v_list,
7600 (varnumber_T)-1) == FAIL
7601 || list_append_number(rettv->vval.v_list,
7602 (varnumber_T)-1) == FAIL))
7603 {
7604 list_free(rettv->vval.v_list);
7605 rettv->vval.v_list = NULL;
7606 goto theend;
7607 }
7608 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007609 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 {
7611 rettv->v_type = VAR_STRING;
7612 rettv->vval.v_string = NULL;
7613 }
7614
7615 if (argvars[0].v_type == VAR_LIST)
7616 {
7617 if ((l = argvars[0].vval.v_list) == NULL)
7618 goto theend;
7619 li = l->lv_first;
7620 }
7621 else
7622 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007623 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 len = (long)STRLEN(str);
7625 }
7626
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007627 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 if (pat == NULL)
7629 goto theend;
7630
7631 if (argvars[2].v_type != VAR_UNKNOWN)
7632 {
7633 int error = FALSE;
7634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 if (error)
7637 goto theend;
7638 if (l != NULL)
7639 {
7640 li = list_find(l, start);
7641 if (li == NULL)
7642 goto theend;
7643 idx = l->lv_idx; /* use the cached index */
7644 }
7645 else
7646 {
7647 if (start < 0)
7648 start = 0;
7649 if (start > len)
7650 goto theend;
7651 /* When "count" argument is there ignore matches before "start",
7652 * otherwise skip part of the string. Differs when pattern is "^"
7653 * or "\<". */
7654 if (argvars[3].v_type != VAR_UNKNOWN)
7655 startcol = start;
7656 else
7657 {
7658 str += start;
7659 len -= start;
7660 }
7661 }
7662
7663 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 if (error)
7666 goto theend;
7667 }
7668
7669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7670 if (regmatch.regprog != NULL)
7671 {
7672 regmatch.rm_ic = p_ic;
7673
7674 for (;;)
7675 {
7676 if (l != NULL)
7677 {
7678 if (li == NULL)
7679 {
7680 match = FALSE;
7681 break;
7682 }
7683 vim_free(tofree);
7684 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7685 if (str == NULL)
7686 break;
7687 }
7688
7689 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7690
7691 if (match && --nth <= 0)
7692 break;
7693 if (l == NULL && !match)
7694 break;
7695
7696 /* Advance to just after the match. */
7697 if (l != NULL)
7698 {
7699 li = li->li_next;
7700 ++idx;
7701 }
7702 else
7703 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 startcol = (colnr_T)(regmatch.startp[0]
7705 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 if (startcol > (colnr_T)len
7707 || str + startcol <= regmatch.startp[0])
7708 {
7709 match = FALSE;
7710 break;
7711 }
7712 }
7713 }
7714
7715 if (match)
7716 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007717 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 {
7719 listitem_T *li1 = rettv->vval.v_list->lv_first;
7720 listitem_T *li2 = li1->li_next;
7721 listitem_T *li3 = li2->li_next;
7722 listitem_T *li4 = li3->li_next;
7723
7724 vim_free(li1->li_tv.vval.v_string);
7725 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7726 (int)(regmatch.endp[0] - regmatch.startp[0]));
7727 li3->li_tv.vval.v_number =
7728 (varnumber_T)(regmatch.startp[0] - expr);
7729 li4->li_tv.vval.v_number =
7730 (varnumber_T)(regmatch.endp[0] - expr);
7731 if (l != NULL)
7732 li2->li_tv.vval.v_number = (varnumber_T)idx;
7733 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007734 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 {
7736 int i;
7737
7738 /* return list with matched string and submatches */
7739 for (i = 0; i < NSUBEXP; ++i)
7740 {
7741 if (regmatch.endp[i] == NULL)
7742 {
7743 if (list_append_string(rettv->vval.v_list,
7744 (char_u *)"", 0) == FAIL)
7745 break;
7746 }
7747 else if (list_append_string(rettv->vval.v_list,
7748 regmatch.startp[i],
7749 (int)(regmatch.endp[i] - regmatch.startp[i]))
7750 == FAIL)
7751 break;
7752 }
7753 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007754 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755 {
7756 /* return matched string */
7757 if (l != NULL)
7758 copy_tv(&li->li_tv, rettv);
7759 else
7760 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7761 (int)(regmatch.endp[0] - regmatch.startp[0]));
7762 }
7763 else if (l != NULL)
7764 rettv->vval.v_number = idx;
7765 else
7766 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007767 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007768 rettv->vval.v_number =
7769 (varnumber_T)(regmatch.startp[0] - str);
7770 else
7771 rettv->vval.v_number =
7772 (varnumber_T)(regmatch.endp[0] - str);
7773 rettv->vval.v_number += (varnumber_T)(str - expr);
7774 }
7775 }
7776 vim_regfree(regmatch.regprog);
7777 }
7778
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007779theend:
7780 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 /* matchstrpos() without a list: drop the second item. */
7782 listitem_remove(rettv->vval.v_list,
7783 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 vim_free(tofree);
7785 p_cpo = save_cpo;
7786}
7787
7788/*
7789 * "match()" function
7790 */
7791 static void
7792f_match(typval_T *argvars, typval_T *rettv)
7793{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007794 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795}
7796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797/*
7798 * "matchend()" function
7799 */
7800 static void
7801f_matchend(typval_T *argvars, typval_T *rettv)
7802{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007803 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804}
7805
7806/*
7807 * "matchlist()" function
7808 */
7809 static void
7810f_matchlist(typval_T *argvars, typval_T *rettv)
7811{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007812 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813}
7814
7815/*
7816 * "matchstr()" function
7817 */
7818 static void
7819f_matchstr(typval_T *argvars, typval_T *rettv)
7820{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007821 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822}
7823
7824/*
7825 * "matchstrpos()" function
7826 */
7827 static void
7828f_matchstrpos(typval_T *argvars, typval_T *rettv)
7829{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007830 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831}
7832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 static void
7834max_min(typval_T *argvars, typval_T *rettv, int domax)
7835{
7836 varnumber_T n = 0;
7837 varnumber_T i;
7838 int error = FALSE;
7839
7840 if (argvars[0].v_type == VAR_LIST)
7841 {
7842 list_T *l;
7843 listitem_T *li;
7844
7845 l = argvars[0].vval.v_list;
7846 if (l != NULL)
7847 {
7848 li = l->lv_first;
7849 if (li != NULL)
7850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 for (;;)
7853 {
7854 li = li->li_next;
7855 if (li == NULL)
7856 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007857 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 if (domax ? i > n : i < n)
7859 n = i;
7860 }
7861 }
7862 }
7863 }
7864 else if (argvars[0].v_type == VAR_DICT)
7865 {
7866 dict_T *d;
7867 int first = TRUE;
7868 hashitem_T *hi;
7869 int todo;
7870
7871 d = argvars[0].vval.v_dict;
7872 if (d != NULL)
7873 {
7874 todo = (int)d->dv_hashtab.ht_used;
7875 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7876 {
7877 if (!HASHITEM_EMPTY(hi))
7878 {
7879 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007880 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 if (first)
7882 {
7883 n = i;
7884 first = FALSE;
7885 }
7886 else if (domax ? i > n : i < n)
7887 n = i;
7888 }
7889 }
7890 }
7891 }
7892 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007893 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 rettv->vval.v_number = error ? 0 : n;
7895}
7896
7897/*
7898 * "max()" function
7899 */
7900 static void
7901f_max(typval_T *argvars, typval_T *rettv)
7902{
7903 max_min(argvars, rettv, TRUE);
7904}
7905
7906/*
7907 * "min()" function
7908 */
7909 static void
7910f_min(typval_T *argvars, typval_T *rettv)
7911{
7912 max_min(argvars, rettv, FALSE);
7913}
7914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915/*
7916 * Create the directory in which "dir" is located, and higher levels when
7917 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007918 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 */
7920 static int
7921mkdir_recurse(char_u *dir, int prot)
7922{
7923 char_u *p;
7924 char_u *updir;
7925 int r = FAIL;
7926
7927 /* Get end of directory name in "dir".
7928 * We're done when it's "/" or "c:/". */
7929 p = gettail_sep(dir);
7930 if (p <= get_past_head(dir))
7931 return OK;
7932
7933 /* If the directory exists we're done. Otherwise: create it.*/
7934 updir = vim_strnsave(dir, (int)(p - dir));
7935 if (updir == NULL)
7936 return FAIL;
7937 if (mch_isdir(updir))
7938 r = OK;
7939 else if (mkdir_recurse(updir, prot) == OK)
7940 r = vim_mkdir_emsg(updir, prot);
7941 vim_free(updir);
7942 return r;
7943}
7944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945/*
7946 * "mkdir()" function
7947 */
7948 static void
7949f_mkdir(typval_T *argvars, typval_T *rettv)
7950{
7951 char_u *dir;
7952 char_u buf[NUMBUFLEN];
7953 int prot = 0755;
7954
7955 rettv->vval.v_number = FAIL;
7956 if (check_restricted() || check_secure())
7957 return;
7958
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007959 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007961 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007963 if (*gettail(dir) == NUL)
7964 /* remove trailing slashes */
7965 *gettail_sep(dir) = NUL;
7966
7967 if (argvars[1].v_type != VAR_UNKNOWN)
7968 {
7969 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007971 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007972 if (prot == -1)
7973 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007975 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007976 {
7977 if (mch_isdir(dir))
7978 {
7979 /* With the "p" flag it's OK if the dir already exists. */
7980 rettv->vval.v_number = OK;
7981 return;
7982 }
7983 mkdir_recurse(dir, prot);
7984 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007986 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988
7989/*
7990 * "mode()" function
7991 */
7992 static void
7993f_mode(typval_T *argvars, typval_T *rettv)
7994{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007995 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996
Bram Moolenaar612cc382018-07-29 15:34:26 +02007997 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998
7999 if (time_for_testing == 93784)
8000 {
8001 /* Testing the two-character code. */
8002 buf[0] = 'x';
8003 buf[1] = '!';
8004 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008005#ifdef FEAT_TERMINAL
8006 else if (term_use_loop())
8007 buf[0] = 't';
8008#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 else if (VIsual_active)
8010 {
8011 if (VIsual_select)
8012 buf[0] = VIsual_mode + 's' - 'v';
8013 else
8014 buf[0] = VIsual_mode;
8015 }
8016 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8017 || State == CONFIRM)
8018 {
8019 buf[0] = 'r';
8020 if (State == ASKMORE)
8021 buf[1] = 'm';
8022 else if (State == CONFIRM)
8023 buf[1] = '?';
8024 }
8025 else if (State == EXTERNCMD)
8026 buf[0] = '!';
8027 else if (State & INSERT)
8028 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if (State & VREPLACE_FLAG)
8030 {
8031 buf[0] = 'R';
8032 buf[1] = 'v';
8033 }
8034 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008035 {
8036 if (State & REPLACE_FLAG)
8037 buf[0] = 'R';
8038 else
8039 buf[0] = 'i';
8040#ifdef FEAT_INS_EXPAND
8041 if (ins_compl_active())
8042 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008043 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008044 buf[1] = 'x';
8045#endif
8046 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008048 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 {
8050 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008051 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008053 else if (exmode_active == EXMODE_NORMAL)
8054 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 }
8056 else
8057 {
8058 buf[0] = 'n';
8059 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008060 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008062 // to be able to detect force-linewise/blockwise/characterwise operations
8063 buf[2] = motion_force;
8064 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008065 else if (restart_edit == 'I' || restart_edit == 'R'
8066 || restart_edit == 'V')
8067 {
8068 buf[1] = 'i';
8069 buf[2] = restart_edit;
8070 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 }
8072
8073 /* Clear out the minor mode when the argument is not a non-zero number or
8074 * non-empty string. */
8075 if (!non_zero_arg(&argvars[0]))
8076 buf[1] = NUL;
8077
8078 rettv->vval.v_string = vim_strsave(buf);
8079 rettv->v_type = VAR_STRING;
8080}
8081
8082#if defined(FEAT_MZSCHEME) || defined(PROTO)
8083/*
8084 * "mzeval()" function
8085 */
8086 static void
8087f_mzeval(typval_T *argvars, typval_T *rettv)
8088{
8089 char_u *str;
8090 char_u buf[NUMBUFLEN];
8091
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008092 if (check_restricted() || check_secure())
8093 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008094 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 do_mzeval(str, rettv);
8096}
8097
8098 void
8099mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8100{
8101 typval_T argvars[3];
8102
8103 argvars[0].v_type = VAR_STRING;
8104 argvars[0].vval.v_string = name;
8105 copy_tv(args, &argvars[1]);
8106 argvars[2].v_type = VAR_UNKNOWN;
8107 f_call(argvars, rettv);
8108 clear_tv(&argvars[1]);
8109}
8110#endif
8111
8112/*
8113 * "nextnonblank()" function
8114 */
8115 static void
8116f_nextnonblank(typval_T *argvars, typval_T *rettv)
8117{
8118 linenr_T lnum;
8119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008120 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 {
8122 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8123 {
8124 lnum = 0;
8125 break;
8126 }
8127 if (*skipwhite(ml_get(lnum)) != NUL)
8128 break;
8129 }
8130 rettv->vval.v_number = lnum;
8131}
8132
8133/*
8134 * "nr2char()" function
8135 */
8136 static void
8137f_nr2char(typval_T *argvars, typval_T *rettv)
8138{
8139 char_u buf[NUMBUFLEN];
8140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 if (has_mbyte)
8142 {
8143 int utf8 = 0;
8144
8145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008146 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008148 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008150 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 }
8152 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008154 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 buf[1] = NUL;
8156 }
8157 rettv->v_type = VAR_STRING;
8158 rettv->vval.v_string = vim_strsave(buf);
8159}
8160
8161/*
8162 * "or(expr, expr)" function
8163 */
8164 static void
8165f_or(typval_T *argvars, typval_T *rettv)
8166{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008167 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8168 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169}
8170
8171/*
8172 * "pathshorten()" function
8173 */
8174 static void
8175f_pathshorten(typval_T *argvars, typval_T *rettv)
8176{
8177 char_u *p;
8178
8179 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008180 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 if (p == NULL)
8182 rettv->vval.v_string = NULL;
8183 else
8184 {
8185 p = vim_strsave(p);
8186 rettv->vval.v_string = p;
8187 if (p != NULL)
8188 shorten_dir(p);
8189 }
8190}
8191
8192#ifdef FEAT_PERL
8193/*
8194 * "perleval()" function
8195 */
8196 static void
8197f_perleval(typval_T *argvars, typval_T *rettv)
8198{
8199 char_u *str;
8200 char_u buf[NUMBUFLEN];
8201
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008202 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 do_perleval(str, rettv);
8204}
8205#endif
8206
8207#ifdef FEAT_FLOAT
8208/*
8209 * "pow()" function
8210 */
8211 static void
8212f_pow(typval_T *argvars, typval_T *rettv)
8213{
8214 float_T fx = 0.0, fy = 0.0;
8215
8216 rettv->v_type = VAR_FLOAT;
8217 if (get_float_arg(argvars, &fx) == OK
8218 && get_float_arg(&argvars[1], &fy) == OK)
8219 rettv->vval.v_float = pow(fx, fy);
8220 else
8221 rettv->vval.v_float = 0.0;
8222}
8223#endif
8224
8225/*
8226 * "prevnonblank()" function
8227 */
8228 static void
8229f_prevnonblank(typval_T *argvars, typval_T *rettv)
8230{
8231 linenr_T lnum;
8232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008233 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8235 lnum = 0;
8236 else
8237 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8238 --lnum;
8239 rettv->vval.v_number = lnum;
8240}
8241
8242/* This dummy va_list is here because:
8243 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8244 * - locally in the function results in a "used before set" warning
8245 * - using va_start() to initialize it gives "function with fixed args" error */
8246static va_list ap;
8247
8248/*
8249 * "printf()" function
8250 */
8251 static void
8252f_printf(typval_T *argvars, typval_T *rettv)
8253{
8254 char_u buf[NUMBUFLEN];
8255 int len;
8256 char_u *s;
8257 int saved_did_emsg = did_emsg;
8258 char *fmt;
8259
8260 rettv->v_type = VAR_STRING;
8261 rettv->vval.v_string = NULL;
8262
8263 /* Get the required length, allocate the buffer and do it for real. */
8264 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008265 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008266 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267 if (!did_emsg)
8268 {
8269 s = alloc(len + 1);
8270 if (s != NULL)
8271 {
8272 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008273 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8274 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 }
8276 }
8277 did_emsg |= saved_did_emsg;
8278}
8279
8280/*
8281 * "pumvisible()" function
8282 */
8283 static void
8284f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8285{
8286#ifdef FEAT_INS_EXPAND
8287 if (pum_visible())
8288 rettv->vval.v_number = 1;
8289#endif
8290}
8291
8292#ifdef FEAT_PYTHON3
8293/*
8294 * "py3eval()" function
8295 */
8296 static void
8297f_py3eval(typval_T *argvars, typval_T *rettv)
8298{
8299 char_u *str;
8300 char_u buf[NUMBUFLEN];
8301
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008302 if (check_restricted() || check_secure())
8303 return;
8304
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008305 if (p_pyx == 0)
8306 p_pyx = 3;
8307
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008308 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 do_py3eval(str, rettv);
8310}
8311#endif
8312
8313#ifdef FEAT_PYTHON
8314/*
8315 * "pyeval()" function
8316 */
8317 static void
8318f_pyeval(typval_T *argvars, typval_T *rettv)
8319{
8320 char_u *str;
8321 char_u buf[NUMBUFLEN];
8322
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008323 if (check_restricted() || check_secure())
8324 return;
8325
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008326 if (p_pyx == 0)
8327 p_pyx = 2;
8328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008329 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 do_pyeval(str, rettv);
8331}
8332#endif
8333
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008334#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8335/*
8336 * "pyxeval()" function
8337 */
8338 static void
8339f_pyxeval(typval_T *argvars, typval_T *rettv)
8340{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008341 if (check_restricted() || check_secure())
8342 return;
8343
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008344# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8345 init_pyxversion();
8346 if (p_pyx == 2)
8347 f_pyeval(argvars, rettv);
8348 else
8349 f_py3eval(argvars, rettv);
8350# elif defined(FEAT_PYTHON)
8351 f_pyeval(argvars, rettv);
8352# elif defined(FEAT_PYTHON3)
8353 f_py3eval(argvars, rettv);
8354# endif
8355}
8356#endif
8357
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358/*
8359 * "range()" function
8360 */
8361 static void
8362f_range(typval_T *argvars, typval_T *rettv)
8363{
8364 varnumber_T start;
8365 varnumber_T end;
8366 varnumber_T stride = 1;
8367 varnumber_T i;
8368 int error = FALSE;
8369
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008370 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 if (argvars[1].v_type == VAR_UNKNOWN)
8372 {
8373 end = start - 1;
8374 start = 0;
8375 }
8376 else
8377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008378 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008380 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 }
8382
8383 if (error)
8384 return; /* type error; errmsg already given */
8385 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008388 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008389 else
8390 {
8391 if (rettv_list_alloc(rettv) == OK)
8392 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8393 if (list_append_number(rettv->vval.v_list,
8394 (varnumber_T)i) == FAIL)
8395 break;
8396 }
8397}
8398
8399/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008400 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008401 */
8402 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008403readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008404{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008405 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008406 typval_T save_val;
8407 typval_T rettv;
8408 typval_T argv[2];
8409 int retval = 0;
8410 int error = FALSE;
8411
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008412 if (expr->v_type == VAR_UNKNOWN)
8413 return 1;
8414
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008415 prepare_vimvar(VV_VAL, &save_val);
8416 set_vim_var_string(VV_VAL, name, -1);
8417 argv[0].v_type = VAR_STRING;
8418 argv[0].vval.v_string = name;
8419
8420 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8421 goto theend;
8422
8423 retval = tv_get_number_chk(&rettv, &error);
8424 if (error)
8425 retval = -1;
8426 clear_tv(&rettv);
8427
8428theend:
8429 set_vim_var_string(VV_VAL, NULL, 0);
8430 restore_vimvar(VV_VAL, &save_val);
8431 return retval;
8432}
8433
8434/*
8435 * "readdir()" function
8436 */
8437 static void
8438f_readdir(typval_T *argvars, typval_T *rettv)
8439{
8440 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008441 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008442 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008443 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008444 garray_T ga;
8445 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008446
8447 if (rettv_list_alloc(rettv) == FAIL)
8448 return;
8449 path = tv_get_string(&argvars[0]);
8450 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008451
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008452 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8453 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008454 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008455 for (i = 0; i < ga.ga_len; i++)
8456 {
8457 p = ((char_u **)ga.ga_data)[i];
8458 list_append_string(rettv->vval.v_list, p, -1);
8459 }
8460 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008461 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008462}
8463
8464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 * "readfile()" function
8466 */
8467 static void
8468f_readfile(typval_T *argvars, typval_T *rettv)
8469{
8470 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008471 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 int failed = FALSE;
8473 char_u *fname;
8474 FILE *fd;
8475 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8476 int io_size = sizeof(buf);
8477 int readlen; /* size of last fread() */
8478 char_u *prev = NULL; /* previously read bytes, if any */
8479 long prevlen = 0; /* length of data in prev */
8480 long prevsize = 0; /* size of prev buffer */
8481 long maxline = MAXLNUM;
8482 long cnt = 0;
8483 char_u *p; /* position in buf */
8484 char_u *start; /* start of current line */
8485
8486 if (argvars[1].v_type != VAR_UNKNOWN)
8487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008488 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008490 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8491 blob = TRUE;
8492
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008494 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 }
8496
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008497 if (blob)
8498 {
8499 if (rettv_blob_alloc(rettv) == FAIL)
8500 return;
8501 }
8502 else
8503 {
8504 if (rettv_list_alloc(rettv) == FAIL)
8505 return;
8506 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507
8508 /* Always open the file in binary mode, library functions have a mind of
8509 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008510 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008513 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 return;
8515 }
8516
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008517 if (blob)
8518 {
8519 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008521 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008522 blob_free(rettv->vval.v_blob);
8523 }
8524 fclose(fd);
8525 return;
8526 }
8527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 while (cnt < maxline || maxline < 0)
8529 {
8530 readlen = (int)fread(buf, 1, io_size, fd);
8531
8532 /* This for loop processes what was read, but is also entered at end
8533 * of file so that either:
8534 * - an incomplete line gets written
8535 * - a "binary" file gets an empty line at the end if it ends in a
8536 * newline. */
8537 for (p = buf, start = buf;
8538 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8539 ++p)
8540 {
8541 if (*p == '\n' || readlen <= 0)
8542 {
8543 listitem_T *li;
8544 char_u *s = NULL;
8545 long_u len = p - start;
8546
8547 /* Finished a line. Remove CRs before NL. */
8548 if (readlen > 0 && !binary)
8549 {
8550 while (len > 0 && start[len - 1] == '\r')
8551 --len;
8552 /* removal may cross back to the "prev" string */
8553 if (len == 0)
8554 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8555 --prevlen;
8556 }
8557 if (prevlen == 0)
8558 s = vim_strnsave(start, (int)len);
8559 else
8560 {
8561 /* Change "prev" buffer to be the right size. This way
8562 * the bytes are only copied once, and very long lines are
8563 * allocated only once. */
8564 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8565 {
8566 mch_memmove(s + prevlen, start, len);
8567 s[prevlen + len] = NUL;
8568 prev = NULL; /* the list will own the string */
8569 prevlen = prevsize = 0;
8570 }
8571 }
8572 if (s == NULL)
8573 {
8574 do_outofmem_msg((long_u) prevlen + len + 1);
8575 failed = TRUE;
8576 break;
8577 }
8578
8579 if ((li = listitem_alloc()) == NULL)
8580 {
8581 vim_free(s);
8582 failed = TRUE;
8583 break;
8584 }
8585 li->li_tv.v_type = VAR_STRING;
8586 li->li_tv.v_lock = 0;
8587 li->li_tv.vval.v_string = s;
8588 list_append(rettv->vval.v_list, li);
8589
8590 start = p + 1; /* step over newline */
8591 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8592 break;
8593 }
8594 else if (*p == NUL)
8595 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8597 * when finding the BF and check the previous two bytes. */
8598 else if (*p == 0xbf && enc_utf8 && !binary)
8599 {
8600 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8601 * + 1, these may be in the "prev" string. */
8602 char_u back1 = p >= buf + 1 ? p[-1]
8603 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8604 char_u back2 = p >= buf + 2 ? p[-2]
8605 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8606 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8607
8608 if (back2 == 0xef && back1 == 0xbb)
8609 {
8610 char_u *dest = p - 2;
8611
8612 /* Usually a BOM is at the beginning of a file, and so at
8613 * the beginning of a line; then we can just step over it.
8614 */
8615 if (start == dest)
8616 start = p + 1;
8617 else
8618 {
8619 /* have to shuffle buf to close gap */
8620 int adjust_prevlen = 0;
8621
8622 if (dest < buf)
8623 {
8624 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8625 dest = buf;
8626 }
8627 if (readlen > p - buf + 1)
8628 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8629 readlen -= 3 - adjust_prevlen;
8630 prevlen -= adjust_prevlen;
8631 p = dest - 1;
8632 }
8633 }
8634 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 } /* for */
8636
8637 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8638 break;
8639 if (start < p)
8640 {
8641 /* There's part of a line in buf, store it in "prev". */
8642 if (p - start + prevlen >= prevsize)
8643 {
8644 /* need bigger "prev" buffer */
8645 char_u *newprev;
8646
8647 /* A common use case is ordinary text files and "prev" gets a
8648 * fragment of a line, so the first allocation is made
8649 * small, to avoid repeatedly 'allocing' large and
8650 * 'reallocing' small. */
8651 if (prevsize == 0)
8652 prevsize = (long)(p - start);
8653 else
8654 {
8655 long grow50pc = (prevsize * 3) / 2;
8656 long growmin = (long)((p - start) * 2 + prevlen);
8657 prevsize = grow50pc > growmin ? grow50pc : growmin;
8658 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008659 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 if (newprev == NULL)
8661 {
8662 do_outofmem_msg((long_u)prevsize);
8663 failed = TRUE;
8664 break;
8665 }
8666 prev = newprev;
8667 }
8668 /* Add the line part to end of "prev". */
8669 mch_memmove(prev + prevlen, start, p - start);
8670 prevlen += (long)(p - start);
8671 }
8672 } /* while */
8673
8674 /*
8675 * For a negative line count use only the lines at the end of the file,
8676 * free the rest.
8677 */
8678 if (!failed && maxline < 0)
8679 while (cnt > -maxline)
8680 {
8681 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8682 --cnt;
8683 }
8684
8685 if (failed)
8686 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008687 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008689 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 }
8691
8692 vim_free(prev);
8693 fclose(fd);
8694}
8695
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008696 static void
8697return_register(int regname, typval_T *rettv)
8698{
8699 char_u buf[2] = {0, 0};
8700
8701 buf[0] = (char_u)regname;
8702 rettv->v_type = VAR_STRING;
8703 rettv->vval.v_string = vim_strsave(buf);
8704}
8705
8706/*
8707 * "reg_executing()" function
8708 */
8709 static void
8710f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8711{
8712 return_register(reg_executing, rettv);
8713}
8714
8715/*
8716 * "reg_recording()" function
8717 */
8718 static void
8719f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8720{
8721 return_register(reg_recording, rettv);
8722}
8723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725/*
8726 * Convert a List to proftime_T.
8727 * Return FAIL when there is something wrong.
8728 */
8729 static int
8730list2proftime(typval_T *arg, proftime_T *tm)
8731{
8732 long n1, n2;
8733 int error = FALSE;
8734
8735 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8736 || arg->vval.v_list->lv_len != 2)
8737 return FAIL;
8738 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8739 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008740# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 tm->HighPart = n1;
8742 tm->LowPart = n2;
8743# else
8744 tm->tv_sec = n1;
8745 tm->tv_usec = n2;
8746# endif
8747 return error ? FAIL : OK;
8748}
8749#endif /* FEAT_RELTIME */
8750
8751/*
8752 * "reltime()" function
8753 */
8754 static void
8755f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8756{
8757#ifdef FEAT_RELTIME
8758 proftime_T res;
8759 proftime_T start;
8760
8761 if (argvars[0].v_type == VAR_UNKNOWN)
8762 {
8763 /* No arguments: get current time. */
8764 profile_start(&res);
8765 }
8766 else if (argvars[1].v_type == VAR_UNKNOWN)
8767 {
8768 if (list2proftime(&argvars[0], &res) == FAIL)
8769 return;
8770 profile_end(&res);
8771 }
8772 else
8773 {
8774 /* Two arguments: compute the difference. */
8775 if (list2proftime(&argvars[0], &start) == FAIL
8776 || list2proftime(&argvars[1], &res) == FAIL)
8777 return;
8778 profile_sub(&res, &start);
8779 }
8780
8781 if (rettv_list_alloc(rettv) == OK)
8782 {
8783 long n1, n2;
8784
Bram Moolenaar4f974752019-02-17 17:44:42 +01008785# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 n1 = res.HighPart;
8787 n2 = res.LowPart;
8788# else
8789 n1 = res.tv_sec;
8790 n2 = res.tv_usec;
8791# endif
8792 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8793 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8794 }
8795#endif
8796}
8797
8798#ifdef FEAT_FLOAT
8799/*
8800 * "reltimefloat()" function
8801 */
8802 static void
8803f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8804{
8805# ifdef FEAT_RELTIME
8806 proftime_T tm;
8807# endif
8808
8809 rettv->v_type = VAR_FLOAT;
8810 rettv->vval.v_float = 0;
8811# ifdef FEAT_RELTIME
8812 if (list2proftime(&argvars[0], &tm) == OK)
8813 rettv->vval.v_float = profile_float(&tm);
8814# endif
8815}
8816#endif
8817
8818/*
8819 * "reltimestr()" function
8820 */
8821 static void
8822f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8823{
8824#ifdef FEAT_RELTIME
8825 proftime_T tm;
8826#endif
8827
8828 rettv->v_type = VAR_STRING;
8829 rettv->vval.v_string = NULL;
8830#ifdef FEAT_RELTIME
8831 if (list2proftime(&argvars[0], &tm) == OK)
8832 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8833#endif
8834}
8835
8836#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 static void
8838make_connection(void)
8839{
8840 if (X_DISPLAY == NULL
8841# ifdef FEAT_GUI
8842 && !gui.in_use
8843# endif
8844 )
8845 {
8846 x_force_connect = TRUE;
8847 setup_term_clip();
8848 x_force_connect = FALSE;
8849 }
8850}
8851
8852 static int
8853check_connection(void)
8854{
8855 make_connection();
8856 if (X_DISPLAY == NULL)
8857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008858 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 return FAIL;
8860 }
8861 return OK;
8862}
8863#endif
8864
8865#ifdef FEAT_CLIENTSERVER
8866 static void
8867remote_common(typval_T *argvars, typval_T *rettv, int expr)
8868{
8869 char_u *server_name;
8870 char_u *keys;
8871 char_u *r = NULL;
8872 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008873 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008874# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 HWND w;
8876# else
8877 Window w;
8878# endif
8879
8880 if (check_restricted() || check_secure())
8881 return;
8882
8883# ifdef FEAT_X11
8884 if (check_connection() == FAIL)
8885 return;
8886# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008887 if (argvars[2].v_type != VAR_UNKNOWN
8888 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008889 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008891 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 if (server_name == NULL)
8893 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008894 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008895# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008896 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008898 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8899 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900# endif
8901 {
8902 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008904 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008905 vim_free(r);
8906 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008908 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 return;
8910 }
8911
8912 rettv->vval.v_string = r;
8913
8914 if (argvars[2].v_type != VAR_UNKNOWN)
8915 {
8916 dictitem_T v;
8917 char_u str[30];
8918 char_u *idvar;
8919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008920 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008921 if (idvar != NULL && *idvar != NUL)
8922 {
8923 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8924 v.di_tv.v_type = VAR_STRING;
8925 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008927 vim_free(v.di_tv.vval.v_string);
8928 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 }
8930}
8931#endif
8932
8933/*
8934 * "remote_expr()" function
8935 */
8936 static void
8937f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8938{
8939 rettv->v_type = VAR_STRING;
8940 rettv->vval.v_string = NULL;
8941#ifdef FEAT_CLIENTSERVER
8942 remote_common(argvars, rettv, TRUE);
8943#endif
8944}
8945
8946/*
8947 * "remote_foreground()" function
8948 */
8949 static void
8950f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8951{
8952#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008953# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 /* On Win32 it's done in this application. */
8955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008956 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957
8958 if (server_name != NULL)
8959 serverForeground(server_name);
8960 }
8961# else
8962 /* Send a foreground() expression to the server. */
8963 argvars[1].v_type = VAR_STRING;
8964 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8965 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008966 rettv->v_type = VAR_STRING;
8967 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 remote_common(argvars, rettv, TRUE);
8969 vim_free(argvars[1].vval.v_string);
8970# endif
8971#endif
8972}
8973
8974 static void
8975f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8976{
8977#ifdef FEAT_CLIENTSERVER
8978 dictitem_T v;
8979 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008980# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 long_u n = 0;
8982# endif
8983 char_u *serverid;
8984
8985 if (check_restricted() || check_secure())
8986 {
8987 rettv->vval.v_number = -1;
8988 return;
8989 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008990 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 if (serverid == NULL)
8992 {
8993 rettv->vval.v_number = -1;
8994 return; /* type error; errmsg already given */
8995 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008996# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8998 if (n == 0)
8999 rettv->vval.v_number = -1;
9000 else
9001 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009002 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 rettv->vval.v_number = (s != NULL);
9004 }
9005# else
9006 if (check_connection() == FAIL)
9007 return;
9008
9009 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9010 serverStrToWin(serverid), &s);
9011# endif
9012
9013 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9014 {
9015 char_u *retvar;
9016
9017 v.di_tv.v_type = VAR_STRING;
9018 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009019 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (retvar != NULL)
9021 set_var(retvar, &v.di_tv, FALSE);
9022 vim_free(v.di_tv.vval.v_string);
9023 }
9024#else
9025 rettv->vval.v_number = -1;
9026#endif
9027}
9028
9029 static void
9030f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9031{
9032 char_u *r = NULL;
9033
9034#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009035 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036
9037 if (serverid != NULL && !check_restricted() && !check_secure())
9038 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009039 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009040# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009041 /* The server's HWND is encoded in the 'id' parameter */
9042 long_u n = 0;
9043# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009044
9045 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009047
Bram Moolenaar4f974752019-02-17 17:44:42 +01009048# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9050 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009051 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009052 if (r == NULL)
9053# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009054 if (check_connection() == FAIL
9055 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9056 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009058 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 }
9060#endif
9061 rettv->v_type = VAR_STRING;
9062 rettv->vval.v_string = r;
9063}
9064
9065/*
9066 * "remote_send()" function
9067 */
9068 static void
9069f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9070{
9071 rettv->v_type = VAR_STRING;
9072 rettv->vval.v_string = NULL;
9073#ifdef FEAT_CLIENTSERVER
9074 remote_common(argvars, rettv, FALSE);
9075#endif
9076}
9077
9078/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009079 * "remote_startserver()" function
9080 */
9081 static void
9082f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9083{
9084#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009086
9087 if (server == NULL)
9088 return; /* type error; errmsg already given */
9089 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009090 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009091 else
9092 {
9093# ifdef FEAT_X11
9094 if (check_connection() == OK)
9095 serverRegisterName(X_DISPLAY, server);
9096# else
9097 serverSetName(server);
9098# endif
9099 }
9100#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009101 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009102#endif
9103}
9104
9105/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 * "remove()" function
9107 */
9108 static void
9109f_remove(typval_T *argvars, typval_T *rettv)
9110{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9112
9113 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009114 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009115 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009116 blob_remove(argvars, rettv);
9117 else if (argvars[0].v_type == VAR_LIST)
9118 list_remove(argvars, rettv, arg_errmsg);
9119 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009120 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121}
9122
9123/*
9124 * "rename({from}, {to})" function
9125 */
9126 static void
9127f_rename(typval_T *argvars, typval_T *rettv)
9128{
9129 char_u buf[NUMBUFLEN];
9130
9131 if (check_restricted() || check_secure())
9132 rettv->vval.v_number = -1;
9133 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9135 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136}
9137
9138/*
9139 * "repeat()" function
9140 */
9141 static void
9142f_repeat(typval_T *argvars, typval_T *rettv)
9143{
9144 char_u *p;
9145 int n;
9146 int slen;
9147 int len;
9148 char_u *r;
9149 int i;
9150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009151 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152 if (argvars[0].v_type == VAR_LIST)
9153 {
9154 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9155 while (n-- > 0)
9156 if (list_extend(rettv->vval.v_list,
9157 argvars[0].vval.v_list, NULL) == FAIL)
9158 break;
9159 }
9160 else
9161 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009162 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 rettv->v_type = VAR_STRING;
9164 rettv->vval.v_string = NULL;
9165
9166 slen = (int)STRLEN(p);
9167 len = slen * n;
9168 if (len <= 0)
9169 return;
9170
9171 r = alloc(len + 1);
9172 if (r != NULL)
9173 {
9174 for (i = 0; i < n; i++)
9175 mch_memmove(r + i * slen, p, (size_t)slen);
9176 r[len] = NUL;
9177 }
9178
9179 rettv->vval.v_string = r;
9180 }
9181}
9182
9183/*
9184 * "resolve()" function
9185 */
9186 static void
9187f_resolve(typval_T *argvars, typval_T *rettv)
9188{
9189 char_u *p;
9190#ifdef HAVE_READLINK
9191 char_u *buf = NULL;
9192#endif
9193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009194 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195#ifdef FEAT_SHORTCUT
9196 {
9197 char_u *v = NULL;
9198
Bram Moolenaardce1e892019-02-10 23:18:53 +01009199 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 if (v != NULL)
9201 rettv->vval.v_string = v;
9202 else
9203 rettv->vval.v_string = vim_strsave(p);
9204 }
9205#else
9206# ifdef HAVE_READLINK
9207 {
9208 char_u *cpy;
9209 int len;
9210 char_u *remain = NULL;
9211 char_u *q;
9212 int is_relative_to_current = FALSE;
9213 int has_trailing_pathsep = FALSE;
9214 int limit = 100;
9215
9216 p = vim_strsave(p);
9217
9218 if (p[0] == '.' && (vim_ispathsep(p[1])
9219 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9220 is_relative_to_current = TRUE;
9221
9222 len = STRLEN(p);
9223 if (len > 0 && after_pathsep(p, p + len))
9224 {
9225 has_trailing_pathsep = TRUE;
9226 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9227 }
9228
9229 q = getnextcomp(p);
9230 if (*q != NUL)
9231 {
9232 /* Separate the first path component in "p", and keep the
9233 * remainder (beginning with the path separator). */
9234 remain = vim_strsave(q - 1);
9235 q[-1] = NUL;
9236 }
9237
9238 buf = alloc(MAXPATHL + 1);
9239 if (buf == NULL)
9240 goto fail;
9241
9242 for (;;)
9243 {
9244 for (;;)
9245 {
9246 len = readlink((char *)p, (char *)buf, MAXPATHL);
9247 if (len <= 0)
9248 break;
9249 buf[len] = NUL;
9250
9251 if (limit-- == 0)
9252 {
9253 vim_free(p);
9254 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009255 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 rettv->vval.v_string = NULL;
9257 goto fail;
9258 }
9259
9260 /* Ensure that the result will have a trailing path separator
9261 * if the argument has one. */
9262 if (remain == NULL && has_trailing_pathsep)
9263 add_pathsep(buf);
9264
9265 /* Separate the first path component in the link value and
9266 * concatenate the remainders. */
9267 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9268 if (*q != NUL)
9269 {
9270 if (remain == NULL)
9271 remain = vim_strsave(q - 1);
9272 else
9273 {
9274 cpy = concat_str(q - 1, remain);
9275 if (cpy != NULL)
9276 {
9277 vim_free(remain);
9278 remain = cpy;
9279 }
9280 }
9281 q[-1] = NUL;
9282 }
9283
9284 q = gettail(p);
9285 if (q > p && *q == NUL)
9286 {
9287 /* Ignore trailing path separator. */
9288 q[-1] = NUL;
9289 q = gettail(p);
9290 }
9291 if (q > p && !mch_isFullName(buf))
9292 {
9293 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009294 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 if (cpy != NULL)
9296 {
9297 STRCPY(cpy, p);
9298 STRCPY(gettail(cpy), buf);
9299 vim_free(p);
9300 p = cpy;
9301 }
9302 }
9303 else
9304 {
9305 vim_free(p);
9306 p = vim_strsave(buf);
9307 }
9308 }
9309
9310 if (remain == NULL)
9311 break;
9312
9313 /* Append the first path component of "remain" to "p". */
9314 q = getnextcomp(remain + 1);
9315 len = q - remain - (*q != NUL);
9316 cpy = vim_strnsave(p, STRLEN(p) + len);
9317 if (cpy != NULL)
9318 {
9319 STRNCAT(cpy, remain, len);
9320 vim_free(p);
9321 p = cpy;
9322 }
9323 /* Shorten "remain". */
9324 if (*q != NUL)
9325 STRMOVE(remain, q - 1);
9326 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009327 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 }
9329
9330 /* If the result is a relative path name, make it explicitly relative to
9331 * the current directory if and only if the argument had this form. */
9332 if (!vim_ispathsep(*p))
9333 {
9334 if (is_relative_to_current
9335 && *p != NUL
9336 && !(p[0] == '.'
9337 && (p[1] == NUL
9338 || vim_ispathsep(p[1])
9339 || (p[1] == '.'
9340 && (p[2] == NUL
9341 || vim_ispathsep(p[2]))))))
9342 {
9343 /* Prepend "./". */
9344 cpy = concat_str((char_u *)"./", p);
9345 if (cpy != NULL)
9346 {
9347 vim_free(p);
9348 p = cpy;
9349 }
9350 }
9351 else if (!is_relative_to_current)
9352 {
9353 /* Strip leading "./". */
9354 q = p;
9355 while (q[0] == '.' && vim_ispathsep(q[1]))
9356 q += 2;
9357 if (q > p)
9358 STRMOVE(p, p + 2);
9359 }
9360 }
9361
9362 /* Ensure that the result will have no trailing path separator
9363 * if the argument had none. But keep "/" or "//". */
9364 if (!has_trailing_pathsep)
9365 {
9366 q = p + STRLEN(p);
9367 if (after_pathsep(p, q))
9368 *gettail_sep(p) = NUL;
9369 }
9370
9371 rettv->vval.v_string = p;
9372 }
9373# else
9374 rettv->vval.v_string = vim_strsave(p);
9375# endif
9376#endif
9377
9378 simplify_filename(rettv->vval.v_string);
9379
9380#ifdef HAVE_READLINK
9381fail:
9382 vim_free(buf);
9383#endif
9384 rettv->v_type = VAR_STRING;
9385}
9386
9387/*
9388 * "reverse({list})" function
9389 */
9390 static void
9391f_reverse(typval_T *argvars, typval_T *rettv)
9392{
9393 list_T *l;
9394 listitem_T *li, *ni;
9395
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009396 if (argvars[0].v_type == VAR_BLOB)
9397 {
9398 blob_T *b = argvars[0].vval.v_blob;
9399 int i, len = blob_len(b);
9400
9401 for (i = 0; i < len / 2; i++)
9402 {
9403 int tmp = blob_get(b, i);
9404
9405 blob_set(b, i, blob_get(b, len - i - 1));
9406 blob_set(b, len - i - 1, tmp);
9407 }
9408 rettv_blob_set(rettv, b);
9409 return;
9410 }
9411
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009413 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009415 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009416 (char_u *)N_("reverse() argument"), TRUE))
9417 {
9418 li = l->lv_last;
9419 l->lv_first = l->lv_last = NULL;
9420 l->lv_len = 0;
9421 while (li != NULL)
9422 {
9423 ni = li->li_prev;
9424 list_append(l, li);
9425 li = ni;
9426 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009427 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428 l->lv_idx = l->lv_len - l->lv_idx - 1;
9429 }
9430}
9431
9432#define SP_NOMOVE 0x01 /* don't move cursor */
9433#define SP_REPEAT 0x02 /* repeat to find outer pair */
9434#define SP_RETCOUNT 0x04 /* return matchcount */
9435#define SP_SETPCMARK 0x08 /* set previous context mark */
9436#define SP_START 0x10 /* accept match at start position */
9437#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9438#define SP_END 0x40 /* leave cursor at end of match */
9439#define SP_COLUMN 0x80 /* start at cursor column */
9440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441/*
9442 * Get flags for a search function.
9443 * Possibly sets "p_ws".
9444 * Returns BACKWARD, FORWARD or zero (for an error).
9445 */
9446 static int
9447get_search_arg(typval_T *varp, int *flagsp)
9448{
9449 int dir = FORWARD;
9450 char_u *flags;
9451 char_u nbuf[NUMBUFLEN];
9452 int mask;
9453
9454 if (varp->v_type != VAR_UNKNOWN)
9455 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009456 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457 if (flags == NULL)
9458 return 0; /* type error; errmsg already given */
9459 while (*flags != NUL)
9460 {
9461 switch (*flags)
9462 {
9463 case 'b': dir = BACKWARD; break;
9464 case 'w': p_ws = TRUE; break;
9465 case 'W': p_ws = FALSE; break;
9466 default: mask = 0;
9467 if (flagsp != NULL)
9468 switch (*flags)
9469 {
9470 case 'c': mask = SP_START; break;
9471 case 'e': mask = SP_END; break;
9472 case 'm': mask = SP_RETCOUNT; break;
9473 case 'n': mask = SP_NOMOVE; break;
9474 case 'p': mask = SP_SUBPAT; break;
9475 case 'r': mask = SP_REPEAT; break;
9476 case 's': mask = SP_SETPCMARK; break;
9477 case 'z': mask = SP_COLUMN; break;
9478 }
9479 if (mask == 0)
9480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009481 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 dir = 0;
9483 }
9484 else
9485 *flagsp |= mask;
9486 }
9487 if (dir == 0)
9488 break;
9489 ++flags;
9490 }
9491 }
9492 return dir;
9493}
9494
9495/*
9496 * Shared by search() and searchpos() functions.
9497 */
9498 static int
9499search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9500{
9501 int flags;
9502 char_u *pat;
9503 pos_T pos;
9504 pos_T save_cursor;
9505 int save_p_ws = p_ws;
9506 int dir;
9507 int retval = 0; /* default: FAIL */
9508 long lnum_stop = 0;
9509 proftime_T tm;
9510#ifdef FEAT_RELTIME
9511 long time_limit = 0;
9512#endif
9513 int options = SEARCH_KEEP;
9514 int subpatnum;
9515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009516 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009517 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9518 if (dir == 0)
9519 goto theend;
9520 flags = *flagsp;
9521 if (flags & SP_START)
9522 options |= SEARCH_START;
9523 if (flags & SP_END)
9524 options |= SEARCH_END;
9525 if (flags & SP_COLUMN)
9526 options |= SEARCH_COL;
9527
9528 /* Optional arguments: line number to stop searching and timeout. */
9529 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009531 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 if (lnum_stop < 0)
9533 goto theend;
9534#ifdef FEAT_RELTIME
9535 if (argvars[3].v_type != VAR_UNKNOWN)
9536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009537 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 if (time_limit < 0)
9539 goto theend;
9540 }
9541#endif
9542 }
9543
9544#ifdef FEAT_RELTIME
9545 /* Set the time limit, if there is one. */
9546 profile_setlimit(time_limit, &tm);
9547#endif
9548
9549 /*
9550 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9551 * Check to make sure only those flags are set.
9552 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9553 * flags cannot be set. Check for that condition also.
9554 */
9555 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9556 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009558 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559 goto theend;
9560 }
9561
9562 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009563 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009564 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 if (subpatnum != FAIL)
9566 {
9567 if (flags & SP_SUBPAT)
9568 retval = subpatnum;
9569 else
9570 retval = pos.lnum;
9571 if (flags & SP_SETPCMARK)
9572 setpcmark();
9573 curwin->w_cursor = pos;
9574 if (match_pos != NULL)
9575 {
9576 /* Store the match cursor position */
9577 match_pos->lnum = pos.lnum;
9578 match_pos->col = pos.col + 1;
9579 }
9580 /* "/$" will put the cursor after the end of the line, may need to
9581 * correct that here */
9582 check_cursor();
9583 }
9584
9585 /* If 'n' flag is used: restore cursor position. */
9586 if (flags & SP_NOMOVE)
9587 curwin->w_cursor = save_cursor;
9588 else
9589 curwin->w_set_curswant = TRUE;
9590theend:
9591 p_ws = save_p_ws;
9592
9593 return retval;
9594}
9595
9596#ifdef FEAT_FLOAT
9597
9598/*
9599 * round() is not in C90, use ceil() or floor() instead.
9600 */
9601 float_T
9602vim_round(float_T f)
9603{
9604 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9605}
9606
9607/*
9608 * "round({float})" function
9609 */
9610 static void
9611f_round(typval_T *argvars, typval_T *rettv)
9612{
9613 float_T f = 0.0;
9614
9615 rettv->v_type = VAR_FLOAT;
9616 if (get_float_arg(argvars, &f) == OK)
9617 rettv->vval.v_float = vim_round(f);
9618 else
9619 rettv->vval.v_float = 0.0;
9620}
9621#endif
9622
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009623#ifdef FEAT_RUBY
9624/*
9625 * "rubyeval()" function
9626 */
9627 static void
9628f_rubyeval(typval_T *argvars, typval_T *rettv)
9629{
9630 char_u *str;
9631 char_u buf[NUMBUFLEN];
9632
9633 str = tv_get_string_buf(&argvars[0], buf);
9634 do_rubyeval(str, rettv);
9635}
9636#endif
9637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638/*
9639 * "screenattr()" function
9640 */
9641 static void
9642f_screenattr(typval_T *argvars, typval_T *rettv)
9643{
9644 int row;
9645 int col;
9646 int c;
9647
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009648 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9649 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 if (row < 0 || row >= screen_Rows
9651 || col < 0 || col >= screen_Columns)
9652 c = -1;
9653 else
9654 c = ScreenAttrs[LineOffset[row] + col];
9655 rettv->vval.v_number = c;
9656}
9657
9658/*
9659 * "screenchar()" function
9660 */
9661 static void
9662f_screenchar(typval_T *argvars, typval_T *rettv)
9663{
9664 int row;
9665 int col;
9666 int off;
9667 int c;
9668
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009669 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9670 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009671 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672 c = -1;
9673 else
9674 {
9675 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676 if (enc_utf8 && ScreenLinesUC[off] != 0)
9677 c = ScreenLinesUC[off];
9678 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 c = ScreenLines[off];
9680 }
9681 rettv->vval.v_number = c;
9682}
9683
9684/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009685 * "screenchars()" function
9686 */
9687 static void
9688f_screenchars(typval_T *argvars, typval_T *rettv)
9689{
9690 int row;
9691 int col;
9692 int off;
9693 int c;
9694 int i;
9695
9696 if (rettv_list_alloc(rettv) == FAIL)
9697 return;
9698 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9699 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9700 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9701 return;
9702
9703 off = LineOffset[row] + col;
9704 if (enc_utf8 && ScreenLinesUC[off] != 0)
9705 c = ScreenLinesUC[off];
9706 else
9707 c = ScreenLines[off];
9708 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9709
9710 if (enc_utf8)
9711
9712 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9713 list_append_number(rettv->vval.v_list,
9714 (varnumber_T)ScreenLinesC[i][off]);
9715}
9716
9717/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718 * "screencol()" function
9719 *
9720 * First column is 1 to be consistent with virtcol().
9721 */
9722 static void
9723f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9724{
9725 rettv->vval.v_number = screen_screencol() + 1;
9726}
9727
9728/*
9729 * "screenrow()" function
9730 */
9731 static void
9732f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9733{
9734 rettv->vval.v_number = screen_screenrow() + 1;
9735}
9736
9737/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009738 * "screenstring()" function
9739 */
9740 static void
9741f_screenstring(typval_T *argvars, typval_T *rettv)
9742{
9743 int row;
9744 int col;
9745 int off;
9746 int c;
9747 int i;
9748 char_u buf[MB_MAXBYTES + 1];
9749 int buflen = 0;
9750
9751 rettv->vval.v_string = NULL;
9752 rettv->v_type = VAR_STRING;
9753
9754 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9755 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9756 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9757 return;
9758
9759 off = LineOffset[row] + col;
9760 if (enc_utf8 && ScreenLinesUC[off] != 0)
9761 c = ScreenLinesUC[off];
9762 else
9763 c = ScreenLines[off];
9764 buflen += mb_char2bytes(c, buf);
9765
9766 if (enc_utf8)
9767 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9768 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9769
9770 buf[buflen] = NUL;
9771 rettv->vval.v_string = vim_strsave(buf);
9772}
9773
9774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775 * "search()" function
9776 */
9777 static void
9778f_search(typval_T *argvars, typval_T *rettv)
9779{
9780 int flags = 0;
9781
9782 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9783}
9784
9785/*
9786 * "searchdecl()" function
9787 */
9788 static void
9789f_searchdecl(typval_T *argvars, typval_T *rettv)
9790{
9791 int locally = 1;
9792 int thisblock = 0;
9793 int error = FALSE;
9794 char_u *name;
9795
9796 rettv->vval.v_number = 1; /* default: FAIL */
9797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009798 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 if (argvars[1].v_type != VAR_UNKNOWN)
9800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009801 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009803 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 }
9805 if (!error && name != NULL)
9806 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9807 locally, thisblock, SEARCH_KEEP) == FAIL;
9808}
9809
9810/*
9811 * Used by searchpair() and searchpairpos()
9812 */
9813 static int
9814searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9815{
9816 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009817 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 int save_p_ws = p_ws;
9819 int dir;
9820 int flags = 0;
9821 char_u nbuf1[NUMBUFLEN];
9822 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 int retval = 0; /* default: FAIL */
9824 long lnum_stop = 0;
9825 long time_limit = 0;
9826
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009827 /* Get the three pattern arguments: start, middle, end. Will result in an
9828 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009829 spat = tv_get_string_chk(&argvars[0]);
9830 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9831 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 if (spat == NULL || mpat == NULL || epat == NULL)
9833 goto theend; /* type error */
9834
9835 /* Handle the optional fourth argument: flags */
9836 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9837 if (dir == 0)
9838 goto theend;
9839
9840 /* Don't accept SP_END or SP_SUBPAT.
9841 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9842 */
9843 if ((flags & (SP_END | SP_SUBPAT)) != 0
9844 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009846 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 goto theend;
9848 }
9849
9850 /* Using 'r' implies 'W', otherwise it doesn't work. */
9851 if (flags & SP_REPEAT)
9852 p_ws = FALSE;
9853
9854 /* Optional fifth argument: skip expression */
9855 if (argvars[3].v_type == VAR_UNKNOWN
9856 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009857 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 else
9859 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009860 skip = &argvars[4];
9861 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9862 && skip->v_type != VAR_STRING)
9863 {
9864 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009865 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009866 goto theend;
9867 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 if (argvars[5].v_type != VAR_UNKNOWN)
9869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009870 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009873 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009875 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876#ifdef FEAT_RELTIME
9877 if (argvars[6].v_type != VAR_UNKNOWN)
9878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009882 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 }
9886#endif
9887 }
9888 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889
9890 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9891 match_pos, lnum_stop, time_limit);
9892
9893theend:
9894 p_ws = save_p_ws;
9895
9896 return retval;
9897}
9898
9899/*
9900 * "searchpair()" function
9901 */
9902 static void
9903f_searchpair(typval_T *argvars, typval_T *rettv)
9904{
9905 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9906}
9907
9908/*
9909 * "searchpairpos()" function
9910 */
9911 static void
9912f_searchpairpos(typval_T *argvars, typval_T *rettv)
9913{
9914 pos_T match_pos;
9915 int lnum = 0;
9916 int col = 0;
9917
9918 if (rettv_list_alloc(rettv) == FAIL)
9919 return;
9920
9921 if (searchpair_cmn(argvars, &match_pos) > 0)
9922 {
9923 lnum = match_pos.lnum;
9924 col = match_pos.col;
9925 }
9926
9927 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9928 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9929}
9930
9931/*
9932 * Search for a start/middle/end thing.
9933 * Used by searchpair(), see its documentation for the details.
9934 * Returns 0 or -1 for no match,
9935 */
9936 long
9937do_searchpair(
9938 char_u *spat, /* start pattern */
9939 char_u *mpat, /* middle pattern */
9940 char_u *epat, /* end pattern */
9941 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009942 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 int flags, /* SP_SETPCMARK and other SP_ values */
9944 pos_T *match_pos,
9945 linenr_T lnum_stop, /* stop at this line if not zero */
9946 long time_limit UNUSED) /* stop after this many msec */
9947{
9948 char_u *save_cpo;
9949 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9950 long retval = 0;
9951 pos_T pos;
9952 pos_T firstpos;
9953 pos_T foundpos;
9954 pos_T save_cursor;
9955 pos_T save_pos;
9956 int n;
9957 int r;
9958 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009959 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 int err;
9961 int options = SEARCH_KEEP;
9962 proftime_T tm;
9963
9964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9965 save_cpo = p_cpo;
9966 p_cpo = empty_option;
9967
9968#ifdef FEAT_RELTIME
9969 /* Set the time limit, if there is one. */
9970 profile_setlimit(time_limit, &tm);
9971#endif
9972
9973 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9974 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009975 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9976 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009977 if (pat2 == NULL || pat3 == NULL)
9978 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009979 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 if (*mpat == NUL)
9981 STRCPY(pat3, pat2);
9982 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009983 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009984 spat, epat, mpat);
9985 if (flags & SP_START)
9986 options |= SEARCH_START;
9987
Bram Moolenaar48570482017-10-30 21:48:41 +01009988 if (skip != NULL)
9989 {
9990 /* Empty string means to not use the skip expression. */
9991 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9992 use_skip = skip->vval.v_string != NULL
9993 && *skip->vval.v_string != NUL;
9994 }
9995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 save_cursor = curwin->w_cursor;
9997 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009998 CLEAR_POS(&firstpos);
9999 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010000 pat = pat3;
10001 for (;;)
10002 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010003 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010004 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010005 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 /* didn't find it or found the first match again: FAIL */
10007 break;
10008
10009 if (firstpos.lnum == 0)
10010 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010011 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 {
10013 /* Found the same position again. Can happen with a pattern that
10014 * has "\zs" at the end and searching backwards. Advance one
10015 * character and try again. */
10016 if (dir == BACKWARD)
10017 decl(&pos);
10018 else
10019 incl(&pos);
10020 }
10021 foundpos = pos;
10022
10023 /* clear the start flag to avoid getting stuck here */
10024 options &= ~SEARCH_START;
10025
10026 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010027 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028 {
10029 save_pos = curwin->w_cursor;
10030 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010031 err = FALSE;
10032 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010033 curwin->w_cursor = save_pos;
10034 if (err)
10035 {
10036 /* Evaluating {skip} caused an error, break here. */
10037 curwin->w_cursor = save_cursor;
10038 retval = -1;
10039 break;
10040 }
10041 if (r)
10042 continue;
10043 }
10044
10045 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10046 {
10047 /* Found end when searching backwards or start when searching
10048 * forward: nested pair. */
10049 ++nest;
10050 pat = pat2; /* nested, don't search for middle */
10051 }
10052 else
10053 {
10054 /* Found end when searching forward or start when searching
10055 * backward: end of (nested) pair; or found middle in outer pair. */
10056 if (--nest == 1)
10057 pat = pat3; /* outer level, search for middle */
10058 }
10059
10060 if (nest == 0)
10061 {
10062 /* Found the match: return matchcount or line number. */
10063 if (flags & SP_RETCOUNT)
10064 ++retval;
10065 else
10066 retval = pos.lnum;
10067 if (flags & SP_SETPCMARK)
10068 setpcmark();
10069 curwin->w_cursor = pos;
10070 if (!(flags & SP_REPEAT))
10071 break;
10072 nest = 1; /* search for next unmatched */
10073 }
10074 }
10075
10076 if (match_pos != NULL)
10077 {
10078 /* Store the match cursor position */
10079 match_pos->lnum = curwin->w_cursor.lnum;
10080 match_pos->col = curwin->w_cursor.col + 1;
10081 }
10082
10083 /* If 'n' flag is used or search failed: restore cursor position. */
10084 if ((flags & SP_NOMOVE) || retval == 0)
10085 curwin->w_cursor = save_cursor;
10086
10087theend:
10088 vim_free(pat2);
10089 vim_free(pat3);
10090 if (p_cpo == empty_option)
10091 p_cpo = save_cpo;
10092 else
10093 /* Darn, evaluating the {skip} expression changed the value. */
10094 free_string_option(save_cpo);
10095
10096 return retval;
10097}
10098
10099/*
10100 * "searchpos()" function
10101 */
10102 static void
10103f_searchpos(typval_T *argvars, typval_T *rettv)
10104{
10105 pos_T match_pos;
10106 int lnum = 0;
10107 int col = 0;
10108 int n;
10109 int flags = 0;
10110
10111 if (rettv_list_alloc(rettv) == FAIL)
10112 return;
10113
10114 n = search_cmn(argvars, &match_pos, &flags);
10115 if (n > 0)
10116 {
10117 lnum = match_pos.lnum;
10118 col = match_pos.col;
10119 }
10120
10121 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10122 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10123 if (flags & SP_SUBPAT)
10124 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10125}
10126
10127 static void
10128f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10129{
10130#ifdef FEAT_CLIENTSERVER
10131 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010132 char_u *server = tv_get_string_chk(&argvars[0]);
10133 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010134
10135 rettv->vval.v_number = -1;
10136 if (server == NULL || reply == NULL)
10137 return;
10138 if (check_restricted() || check_secure())
10139 return;
10140# ifdef FEAT_X11
10141 if (check_connection() == FAIL)
10142 return;
10143# endif
10144
10145 if (serverSendReply(server, reply) < 0)
10146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010147 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010148 return;
10149 }
10150 rettv->vval.v_number = 0;
10151#else
10152 rettv->vval.v_number = -1;
10153#endif
10154}
10155
10156 static void
10157f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10158{
10159 char_u *r = NULL;
10160
10161#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010162# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 r = serverGetVimNames();
10164# else
10165 make_connection();
10166 if (X_DISPLAY != NULL)
10167 r = serverGetVimNames(X_DISPLAY);
10168# endif
10169#endif
10170 rettv->v_type = VAR_STRING;
10171 rettv->vval.v_string = r;
10172}
10173
10174/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010175 * "setbufline()" function
10176 */
10177 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010178f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010179{
10180 linenr_T lnum;
10181 buf_T *buf;
10182
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010183 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010184 if (buf == NULL)
10185 rettv->vval.v_number = 1; /* FAIL */
10186 else
10187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010188 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010189 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010190 }
10191}
10192
10193/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010194 * "setbufvar()" function
10195 */
10196 static void
10197f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10198{
10199 buf_T *buf;
10200 char_u *varname, *bufvarname;
10201 typval_T *varp;
10202 char_u nbuf[NUMBUFLEN];
10203
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010204 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010206 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10207 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010208 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 varp = &argvars[2];
10210
10211 if (buf != NULL && varname != NULL && varp != NULL)
10212 {
10213 if (*varname == '&')
10214 {
10215 long numval;
10216 char_u *strval;
10217 int error = FALSE;
10218 aco_save_T aco;
10219
10220 /* set curbuf to be our buf, temporarily */
10221 aucmd_prepbuf(&aco, buf);
10222
10223 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010224 numval = (long)tv_get_number_chk(varp, &error);
10225 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010226 if (!error && strval != NULL)
10227 set_option_value(varname, numval, strval, OPT_LOCAL);
10228
10229 /* reset notion of buffer */
10230 aucmd_restbuf(&aco);
10231 }
10232 else
10233 {
10234 buf_T *save_curbuf = curbuf;
10235
Bram Moolenaar964b3742019-05-24 18:54:09 +020010236 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237 if (bufvarname != NULL)
10238 {
10239 curbuf = buf;
10240 STRCPY(bufvarname, "b:");
10241 STRCPY(bufvarname + 2, varname);
10242 set_var(bufvarname, varp, TRUE);
10243 vim_free(bufvarname);
10244 curbuf = save_curbuf;
10245 }
10246 }
10247 }
10248}
10249
10250 static void
10251f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10252{
10253 dict_T *d;
10254 dictitem_T *di;
10255 char_u *csearch;
10256
10257 if (argvars[0].v_type != VAR_DICT)
10258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010259 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 return;
10261 }
10262
10263 if ((d = argvars[0].vval.v_dict) != NULL)
10264 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010265 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 if (csearch != NULL)
10267 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 if (enc_utf8)
10269 {
10270 int pcc[MAX_MCO];
10271 int c = utfc_ptr2char(csearch, pcc);
10272
10273 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10274 }
10275 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 set_last_csearch(PTR2CHAR(csearch),
10277 csearch, MB_PTR2LEN(csearch));
10278 }
10279
10280 di = dict_find(d, (char_u *)"forward", -1);
10281 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010282 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 ? FORWARD : BACKWARD);
10284
10285 di = dict_find(d, (char_u *)"until", -1);
10286 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010287 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 }
10289}
10290
10291/*
10292 * "setcmdpos()" function
10293 */
10294 static void
10295f_setcmdpos(typval_T *argvars, typval_T *rettv)
10296{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010297 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010298
10299 if (pos >= 0)
10300 rettv->vval.v_number = set_cmdline_pos(pos);
10301}
10302
10303/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010304 * "setenv()" function
10305 */
10306 static void
10307f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10308{
10309 char_u namebuf[NUMBUFLEN];
10310 char_u valbuf[NUMBUFLEN];
10311 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10312
10313 if (argvars[1].v_type == VAR_SPECIAL
10314 && argvars[1].vval.v_number == VVAL_NULL)
10315 vim_unsetenv(name);
10316 else
10317 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10318}
10319
10320/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321 * "setfperm({fname}, {mode})" function
10322 */
10323 static void
10324f_setfperm(typval_T *argvars, typval_T *rettv)
10325{
10326 char_u *fname;
10327 char_u modebuf[NUMBUFLEN];
10328 char_u *mode_str;
10329 int i;
10330 int mask;
10331 int mode = 0;
10332
10333 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010334 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010335 if (fname == NULL)
10336 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010337 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010338 if (mode_str == NULL)
10339 return;
10340 if (STRLEN(mode_str) != 9)
10341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010342 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010343 return;
10344 }
10345
10346 mask = 1;
10347 for (i = 8; i >= 0; --i)
10348 {
10349 if (mode_str[i] != '-')
10350 mode |= mask;
10351 mask = mask << 1;
10352 }
10353 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10354}
10355
10356/*
10357 * "setline()" function
10358 */
10359 static void
10360f_setline(typval_T *argvars, typval_T *rettv)
10361{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010362 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010363
Bram Moolenaarca851592018-06-06 21:04:07 +020010364 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010365}
10366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010367/*
10368 * Used by "setqflist()" and "setloclist()" functions
10369 */
10370 static void
10371set_qf_ll_list(
10372 win_T *wp UNUSED,
10373 typval_T *list_arg UNUSED,
10374 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010375 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 typval_T *rettv)
10377{
10378#ifdef FEAT_QUICKFIX
10379 static char *e_invact = N_("E927: Invalid action: '%s'");
10380 char_u *act;
10381 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010382 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010383#endif
10384
10385 rettv->vval.v_number = -1;
10386
10387#ifdef FEAT_QUICKFIX
10388 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010389 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010390 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010391 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 else
10393 {
10394 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010395 dict_T *d = NULL;
10396 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397
10398 if (action_arg->v_type == VAR_STRING)
10399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010400 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010401 if (act == NULL)
10402 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010403 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10404 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 action = *act;
10406 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010407 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 }
10409 else if (action_arg->v_type == VAR_UNKNOWN)
10410 action = ' ';
10411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010412 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413
Bram Moolenaard823fa92016-08-12 16:29:27 +020010414 if (action_arg->v_type != VAR_UNKNOWN
10415 && what_arg->v_type != VAR_UNKNOWN)
10416 {
10417 if (what_arg->v_type == VAR_DICT)
10418 d = what_arg->vval.v_dict;
10419 else
10420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010421 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010422 valid_dict = FALSE;
10423 }
10424 }
10425
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010426 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010427 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010428 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10429 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010431 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 }
10433#endif
10434}
10435
10436/*
10437 * "setloclist()" function
10438 */
10439 static void
10440f_setloclist(typval_T *argvars, typval_T *rettv)
10441{
10442 win_T *win;
10443
10444 rettv->vval.v_number = -1;
10445
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010446 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010447 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010448 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010449}
10450
10451/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010452 * "setpos()" function
10453 */
10454 static void
10455f_setpos(typval_T *argvars, typval_T *rettv)
10456{
10457 pos_T pos;
10458 int fnum;
10459 char_u *name;
10460 colnr_T curswant = -1;
10461
10462 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010463 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010464 if (name != NULL)
10465 {
10466 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10467 {
10468 if (--pos.col < 0)
10469 pos.col = 0;
10470 if (name[0] == '.' && name[1] == NUL)
10471 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010472 /* set cursor; "fnum" is ignored */
10473 curwin->w_cursor = pos;
10474 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010475 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010476 curwin->w_curswant = curswant - 1;
10477 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010478 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010479 check_cursor();
10480 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010481 }
10482 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10483 {
10484 /* set mark */
10485 if (setmark_pos(name[1], &pos, fnum) == OK)
10486 rettv->vval.v_number = 0;
10487 }
10488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010489 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010490 }
10491 }
10492}
10493
10494/*
10495 * "setqflist()" function
10496 */
10497 static void
10498f_setqflist(typval_T *argvars, typval_T *rettv)
10499{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010500 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501}
10502
10503/*
10504 * "setreg()" function
10505 */
10506 static void
10507f_setreg(typval_T *argvars, typval_T *rettv)
10508{
10509 int regname;
10510 char_u *strregname;
10511 char_u *stropt;
10512 char_u *strval;
10513 int append;
10514 char_u yank_type;
10515 long block_len;
10516
10517 block_len = -1;
10518 yank_type = MAUTO;
10519 append = FALSE;
10520
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010521 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522 rettv->vval.v_number = 1; /* FAIL is default */
10523
10524 if (strregname == NULL)
10525 return; /* type error; errmsg already given */
10526 regname = *strregname;
10527 if (regname == 0 || regname == '@')
10528 regname = '"';
10529
10530 if (argvars[2].v_type != VAR_UNKNOWN)
10531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010532 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010533 if (stropt == NULL)
10534 return; /* type error */
10535 for (; *stropt != NUL; ++stropt)
10536 switch (*stropt)
10537 {
10538 case 'a': case 'A': /* append */
10539 append = TRUE;
10540 break;
10541 case 'v': case 'c': /* character-wise selection */
10542 yank_type = MCHAR;
10543 break;
10544 case 'V': case 'l': /* line-wise selection */
10545 yank_type = MLINE;
10546 break;
10547 case 'b': case Ctrl_V: /* block-wise selection */
10548 yank_type = MBLOCK;
10549 if (VIM_ISDIGIT(stropt[1]))
10550 {
10551 ++stropt;
10552 block_len = getdigits(&stropt) - 1;
10553 --stropt;
10554 }
10555 break;
10556 }
10557 }
10558
10559 if (argvars[1].v_type == VAR_LIST)
10560 {
10561 char_u **lstval;
10562 char_u **allocval;
10563 char_u buf[NUMBUFLEN];
10564 char_u **curval;
10565 char_u **curallocval;
10566 list_T *ll = argvars[1].vval.v_list;
10567 listitem_T *li;
10568 int len;
10569
10570 /* If the list is NULL handle like an empty list. */
10571 len = ll == NULL ? 0 : ll->lv_len;
10572
10573 /* First half: use for pointers to result lines; second half: use for
10574 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010575 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 if (lstval == NULL)
10577 return;
10578 curval = lstval;
10579 allocval = lstval + len + 2;
10580 curallocval = allocval;
10581
10582 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10583 li = li->li_next)
10584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010585 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 if (strval == NULL)
10587 goto free_lstval;
10588 if (strval == buf)
10589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010590 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010591 * overwrite the string. */
10592 strval = vim_strsave(buf);
10593 if (strval == NULL)
10594 goto free_lstval;
10595 *curallocval++ = strval;
10596 }
10597 *curval++ = strval;
10598 }
10599 *curval++ = NULL;
10600
10601 write_reg_contents_lst(regname, lstval, -1,
10602 append, yank_type, block_len);
10603free_lstval:
10604 while (curallocval > allocval)
10605 vim_free(*--curallocval);
10606 vim_free(lstval);
10607 }
10608 else
10609 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010610 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 if (strval == NULL)
10612 return;
10613 write_reg_contents_ex(regname, strval, -1,
10614 append, yank_type, block_len);
10615 }
10616 rettv->vval.v_number = 0;
10617}
10618
10619/*
10620 * "settabvar()" function
10621 */
10622 static void
10623f_settabvar(typval_T *argvars, typval_T *rettv)
10624{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010625 tabpage_T *save_curtab;
10626 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010627 char_u *varname, *tabvarname;
10628 typval_T *varp;
10629
10630 rettv->vval.v_number = 0;
10631
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010632 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 return;
10634
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010635 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10636 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 varp = &argvars[2];
10638
Bram Moolenaar4033c552017-09-16 20:54:51 +020010639 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010641 save_curtab = curtab;
10642 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010643
Bram Moolenaar964b3742019-05-24 18:54:09 +020010644 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010645 if (tabvarname != NULL)
10646 {
10647 STRCPY(tabvarname, "t:");
10648 STRCPY(tabvarname + 2, varname);
10649 set_var(tabvarname, varp, TRUE);
10650 vim_free(tabvarname);
10651 }
10652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010653 /* Restore current tabpage */
10654 if (valid_tabpage(save_curtab))
10655 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 }
10657}
10658
10659/*
10660 * "settabwinvar()" function
10661 */
10662 static void
10663f_settabwinvar(typval_T *argvars, typval_T *rettv)
10664{
10665 setwinvar(argvars, rettv, 1);
10666}
10667
10668/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010669 * "settagstack()" function
10670 */
10671 static void
10672f_settagstack(typval_T *argvars, typval_T *rettv)
10673{
10674 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10675 win_T *wp;
10676 dict_T *d;
10677 int action = 'r';
10678
10679 rettv->vval.v_number = -1;
10680
10681 // first argument: window number or id
10682 wp = find_win_by_nr_or_id(&argvars[0]);
10683 if (wp == NULL)
10684 return;
10685
10686 // second argument: dict with items to set in the tag stack
10687 if (argvars[1].v_type != VAR_DICT)
10688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010689 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010690 return;
10691 }
10692 d = argvars[1].vval.v_dict;
10693 if (d == NULL)
10694 return;
10695
10696 // third argument: action - 'a' for append and 'r' for replace.
10697 // default is to replace the stack.
10698 if (argvars[2].v_type == VAR_UNKNOWN)
10699 action = 'r';
10700 else if (argvars[2].v_type == VAR_STRING)
10701 {
10702 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010703 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010704 if (actstr == NULL)
10705 return;
10706 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10707 action = *actstr;
10708 else
10709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010710 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010711 return;
10712 }
10713 }
10714 else
10715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010716 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010717 return;
10718 }
10719
10720 if (set_tagstack(wp, d, action) == OK)
10721 rettv->vval.v_number = 0;
10722}
10723
10724/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 * "setwinvar()" function
10726 */
10727 static void
10728f_setwinvar(typval_T *argvars, typval_T *rettv)
10729{
10730 setwinvar(argvars, rettv, 0);
10731}
10732
10733#ifdef FEAT_CRYPT
10734/*
10735 * "sha256({string})" function
10736 */
10737 static void
10738f_sha256(typval_T *argvars, typval_T *rettv)
10739{
10740 char_u *p;
10741
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010742 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 rettv->vval.v_string = vim_strsave(
10744 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10745 rettv->v_type = VAR_STRING;
10746}
10747#endif /* FEAT_CRYPT */
10748
10749/*
10750 * "shellescape({string})" function
10751 */
10752 static void
10753f_shellescape(typval_T *argvars, typval_T *rettv)
10754{
Bram Moolenaar20615522017-06-05 18:46:26 +020010755 int do_special = non_zero_arg(&argvars[1]);
10756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010758 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 rettv->v_type = VAR_STRING;
10760}
10761
10762/*
10763 * shiftwidth() function
10764 */
10765 static void
10766f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10767{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010768 rettv->vval.v_number = 0;
10769
10770 if (argvars[0].v_type != VAR_UNKNOWN)
10771 {
10772 long col;
10773
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010774 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010775 if (col < 0)
10776 return; // type error; errmsg already given
10777#ifdef FEAT_VARTABS
10778 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10779 return;
10780#endif
10781 }
10782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 rettv->vval.v_number = get_sw_value(curbuf);
10784}
10785
10786/*
10787 * "simplify()" function
10788 */
10789 static void
10790f_simplify(typval_T *argvars, typval_T *rettv)
10791{
10792 char_u *p;
10793
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010794 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 rettv->vval.v_string = vim_strsave(p);
10796 simplify_filename(rettv->vval.v_string); /* simplify in place */
10797 rettv->v_type = VAR_STRING;
10798}
10799
10800#ifdef FEAT_FLOAT
10801/*
10802 * "sin()" function
10803 */
10804 static void
10805f_sin(typval_T *argvars, typval_T *rettv)
10806{
10807 float_T f = 0.0;
10808
10809 rettv->v_type = VAR_FLOAT;
10810 if (get_float_arg(argvars, &f) == OK)
10811 rettv->vval.v_float = sin(f);
10812 else
10813 rettv->vval.v_float = 0.0;
10814}
10815
10816/*
10817 * "sinh()" function
10818 */
10819 static void
10820f_sinh(typval_T *argvars, typval_T *rettv)
10821{
10822 float_T f = 0.0;
10823
10824 rettv->v_type = VAR_FLOAT;
10825 if (get_float_arg(argvars, &f) == OK)
10826 rettv->vval.v_float = sinh(f);
10827 else
10828 rettv->vval.v_float = 0.0;
10829}
10830#endif
10831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010832/*
10833 * "soundfold({word})" function
10834 */
10835 static void
10836f_soundfold(typval_T *argvars, typval_T *rettv)
10837{
10838 char_u *s;
10839
10840 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010841 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842#ifdef FEAT_SPELL
10843 rettv->vval.v_string = eval_soundfold(s);
10844#else
10845 rettv->vval.v_string = vim_strsave(s);
10846#endif
10847}
10848
10849/*
10850 * "spellbadword()" function
10851 */
10852 static void
10853f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10854{
10855 char_u *word = (char_u *)"";
10856 hlf_T attr = HLF_COUNT;
10857 int len = 0;
10858
10859 if (rettv_list_alloc(rettv) == FAIL)
10860 return;
10861
10862#ifdef FEAT_SPELL
10863 if (argvars[0].v_type == VAR_UNKNOWN)
10864 {
10865 /* Find the start and length of the badly spelled word. */
10866 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10867 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010868 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010870 curwin->w_set_curswant = TRUE;
10871 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010872 }
10873 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010875 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 int capcol = -1;
10877
10878 if (str != NULL)
10879 {
10880 /* Check the argument for spelling. */
10881 while (*str != NUL)
10882 {
10883 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10884 if (attr != HLF_COUNT)
10885 {
10886 word = str;
10887 break;
10888 }
10889 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010890 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010891 }
10892 }
10893 }
10894#endif
10895
10896 list_append_string(rettv->vval.v_list, word, len);
10897 list_append_string(rettv->vval.v_list, (char_u *)(
10898 attr == HLF_SPB ? "bad" :
10899 attr == HLF_SPR ? "rare" :
10900 attr == HLF_SPL ? "local" :
10901 attr == HLF_SPC ? "caps" :
10902 ""), -1);
10903}
10904
10905/*
10906 * "spellsuggest()" function
10907 */
10908 static void
10909f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10910{
10911#ifdef FEAT_SPELL
10912 char_u *str;
10913 int typeerr = FALSE;
10914 int maxcount;
10915 garray_T ga;
10916 int i;
10917 listitem_T *li;
10918 int need_capital = FALSE;
10919#endif
10920
10921 if (rettv_list_alloc(rettv) == FAIL)
10922 return;
10923
10924#ifdef FEAT_SPELL
10925 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010927 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010928 if (argvars[1].v_type != VAR_UNKNOWN)
10929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010930 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931 if (maxcount <= 0)
10932 return;
10933 if (argvars[2].v_type != VAR_UNKNOWN)
10934 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010935 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010936 if (typeerr)
10937 return;
10938 }
10939 }
10940 else
10941 maxcount = 25;
10942
10943 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10944
10945 for (i = 0; i < ga.ga_len; ++i)
10946 {
10947 str = ((char_u **)ga.ga_data)[i];
10948
10949 li = listitem_alloc();
10950 if (li == NULL)
10951 vim_free(str);
10952 else
10953 {
10954 li->li_tv.v_type = VAR_STRING;
10955 li->li_tv.v_lock = 0;
10956 li->li_tv.vval.v_string = str;
10957 list_append(rettv->vval.v_list, li);
10958 }
10959 }
10960 ga_clear(&ga);
10961 }
10962#endif
10963}
10964
10965 static void
10966f_split(typval_T *argvars, typval_T *rettv)
10967{
10968 char_u *str;
10969 char_u *end;
10970 char_u *pat = NULL;
10971 regmatch_T regmatch;
10972 char_u patbuf[NUMBUFLEN];
10973 char_u *save_cpo;
10974 int match;
10975 colnr_T col = 0;
10976 int keepempty = FALSE;
10977 int typeerr = FALSE;
10978
10979 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10980 save_cpo = p_cpo;
10981 p_cpo = (char_u *)"";
10982
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010983 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 if (argvars[1].v_type != VAR_UNKNOWN)
10985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010986 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987 if (pat == NULL)
10988 typeerr = TRUE;
10989 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010990 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991 }
10992 if (pat == NULL || *pat == NUL)
10993 pat = (char_u *)"[\\x01- ]\\+";
10994
10995 if (rettv_list_alloc(rettv) == FAIL)
10996 return;
10997 if (typeerr)
10998 return;
10999
11000 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11001 if (regmatch.regprog != NULL)
11002 {
11003 regmatch.rm_ic = FALSE;
11004 while (*str != NUL || keepempty)
11005 {
11006 if (*str == NUL)
11007 match = FALSE; /* empty item at the end */
11008 else
11009 match = vim_regexec_nl(&regmatch, str, col);
11010 if (match)
11011 end = regmatch.startp[0];
11012 else
11013 end = str + STRLEN(str);
11014 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11015 && *str != NUL && match && end < regmatch.endp[0]))
11016 {
11017 if (list_append_string(rettv->vval.v_list, str,
11018 (int)(end - str)) == FAIL)
11019 break;
11020 }
11021 if (!match)
11022 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011023 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 if (regmatch.endp[0] > str)
11025 col = 0;
11026 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011027 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029 str = regmatch.endp[0];
11030 }
11031
11032 vim_regfree(regmatch.regprog);
11033 }
11034
11035 p_cpo = save_cpo;
11036}
11037
11038#ifdef FEAT_FLOAT
11039/*
11040 * "sqrt()" function
11041 */
11042 static void
11043f_sqrt(typval_T *argvars, typval_T *rettv)
11044{
11045 float_T f = 0.0;
11046
11047 rettv->v_type = VAR_FLOAT;
11048 if (get_float_arg(argvars, &f) == OK)
11049 rettv->vval.v_float = sqrt(f);
11050 else
11051 rettv->vval.v_float = 0.0;
11052}
11053
11054/*
11055 * "str2float()" function
11056 */
11057 static void
11058f_str2float(typval_T *argvars, typval_T *rettv)
11059{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011060 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011061 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062
Bram Moolenaar08243d22017-01-10 16:12:29 +010011063 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 p = skipwhite(p + 1);
11065 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011066 if (isneg)
11067 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 rettv->v_type = VAR_FLOAT;
11069}
11070#endif
11071
11072/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011073 * "str2list()" function
11074 */
11075 static void
11076f_str2list(typval_T *argvars, typval_T *rettv)
11077{
11078 char_u *p;
11079 int utf8 = FALSE;
11080
11081 if (rettv_list_alloc(rettv) == FAIL)
11082 return;
11083
11084 if (argvars[1].v_type != VAR_UNKNOWN)
11085 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11086
11087 p = tv_get_string(&argvars[0]);
11088
11089 if (has_mbyte || utf8)
11090 {
11091 int (*ptr2len)(char_u *);
11092 int (*ptr2char)(char_u *);
11093
11094 if (utf8 || enc_utf8)
11095 {
11096 ptr2len = utf_ptr2len;
11097 ptr2char = utf_ptr2char;
11098 }
11099 else
11100 {
11101 ptr2len = mb_ptr2len;
11102 ptr2char = mb_ptr2char;
11103 }
11104
11105 for ( ; *p != NUL; p += (*ptr2len)(p))
11106 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11107 }
11108 else
11109 for ( ; *p != NUL; ++p)
11110 list_append_number(rettv->vval.v_list, *p);
11111}
11112
11113/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114 * "str2nr()" function
11115 */
11116 static void
11117f_str2nr(typval_T *argvars, typval_T *rettv)
11118{
11119 int base = 10;
11120 char_u *p;
11121 varnumber_T n;
11122 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011123 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124
11125 if (argvars[1].v_type != VAR_UNKNOWN)
11126 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011127 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 if (base != 2 && base != 8 && base != 10 && base != 16)
11129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011130 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 return;
11132 }
11133 }
11134
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011135 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011136 isneg = (*p == '-');
11137 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 p = skipwhite(p + 1);
11139 switch (base)
11140 {
11141 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11142 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11143 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11144 default: what = 0;
11145 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011146 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11147 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011148 if (isneg)
11149 rettv->vval.v_number = -n;
11150 else
11151 rettv->vval.v_number = n;
11152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153}
11154
11155#ifdef HAVE_STRFTIME
11156/*
11157 * "strftime({format}[, {time}])" function
11158 */
11159 static void
11160f_strftime(typval_T *argvars, typval_T *rettv)
11161{
11162 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011163 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 struct tm *curtime;
11165 time_t seconds;
11166 char_u *p;
11167
11168 rettv->v_type = VAR_STRING;
11169
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011170 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 if (argvars[1].v_type == VAR_UNKNOWN)
11172 seconds = time(NULL);
11173 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011174 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011175 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 /* MSVC returns NULL for an invalid value of seconds. */
11177 if (curtime == NULL)
11178 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11179 else
11180 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181 vimconv_T conv;
11182 char_u *enc;
11183
11184 conv.vc_type = CONV_NONE;
11185 enc = enc_locale();
11186 convert_setup(&conv, p_enc, enc);
11187 if (conv.vc_type != CONV_NONE)
11188 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189 if (p != NULL)
11190 (void)strftime((char *)result_buf, sizeof(result_buf),
11191 (char *)p, curtime);
11192 else
11193 result_buf[0] = NUL;
11194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 if (conv.vc_type != CONV_NONE)
11196 vim_free(p);
11197 convert_setup(&conv, enc, p_enc);
11198 if (conv.vc_type != CONV_NONE)
11199 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11200 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011201 rettv->vval.v_string = vim_strsave(result_buf);
11202
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 /* Release conversion descriptors */
11204 convert_setup(&conv, NULL, NULL);
11205 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206 }
11207}
11208#endif
11209
11210/*
11211 * "strgetchar()" function
11212 */
11213 static void
11214f_strgetchar(typval_T *argvars, typval_T *rettv)
11215{
11216 char_u *str;
11217 int len;
11218 int error = FALSE;
11219 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011220 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221
11222 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011223 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224 if (str == NULL)
11225 return;
11226 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011227 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011228 if (error)
11229 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230
Bram Moolenaar13505972019-01-24 15:04:48 +010011231 while (charidx >= 0 && byteidx < len)
11232 {
11233 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011234 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011235 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11236 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011238 --charidx;
11239 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241}
11242
11243/*
11244 * "stridx()" function
11245 */
11246 static void
11247f_stridx(typval_T *argvars, typval_T *rettv)
11248{
11249 char_u buf[NUMBUFLEN];
11250 char_u *needle;
11251 char_u *haystack;
11252 char_u *save_haystack;
11253 char_u *pos;
11254 int start_idx;
11255
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011256 needle = tv_get_string_chk(&argvars[1]);
11257 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011258 rettv->vval.v_number = -1;
11259 if (needle == NULL || haystack == NULL)
11260 return; /* type error; errmsg already given */
11261
11262 if (argvars[2].v_type != VAR_UNKNOWN)
11263 {
11264 int error = FALSE;
11265
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011266 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011267 if (error || start_idx >= (int)STRLEN(haystack))
11268 return;
11269 if (start_idx >= 0)
11270 haystack += start_idx;
11271 }
11272
11273 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11274 if (pos != NULL)
11275 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11276}
11277
11278/*
11279 * "string()" function
11280 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011281 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011282f_string(typval_T *argvars, typval_T *rettv)
11283{
11284 char_u *tofree;
11285 char_u numbuf[NUMBUFLEN];
11286
11287 rettv->v_type = VAR_STRING;
11288 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11289 get_copyID());
11290 /* Make a copy if we have a value but it's not in allocated memory. */
11291 if (rettv->vval.v_string != NULL && tofree == NULL)
11292 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11293}
11294
11295/*
11296 * "strlen()" function
11297 */
11298 static void
11299f_strlen(typval_T *argvars, typval_T *rettv)
11300{
11301 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011302 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011303}
11304
11305/*
11306 * "strchars()" function
11307 */
11308 static void
11309f_strchars(typval_T *argvars, typval_T *rettv)
11310{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011311 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011312 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011313 varnumber_T len = 0;
11314 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011315
11316 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011317 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011318 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011319 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011320 else
11321 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011322 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11323 while (*s != NUL)
11324 {
11325 func_mb_ptr2char_adv(&s);
11326 ++len;
11327 }
11328 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 }
11330}
11331
11332/*
11333 * "strdisplaywidth()" function
11334 */
11335 static void
11336f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11337{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011338 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 int col = 0;
11340
11341 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011342 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343
11344 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11345}
11346
11347/*
11348 * "strwidth()" function
11349 */
11350 static void
11351f_strwidth(typval_T *argvars, typval_T *rettv)
11352{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011353 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354
Bram Moolenaar13505972019-01-24 15:04:48 +010011355 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011356}
11357
11358/*
11359 * "strcharpart()" function
11360 */
11361 static void
11362f_strcharpart(typval_T *argvars, typval_T *rettv)
11363{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364 char_u *p;
11365 int nchar;
11366 int nbyte = 0;
11367 int charlen;
11368 int len = 0;
11369 int slen;
11370 int error = FALSE;
11371
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011372 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011373 slen = (int)STRLEN(p);
11374
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011375 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011376 if (!error)
11377 {
11378 if (nchar > 0)
11379 while (nchar > 0 && nbyte < slen)
11380 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011381 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 --nchar;
11383 }
11384 else
11385 nbyte = nchar;
11386 if (argvars[2].v_type != VAR_UNKNOWN)
11387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011388 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011389 while (charlen > 0 && nbyte + len < slen)
11390 {
11391 int off = nbyte + len;
11392
11393 if (off < 0)
11394 len += 1;
11395 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011396 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397 --charlen;
11398 }
11399 }
11400 else
11401 len = slen - nbyte; /* default: all bytes that are available. */
11402 }
11403
11404 /*
11405 * Only return the overlap between the specified part and the actual
11406 * string.
11407 */
11408 if (nbyte < 0)
11409 {
11410 len += nbyte;
11411 nbyte = 0;
11412 }
11413 else if (nbyte > slen)
11414 nbyte = slen;
11415 if (len < 0)
11416 len = 0;
11417 else if (nbyte + len > slen)
11418 len = slen - nbyte;
11419
11420 rettv->v_type = VAR_STRING;
11421 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011422}
11423
11424/*
11425 * "strpart()" function
11426 */
11427 static void
11428f_strpart(typval_T *argvars, typval_T *rettv)
11429{
11430 char_u *p;
11431 int n;
11432 int len;
11433 int slen;
11434 int error = FALSE;
11435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011436 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011437 slen = (int)STRLEN(p);
11438
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011439 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 if (error)
11441 len = 0;
11442 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011443 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 else
11445 len = slen - n; /* default len: all bytes that are available. */
11446
11447 /*
11448 * Only return the overlap between the specified part and the actual
11449 * string.
11450 */
11451 if (n < 0)
11452 {
11453 len += n;
11454 n = 0;
11455 }
11456 else if (n > slen)
11457 n = slen;
11458 if (len < 0)
11459 len = 0;
11460 else if (n + len > slen)
11461 len = slen - n;
11462
11463 rettv->v_type = VAR_STRING;
11464 rettv->vval.v_string = vim_strnsave(p + n, len);
11465}
11466
11467/*
11468 * "strridx()" function
11469 */
11470 static void
11471f_strridx(typval_T *argvars, typval_T *rettv)
11472{
11473 char_u buf[NUMBUFLEN];
11474 char_u *needle;
11475 char_u *haystack;
11476 char_u *rest;
11477 char_u *lastmatch = NULL;
11478 int haystack_len, end_idx;
11479
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011480 needle = tv_get_string_chk(&argvars[1]);
11481 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011482
11483 rettv->vval.v_number = -1;
11484 if (needle == NULL || haystack == NULL)
11485 return; /* type error; errmsg already given */
11486
11487 haystack_len = (int)STRLEN(haystack);
11488 if (argvars[2].v_type != VAR_UNKNOWN)
11489 {
11490 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011491 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492 if (end_idx < 0)
11493 return; /* can never find a match */
11494 }
11495 else
11496 end_idx = haystack_len;
11497
11498 if (*needle == NUL)
11499 {
11500 /* Empty string matches past the end. */
11501 lastmatch = haystack + end_idx;
11502 }
11503 else
11504 {
11505 for (rest = haystack; *rest != '\0'; ++rest)
11506 {
11507 rest = (char_u *)strstr((char *)rest, (char *)needle);
11508 if (rest == NULL || rest > haystack + end_idx)
11509 break;
11510 lastmatch = rest;
11511 }
11512 }
11513
11514 if (lastmatch == NULL)
11515 rettv->vval.v_number = -1;
11516 else
11517 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11518}
11519
11520/*
11521 * "strtrans()" function
11522 */
11523 static void
11524f_strtrans(typval_T *argvars, typval_T *rettv)
11525{
11526 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011527 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528}
11529
11530/*
11531 * "submatch()" function
11532 */
11533 static void
11534f_submatch(typval_T *argvars, typval_T *rettv)
11535{
11536 int error = FALSE;
11537 int no;
11538 int retList = 0;
11539
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011540 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 if (error)
11542 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011543 if (no < 0 || no >= NSUBEXP)
11544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011545 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011546 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011549 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550 if (error)
11551 return;
11552
11553 if (retList == 0)
11554 {
11555 rettv->v_type = VAR_STRING;
11556 rettv->vval.v_string = reg_submatch(no);
11557 }
11558 else
11559 {
11560 rettv->v_type = VAR_LIST;
11561 rettv->vval.v_list = reg_submatch_list(no);
11562 }
11563}
11564
11565/*
11566 * "substitute()" function
11567 */
11568 static void
11569f_substitute(typval_T *argvars, typval_T *rettv)
11570{
11571 char_u patbuf[NUMBUFLEN];
11572 char_u subbuf[NUMBUFLEN];
11573 char_u flagsbuf[NUMBUFLEN];
11574
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011575 char_u *str = tv_get_string_chk(&argvars[0]);
11576 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011577 char_u *sub = NULL;
11578 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011579 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011581 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11582 expr = &argvars[2];
11583 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011584 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011587 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11588 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589 rettv->vval.v_string = NULL;
11590 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011591 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011592}
11593
11594/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011595 * "swapinfo(swap_filename)" function
11596 */
11597 static void
11598f_swapinfo(typval_T *argvars, typval_T *rettv)
11599{
11600 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011601 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011602}
11603
11604/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011605 * "swapname(expr)" function
11606 */
11607 static void
11608f_swapname(typval_T *argvars, typval_T *rettv)
11609{
11610 buf_T *buf;
11611
11612 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011613 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011614 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11615 || buf->b_ml.ml_mfp->mf_fname == NULL)
11616 rettv->vval.v_string = NULL;
11617 else
11618 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11619}
11620
11621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622 * "synID(lnum, col, trans)" function
11623 */
11624 static void
11625f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11626{
11627 int id = 0;
11628#ifdef FEAT_SYN_HL
11629 linenr_T lnum;
11630 colnr_T col;
11631 int trans;
11632 int transerr = FALSE;
11633
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011634 lnum = tv_get_lnum(argvars); /* -1 on type error */
11635 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11636 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637
11638 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11639 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11640 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11641#endif
11642
11643 rettv->vval.v_number = id;
11644}
11645
11646/*
11647 * "synIDattr(id, what [, mode])" function
11648 */
11649 static void
11650f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11651{
11652 char_u *p = NULL;
11653#ifdef FEAT_SYN_HL
11654 int id;
11655 char_u *what;
11656 char_u *mode;
11657 char_u modebuf[NUMBUFLEN];
11658 int modec;
11659
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011660 id = (int)tv_get_number(&argvars[0]);
11661 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011662 if (argvars[2].v_type != VAR_UNKNOWN)
11663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011664 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 modec = TOLOWER_ASC(mode[0]);
11666 if (modec != 't' && modec != 'c' && modec != 'g')
11667 modec = 0; /* replace invalid with current */
11668 }
11669 else
11670 {
11671#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11672 if (USE_24BIT)
11673 modec = 'g';
11674 else
11675#endif
11676 if (t_colors > 1)
11677 modec = 'c';
11678 else
11679 modec = 't';
11680 }
11681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682 switch (TOLOWER_ASC(what[0]))
11683 {
11684 case 'b':
11685 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11686 p = highlight_color(id, what, modec);
11687 else /* bold */
11688 p = highlight_has_attr(id, HL_BOLD, modec);
11689 break;
11690
11691 case 'f': /* fg[#] or font */
11692 p = highlight_color(id, what, modec);
11693 break;
11694
11695 case 'i':
11696 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11697 p = highlight_has_attr(id, HL_INVERSE, modec);
11698 else /* italic */
11699 p = highlight_has_attr(id, HL_ITALIC, modec);
11700 break;
11701
11702 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011703 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011704 break;
11705
11706 case 'r': /* reverse */
11707 p = highlight_has_attr(id, HL_INVERSE, modec);
11708 break;
11709
11710 case 's':
11711 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11712 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011713 /* strikeout */
11714 else if (TOLOWER_ASC(what[1]) == 't' &&
11715 TOLOWER_ASC(what[2]) == 'r')
11716 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 else /* standout */
11718 p = highlight_has_attr(id, HL_STANDOUT, modec);
11719 break;
11720
11721 case 'u':
11722 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11723 /* underline */
11724 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11725 else
11726 /* undercurl */
11727 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11728 break;
11729 }
11730
11731 if (p != NULL)
11732 p = vim_strsave(p);
11733#endif
11734 rettv->v_type = VAR_STRING;
11735 rettv->vval.v_string = p;
11736}
11737
11738/*
11739 * "synIDtrans(id)" function
11740 */
11741 static void
11742f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11743{
11744 int id;
11745
11746#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011748
11749 if (id > 0)
11750 id = syn_get_final_id(id);
11751 else
11752#endif
11753 id = 0;
11754
11755 rettv->vval.v_number = id;
11756}
11757
11758/*
11759 * "synconcealed(lnum, col)" function
11760 */
11761 static void
11762f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11763{
11764#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11765 linenr_T lnum;
11766 colnr_T col;
11767 int syntax_flags = 0;
11768 int cchar;
11769 int matchid = 0;
11770 char_u str[NUMBUFLEN];
11771#endif
11772
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011773 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011774
11775#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011776 lnum = tv_get_lnum(argvars); /* -1 on type error */
11777 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011778
11779 vim_memset(str, NUL, sizeof(str));
11780
11781 if (rettv_list_alloc(rettv) != FAIL)
11782 {
11783 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11784 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11785 && curwin->w_p_cole > 0)
11786 {
11787 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11788 syntax_flags = get_syntax_info(&matchid);
11789
11790 /* get the conceal character */
11791 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11792 {
11793 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011794 if (cchar == NUL && curwin->w_p_cole == 1)
11795 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 if (cchar != NUL)
11797 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011798 if (has_mbyte)
11799 (*mb_char2bytes)(cchar, str);
11800 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011801 str[0] = cchar;
11802 }
11803 }
11804 }
11805
11806 list_append_number(rettv->vval.v_list,
11807 (syntax_flags & HL_CONCEAL) != 0);
11808 /* -1 to auto-determine strlen */
11809 list_append_string(rettv->vval.v_list, str, -1);
11810 list_append_number(rettv->vval.v_list, matchid);
11811 }
11812#endif
11813}
11814
11815/*
11816 * "synstack(lnum, col)" function
11817 */
11818 static void
11819f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11820{
11821#ifdef FEAT_SYN_HL
11822 linenr_T lnum;
11823 colnr_T col;
11824 int i;
11825 int id;
11826#endif
11827
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011828 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829
11830#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011831 lnum = tv_get_lnum(argvars); /* -1 on type error */
11832 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011833
11834 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11835 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11836 && rettv_list_alloc(rettv) != FAIL)
11837 {
11838 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11839 for (i = 0; ; ++i)
11840 {
11841 id = syn_get_stack_item(i);
11842 if (id < 0)
11843 break;
11844 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11845 break;
11846 }
11847 }
11848#endif
11849}
11850
11851 static void
11852get_cmd_output_as_rettv(
11853 typval_T *argvars,
11854 typval_T *rettv,
11855 int retlist)
11856{
11857 char_u *res = NULL;
11858 char_u *p;
11859 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011860 int err = FALSE;
11861 FILE *fd;
11862 list_T *list = NULL;
11863 int flags = SHELL_SILENT;
11864
11865 rettv->v_type = VAR_STRING;
11866 rettv->vval.v_string = NULL;
11867 if (check_restricted() || check_secure())
11868 goto errret;
11869
11870 if (argvars[1].v_type != VAR_UNKNOWN)
11871 {
11872 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011873 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011874 * command.
11875 */
11876 if ((infile = vim_tempname('i', TRUE)) == NULL)
11877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011878 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879 goto errret;
11880 }
11881
11882 fd = mch_fopen((char *)infile, WRITEBIN);
11883 if (fd == NULL)
11884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011885 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011886 goto errret;
11887 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011888 if (argvars[1].v_type == VAR_NUMBER)
11889 {
11890 linenr_T lnum;
11891 buf_T *buf;
11892
11893 buf = buflist_findnr(argvars[1].vval.v_number);
11894 if (buf == NULL)
11895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011896 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011897 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011898 goto errret;
11899 }
11900
11901 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11902 {
11903 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11904 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11905 {
11906 err = TRUE;
11907 break;
11908 }
11909 if (putc(NL, fd) == EOF)
11910 {
11911 err = TRUE;
11912 break;
11913 }
11914 }
11915 }
11916 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 {
11918 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11919 err = TRUE;
11920 }
11921 else
11922 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011923 size_t len;
11924 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011926 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011927 if (p == NULL)
11928 {
11929 fclose(fd);
11930 goto errret; /* type error; errmsg already given */
11931 }
11932 len = STRLEN(p);
11933 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11934 err = TRUE;
11935 }
11936 if (fclose(fd) != 0)
11937 err = TRUE;
11938 if (err)
11939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011940 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011941 goto errret;
11942 }
11943 }
11944
11945 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11946 * echoes typeahead, that messes up the display. */
11947 if (!msg_silent)
11948 flags += SHELL_COOKED;
11949
11950 if (retlist)
11951 {
11952 int len;
11953 listitem_T *li;
11954 char_u *s = NULL;
11955 char_u *start;
11956 char_u *end;
11957 int i;
11958
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011959 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960 if (res == NULL)
11961 goto errret;
11962
11963 list = list_alloc();
11964 if (list == NULL)
11965 goto errret;
11966
11967 for (i = 0; i < len; ++i)
11968 {
11969 start = res + i;
11970 while (i < len && res[i] != NL)
11971 ++i;
11972 end = res + i;
11973
Bram Moolenaar964b3742019-05-24 18:54:09 +020011974 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 if (s == NULL)
11976 goto errret;
11977
11978 for (p = s; start < end; ++p, ++start)
11979 *p = *start == NUL ? NL : *start;
11980 *p = NUL;
11981
11982 li = listitem_alloc();
11983 if (li == NULL)
11984 {
11985 vim_free(s);
11986 goto errret;
11987 }
11988 li->li_tv.v_type = VAR_STRING;
11989 li->li_tv.v_lock = 0;
11990 li->li_tv.vval.v_string = s;
11991 list_append(list, li);
11992 }
11993
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011994 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011995 list = NULL;
11996 }
11997 else
11998 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011999 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010012000#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012001 /* translate <CR><NL> into <NL> */
12002 if (res != NULL)
12003 {
12004 char_u *s, *d;
12005
12006 d = res;
12007 for (s = res; *s; ++s)
12008 {
12009 if (s[0] == CAR && s[1] == NL)
12010 ++s;
12011 *d++ = *s;
12012 }
12013 *d = NUL;
12014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012015#endif
12016 rettv->vval.v_string = res;
12017 res = NULL;
12018 }
12019
12020errret:
12021 if (infile != NULL)
12022 {
12023 mch_remove(infile);
12024 vim_free(infile);
12025 }
12026 if (res != NULL)
12027 vim_free(res);
12028 if (list != NULL)
12029 list_free(list);
12030}
12031
12032/*
12033 * "system()" function
12034 */
12035 static void
12036f_system(typval_T *argvars, typval_T *rettv)
12037{
12038 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12039}
12040
12041/*
12042 * "systemlist()" function
12043 */
12044 static void
12045f_systemlist(typval_T *argvars, typval_T *rettv)
12046{
12047 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12048}
12049
12050/*
12051 * "tabpagebuflist()" function
12052 */
12053 static void
12054f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12055{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012056 tabpage_T *tp;
12057 win_T *wp = NULL;
12058
12059 if (argvars[0].v_type == VAR_UNKNOWN)
12060 wp = firstwin;
12061 else
12062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012063 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064 if (tp != NULL)
12065 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12066 }
12067 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12068 {
12069 for (; wp != NULL; wp = wp->w_next)
12070 if (list_append_number(rettv->vval.v_list,
12071 wp->w_buffer->b_fnum) == FAIL)
12072 break;
12073 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012074}
12075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012076/*
12077 * "tabpagenr()" function
12078 */
12079 static void
12080f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12081{
12082 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012083 char_u *arg;
12084
12085 if (argvars[0].v_type != VAR_UNKNOWN)
12086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012087 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012088 nr = 0;
12089 if (arg != NULL)
12090 {
12091 if (STRCMP(arg, "$") == 0)
12092 nr = tabpage_index(NULL) - 1;
12093 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012094 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095 }
12096 }
12097 else
12098 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012099 rettv->vval.v_number = nr;
12100}
12101
12102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012103/*
12104 * Common code for tabpagewinnr() and winnr().
12105 */
12106 static int
12107get_winnr(tabpage_T *tp, typval_T *argvar)
12108{
12109 win_T *twin;
12110 int nr = 1;
12111 win_T *wp;
12112 char_u *arg;
12113
12114 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12115 if (argvar->v_type != VAR_UNKNOWN)
12116 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012117 int invalid_arg = FALSE;
12118
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012119 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012120 if (arg == NULL)
12121 nr = 0; /* type error; errmsg already given */
12122 else if (STRCMP(arg, "$") == 0)
12123 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12124 else if (STRCMP(arg, "#") == 0)
12125 {
12126 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12127 if (twin == NULL)
12128 nr = 0;
12129 }
12130 else
12131 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012132 long count;
12133 char_u *endp;
12134
12135 // Extract the window count (if specified). e.g. winnr('3j')
12136 count = strtol((char *)arg, (char **)&endp, 10);
12137 if (count <= 0)
12138 count = 1; // if count is not specified, default to 1
12139 if (endp != NULL && *endp != '\0')
12140 {
12141 if (STRCMP(endp, "j") == 0)
12142 twin = win_vert_neighbor(tp, twin, FALSE, count);
12143 else if (STRCMP(endp, "k") == 0)
12144 twin = win_vert_neighbor(tp, twin, TRUE, count);
12145 else if (STRCMP(endp, "h") == 0)
12146 twin = win_horz_neighbor(tp, twin, TRUE, count);
12147 else if (STRCMP(endp, "l") == 0)
12148 twin = win_horz_neighbor(tp, twin, FALSE, count);
12149 else
12150 invalid_arg = TRUE;
12151 }
12152 else
12153 invalid_arg = TRUE;
12154 }
12155
12156 if (invalid_arg)
12157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012158 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012159 nr = 0;
12160 }
12161 }
12162
12163 if (nr > 0)
12164 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12165 wp != twin; wp = wp->w_next)
12166 {
12167 if (wp == NULL)
12168 {
12169 /* didn't find it in this tabpage */
12170 nr = 0;
12171 break;
12172 }
12173 ++nr;
12174 }
12175 return nr;
12176}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012177
12178/*
12179 * "tabpagewinnr()" function
12180 */
12181 static void
12182f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12183{
12184 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012185 tabpage_T *tp;
12186
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012187 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012188 if (tp == NULL)
12189 nr = 0;
12190 else
12191 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012192 rettv->vval.v_number = nr;
12193}
12194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012195/*
12196 * "tagfiles()" function
12197 */
12198 static void
12199f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12200{
12201 char_u *fname;
12202 tagname_T tn;
12203 int first;
12204
12205 if (rettv_list_alloc(rettv) == FAIL)
12206 return;
12207 fname = alloc(MAXPATHL);
12208 if (fname == NULL)
12209 return;
12210
12211 for (first = TRUE; ; first = FALSE)
12212 if (get_tagfname(&tn, first, fname) == FAIL
12213 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12214 break;
12215 tagname_free(&tn);
12216 vim_free(fname);
12217}
12218
12219/*
12220 * "taglist()" function
12221 */
12222 static void
12223f_taglist(typval_T *argvars, typval_T *rettv)
12224{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012225 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 char_u *tag_pattern;
12227
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229
12230 rettv->vval.v_number = FALSE;
12231 if (*tag_pattern == NUL)
12232 return;
12233
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012234 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012235 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012236 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012237 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012238}
12239
12240/*
12241 * "tempname()" function
12242 */
12243 static void
12244f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12245{
12246 static int x = 'A';
12247
12248 rettv->v_type = VAR_STRING;
12249 rettv->vval.v_string = vim_tempname(x, FALSE);
12250
12251 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12252 * names. Skip 'I' and 'O', they are used for shell redirection. */
12253 do
12254 {
12255 if (x == 'Z')
12256 x = '0';
12257 else if (x == '9')
12258 x = 'A';
12259 else
12260 {
12261#ifdef EBCDIC
12262 if (x == 'I')
12263 x = 'J';
12264 else if (x == 'R')
12265 x = 'S';
12266 else
12267#endif
12268 ++x;
12269 }
12270 } while (x == 'I' || x == 'O');
12271}
12272
12273#ifdef FEAT_FLOAT
12274/*
12275 * "tan()" function
12276 */
12277 static void
12278f_tan(typval_T *argvars, typval_T *rettv)
12279{
12280 float_T f = 0.0;
12281
12282 rettv->v_type = VAR_FLOAT;
12283 if (get_float_arg(argvars, &f) == OK)
12284 rettv->vval.v_float = tan(f);
12285 else
12286 rettv->vval.v_float = 0.0;
12287}
12288
12289/*
12290 * "tanh()" function
12291 */
12292 static void
12293f_tanh(typval_T *argvars, typval_T *rettv)
12294{
12295 float_T f = 0.0;
12296
12297 rettv->v_type = VAR_FLOAT;
12298 if (get_float_arg(argvars, &f) == OK)
12299 rettv->vval.v_float = tanh(f);
12300 else
12301 rettv->vval.v_float = 0.0;
12302}
12303#endif
12304
12305/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 * Get a callback from "arg". It can be a Funcref or a function name.
12307 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012308 * "cb_name" is not allocated.
12309 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012310 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012311 callback_T
12312get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012314 callback_T res;
12315
12316 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012317 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12318 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012319 res.cb_partial = arg->vval.v_partial;
12320 ++res.cb_partial->pt_refcount;
12321 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012322 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012323 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012325 res.cb_partial = NULL;
12326 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12327 {
12328 // Note that we don't make a copy of the string.
12329 res.cb_name = arg->vval.v_string;
12330 func_ref(res.cb_name);
12331 }
12332 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12333 {
12334 res.cb_name = (char_u *)"";
12335 }
12336 else
12337 {
12338 emsg(_("E921: Invalid callback argument"));
12339 res.cb_name = NULL;
12340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012341 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012342 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012343}
12344
12345/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012346 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012347 */
12348 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012349put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012350{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012351 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012353 tv->v_type = VAR_PARTIAL;
12354 tv->vval.v_partial = cb->cb_partial;
12355 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012356 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012357 else
12358 {
12359 tv->v_type = VAR_FUNC;
12360 tv->vval.v_string = vim_strsave(cb->cb_name);
12361 func_ref(cb->cb_name);
12362 }
12363}
12364
12365/*
12366 * Make a copy of "src" into "dest", allocating the function name if needed,
12367 * without incrementing the refcount.
12368 */
12369 void
12370set_callback(callback_T *dest, callback_T *src)
12371{
12372 if (src->cb_partial == NULL)
12373 {
12374 // just a function name, make a copy
12375 dest->cb_name = vim_strsave(src->cb_name);
12376 dest->cb_free_name = TRUE;
12377 }
12378 else
12379 {
12380 // cb_name is a pointer into cb_partial
12381 dest->cb_name = src->cb_name;
12382 dest->cb_free_name = FALSE;
12383 }
12384 dest->cb_partial = src->cb_partial;
12385}
12386
12387/*
12388 * Unref/free "callback" returned by get_callback() or set_callback().
12389 */
12390 void
12391free_callback(callback_T *callback)
12392{
12393 if (callback->cb_partial != NULL)
12394 {
12395 partial_unref(callback->cb_partial);
12396 callback->cb_partial = NULL;
12397 }
12398 else if (callback->cb_name != NULL)
12399 func_unref(callback->cb_name);
12400 if (callback->cb_free_name)
12401 {
12402 vim_free(callback->cb_name);
12403 callback->cb_free_name = FALSE;
12404 }
12405 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012406}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407
12408#ifdef FEAT_TIMERS
12409/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012410 * "timer_info([timer])" function
12411 */
12412 static void
12413f_timer_info(typval_T *argvars, typval_T *rettv)
12414{
12415 timer_T *timer = NULL;
12416
12417 if (rettv_list_alloc(rettv) != OK)
12418 return;
12419 if (argvars[0].v_type != VAR_UNKNOWN)
12420 {
12421 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012422 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012423 else
12424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012425 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012426 if (timer != NULL)
12427 add_timer_info(rettv, timer);
12428 }
12429 }
12430 else
12431 add_timer_info_all(rettv);
12432}
12433
12434/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012435 * "timer_pause(timer, paused)" function
12436 */
12437 static void
12438f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12439{
12440 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012441 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012442
12443 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012444 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012445 else
12446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012447 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012448 if (timer != NULL)
12449 timer->tr_paused = paused;
12450 }
12451}
12452
12453/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 * "timer_start(time, callback [, options])" function
12455 */
12456 static void
12457f_timer_start(typval_T *argvars, typval_T *rettv)
12458{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012459 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012460 timer_T *timer;
12461 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012462 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012463 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012464
Bram Moolenaar75537a92016-09-05 22:45:28 +020012465 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466 if (check_secure())
12467 return;
12468 if (argvars[2].v_type != VAR_UNKNOWN)
12469 {
12470 if (argvars[2].v_type != VAR_DICT
12471 || (dict = argvars[2].vval.v_dict) == NULL)
12472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012473 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474 return;
12475 }
12476 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012477 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478 }
12479
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012480 callback = get_callback(&argvars[1]);
12481 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012482 return;
12483
12484 timer = create_timer(msec, repeat);
12485 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012486 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487 else
12488 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012489 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012490 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012491 }
12492}
12493
12494/*
12495 * "timer_stop(timer)" function
12496 */
12497 static void
12498f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12499{
12500 timer_T *timer;
12501
12502 if (argvars[0].v_type != VAR_NUMBER)
12503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012504 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505 return;
12506 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012507 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508 if (timer != NULL)
12509 stop_timer(timer);
12510}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012511
12512/*
12513 * "timer_stopall()" function
12514 */
12515 static void
12516f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12517{
12518 stop_all_timers();
12519}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012520#endif
12521
12522/*
12523 * "tolower(string)" function
12524 */
12525 static void
12526f_tolower(typval_T *argvars, typval_T *rettv)
12527{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012528 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012529 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530}
12531
12532/*
12533 * "toupper(string)" function
12534 */
12535 static void
12536f_toupper(typval_T *argvars, typval_T *rettv)
12537{
12538 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540}
12541
12542/*
12543 * "tr(string, fromstr, tostr)" function
12544 */
12545 static void
12546f_tr(typval_T *argvars, typval_T *rettv)
12547{
12548 char_u *in_str;
12549 char_u *fromstr;
12550 char_u *tostr;
12551 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 int inlen;
12553 int fromlen;
12554 int tolen;
12555 int idx;
12556 char_u *cpstr;
12557 int cplen;
12558 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559 char_u buf[NUMBUFLEN];
12560 char_u buf2[NUMBUFLEN];
12561 garray_T ga;
12562
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012563 in_str = tv_get_string(&argvars[0]);
12564 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12565 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012566
12567 /* Default return value: empty string. */
12568 rettv->v_type = VAR_STRING;
12569 rettv->vval.v_string = NULL;
12570 if (fromstr == NULL || tostr == NULL)
12571 return; /* type error; errmsg already given */
12572 ga_init2(&ga, (int)sizeof(char), 80);
12573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575 /* not multi-byte: fromstr and tostr must be the same length */
12576 if (STRLEN(fromstr) != STRLEN(tostr))
12577 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012578error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012579 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 ga_clear(&ga);
12581 return;
12582 }
12583
12584 /* fromstr and tostr have to contain the same number of chars */
12585 while (*in_str != NUL)
12586 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 if (has_mbyte)
12588 {
12589 inlen = (*mb_ptr2len)(in_str);
12590 cpstr = in_str;
12591 cplen = inlen;
12592 idx = 0;
12593 for (p = fromstr; *p != NUL; p += fromlen)
12594 {
12595 fromlen = (*mb_ptr2len)(p);
12596 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12597 {
12598 for (p = tostr; *p != NUL; p += tolen)
12599 {
12600 tolen = (*mb_ptr2len)(p);
12601 if (idx-- == 0)
12602 {
12603 cplen = tolen;
12604 cpstr = p;
12605 break;
12606 }
12607 }
12608 if (*p == NUL) /* tostr is shorter than fromstr */
12609 goto error;
12610 break;
12611 }
12612 ++idx;
12613 }
12614
12615 if (first && cpstr == in_str)
12616 {
12617 /* Check that fromstr and tostr have the same number of
12618 * (multi-byte) characters. Done only once when a character
12619 * of in_str doesn't appear in fromstr. */
12620 first = FALSE;
12621 for (p = tostr; *p != NUL; p += tolen)
12622 {
12623 tolen = (*mb_ptr2len)(p);
12624 --idx;
12625 }
12626 if (idx != 0)
12627 goto error;
12628 }
12629
12630 (void)ga_grow(&ga, cplen);
12631 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12632 ga.ga_len += cplen;
12633
12634 in_str += inlen;
12635 }
12636 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012637 {
12638 /* When not using multi-byte chars we can do it faster. */
12639 p = vim_strchr(fromstr, *in_str);
12640 if (p != NULL)
12641 ga_append(&ga, tostr[p - fromstr]);
12642 else
12643 ga_append(&ga, *in_str);
12644 ++in_str;
12645 }
12646 }
12647
12648 /* add a terminating NUL */
12649 (void)ga_grow(&ga, 1);
12650 ga_append(&ga, NUL);
12651
12652 rettv->vval.v_string = ga.ga_data;
12653}
12654
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012655/*
12656 * "trim({expr})" function
12657 */
12658 static void
12659f_trim(typval_T *argvars, typval_T *rettv)
12660{
12661 char_u buf1[NUMBUFLEN];
12662 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012663 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012664 char_u *mask = NULL;
12665 char_u *tail;
12666 char_u *prev;
12667 char_u *p;
12668 int c1;
12669
12670 rettv->v_type = VAR_STRING;
12671 if (head == NULL)
12672 {
12673 rettv->vval.v_string = NULL;
12674 return;
12675 }
12676
12677 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012678 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012679
12680 while (*head != NUL)
12681 {
12682 c1 = PTR2CHAR(head);
12683 if (mask == NULL)
12684 {
12685 if (c1 > ' ' && c1 != 0xa0)
12686 break;
12687 }
12688 else
12689 {
12690 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12691 if (c1 == PTR2CHAR(p))
12692 break;
12693 if (*p == NUL)
12694 break;
12695 }
12696 MB_PTR_ADV(head);
12697 }
12698
12699 for (tail = head + STRLEN(head); tail > head; tail = prev)
12700 {
12701 prev = tail;
12702 MB_PTR_BACK(head, prev);
12703 c1 = PTR2CHAR(prev);
12704 if (mask == NULL)
12705 {
12706 if (c1 > ' ' && c1 != 0xa0)
12707 break;
12708 }
12709 else
12710 {
12711 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12712 if (c1 == PTR2CHAR(p))
12713 break;
12714 if (*p == NUL)
12715 break;
12716 }
12717 }
12718 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12719}
12720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012721#ifdef FEAT_FLOAT
12722/*
12723 * "trunc({float})" function
12724 */
12725 static void
12726f_trunc(typval_T *argvars, typval_T *rettv)
12727{
12728 float_T f = 0.0;
12729
12730 rettv->v_type = VAR_FLOAT;
12731 if (get_float_arg(argvars, &f) == OK)
12732 /* trunc() is not in C90, use floor() or ceil() instead. */
12733 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12734 else
12735 rettv->vval.v_float = 0.0;
12736}
12737#endif
12738
12739/*
12740 * "type(expr)" function
12741 */
12742 static void
12743f_type(typval_T *argvars, typval_T *rettv)
12744{
12745 int n = -1;
12746
12747 switch (argvars[0].v_type)
12748 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012749 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12750 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012751 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012752 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12753 case VAR_LIST: n = VAR_TYPE_LIST; break;
12754 case VAR_DICT: n = VAR_TYPE_DICT; break;
12755 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 case VAR_SPECIAL:
12757 if (argvars[0].vval.v_number == VVAL_FALSE
12758 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012759 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012760 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012761 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012763 case VAR_JOB: n = VAR_TYPE_JOB; break;
12764 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012765 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012767 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768 n = -1;
12769 break;
12770 }
12771 rettv->vval.v_number = n;
12772}
12773
12774/*
12775 * "undofile(name)" function
12776 */
12777 static void
12778f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12779{
12780 rettv->v_type = VAR_STRING;
12781#ifdef FEAT_PERSISTENT_UNDO
12782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012783 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784
12785 if (*fname == NUL)
12786 {
12787 /* If there is no file name there will be no undo file. */
12788 rettv->vval.v_string = NULL;
12789 }
12790 else
12791 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012792 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012793
12794 if (ffname != NULL)
12795 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12796 vim_free(ffname);
12797 }
12798 }
12799#else
12800 rettv->vval.v_string = NULL;
12801#endif
12802}
12803
12804/*
12805 * "undotree()" function
12806 */
12807 static void
12808f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12809{
12810 if (rettv_dict_alloc(rettv) == OK)
12811 {
12812 dict_T *dict = rettv->vval.v_dict;
12813 list_T *list;
12814
Bram Moolenaare0be1672018-07-08 16:50:37 +020012815 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12816 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12817 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12818 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12819 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12820 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821
12822 list = list_alloc();
12823 if (list != NULL)
12824 {
12825 u_eval_tree(curbuf->b_u_oldhead, list);
12826 dict_add_list(dict, "entries", list);
12827 }
12828 }
12829}
12830
12831/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012832 * "virtcol(string)" function
12833 */
12834 static void
12835f_virtcol(typval_T *argvars, typval_T *rettv)
12836{
12837 colnr_T vcol = 0;
12838 pos_T *fp;
12839 int fnum = curbuf->b_fnum;
12840
12841 fp = var2fpos(&argvars[0], FALSE, &fnum);
12842 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12843 && fnum == curbuf->b_fnum)
12844 {
12845 getvvcol(curwin, fp, NULL, NULL, &vcol);
12846 ++vcol;
12847 }
12848
12849 rettv->vval.v_number = vcol;
12850}
12851
12852/*
12853 * "visualmode()" function
12854 */
12855 static void
12856f_visualmode(typval_T *argvars, typval_T *rettv)
12857{
12858 char_u str[2];
12859
12860 rettv->v_type = VAR_STRING;
12861 str[0] = curbuf->b_visual_mode_eval;
12862 str[1] = NUL;
12863 rettv->vval.v_string = vim_strsave(str);
12864
12865 /* A non-zero number or non-empty string argument: reset mode. */
12866 if (non_zero_arg(&argvars[0]))
12867 curbuf->b_visual_mode_eval = NUL;
12868}
12869
12870/*
12871 * "wildmenumode()" function
12872 */
12873 static void
12874f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12875{
12876#ifdef FEAT_WILDMENU
12877 if (wild_menu_showing)
12878 rettv->vval.v_number = 1;
12879#endif
12880}
12881
12882/*
12883 * "winbufnr(nr)" function
12884 */
12885 static void
12886f_winbufnr(typval_T *argvars, typval_T *rettv)
12887{
12888 win_T *wp;
12889
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012890 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891 if (wp == NULL)
12892 rettv->vval.v_number = -1;
12893 else
12894 rettv->vval.v_number = wp->w_buffer->b_fnum;
12895}
12896
12897/*
12898 * "wincol()" function
12899 */
12900 static void
12901f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12902{
12903 validate_cursor();
12904 rettv->vval.v_number = curwin->w_wcol + 1;
12905}
12906
12907/*
12908 * "winheight(nr)" function
12909 */
12910 static void
12911f_winheight(typval_T *argvars, typval_T *rettv)
12912{
12913 win_T *wp;
12914
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012915 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 if (wp == NULL)
12917 rettv->vval.v_number = -1;
12918 else
12919 rettv->vval.v_number = wp->w_height;
12920}
12921
12922/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012923 * "winlayout()" function
12924 */
12925 static void
12926f_winlayout(typval_T *argvars, typval_T *rettv)
12927{
12928 tabpage_T *tp;
12929
12930 if (rettv_list_alloc(rettv) != OK)
12931 return;
12932
12933 if (argvars[0].v_type == VAR_UNKNOWN)
12934 tp = curtab;
12935 else
12936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012937 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012938 if (tp == NULL)
12939 return;
12940 }
12941
12942 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12943}
12944
12945/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012946 * "winline()" function
12947 */
12948 static void
12949f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12950{
12951 validate_cursor();
12952 rettv->vval.v_number = curwin->w_wrow + 1;
12953}
12954
12955/*
12956 * "winnr()" function
12957 */
12958 static void
12959f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12960{
12961 int nr = 1;
12962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964 rettv->vval.v_number = nr;
12965}
12966
12967/*
12968 * "winrestcmd()" function
12969 */
12970 static void
12971f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12972{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 win_T *wp;
12974 int winnr = 1;
12975 garray_T ga;
12976 char_u buf[50];
12977
12978 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012979 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 {
12981 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12982 ga_concat(&ga, buf);
12983 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12984 ga_concat(&ga, buf);
12985 ++winnr;
12986 }
12987 ga_append(&ga, NUL);
12988
12989 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990 rettv->v_type = VAR_STRING;
12991}
12992
12993/*
12994 * "winrestview()" function
12995 */
12996 static void
12997f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12998{
12999 dict_T *dict;
13000
13001 if (argvars[0].v_type != VAR_DICT
13002 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013003 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 else
13005 {
13006 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013007 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013009 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013011 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13013 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010013014 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 curwin->w_set_curswant = FALSE;
13016 }
13017
13018 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013019 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020#ifdef FEAT_DIFF
13021 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013022 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023#endif
13024 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013025 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013026 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013027 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028
13029 check_cursor();
13030 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013031 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032 changed_window_setting();
13033
13034 if (curwin->w_topline <= 0)
13035 curwin->w_topline = 1;
13036 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13037 curwin->w_topline = curbuf->b_ml.ml_line_count;
13038#ifdef FEAT_DIFF
13039 check_topfill(curwin, TRUE);
13040#endif
13041 }
13042}
13043
13044/*
13045 * "winsaveview()" function
13046 */
13047 static void
13048f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13049{
13050 dict_T *dict;
13051
13052 if (rettv_dict_alloc(rettv) == FAIL)
13053 return;
13054 dict = rettv->vval.v_dict;
13055
Bram Moolenaare0be1672018-07-08 16:50:37 +020013056 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13057 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013058 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013060 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013061
Bram Moolenaare0be1672018-07-08 16:50:37 +020013062 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013064 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013066 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13067 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068}
13069
13070/*
13071 * "winwidth(nr)" function
13072 */
13073 static void
13074f_winwidth(typval_T *argvars, typval_T *rettv)
13075{
13076 win_T *wp;
13077
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013078 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079 if (wp == NULL)
13080 rettv->vval.v_number = -1;
13081 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083}
13084
13085/*
13086 * "wordcount()" function
13087 */
13088 static void
13089f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13090{
13091 if (rettv_dict_alloc(rettv) == FAIL)
13092 return;
13093 cursor_pos_info(rettv->vval.v_dict);
13094}
13095
13096/*
13097 * "writefile()" function
13098 */
13099 static void
13100f_writefile(typval_T *argvars, typval_T *rettv)
13101{
13102 int binary = FALSE;
13103 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013104#ifdef HAVE_FSYNC
13105 int do_fsync = p_fs;
13106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013107 char_u *fname;
13108 FILE *fd;
13109 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013110 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013111 list_T *list = NULL;
13112 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013113
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013114 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013115 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 return;
13117
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013118 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013120 list = argvars[0].vval.v_list;
13121 if (list == NULL)
13122 return;
13123 for (li = list->lv_first; li != NULL; li = li->li_next)
13124 if (tv_get_string_chk(&li->li_tv) == NULL)
13125 return;
13126 }
13127 else if (argvars[0].v_type == VAR_BLOB)
13128 {
13129 blob = argvars[0].vval.v_blob;
13130 if (blob == NULL)
13131 return;
13132 }
13133 else
13134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013135 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 return;
13137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138
13139 if (argvars[2].v_type != VAR_UNKNOWN)
13140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013141 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013142
13143 if (arg2 == NULL)
13144 return;
13145 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013147 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013149#ifdef HAVE_FSYNC
13150 if (vim_strchr(arg2, 's') != NULL)
13151 do_fsync = TRUE;
13152 else if (vim_strchr(arg2, 'S') != NULL)
13153 do_fsync = FALSE;
13154#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155 }
13156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013158 if (fname == NULL)
13159 return;
13160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 /* Always open the file in binary mode, library functions have a mind of
13162 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13164 append ? APPENDBIN : WRITEBIN)) == NULL)
13165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013166 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167 ret = -1;
13168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013169 else if (blob)
13170 {
13171 if (write_blob(fd, blob) == FAIL)
13172 ret = -1;
13173#ifdef HAVE_FSYNC
13174 else if (do_fsync)
13175 // Ignore the error, the user wouldn't know what to do about it.
13176 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013177 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013178#endif
13179 fclose(fd);
13180 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 else
13182 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013183 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013185#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013186 else if (do_fsync)
13187 /* Ignore the error, the user wouldn't know what to do about it.
13188 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013189 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013190#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 fclose(fd);
13192 }
13193
13194 rettv->vval.v_number = ret;
13195}
13196
13197/*
13198 * "xor(expr, expr)" function
13199 */
13200 static void
13201f_xor(typval_T *argvars, typval_T *rettv)
13202{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013203 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13204 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205}
13206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013207#endif /* FEAT_EVAL */