blob: 54fc2f5489f8a251d23a393c05924cf3c2a2ffef [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{
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005881 int flags = WILD_IGNORE_COMPLETESLASH;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 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
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005888 // 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.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 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 "localmap",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#ifdef FEAT_LUA
6194# ifndef DYNAMIC_LUA
6195 "lua",
6196# endif
6197#endif
6198#ifdef FEAT_MENU
6199 "menu",
6200#endif
6201#ifdef FEAT_SESSION
6202 "mksession",
6203#endif
6204#ifdef FEAT_MODIFY_FNAME
6205 "modify_fname",
6206#endif
6207#ifdef FEAT_MOUSE
6208 "mouse",
6209#endif
6210#ifdef FEAT_MOUSESHAPE
6211 "mouseshape",
6212#endif
6213#if defined(UNIX) || defined(VMS)
6214# ifdef FEAT_MOUSE_DEC
6215 "mouse_dec",
6216# endif
6217# ifdef FEAT_MOUSE_GPM
6218 "mouse_gpm",
6219# endif
6220# ifdef FEAT_MOUSE_JSB
6221 "mouse_jsbterm",
6222# endif
6223# ifdef FEAT_MOUSE_NET
6224 "mouse_netterm",
6225# endif
6226# ifdef FEAT_MOUSE_PTERM
6227 "mouse_pterm",
6228# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006229# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 "mouse_sgr",
6231# endif
6232# ifdef FEAT_SYSMOUSE
6233 "mouse_sysmouse",
6234# endif
6235# ifdef FEAT_MOUSE_URXVT
6236 "mouse_urxvt",
6237# endif
6238# ifdef FEAT_MOUSE_XTERM
6239 "mouse_xterm",
6240# endif
6241#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243#ifdef FEAT_MBYTE_IME
6244 "multi_byte_ime",
6245#endif
6246#ifdef FEAT_MULTI_LANG
6247 "multi_lang",
6248#endif
6249#ifdef FEAT_MZSCHEME
6250#ifndef DYNAMIC_MZSCHEME
6251 "mzscheme",
6252#endif
6253#endif
6254#ifdef FEAT_NUM64
6255 "num64",
6256#endif
6257#ifdef FEAT_OLE
6258 "ole",
6259#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006260#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006262#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263#ifdef FEAT_PATH_EXTRA
6264 "path_extra",
6265#endif
6266#ifdef FEAT_PERL
6267#ifndef DYNAMIC_PERL
6268 "perl",
6269#endif
6270#endif
6271#ifdef FEAT_PERSISTENT_UNDO
6272 "persistent_undo",
6273#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006274#if defined(FEAT_PYTHON)
6275 "python_compiled",
6276# if defined(DYNAMIC_PYTHON)
6277 "python_dynamic",
6278# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006280 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006281# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006283#if defined(FEAT_PYTHON3)
6284 "python3_compiled",
6285# if defined(DYNAMIC_PYTHON3)
6286 "python3_dynamic",
6287# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006289 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006290# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291#endif
6292#ifdef FEAT_POSTSCRIPT
6293 "postscript",
6294#endif
6295#ifdef FEAT_PRINTER
6296 "printer",
6297#endif
6298#ifdef FEAT_PROFILE
6299 "profile",
6300#endif
6301#ifdef FEAT_RELTIME
6302 "reltime",
6303#endif
6304#ifdef FEAT_QUICKFIX
6305 "quickfix",
6306#endif
6307#ifdef FEAT_RIGHTLEFT
6308 "rightleft",
6309#endif
6310#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6311 "ruby",
6312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef FEAT_CMDL_INFO
6315 "showcmd",
6316 "cmdline_info",
6317#endif
6318#ifdef FEAT_SIGNS
6319 "signs",
6320#endif
6321#ifdef FEAT_SMARTINDENT
6322 "smartindent",
6323#endif
6324#ifdef STARTUPTIME
6325 "startuptime",
6326#endif
6327#ifdef FEAT_STL_OPT
6328 "statusline",
6329#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006330#ifdef FEAT_NETBEANS_INTG
6331 "netbeans_intg",
6332#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006333#ifdef FEAT_SOUND
6334 "sound",
6335#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336#ifdef FEAT_SPELL
6337 "spell",
6338#endif
6339#ifdef FEAT_SYN_HL
6340 "syntax",
6341#endif
6342#if defined(USE_SYSTEM) || !defined(UNIX)
6343 "system",
6344#endif
6345#ifdef FEAT_TAG_BINS
6346 "tag_binary",
6347#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348#ifdef FEAT_TCL
6349# ifndef DYNAMIC_TCL
6350 "tcl",
6351# endif
6352#endif
6353#ifdef FEAT_TERMGUICOLORS
6354 "termguicolors",
6355#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006356#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006357 "terminal",
6358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359#ifdef TERMINFO
6360 "terminfo",
6361#endif
6362#ifdef FEAT_TERMRESPONSE
6363 "termresponse",
6364#endif
6365#ifdef FEAT_TEXTOBJ
6366 "textobjects",
6367#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006368#ifdef FEAT_TEXT_PROP
6369 "textprop",
6370#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371#ifdef HAVE_TGETENT
6372 "tgetent",
6373#endif
6374#ifdef FEAT_TIMERS
6375 "timers",
6376#endif
6377#ifdef FEAT_TITLE
6378 "title",
6379#endif
6380#ifdef FEAT_TOOLBAR
6381 "toolbar",
6382#endif
6383#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6384 "unnamedplus",
6385#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 "user-commands", /* was accidentally included in 5.4 */
6387 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006388#ifdef FEAT_VARTABS
6389 "vartabs",
6390#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006391 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392#ifdef FEAT_VIMINFO
6393 "viminfo",
6394#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006395 "vimscript-1",
6396 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006397 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006399 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006402#ifdef FEAT_VTP
6403 "vtp",
6404#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405#ifdef FEAT_WILDIGN
6406 "wildignore",
6407#endif
6408#ifdef FEAT_WILDMENU
6409 "wildmenu",
6410#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412#ifdef FEAT_WAK
6413 "winaltkeys",
6414#endif
6415#ifdef FEAT_WRITEBACKUP
6416 "writebackup",
6417#endif
6418#ifdef FEAT_XIM
6419 "xim",
6420#endif
6421#ifdef FEAT_XFONTSET
6422 "xfontset",
6423#endif
6424#ifdef FEAT_XPM_W32
6425 "xpm",
6426 "xpm_w32", /* for backward compatibility */
6427#else
6428# if defined(HAVE_XPM)
6429 "xpm",
6430# endif
6431#endif
6432#ifdef USE_XSMP
6433 "xsmp",
6434#endif
6435#ifdef USE_XSMP_INTERACT
6436 "xsmp_interact",
6437#endif
6438#ifdef FEAT_XCLIPBOARD
6439 "xterm_clipboard",
6440#endif
6441#ifdef FEAT_XTERM_SAVE
6442 "xterm_save",
6443#endif
6444#if defined(UNIX) && defined(FEAT_X11)
6445 "X11",
6446#endif
6447 NULL
6448 };
6449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006450 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451 for (i = 0; has_list[i] != NULL; ++i)
6452 if (STRICMP(name, has_list[i]) == 0)
6453 {
6454 n = TRUE;
6455 break;
6456 }
6457
6458 if (n == FALSE)
6459 {
6460 if (STRNICMP(name, "patch", 5) == 0)
6461 {
6462 if (name[5] == '-'
6463 && STRLEN(name) >= 11
6464 && vim_isdigit(name[6])
6465 && vim_isdigit(name[8])
6466 && vim_isdigit(name[10]))
6467 {
6468 int major = atoi((char *)name + 6);
6469 int minor = atoi((char *)name + 8);
6470
6471 /* Expect "patch-9.9.01234". */
6472 n = (major < VIM_VERSION_MAJOR
6473 || (major == VIM_VERSION_MAJOR
6474 && (minor < VIM_VERSION_MINOR
6475 || (minor == VIM_VERSION_MINOR
6476 && has_patch(atoi((char *)name + 10))))));
6477 }
6478 else
6479 n = has_patch(atoi((char *)name + 5));
6480 }
6481 else if (STRICMP(name, "vim_starting") == 0)
6482 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006483 else if (STRICMP(name, "ttyin") == 0)
6484 n = mch_input_isatty();
6485 else if (STRICMP(name, "ttyout") == 0)
6486 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487 else if (STRICMP(name, "multi_byte_encoding") == 0)
6488 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006489#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 else if (STRICMP(name, "balloon_multiline") == 0)
6491 n = multiline_balloon_available();
6492#endif
6493#ifdef DYNAMIC_TCL
6494 else if (STRICMP(name, "tcl") == 0)
6495 n = tcl_enabled(FALSE);
6496#endif
6497#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6498 else if (STRICMP(name, "iconv") == 0)
6499 n = iconv_enabled(FALSE);
6500#endif
6501#ifdef DYNAMIC_LUA
6502 else if (STRICMP(name, "lua") == 0)
6503 n = lua_enabled(FALSE);
6504#endif
6505#ifdef DYNAMIC_MZSCHEME
6506 else if (STRICMP(name, "mzscheme") == 0)
6507 n = mzscheme_enabled(FALSE);
6508#endif
6509#ifdef DYNAMIC_RUBY
6510 else if (STRICMP(name, "ruby") == 0)
6511 n = ruby_enabled(FALSE);
6512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef DYNAMIC_PYTHON
6514 else if (STRICMP(name, "python") == 0)
6515 n = python_enabled(FALSE);
6516#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef DYNAMIC_PYTHON3
6518 else if (STRICMP(name, "python3") == 0)
6519 n = python3_enabled(FALSE);
6520#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006521#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6522 else if (STRICMP(name, "pythonx") == 0)
6523 {
6524# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6525 if (p_pyx == 0)
6526 n = python3_enabled(FALSE) || python_enabled(FALSE);
6527 else if (p_pyx == 3)
6528 n = python3_enabled(FALSE);
6529 else if (p_pyx == 2)
6530 n = python_enabled(FALSE);
6531# elif defined(DYNAMIC_PYTHON)
6532 n = python_enabled(FALSE);
6533# elif defined(DYNAMIC_PYTHON3)
6534 n = python3_enabled(FALSE);
6535# endif
6536 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537#endif
6538#ifdef DYNAMIC_PERL
6539 else if (STRICMP(name, "perl") == 0)
6540 n = perl_enabled(FALSE);
6541#endif
6542#ifdef FEAT_GUI
6543 else if (STRICMP(name, "gui_running") == 0)
6544 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545# ifdef FEAT_BROWSE
6546 else if (STRICMP(name, "browse") == 0)
6547 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6548# endif
6549#endif
6550#ifdef FEAT_SYN_HL
6551 else if (STRICMP(name, "syntax_items") == 0)
6552 n = syntax_present(curwin);
6553#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006554#ifdef FEAT_VTP
6555 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006556 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557#endif
6558#ifdef FEAT_NETBEANS_INTG
6559 else if (STRICMP(name, "netbeans_enabled") == 0)
6560 n = netbeans_active();
6561#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006562#ifdef FEAT_MOUSE_GPM
6563 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6564 n = gpm_enabled();
6565#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006566#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006567 else if (STRICMP(name, "terminal") == 0)
6568 n = terminal_enabled();
6569#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006570#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006571 else if (STRICMP(name, "conpty") == 0)
6572 n = use_conpty();
6573#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006574 }
6575
6576 rettv->vval.v_number = n;
6577}
6578
6579/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580 * "haslocaldir()" function
6581 */
6582 static void
6583f_haslocaldir(typval_T *argvars, typval_T *rettv)
6584{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006585 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 win_T *wp = NULL;
6587
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006588 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6589
6590 // Check for window-local and tab-local directories
6591 if (wp != NULL && wp->w_localdir != NULL)
6592 rettv->vval.v_number = 1;
6593 else if (tp != NULL && tp->tp_localdir != NULL)
6594 rettv->vval.v_number = 2;
6595 else
6596 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597}
6598
6599/*
6600 * "hasmapto()" function
6601 */
6602 static void
6603f_hasmapto(typval_T *argvars, typval_T *rettv)
6604{
6605 char_u *name;
6606 char_u *mode;
6607 char_u buf[NUMBUFLEN];
6608 int abbr = FALSE;
6609
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006610 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 if (argvars[1].v_type == VAR_UNKNOWN)
6612 mode = (char_u *)"nvo";
6613 else
6614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006615 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006617 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618 }
6619
6620 if (map_to_exists(name, mode, abbr))
6621 rettv->vval.v_number = TRUE;
6622 else
6623 rettv->vval.v_number = FALSE;
6624}
6625
6626/*
6627 * "histadd()" function
6628 */
6629 static void
6630f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6631{
6632#ifdef FEAT_CMDHIST
6633 int histype;
6634 char_u *str;
6635 char_u buf[NUMBUFLEN];
6636#endif
6637
6638 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006639 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 return;
6641#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006642 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 histype = str != NULL ? get_histtype(str) : -1;
6644 if (histype >= 0)
6645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006646 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 if (*str != NUL)
6648 {
6649 init_history();
6650 add_to_history(histype, str, FALSE, NUL);
6651 rettv->vval.v_number = TRUE;
6652 return;
6653 }
6654 }
6655#endif
6656}
6657
6658/*
6659 * "histdel()" function
6660 */
6661 static void
6662f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6663{
6664#ifdef FEAT_CMDHIST
6665 int n;
6666 char_u buf[NUMBUFLEN];
6667 char_u *str;
6668
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006669 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 if (str == NULL)
6671 n = 0;
6672 else if (argvars[1].v_type == VAR_UNKNOWN)
6673 /* only one argument: clear entire history */
6674 n = clr_history(get_histtype(str));
6675 else if (argvars[1].v_type == VAR_NUMBER)
6676 /* index given: remove that entry */
6677 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006678 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 else
6680 /* string given: remove all matching entries */
6681 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006682 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006683 rettv->vval.v_number = n;
6684#endif
6685}
6686
6687/*
6688 * "histget()" function
6689 */
6690 static void
6691f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6692{
6693#ifdef FEAT_CMDHIST
6694 int type;
6695 int idx;
6696 char_u *str;
6697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006698 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006699 if (str == NULL)
6700 rettv->vval.v_string = NULL;
6701 else
6702 {
6703 type = get_histtype(str);
6704 if (argvars[1].v_type == VAR_UNKNOWN)
6705 idx = get_history_idx(type);
6706 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006707 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708 /* -1 on type error */
6709 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6710 }
6711#else
6712 rettv->vval.v_string = NULL;
6713#endif
6714 rettv->v_type = VAR_STRING;
6715}
6716
6717/*
6718 * "histnr()" function
6719 */
6720 static void
6721f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6722{
6723 int i;
6724
6725#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006726 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727
6728 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6729 if (i >= HIST_CMD && i < HIST_COUNT)
6730 i = get_history_idx(i);
6731 else
6732#endif
6733 i = -1;
6734 rettv->vval.v_number = i;
6735}
6736
6737/*
6738 * "highlightID(name)" function
6739 */
6740 static void
6741f_hlID(typval_T *argvars, typval_T *rettv)
6742{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006743 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744}
6745
6746/*
6747 * "highlight_exists()" function
6748 */
6749 static void
6750f_hlexists(typval_T *argvars, typval_T *rettv)
6751{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006752 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006753}
6754
6755/*
6756 * "hostname()" function
6757 */
6758 static void
6759f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6760{
6761 char_u hostname[256];
6762
6763 mch_get_host_name(hostname, 256);
6764 rettv->v_type = VAR_STRING;
6765 rettv->vval.v_string = vim_strsave(hostname);
6766}
6767
6768/*
6769 * iconv() function
6770 */
6771 static void
6772f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6773{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006774 char_u buf1[NUMBUFLEN];
6775 char_u buf2[NUMBUFLEN];
6776 char_u *from, *to, *str;
6777 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778
6779 rettv->v_type = VAR_STRING;
6780 rettv->vval.v_string = NULL;
6781
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006782 str = tv_get_string(&argvars[0]);
6783 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6784 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785 vimconv.vc_type = CONV_NONE;
6786 convert_setup(&vimconv, from, to);
6787
6788 /* If the encodings are equal, no conversion needed. */
6789 if (vimconv.vc_type == CONV_NONE)
6790 rettv->vval.v_string = vim_strsave(str);
6791 else
6792 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6793
6794 convert_setup(&vimconv, NULL, NULL);
6795 vim_free(from);
6796 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797}
6798
6799/*
6800 * "indent()" function
6801 */
6802 static void
6803f_indent(typval_T *argvars, typval_T *rettv)
6804{
6805 linenr_T lnum;
6806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006807 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6809 rettv->vval.v_number = get_indent_lnum(lnum);
6810 else
6811 rettv->vval.v_number = -1;
6812}
6813
6814/*
6815 * "index()" function
6816 */
6817 static void
6818f_index(typval_T *argvars, typval_T *rettv)
6819{
6820 list_T *l;
6821 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006822 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823 long idx = 0;
6824 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006825 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826
6827 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006828 if (argvars[0].v_type == VAR_BLOB)
6829 {
6830 typval_T tv;
6831 int start = 0;
6832
6833 if (argvars[2].v_type != VAR_UNKNOWN)
6834 {
6835 start = tv_get_number_chk(&argvars[2], &error);
6836 if (error)
6837 return;
6838 }
6839 b = argvars[0].vval.v_blob;
6840 if (b == NULL)
6841 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006842 if (start < 0)
6843 {
6844 start = blob_len(b) + start;
6845 if (start < 0)
6846 start = 0;
6847 }
6848
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006849 for (idx = start; idx < blob_len(b); ++idx)
6850 {
6851 tv.v_type = VAR_NUMBER;
6852 tv.vval.v_number = blob_get(b, idx);
6853 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6854 {
6855 rettv->vval.v_number = idx;
6856 return;
6857 }
6858 }
6859 return;
6860 }
6861 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006862 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006863 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 return;
6865 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 l = argvars[0].vval.v_list;
6868 if (l != NULL)
6869 {
6870 item = l->lv_first;
6871 if (argvars[2].v_type != VAR_UNKNOWN)
6872 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 /* Start at specified item. Use the cached index that list_find()
6874 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 idx = l->lv_idx;
6877 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 if (error)
6880 item = NULL;
6881 }
6882
6883 for ( ; item != NULL; item = item->li_next, ++idx)
6884 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6885 {
6886 rettv->vval.v_number = idx;
6887 break;
6888 }
6889 }
6890}
6891
6892static int inputsecret_flag = 0;
6893
6894/*
6895 * "input()" function
6896 * Also handles inputsecret() when inputsecret is set.
6897 */
6898 static void
6899f_input(typval_T *argvars, typval_T *rettv)
6900{
6901 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6902}
6903
6904/*
6905 * "inputdialog()" function
6906 */
6907 static void
6908f_inputdialog(typval_T *argvars, typval_T *rettv)
6909{
6910#if defined(FEAT_GUI_TEXTDIALOG)
6911 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6912 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6913 {
6914 char_u *message;
6915 char_u buf[NUMBUFLEN];
6916 char_u *defstr = (char_u *)"";
6917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006920 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6922 else
6923 IObuff[0] = NUL;
6924 if (message != NULL && defstr != NULL
6925 && do_dialog(VIM_QUESTION, NULL, message,
6926 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6927 rettv->vval.v_string = vim_strsave(IObuff);
6928 else
6929 {
6930 if (message != NULL && defstr != NULL
6931 && argvars[1].v_type != VAR_UNKNOWN
6932 && argvars[2].v_type != VAR_UNKNOWN)
6933 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 else
6936 rettv->vval.v_string = NULL;
6937 }
6938 rettv->v_type = VAR_STRING;
6939 }
6940 else
6941#endif
6942 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6943}
6944
6945/*
6946 * "inputlist()" function
6947 */
6948 static void
6949f_inputlist(typval_T *argvars, typval_T *rettv)
6950{
6951 listitem_T *li;
6952 int selected;
6953 int mouse_used;
6954
6955#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006956 /* While starting up, there is no place to enter text. When running tests
6957 * with --not-a-term we assume feedkeys() will be used. */
6958 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959 return;
6960#endif
6961 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006963 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006964 return;
6965 }
6966
6967 msg_start();
6968 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6969 lines_left = Rows; /* avoid more prompt */
6970 msg_scroll = TRUE;
6971 msg_clr_eos();
6972
6973 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6974 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006975 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 msg_putchar('\n');
6977 }
6978
6979 /* Ask for choice. */
6980 selected = prompt_for_number(&mouse_used);
6981 if (mouse_used)
6982 selected -= lines_left;
6983
6984 rettv->vval.v_number = selected;
6985}
6986
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6988
6989/*
6990 * "inputrestore()" function
6991 */
6992 static void
6993f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6994{
6995 if (ga_userinput.ga_len > 0)
6996 {
6997 --ga_userinput.ga_len;
6998 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6999 + ga_userinput.ga_len);
7000 /* default return is zero == OK */
7001 }
7002 else if (p_verbose > 1)
7003 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007004 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 rettv->vval.v_number = 1; /* Failed */
7006 }
7007}
7008
7009/*
7010 * "inputsave()" function
7011 */
7012 static void
7013f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7014{
7015 /* Add an entry to the stack of typeahead storage. */
7016 if (ga_grow(&ga_userinput, 1) == OK)
7017 {
7018 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7019 + ga_userinput.ga_len);
7020 ++ga_userinput.ga_len;
7021 /* default return is zero == OK */
7022 }
7023 else
7024 rettv->vval.v_number = 1; /* Failed */
7025}
7026
7027/*
7028 * "inputsecret()" function
7029 */
7030 static void
7031f_inputsecret(typval_T *argvars, typval_T *rettv)
7032{
7033 ++cmdline_star;
7034 ++inputsecret_flag;
7035 f_input(argvars, rettv);
7036 --cmdline_star;
7037 --inputsecret_flag;
7038}
7039
7040/*
7041 * "insert()" function
7042 */
7043 static void
7044f_insert(typval_T *argvars, typval_T *rettv)
7045{
7046 long before = 0;
7047 listitem_T *item;
7048 list_T *l;
7049 int error = FALSE;
7050
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007051 if (argvars[0].v_type == VAR_BLOB)
7052 {
7053 int val, len;
7054 char_u *p;
7055
7056 len = blob_len(argvars[0].vval.v_blob);
7057 if (argvars[2].v_type != VAR_UNKNOWN)
7058 {
7059 before = (long)tv_get_number_chk(&argvars[2], &error);
7060 if (error)
7061 return; // type error; errmsg already given
7062 if (before < 0 || before > len)
7063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007064 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007065 return;
7066 }
7067 }
7068 val = tv_get_number_chk(&argvars[1], &error);
7069 if (error)
7070 return;
7071 if (val < 0 || val > 255)
7072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007073 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007074 return;
7075 }
7076
7077 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7078 return;
7079 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7080 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7081 *(p + before) = val;
7082 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7083
7084 copy_tv(&argvars[0], rettv);
7085 }
7086 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007087 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007088 else if ((l = argvars[0].vval.v_list) != NULL
7089 && !var_check_lock(l->lv_lock,
7090 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091 {
7092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007093 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 if (error)
7095 return; /* type error; errmsg already given */
7096
7097 if (before == l->lv_len)
7098 item = NULL;
7099 else
7100 {
7101 item = list_find(l, before);
7102 if (item == NULL)
7103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007104 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 l = NULL;
7106 }
7107 }
7108 if (l != NULL)
7109 {
7110 list_insert_tv(l, &argvars[1], item);
7111 copy_tv(&argvars[0], rettv);
7112 }
7113 }
7114}
7115
7116/*
7117 * "invert(expr)" function
7118 */
7119 static void
7120f_invert(typval_T *argvars, typval_T *rettv)
7121{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007122 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123}
7124
7125/*
7126 * "isdirectory()" function
7127 */
7128 static void
7129f_isdirectory(typval_T *argvars, typval_T *rettv)
7130{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132}
7133
7134/*
7135 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7136 * or it refers to a List or Dictionary that is locked.
7137 */
7138 static int
7139tv_islocked(typval_T *tv)
7140{
7141 return (tv->v_lock & VAR_LOCKED)
7142 || (tv->v_type == VAR_LIST
7143 && tv->vval.v_list != NULL
7144 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7145 || (tv->v_type == VAR_DICT
7146 && tv->vval.v_dict != NULL
7147 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7148}
7149
7150/*
7151 * "islocked()" function
7152 */
7153 static void
7154f_islocked(typval_T *argvars, typval_T *rettv)
7155{
7156 lval_T lv;
7157 char_u *end;
7158 dictitem_T *di;
7159
7160 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007161 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007162 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 if (end != NULL && lv.ll_name != NULL)
7164 {
7165 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007166 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 else
7168 {
7169 if (lv.ll_tv == NULL)
7170 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007171 di = find_var(lv.ll_name, NULL, TRUE);
7172 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007174 /* Consider a variable locked when:
7175 * 1. the variable itself is locked
7176 * 2. the value of the variable is locked.
7177 * 3. the List or Dict value is locked.
7178 */
7179 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7180 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181 }
7182 }
7183 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007184 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007186 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 else if (lv.ll_list != NULL)
7188 /* List item. */
7189 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7190 else
7191 /* Dictionary item. */
7192 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7193 }
7194 }
7195
7196 clear_lval(&lv);
7197}
7198
7199#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7200/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007201 * "isinf()" function
7202 */
7203 static void
7204f_isinf(typval_T *argvars, typval_T *rettv)
7205{
7206 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7207 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7208}
7209
7210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 * "isnan()" function
7212 */
7213 static void
7214f_isnan(typval_T *argvars, typval_T *rettv)
7215{
7216 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7217 && isnan(argvars[0].vval.v_float);
7218}
7219#endif
7220
7221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 * "last_buffer_nr()" function.
7223 */
7224 static void
7225f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7226{
7227 int n = 0;
7228 buf_T *buf;
7229
Bram Moolenaar29323592016-07-24 22:04:11 +02007230 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 if (n < buf->b_fnum)
7232 n = buf->b_fnum;
7233
7234 rettv->vval.v_number = n;
7235}
7236
7237/*
7238 * "len()" function
7239 */
7240 static void
7241f_len(typval_T *argvars, typval_T *rettv)
7242{
7243 switch (argvars[0].v_type)
7244 {
7245 case VAR_STRING:
7246 case VAR_NUMBER:
7247 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007248 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007249 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007250 case VAR_BLOB:
7251 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7252 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 case VAR_LIST:
7254 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7255 break;
7256 case VAR_DICT:
7257 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7258 break;
7259 case VAR_UNKNOWN:
7260 case VAR_SPECIAL:
7261 case VAR_FLOAT:
7262 case VAR_FUNC:
7263 case VAR_PARTIAL:
7264 case VAR_JOB:
7265 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007266 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 break;
7268 }
7269}
7270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007272libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273{
7274#ifdef FEAT_LIBCALL
7275 char_u *string_in;
7276 char_u **string_result;
7277 int nr_result;
7278#endif
7279
7280 rettv->v_type = type;
7281 if (type != VAR_NUMBER)
7282 rettv->vval.v_string = NULL;
7283
7284 if (check_restricted() || check_secure())
7285 return;
7286
7287#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007288 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7290 {
7291 string_in = NULL;
7292 if (argvars[2].v_type == VAR_STRING)
7293 string_in = argvars[2].vval.v_string;
7294 if (type == VAR_NUMBER)
7295 string_result = NULL;
7296 else
7297 string_result = &rettv->vval.v_string;
7298 if (mch_libcall(argvars[0].vval.v_string,
7299 argvars[1].vval.v_string,
7300 string_in,
7301 argvars[2].vval.v_number,
7302 string_result,
7303 &nr_result) == OK
7304 && type == VAR_NUMBER)
7305 rettv->vval.v_number = nr_result;
7306 }
7307#endif
7308}
7309
7310/*
7311 * "libcall()" function
7312 */
7313 static void
7314f_libcall(typval_T *argvars, typval_T *rettv)
7315{
7316 libcall_common(argvars, rettv, VAR_STRING);
7317}
7318
7319/*
7320 * "libcallnr()" function
7321 */
7322 static void
7323f_libcallnr(typval_T *argvars, typval_T *rettv)
7324{
7325 libcall_common(argvars, rettv, VAR_NUMBER);
7326}
7327
7328/*
7329 * "line(string)" function
7330 */
7331 static void
7332f_line(typval_T *argvars, typval_T *rettv)
7333{
7334 linenr_T lnum = 0;
7335 pos_T *fp;
7336 int fnum;
7337
7338 fp = var2fpos(&argvars[0], TRUE, &fnum);
7339 if (fp != NULL)
7340 lnum = fp->lnum;
7341 rettv->vval.v_number = lnum;
7342}
7343
7344/*
7345 * "line2byte(lnum)" function
7346 */
7347 static void
7348f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7349{
7350#ifndef FEAT_BYTEOFF
7351 rettv->vval.v_number = -1;
7352#else
7353 linenr_T lnum;
7354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007355 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007356 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7357 rettv->vval.v_number = -1;
7358 else
7359 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7360 if (rettv->vval.v_number >= 0)
7361 ++rettv->vval.v_number;
7362#endif
7363}
7364
7365/*
7366 * "lispindent(lnum)" function
7367 */
7368 static void
7369f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7370{
7371#ifdef FEAT_LISP
7372 pos_T pos;
7373 linenr_T lnum;
7374
7375 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7378 {
7379 curwin->w_cursor.lnum = lnum;
7380 rettv->vval.v_number = get_lisp_indent();
7381 curwin->w_cursor = pos;
7382 }
7383 else
7384#endif
7385 rettv->vval.v_number = -1;
7386}
7387
7388/*
7389 * "localtime()" function
7390 */
7391 static void
7392f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7393{
7394 rettv->vval.v_number = (varnumber_T)time(NULL);
7395}
7396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397#ifdef FEAT_FLOAT
7398/*
7399 * "log()" function
7400 */
7401 static void
7402f_log(typval_T *argvars, typval_T *rettv)
7403{
7404 float_T f = 0.0;
7405
7406 rettv->v_type = VAR_FLOAT;
7407 if (get_float_arg(argvars, &f) == OK)
7408 rettv->vval.v_float = log(f);
7409 else
7410 rettv->vval.v_float = 0.0;
7411}
7412
7413/*
7414 * "log10()" function
7415 */
7416 static void
7417f_log10(typval_T *argvars, typval_T *rettv)
7418{
7419 float_T f = 0.0;
7420
7421 rettv->v_type = VAR_FLOAT;
7422 if (get_float_arg(argvars, &f) == OK)
7423 rettv->vval.v_float = log10(f);
7424 else
7425 rettv->vval.v_float = 0.0;
7426}
7427#endif
7428
7429#ifdef FEAT_LUA
7430/*
7431 * "luaeval()" function
7432 */
7433 static void
7434f_luaeval(typval_T *argvars, typval_T *rettv)
7435{
7436 char_u *str;
7437 char_u buf[NUMBUFLEN];
7438
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007439 if (check_restricted() || check_secure())
7440 return;
7441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007442 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 do_luaeval(str, argvars + 1, rettv);
7444}
7445#endif
7446
7447/*
7448 * "map()" function
7449 */
7450 static void
7451f_map(typval_T *argvars, typval_T *rettv)
7452{
7453 filter_map(argvars, rettv, TRUE);
7454}
7455
7456/*
7457 * "maparg()" function
7458 */
7459 static void
7460f_maparg(typval_T *argvars, typval_T *rettv)
7461{
7462 get_maparg(argvars, rettv, TRUE);
7463}
7464
7465/*
7466 * "mapcheck()" function
7467 */
7468 static void
7469f_mapcheck(typval_T *argvars, typval_T *rettv)
7470{
7471 get_maparg(argvars, rettv, FALSE);
7472}
7473
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007474typedef enum
7475{
7476 MATCH_END, /* matchend() */
7477 MATCH_MATCH, /* match() */
7478 MATCH_STR, /* matchstr() */
7479 MATCH_LIST, /* matchlist() */
7480 MATCH_POS /* matchstrpos() */
7481} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482
7483 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007484find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485{
7486 char_u *str = NULL;
7487 long len = 0;
7488 char_u *expr = NULL;
7489 char_u *pat;
7490 regmatch_T regmatch;
7491 char_u patbuf[NUMBUFLEN];
7492 char_u strbuf[NUMBUFLEN];
7493 char_u *save_cpo;
7494 long start = 0;
7495 long nth = 1;
7496 colnr_T startcol = 0;
7497 int match = 0;
7498 list_T *l = NULL;
7499 listitem_T *li = NULL;
7500 long idx = 0;
7501 char_u *tofree = NULL;
7502
7503 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7504 save_cpo = p_cpo;
7505 p_cpo = (char_u *)"";
7506
7507 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007508 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007509 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007510 /* type MATCH_LIST: return empty list when there are no matches.
7511 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 if (rettv_list_alloc(rettv) == FAIL)
7513 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007514 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 && (list_append_string(rettv->vval.v_list,
7516 (char_u *)"", 0) == FAIL
7517 || list_append_number(rettv->vval.v_list,
7518 (varnumber_T)-1) == FAIL
7519 || list_append_number(rettv->vval.v_list,
7520 (varnumber_T)-1) == FAIL
7521 || list_append_number(rettv->vval.v_list,
7522 (varnumber_T)-1) == FAIL))
7523 {
7524 list_free(rettv->vval.v_list);
7525 rettv->vval.v_list = NULL;
7526 goto theend;
7527 }
7528 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007529 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007530 {
7531 rettv->v_type = VAR_STRING;
7532 rettv->vval.v_string = NULL;
7533 }
7534
7535 if (argvars[0].v_type == VAR_LIST)
7536 {
7537 if ((l = argvars[0].vval.v_list) == NULL)
7538 goto theend;
7539 li = l->lv_first;
7540 }
7541 else
7542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007543 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007544 len = (long)STRLEN(str);
7545 }
7546
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007547 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 if (pat == NULL)
7549 goto theend;
7550
7551 if (argvars[2].v_type != VAR_UNKNOWN)
7552 {
7553 int error = FALSE;
7554
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 if (error)
7557 goto theend;
7558 if (l != NULL)
7559 {
7560 li = list_find(l, start);
7561 if (li == NULL)
7562 goto theend;
7563 idx = l->lv_idx; /* use the cached index */
7564 }
7565 else
7566 {
7567 if (start < 0)
7568 start = 0;
7569 if (start > len)
7570 goto theend;
7571 /* When "count" argument is there ignore matches before "start",
7572 * otherwise skip part of the string. Differs when pattern is "^"
7573 * or "\<". */
7574 if (argvars[3].v_type != VAR_UNKNOWN)
7575 startcol = start;
7576 else
7577 {
7578 str += start;
7579 len -= start;
7580 }
7581 }
7582
7583 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007584 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 if (error)
7586 goto theend;
7587 }
7588
7589 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7590 if (regmatch.regprog != NULL)
7591 {
7592 regmatch.rm_ic = p_ic;
7593
7594 for (;;)
7595 {
7596 if (l != NULL)
7597 {
7598 if (li == NULL)
7599 {
7600 match = FALSE;
7601 break;
7602 }
7603 vim_free(tofree);
7604 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7605 if (str == NULL)
7606 break;
7607 }
7608
7609 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7610
7611 if (match && --nth <= 0)
7612 break;
7613 if (l == NULL && !match)
7614 break;
7615
7616 /* Advance to just after the match. */
7617 if (l != NULL)
7618 {
7619 li = li->li_next;
7620 ++idx;
7621 }
7622 else
7623 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 startcol = (colnr_T)(regmatch.startp[0]
7625 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 if (startcol > (colnr_T)len
7627 || str + startcol <= regmatch.startp[0])
7628 {
7629 match = FALSE;
7630 break;
7631 }
7632 }
7633 }
7634
7635 if (match)
7636 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007637 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 {
7639 listitem_T *li1 = rettv->vval.v_list->lv_first;
7640 listitem_T *li2 = li1->li_next;
7641 listitem_T *li3 = li2->li_next;
7642 listitem_T *li4 = li3->li_next;
7643
7644 vim_free(li1->li_tv.vval.v_string);
7645 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7646 (int)(regmatch.endp[0] - regmatch.startp[0]));
7647 li3->li_tv.vval.v_number =
7648 (varnumber_T)(regmatch.startp[0] - expr);
7649 li4->li_tv.vval.v_number =
7650 (varnumber_T)(regmatch.endp[0] - expr);
7651 if (l != NULL)
7652 li2->li_tv.vval.v_number = (varnumber_T)idx;
7653 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007654 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 {
7656 int i;
7657
7658 /* return list with matched string and submatches */
7659 for (i = 0; i < NSUBEXP; ++i)
7660 {
7661 if (regmatch.endp[i] == NULL)
7662 {
7663 if (list_append_string(rettv->vval.v_list,
7664 (char_u *)"", 0) == FAIL)
7665 break;
7666 }
7667 else if (list_append_string(rettv->vval.v_list,
7668 regmatch.startp[i],
7669 (int)(regmatch.endp[i] - regmatch.startp[i]))
7670 == FAIL)
7671 break;
7672 }
7673 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007674 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 {
7676 /* return matched string */
7677 if (l != NULL)
7678 copy_tv(&li->li_tv, rettv);
7679 else
7680 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7681 (int)(regmatch.endp[0] - regmatch.startp[0]));
7682 }
7683 else if (l != NULL)
7684 rettv->vval.v_number = idx;
7685 else
7686 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007687 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 rettv->vval.v_number =
7689 (varnumber_T)(regmatch.startp[0] - str);
7690 else
7691 rettv->vval.v_number =
7692 (varnumber_T)(regmatch.endp[0] - str);
7693 rettv->vval.v_number += (varnumber_T)(str - expr);
7694 }
7695 }
7696 vim_regfree(regmatch.regprog);
7697 }
7698
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007699theend:
7700 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 /* matchstrpos() without a list: drop the second item. */
7702 listitem_remove(rettv->vval.v_list,
7703 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 vim_free(tofree);
7705 p_cpo = save_cpo;
7706}
7707
7708/*
7709 * "match()" function
7710 */
7711 static void
7712f_match(typval_T *argvars, typval_T *rettv)
7713{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007714 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715}
7716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717/*
7718 * "matchend()" function
7719 */
7720 static void
7721f_matchend(typval_T *argvars, typval_T *rettv)
7722{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007723 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724}
7725
7726/*
7727 * "matchlist()" function
7728 */
7729 static void
7730f_matchlist(typval_T *argvars, typval_T *rettv)
7731{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007732 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733}
7734
7735/*
7736 * "matchstr()" function
7737 */
7738 static void
7739f_matchstr(typval_T *argvars, typval_T *rettv)
7740{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007741 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742}
7743
7744/*
7745 * "matchstrpos()" function
7746 */
7747 static void
7748f_matchstrpos(typval_T *argvars, typval_T *rettv)
7749{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007750 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751}
7752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007753 static void
7754max_min(typval_T *argvars, typval_T *rettv, int domax)
7755{
7756 varnumber_T n = 0;
7757 varnumber_T i;
7758 int error = FALSE;
7759
7760 if (argvars[0].v_type == VAR_LIST)
7761 {
7762 list_T *l;
7763 listitem_T *li;
7764
7765 l = argvars[0].vval.v_list;
7766 if (l != NULL)
7767 {
7768 li = l->lv_first;
7769 if (li != NULL)
7770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007771 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 for (;;)
7773 {
7774 li = li->li_next;
7775 if (li == NULL)
7776 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007777 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 if (domax ? i > n : i < n)
7779 n = i;
7780 }
7781 }
7782 }
7783 }
7784 else if (argvars[0].v_type == VAR_DICT)
7785 {
7786 dict_T *d;
7787 int first = TRUE;
7788 hashitem_T *hi;
7789 int todo;
7790
7791 d = argvars[0].vval.v_dict;
7792 if (d != NULL)
7793 {
7794 todo = (int)d->dv_hashtab.ht_used;
7795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7796 {
7797 if (!HASHITEM_EMPTY(hi))
7798 {
7799 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007800 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801 if (first)
7802 {
7803 n = i;
7804 first = FALSE;
7805 }
7806 else if (domax ? i > n : i < n)
7807 n = i;
7808 }
7809 }
7810 }
7811 }
7812 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007813 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 rettv->vval.v_number = error ? 0 : n;
7815}
7816
7817/*
7818 * "max()" function
7819 */
7820 static void
7821f_max(typval_T *argvars, typval_T *rettv)
7822{
7823 max_min(argvars, rettv, TRUE);
7824}
7825
7826/*
7827 * "min()" function
7828 */
7829 static void
7830f_min(typval_T *argvars, typval_T *rettv)
7831{
7832 max_min(argvars, rettv, FALSE);
7833}
7834
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835/*
7836 * Create the directory in which "dir" is located, and higher levels when
7837 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007838 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 */
7840 static int
7841mkdir_recurse(char_u *dir, int prot)
7842{
7843 char_u *p;
7844 char_u *updir;
7845 int r = FAIL;
7846
7847 /* Get end of directory name in "dir".
7848 * We're done when it's "/" or "c:/". */
7849 p = gettail_sep(dir);
7850 if (p <= get_past_head(dir))
7851 return OK;
7852
7853 /* If the directory exists we're done. Otherwise: create it.*/
7854 updir = vim_strnsave(dir, (int)(p - dir));
7855 if (updir == NULL)
7856 return FAIL;
7857 if (mch_isdir(updir))
7858 r = OK;
7859 else if (mkdir_recurse(updir, prot) == OK)
7860 r = vim_mkdir_emsg(updir, prot);
7861 vim_free(updir);
7862 return r;
7863}
7864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865/*
7866 * "mkdir()" function
7867 */
7868 static void
7869f_mkdir(typval_T *argvars, typval_T *rettv)
7870{
7871 char_u *dir;
7872 char_u buf[NUMBUFLEN];
7873 int prot = 0755;
7874
7875 rettv->vval.v_number = FAIL;
7876 if (check_restricted() || check_secure())
7877 return;
7878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007879 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007881 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007883 if (*gettail(dir) == NUL)
7884 /* remove trailing slashes */
7885 *gettail_sep(dir) = NUL;
7886
7887 if (argvars[1].v_type != VAR_UNKNOWN)
7888 {
7889 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007891 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007892 if (prot == -1)
7893 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007895 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007896 {
7897 if (mch_isdir(dir))
7898 {
7899 /* With the "p" flag it's OK if the dir already exists. */
7900 rettv->vval.v_number = OK;
7901 return;
7902 }
7903 mkdir_recurse(dir, prot);
7904 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007906 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908
7909/*
7910 * "mode()" function
7911 */
7912 static void
7913f_mode(typval_T *argvars, typval_T *rettv)
7914{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007915 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916
Bram Moolenaar612cc382018-07-29 15:34:26 +02007917 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918
7919 if (time_for_testing == 93784)
7920 {
7921 /* Testing the two-character code. */
7922 buf[0] = 'x';
7923 buf[1] = '!';
7924 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007925#ifdef FEAT_TERMINAL
7926 else if (term_use_loop())
7927 buf[0] = 't';
7928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 else if (VIsual_active)
7930 {
7931 if (VIsual_select)
7932 buf[0] = VIsual_mode + 's' - 'v';
7933 else
7934 buf[0] = VIsual_mode;
7935 }
7936 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7937 || State == CONFIRM)
7938 {
7939 buf[0] = 'r';
7940 if (State == ASKMORE)
7941 buf[1] = 'm';
7942 else if (State == CONFIRM)
7943 buf[1] = '?';
7944 }
7945 else if (State == EXTERNCMD)
7946 buf[0] = '!';
7947 else if (State & INSERT)
7948 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 if (State & VREPLACE_FLAG)
7950 {
7951 buf[0] = 'R';
7952 buf[1] = 'v';
7953 }
7954 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01007955 {
7956 if (State & REPLACE_FLAG)
7957 buf[0] = 'R';
7958 else
7959 buf[0] = 'i';
7960#ifdef FEAT_INS_EXPAND
7961 if (ins_compl_active())
7962 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01007963 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01007964 buf[1] = 'x';
7965#endif
7966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007968 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 {
7970 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007971 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007973 else if (exmode_active == EXMODE_NORMAL)
7974 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 }
7976 else
7977 {
7978 buf[0] = 'n';
7979 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007980 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007982 // to be able to detect force-linewise/blockwise/characterwise operations
7983 buf[2] = motion_force;
7984 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02007985 else if (restart_edit == 'I' || restart_edit == 'R'
7986 || restart_edit == 'V')
7987 {
7988 buf[1] = 'i';
7989 buf[2] = restart_edit;
7990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 }
7992
7993 /* Clear out the minor mode when the argument is not a non-zero number or
7994 * non-empty string. */
7995 if (!non_zero_arg(&argvars[0]))
7996 buf[1] = NUL;
7997
7998 rettv->vval.v_string = vim_strsave(buf);
7999 rettv->v_type = VAR_STRING;
8000}
8001
8002#if defined(FEAT_MZSCHEME) || defined(PROTO)
8003/*
8004 * "mzeval()" function
8005 */
8006 static void
8007f_mzeval(typval_T *argvars, typval_T *rettv)
8008{
8009 char_u *str;
8010 char_u buf[NUMBUFLEN];
8011
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008012 if (check_restricted() || check_secure())
8013 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008014 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 do_mzeval(str, rettv);
8016}
8017
8018 void
8019mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8020{
8021 typval_T argvars[3];
8022
8023 argvars[0].v_type = VAR_STRING;
8024 argvars[0].vval.v_string = name;
8025 copy_tv(args, &argvars[1]);
8026 argvars[2].v_type = VAR_UNKNOWN;
8027 f_call(argvars, rettv);
8028 clear_tv(&argvars[1]);
8029}
8030#endif
8031
8032/*
8033 * "nextnonblank()" function
8034 */
8035 static void
8036f_nextnonblank(typval_T *argvars, typval_T *rettv)
8037{
8038 linenr_T lnum;
8039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008040 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 {
8042 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8043 {
8044 lnum = 0;
8045 break;
8046 }
8047 if (*skipwhite(ml_get(lnum)) != NUL)
8048 break;
8049 }
8050 rettv->vval.v_number = lnum;
8051}
8052
8053/*
8054 * "nr2char()" function
8055 */
8056 static void
8057f_nr2char(typval_T *argvars, typval_T *rettv)
8058{
8059 char_u buf[NUMBUFLEN];
8060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (has_mbyte)
8062 {
8063 int utf8 = 0;
8064
8065 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008066 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008068 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008070 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 }
8072 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008074 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 buf[1] = NUL;
8076 }
8077 rettv->v_type = VAR_STRING;
8078 rettv->vval.v_string = vim_strsave(buf);
8079}
8080
8081/*
8082 * "or(expr, expr)" function
8083 */
8084 static void
8085f_or(typval_T *argvars, typval_T *rettv)
8086{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008087 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8088 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089}
8090
8091/*
8092 * "pathshorten()" function
8093 */
8094 static void
8095f_pathshorten(typval_T *argvars, typval_T *rettv)
8096{
8097 char_u *p;
8098
8099 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008100 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008101 if (p == NULL)
8102 rettv->vval.v_string = NULL;
8103 else
8104 {
8105 p = vim_strsave(p);
8106 rettv->vval.v_string = p;
8107 if (p != NULL)
8108 shorten_dir(p);
8109 }
8110}
8111
8112#ifdef FEAT_PERL
8113/*
8114 * "perleval()" function
8115 */
8116 static void
8117f_perleval(typval_T *argvars, typval_T *rettv)
8118{
8119 char_u *str;
8120 char_u buf[NUMBUFLEN];
8121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008122 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 do_perleval(str, rettv);
8124}
8125#endif
8126
8127#ifdef FEAT_FLOAT
8128/*
8129 * "pow()" function
8130 */
8131 static void
8132f_pow(typval_T *argvars, typval_T *rettv)
8133{
8134 float_T fx = 0.0, fy = 0.0;
8135
8136 rettv->v_type = VAR_FLOAT;
8137 if (get_float_arg(argvars, &fx) == OK
8138 && get_float_arg(&argvars[1], &fy) == OK)
8139 rettv->vval.v_float = pow(fx, fy);
8140 else
8141 rettv->vval.v_float = 0.0;
8142}
8143#endif
8144
8145/*
8146 * "prevnonblank()" function
8147 */
8148 static void
8149f_prevnonblank(typval_T *argvars, typval_T *rettv)
8150{
8151 linenr_T lnum;
8152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008153 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8155 lnum = 0;
8156 else
8157 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8158 --lnum;
8159 rettv->vval.v_number = lnum;
8160}
8161
8162/* This dummy va_list is here because:
8163 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8164 * - locally in the function results in a "used before set" warning
8165 * - using va_start() to initialize it gives "function with fixed args" error */
8166static va_list ap;
8167
8168/*
8169 * "printf()" function
8170 */
8171 static void
8172f_printf(typval_T *argvars, typval_T *rettv)
8173{
8174 char_u buf[NUMBUFLEN];
8175 int len;
8176 char_u *s;
8177 int saved_did_emsg = did_emsg;
8178 char *fmt;
8179
8180 rettv->v_type = VAR_STRING;
8181 rettv->vval.v_string = NULL;
8182
8183 /* Get the required length, allocate the buffer and do it for real. */
8184 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008185 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008186 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 if (!did_emsg)
8188 {
8189 s = alloc(len + 1);
8190 if (s != NULL)
8191 {
8192 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008193 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8194 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 }
8196 }
8197 did_emsg |= saved_did_emsg;
8198}
8199
8200/*
8201 * "pumvisible()" function
8202 */
8203 static void
8204f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8205{
8206#ifdef FEAT_INS_EXPAND
8207 if (pum_visible())
8208 rettv->vval.v_number = 1;
8209#endif
8210}
8211
8212#ifdef FEAT_PYTHON3
8213/*
8214 * "py3eval()" function
8215 */
8216 static void
8217f_py3eval(typval_T *argvars, typval_T *rettv)
8218{
8219 char_u *str;
8220 char_u buf[NUMBUFLEN];
8221
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008222 if (check_restricted() || check_secure())
8223 return;
8224
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008225 if (p_pyx == 0)
8226 p_pyx = 3;
8227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008228 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229 do_py3eval(str, rettv);
8230}
8231#endif
8232
8233#ifdef FEAT_PYTHON
8234/*
8235 * "pyeval()" function
8236 */
8237 static void
8238f_pyeval(typval_T *argvars, typval_T *rettv)
8239{
8240 char_u *str;
8241 char_u buf[NUMBUFLEN];
8242
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008243 if (check_restricted() || check_secure())
8244 return;
8245
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008246 if (p_pyx == 0)
8247 p_pyx = 2;
8248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 do_pyeval(str, rettv);
8251}
8252#endif
8253
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008254#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8255/*
8256 * "pyxeval()" function
8257 */
8258 static void
8259f_pyxeval(typval_T *argvars, typval_T *rettv)
8260{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008261 if (check_restricted() || check_secure())
8262 return;
8263
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008264# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8265 init_pyxversion();
8266 if (p_pyx == 2)
8267 f_pyeval(argvars, rettv);
8268 else
8269 f_py3eval(argvars, rettv);
8270# elif defined(FEAT_PYTHON)
8271 f_pyeval(argvars, rettv);
8272# elif defined(FEAT_PYTHON3)
8273 f_py3eval(argvars, rettv);
8274# endif
8275}
8276#endif
8277
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278/*
8279 * "range()" function
8280 */
8281 static void
8282f_range(typval_T *argvars, typval_T *rettv)
8283{
8284 varnumber_T start;
8285 varnumber_T end;
8286 varnumber_T stride = 1;
8287 varnumber_T i;
8288 int error = FALSE;
8289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008290 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 if (argvars[1].v_type == VAR_UNKNOWN)
8292 {
8293 end = start - 1;
8294 start = 0;
8295 }
8296 else
8297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008298 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008300 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 }
8302
8303 if (error)
8304 return; /* type error; errmsg already given */
8305 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 else
8310 {
8311 if (rettv_list_alloc(rettv) == OK)
8312 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8313 if (list_append_number(rettv->vval.v_list,
8314 (varnumber_T)i) == FAIL)
8315 break;
8316 }
8317}
8318
8319/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008320 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008321 */
8322 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008323readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008324{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008325 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008326 typval_T save_val;
8327 typval_T rettv;
8328 typval_T argv[2];
8329 int retval = 0;
8330 int error = FALSE;
8331
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008332 if (expr->v_type == VAR_UNKNOWN)
8333 return 1;
8334
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008335 prepare_vimvar(VV_VAL, &save_val);
8336 set_vim_var_string(VV_VAL, name, -1);
8337 argv[0].v_type = VAR_STRING;
8338 argv[0].vval.v_string = name;
8339
8340 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8341 goto theend;
8342
8343 retval = tv_get_number_chk(&rettv, &error);
8344 if (error)
8345 retval = -1;
8346 clear_tv(&rettv);
8347
8348theend:
8349 set_vim_var_string(VV_VAL, NULL, 0);
8350 restore_vimvar(VV_VAL, &save_val);
8351 return retval;
8352}
8353
8354/*
8355 * "readdir()" function
8356 */
8357 static void
8358f_readdir(typval_T *argvars, typval_T *rettv)
8359{
8360 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008361 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008362 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008363 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008364 garray_T ga;
8365 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008366
8367 if (rettv_list_alloc(rettv) == FAIL)
8368 return;
8369 path = tv_get_string(&argvars[0]);
8370 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008371
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008372 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8373 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008374 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008375 for (i = 0; i < ga.ga_len; i++)
8376 {
8377 p = ((char_u **)ga.ga_data)[i];
8378 list_append_string(rettv->vval.v_list, p, -1);
8379 }
8380 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008381 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008382}
8383
8384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 * "readfile()" function
8386 */
8387 static void
8388f_readfile(typval_T *argvars, typval_T *rettv)
8389{
8390 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008391 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 int failed = FALSE;
8393 char_u *fname;
8394 FILE *fd;
8395 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8396 int io_size = sizeof(buf);
8397 int readlen; /* size of last fread() */
8398 char_u *prev = NULL; /* previously read bytes, if any */
8399 long prevlen = 0; /* length of data in prev */
8400 long prevsize = 0; /* size of prev buffer */
8401 long maxline = MAXLNUM;
8402 long cnt = 0;
8403 char_u *p; /* position in buf */
8404 char_u *start; /* start of current line */
8405
8406 if (argvars[1].v_type != VAR_UNKNOWN)
8407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008410 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8411 blob = TRUE;
8412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008414 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 }
8416
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008417 if (blob)
8418 {
8419 if (rettv_blob_alloc(rettv) == FAIL)
8420 return;
8421 }
8422 else
8423 {
8424 if (rettv_list_alloc(rettv) == FAIL)
8425 return;
8426 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427
8428 /* Always open the file in binary mode, library functions have a mind of
8429 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008430 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 return;
8435 }
8436
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008437 if (blob)
8438 {
8439 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008441 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008442 blob_free(rettv->vval.v_blob);
8443 }
8444 fclose(fd);
8445 return;
8446 }
8447
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008448 while (cnt < maxline || maxline < 0)
8449 {
8450 readlen = (int)fread(buf, 1, io_size, fd);
8451
8452 /* This for loop processes what was read, but is also entered at end
8453 * of file so that either:
8454 * - an incomplete line gets written
8455 * - a "binary" file gets an empty line at the end if it ends in a
8456 * newline. */
8457 for (p = buf, start = buf;
8458 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8459 ++p)
8460 {
8461 if (*p == '\n' || readlen <= 0)
8462 {
8463 listitem_T *li;
8464 char_u *s = NULL;
8465 long_u len = p - start;
8466
8467 /* Finished a line. Remove CRs before NL. */
8468 if (readlen > 0 && !binary)
8469 {
8470 while (len > 0 && start[len - 1] == '\r')
8471 --len;
8472 /* removal may cross back to the "prev" string */
8473 if (len == 0)
8474 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8475 --prevlen;
8476 }
8477 if (prevlen == 0)
8478 s = vim_strnsave(start, (int)len);
8479 else
8480 {
8481 /* Change "prev" buffer to be the right size. This way
8482 * the bytes are only copied once, and very long lines are
8483 * allocated only once. */
8484 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8485 {
8486 mch_memmove(s + prevlen, start, len);
8487 s[prevlen + len] = NUL;
8488 prev = NULL; /* the list will own the string */
8489 prevlen = prevsize = 0;
8490 }
8491 }
8492 if (s == NULL)
8493 {
8494 do_outofmem_msg((long_u) prevlen + len + 1);
8495 failed = TRUE;
8496 break;
8497 }
8498
8499 if ((li = listitem_alloc()) == NULL)
8500 {
8501 vim_free(s);
8502 failed = TRUE;
8503 break;
8504 }
8505 li->li_tv.v_type = VAR_STRING;
8506 li->li_tv.v_lock = 0;
8507 li->li_tv.vval.v_string = s;
8508 list_append(rettv->vval.v_list, li);
8509
8510 start = p + 1; /* step over newline */
8511 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8512 break;
8513 }
8514 else if (*p == NUL)
8515 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8517 * when finding the BF and check the previous two bytes. */
8518 else if (*p == 0xbf && enc_utf8 && !binary)
8519 {
8520 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8521 * + 1, these may be in the "prev" string. */
8522 char_u back1 = p >= buf + 1 ? p[-1]
8523 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8524 char_u back2 = p >= buf + 2 ? p[-2]
8525 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8526 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8527
8528 if (back2 == 0xef && back1 == 0xbb)
8529 {
8530 char_u *dest = p - 2;
8531
8532 /* Usually a BOM is at the beginning of a file, and so at
8533 * the beginning of a line; then we can just step over it.
8534 */
8535 if (start == dest)
8536 start = p + 1;
8537 else
8538 {
8539 /* have to shuffle buf to close gap */
8540 int adjust_prevlen = 0;
8541
8542 if (dest < buf)
8543 {
8544 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8545 dest = buf;
8546 }
8547 if (readlen > p - buf + 1)
8548 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8549 readlen -= 3 - adjust_prevlen;
8550 prevlen -= adjust_prevlen;
8551 p = dest - 1;
8552 }
8553 }
8554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 } /* for */
8556
8557 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8558 break;
8559 if (start < p)
8560 {
8561 /* There's part of a line in buf, store it in "prev". */
8562 if (p - start + prevlen >= prevsize)
8563 {
8564 /* need bigger "prev" buffer */
8565 char_u *newprev;
8566
8567 /* A common use case is ordinary text files and "prev" gets a
8568 * fragment of a line, so the first allocation is made
8569 * small, to avoid repeatedly 'allocing' large and
8570 * 'reallocing' small. */
8571 if (prevsize == 0)
8572 prevsize = (long)(p - start);
8573 else
8574 {
8575 long grow50pc = (prevsize * 3) / 2;
8576 long growmin = (long)((p - start) * 2 + prevlen);
8577 prevsize = grow50pc > growmin ? grow50pc : growmin;
8578 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008579 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580 if (newprev == NULL)
8581 {
8582 do_outofmem_msg((long_u)prevsize);
8583 failed = TRUE;
8584 break;
8585 }
8586 prev = newprev;
8587 }
8588 /* Add the line part to end of "prev". */
8589 mch_memmove(prev + prevlen, start, p - start);
8590 prevlen += (long)(p - start);
8591 }
8592 } /* while */
8593
8594 /*
8595 * For a negative line count use only the lines at the end of the file,
8596 * free the rest.
8597 */
8598 if (!failed && maxline < 0)
8599 while (cnt > -maxline)
8600 {
8601 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8602 --cnt;
8603 }
8604
8605 if (failed)
8606 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008607 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008609 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 }
8611
8612 vim_free(prev);
8613 fclose(fd);
8614}
8615
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008616 static void
8617return_register(int regname, typval_T *rettv)
8618{
8619 char_u buf[2] = {0, 0};
8620
8621 buf[0] = (char_u)regname;
8622 rettv->v_type = VAR_STRING;
8623 rettv->vval.v_string = vim_strsave(buf);
8624}
8625
8626/*
8627 * "reg_executing()" function
8628 */
8629 static void
8630f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8631{
8632 return_register(reg_executing, rettv);
8633}
8634
8635/*
8636 * "reg_recording()" function
8637 */
8638 static void
8639f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8640{
8641 return_register(reg_recording, rettv);
8642}
8643
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645/*
8646 * Convert a List to proftime_T.
8647 * Return FAIL when there is something wrong.
8648 */
8649 static int
8650list2proftime(typval_T *arg, proftime_T *tm)
8651{
8652 long n1, n2;
8653 int error = FALSE;
8654
8655 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8656 || arg->vval.v_list->lv_len != 2)
8657 return FAIL;
8658 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8659 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008660# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 tm->HighPart = n1;
8662 tm->LowPart = n2;
8663# else
8664 tm->tv_sec = n1;
8665 tm->tv_usec = n2;
8666# endif
8667 return error ? FAIL : OK;
8668}
8669#endif /* FEAT_RELTIME */
8670
8671/*
8672 * "reltime()" function
8673 */
8674 static void
8675f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8676{
8677#ifdef FEAT_RELTIME
8678 proftime_T res;
8679 proftime_T start;
8680
8681 if (argvars[0].v_type == VAR_UNKNOWN)
8682 {
8683 /* No arguments: get current time. */
8684 profile_start(&res);
8685 }
8686 else if (argvars[1].v_type == VAR_UNKNOWN)
8687 {
8688 if (list2proftime(&argvars[0], &res) == FAIL)
8689 return;
8690 profile_end(&res);
8691 }
8692 else
8693 {
8694 /* Two arguments: compute the difference. */
8695 if (list2proftime(&argvars[0], &start) == FAIL
8696 || list2proftime(&argvars[1], &res) == FAIL)
8697 return;
8698 profile_sub(&res, &start);
8699 }
8700
8701 if (rettv_list_alloc(rettv) == OK)
8702 {
8703 long n1, n2;
8704
Bram Moolenaar4f974752019-02-17 17:44:42 +01008705# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 n1 = res.HighPart;
8707 n2 = res.LowPart;
8708# else
8709 n1 = res.tv_sec;
8710 n2 = res.tv_usec;
8711# endif
8712 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8713 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8714 }
8715#endif
8716}
8717
8718#ifdef FEAT_FLOAT
8719/*
8720 * "reltimefloat()" function
8721 */
8722 static void
8723f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8724{
8725# ifdef FEAT_RELTIME
8726 proftime_T tm;
8727# endif
8728
8729 rettv->v_type = VAR_FLOAT;
8730 rettv->vval.v_float = 0;
8731# ifdef FEAT_RELTIME
8732 if (list2proftime(&argvars[0], &tm) == OK)
8733 rettv->vval.v_float = profile_float(&tm);
8734# endif
8735}
8736#endif
8737
8738/*
8739 * "reltimestr()" function
8740 */
8741 static void
8742f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8743{
8744#ifdef FEAT_RELTIME
8745 proftime_T tm;
8746#endif
8747
8748 rettv->v_type = VAR_STRING;
8749 rettv->vval.v_string = NULL;
8750#ifdef FEAT_RELTIME
8751 if (list2proftime(&argvars[0], &tm) == OK)
8752 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8753#endif
8754}
8755
8756#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 static void
8758make_connection(void)
8759{
8760 if (X_DISPLAY == NULL
8761# ifdef FEAT_GUI
8762 && !gui.in_use
8763# endif
8764 )
8765 {
8766 x_force_connect = TRUE;
8767 setup_term_clip();
8768 x_force_connect = FALSE;
8769 }
8770}
8771
8772 static int
8773check_connection(void)
8774{
8775 make_connection();
8776 if (X_DISPLAY == NULL)
8777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008778 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 return FAIL;
8780 }
8781 return OK;
8782}
8783#endif
8784
8785#ifdef FEAT_CLIENTSERVER
8786 static void
8787remote_common(typval_T *argvars, typval_T *rettv, int expr)
8788{
8789 char_u *server_name;
8790 char_u *keys;
8791 char_u *r = NULL;
8792 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008793 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008794# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 HWND w;
8796# else
8797 Window w;
8798# endif
8799
8800 if (check_restricted() || check_secure())
8801 return;
8802
8803# ifdef FEAT_X11
8804 if (check_connection() == FAIL)
8805 return;
8806# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008807 if (argvars[2].v_type != VAR_UNKNOWN
8808 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008809 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008811 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 if (server_name == NULL)
8813 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008814 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008815# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008816 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008818 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8819 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820# endif
8821 {
8822 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008824 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008825 vim_free(r);
8826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008828 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 return;
8830 }
8831
8832 rettv->vval.v_string = r;
8833
8834 if (argvars[2].v_type != VAR_UNKNOWN)
8835 {
8836 dictitem_T v;
8837 char_u str[30];
8838 char_u *idvar;
8839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008841 if (idvar != NULL && *idvar != NUL)
8842 {
8843 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8844 v.di_tv.v_type = VAR_STRING;
8845 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008847 vim_free(v.di_tv.vval.v_string);
8848 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 }
8850}
8851#endif
8852
8853/*
8854 * "remote_expr()" function
8855 */
8856 static void
8857f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8858{
8859 rettv->v_type = VAR_STRING;
8860 rettv->vval.v_string = NULL;
8861#ifdef FEAT_CLIENTSERVER
8862 remote_common(argvars, rettv, TRUE);
8863#endif
8864}
8865
8866/*
8867 * "remote_foreground()" function
8868 */
8869 static void
8870f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8871{
8872#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008873# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 /* On Win32 it's done in this application. */
8875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008876 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877
8878 if (server_name != NULL)
8879 serverForeground(server_name);
8880 }
8881# else
8882 /* Send a foreground() expression to the server. */
8883 argvars[1].v_type = VAR_STRING;
8884 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8885 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008886 rettv->v_type = VAR_STRING;
8887 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 remote_common(argvars, rettv, TRUE);
8889 vim_free(argvars[1].vval.v_string);
8890# endif
8891#endif
8892}
8893
8894 static void
8895f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8896{
8897#ifdef FEAT_CLIENTSERVER
8898 dictitem_T v;
8899 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008900# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901 long_u n = 0;
8902# endif
8903 char_u *serverid;
8904
8905 if (check_restricted() || check_secure())
8906 {
8907 rettv->vval.v_number = -1;
8908 return;
8909 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008910 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 if (serverid == NULL)
8912 {
8913 rettv->vval.v_number = -1;
8914 return; /* type error; errmsg already given */
8915 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008916# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8918 if (n == 0)
8919 rettv->vval.v_number = -1;
8920 else
8921 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008922 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 rettv->vval.v_number = (s != NULL);
8924 }
8925# else
8926 if (check_connection() == FAIL)
8927 return;
8928
8929 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8930 serverStrToWin(serverid), &s);
8931# endif
8932
8933 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8934 {
8935 char_u *retvar;
8936
8937 v.di_tv.v_type = VAR_STRING;
8938 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008939 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 if (retvar != NULL)
8941 set_var(retvar, &v.di_tv, FALSE);
8942 vim_free(v.di_tv.vval.v_string);
8943 }
8944#else
8945 rettv->vval.v_number = -1;
8946#endif
8947}
8948
8949 static void
8950f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8951{
8952 char_u *r = NULL;
8953
8954#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008955 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956
8957 if (serverid != NULL && !check_restricted() && !check_secure())
8958 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008959 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008960# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008961 /* The server's HWND is encoded in the 'id' parameter */
8962 long_u n = 0;
8963# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008964
8965 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008966 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008967
Bram Moolenaar4f974752019-02-17 17:44:42 +01008968# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8970 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008971 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 if (r == NULL)
8973# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008974 if (check_connection() == FAIL
8975 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8976 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008978 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 }
8980#endif
8981 rettv->v_type = VAR_STRING;
8982 rettv->vval.v_string = r;
8983}
8984
8985/*
8986 * "remote_send()" function
8987 */
8988 static void
8989f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8990{
8991 rettv->v_type = VAR_STRING;
8992 rettv->vval.v_string = NULL;
8993#ifdef FEAT_CLIENTSERVER
8994 remote_common(argvars, rettv, FALSE);
8995#endif
8996}
8997
8998/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008999 * "remote_startserver()" function
9000 */
9001 static void
9002f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9003{
9004#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009005 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009006
9007 if (server == NULL)
9008 return; /* type error; errmsg already given */
9009 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009010 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009011 else
9012 {
9013# ifdef FEAT_X11
9014 if (check_connection() == OK)
9015 serverRegisterName(X_DISPLAY, server);
9016# else
9017 serverSetName(server);
9018# endif
9019 }
9020#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009021 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009022#endif
9023}
9024
9025/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 * "remove()" function
9027 */
9028 static void
9029f_remove(typval_T *argvars, typval_T *rettv)
9030{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9032
9033 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009034 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009035 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009036 blob_remove(argvars, rettv);
9037 else if (argvars[0].v_type == VAR_LIST)
9038 list_remove(argvars, rettv, arg_errmsg);
9039 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009040 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041}
9042
9043/*
9044 * "rename({from}, {to})" function
9045 */
9046 static void
9047f_rename(typval_T *argvars, typval_T *rettv)
9048{
9049 char_u buf[NUMBUFLEN];
9050
9051 if (check_restricted() || check_secure())
9052 rettv->vval.v_number = -1;
9053 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009054 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9055 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056}
9057
9058/*
9059 * "repeat()" function
9060 */
9061 static void
9062f_repeat(typval_T *argvars, typval_T *rettv)
9063{
9064 char_u *p;
9065 int n;
9066 int slen;
9067 int len;
9068 char_u *r;
9069 int i;
9070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009071 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 if (argvars[0].v_type == VAR_LIST)
9073 {
9074 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9075 while (n-- > 0)
9076 if (list_extend(rettv->vval.v_list,
9077 argvars[0].vval.v_list, NULL) == FAIL)
9078 break;
9079 }
9080 else
9081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 rettv->v_type = VAR_STRING;
9084 rettv->vval.v_string = NULL;
9085
9086 slen = (int)STRLEN(p);
9087 len = slen * n;
9088 if (len <= 0)
9089 return;
9090
9091 r = alloc(len + 1);
9092 if (r != NULL)
9093 {
9094 for (i = 0; i < n; i++)
9095 mch_memmove(r + i * slen, p, (size_t)slen);
9096 r[len] = NUL;
9097 }
9098
9099 rettv->vval.v_string = r;
9100 }
9101}
9102
9103/*
9104 * "resolve()" function
9105 */
9106 static void
9107f_resolve(typval_T *argvars, typval_T *rettv)
9108{
9109 char_u *p;
9110#ifdef HAVE_READLINK
9111 char_u *buf = NULL;
9112#endif
9113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009114 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115#ifdef FEAT_SHORTCUT
9116 {
9117 char_u *v = NULL;
9118
Bram Moolenaardce1e892019-02-10 23:18:53 +01009119 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120 if (v != NULL)
9121 rettv->vval.v_string = v;
9122 else
9123 rettv->vval.v_string = vim_strsave(p);
9124 }
9125#else
9126# ifdef HAVE_READLINK
9127 {
9128 char_u *cpy;
9129 int len;
9130 char_u *remain = NULL;
9131 char_u *q;
9132 int is_relative_to_current = FALSE;
9133 int has_trailing_pathsep = FALSE;
9134 int limit = 100;
9135
9136 p = vim_strsave(p);
9137
9138 if (p[0] == '.' && (vim_ispathsep(p[1])
9139 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9140 is_relative_to_current = TRUE;
9141
9142 len = STRLEN(p);
9143 if (len > 0 && after_pathsep(p, p + len))
9144 {
9145 has_trailing_pathsep = TRUE;
9146 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9147 }
9148
9149 q = getnextcomp(p);
9150 if (*q != NUL)
9151 {
9152 /* Separate the first path component in "p", and keep the
9153 * remainder (beginning with the path separator). */
9154 remain = vim_strsave(q - 1);
9155 q[-1] = NUL;
9156 }
9157
9158 buf = alloc(MAXPATHL + 1);
9159 if (buf == NULL)
9160 goto fail;
9161
9162 for (;;)
9163 {
9164 for (;;)
9165 {
9166 len = readlink((char *)p, (char *)buf, MAXPATHL);
9167 if (len <= 0)
9168 break;
9169 buf[len] = NUL;
9170
9171 if (limit-- == 0)
9172 {
9173 vim_free(p);
9174 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009175 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009176 rettv->vval.v_string = NULL;
9177 goto fail;
9178 }
9179
9180 /* Ensure that the result will have a trailing path separator
9181 * if the argument has one. */
9182 if (remain == NULL && has_trailing_pathsep)
9183 add_pathsep(buf);
9184
9185 /* Separate the first path component in the link value and
9186 * concatenate the remainders. */
9187 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9188 if (*q != NUL)
9189 {
9190 if (remain == NULL)
9191 remain = vim_strsave(q - 1);
9192 else
9193 {
9194 cpy = concat_str(q - 1, remain);
9195 if (cpy != NULL)
9196 {
9197 vim_free(remain);
9198 remain = cpy;
9199 }
9200 }
9201 q[-1] = NUL;
9202 }
9203
9204 q = gettail(p);
9205 if (q > p && *q == NUL)
9206 {
9207 /* Ignore trailing path separator. */
9208 q[-1] = NUL;
9209 q = gettail(p);
9210 }
9211 if (q > p && !mch_isFullName(buf))
9212 {
9213 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009214 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 if (cpy != NULL)
9216 {
9217 STRCPY(cpy, p);
9218 STRCPY(gettail(cpy), buf);
9219 vim_free(p);
9220 p = cpy;
9221 }
9222 }
9223 else
9224 {
9225 vim_free(p);
9226 p = vim_strsave(buf);
9227 }
9228 }
9229
9230 if (remain == NULL)
9231 break;
9232
9233 /* Append the first path component of "remain" to "p". */
9234 q = getnextcomp(remain + 1);
9235 len = q - remain - (*q != NUL);
9236 cpy = vim_strnsave(p, STRLEN(p) + len);
9237 if (cpy != NULL)
9238 {
9239 STRNCAT(cpy, remain, len);
9240 vim_free(p);
9241 p = cpy;
9242 }
9243 /* Shorten "remain". */
9244 if (*q != NUL)
9245 STRMOVE(remain, q - 1);
9246 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009247 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248 }
9249
9250 /* If the result is a relative path name, make it explicitly relative to
9251 * the current directory if and only if the argument had this form. */
9252 if (!vim_ispathsep(*p))
9253 {
9254 if (is_relative_to_current
9255 && *p != NUL
9256 && !(p[0] == '.'
9257 && (p[1] == NUL
9258 || vim_ispathsep(p[1])
9259 || (p[1] == '.'
9260 && (p[2] == NUL
9261 || vim_ispathsep(p[2]))))))
9262 {
9263 /* Prepend "./". */
9264 cpy = concat_str((char_u *)"./", p);
9265 if (cpy != NULL)
9266 {
9267 vim_free(p);
9268 p = cpy;
9269 }
9270 }
9271 else if (!is_relative_to_current)
9272 {
9273 /* Strip leading "./". */
9274 q = p;
9275 while (q[0] == '.' && vim_ispathsep(q[1]))
9276 q += 2;
9277 if (q > p)
9278 STRMOVE(p, p + 2);
9279 }
9280 }
9281
9282 /* Ensure that the result will have no trailing path separator
9283 * if the argument had none. But keep "/" or "//". */
9284 if (!has_trailing_pathsep)
9285 {
9286 q = p + STRLEN(p);
9287 if (after_pathsep(p, q))
9288 *gettail_sep(p) = NUL;
9289 }
9290
9291 rettv->vval.v_string = p;
9292 }
9293# else
9294 rettv->vval.v_string = vim_strsave(p);
9295# endif
9296#endif
9297
9298 simplify_filename(rettv->vval.v_string);
9299
9300#ifdef HAVE_READLINK
9301fail:
9302 vim_free(buf);
9303#endif
9304 rettv->v_type = VAR_STRING;
9305}
9306
9307/*
9308 * "reverse({list})" function
9309 */
9310 static void
9311f_reverse(typval_T *argvars, typval_T *rettv)
9312{
9313 list_T *l;
9314 listitem_T *li, *ni;
9315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009316 if (argvars[0].v_type == VAR_BLOB)
9317 {
9318 blob_T *b = argvars[0].vval.v_blob;
9319 int i, len = blob_len(b);
9320
9321 for (i = 0; i < len / 2; i++)
9322 {
9323 int tmp = blob_get(b, i);
9324
9325 blob_set(b, i, blob_get(b, len - i - 1));
9326 blob_set(b, len - i - 1, tmp);
9327 }
9328 rettv_blob_set(rettv, b);
9329 return;
9330 }
9331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009333 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009334 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009335 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 (char_u *)N_("reverse() argument"), TRUE))
9337 {
9338 li = l->lv_last;
9339 l->lv_first = l->lv_last = NULL;
9340 l->lv_len = 0;
9341 while (li != NULL)
9342 {
9343 ni = li->li_prev;
9344 list_append(l, li);
9345 li = ni;
9346 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009347 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 l->lv_idx = l->lv_len - l->lv_idx - 1;
9349 }
9350}
9351
9352#define SP_NOMOVE 0x01 /* don't move cursor */
9353#define SP_REPEAT 0x02 /* repeat to find outer pair */
9354#define SP_RETCOUNT 0x04 /* return matchcount */
9355#define SP_SETPCMARK 0x08 /* set previous context mark */
9356#define SP_START 0x10 /* accept match at start position */
9357#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9358#define SP_END 0x40 /* leave cursor at end of match */
9359#define SP_COLUMN 0x80 /* start at cursor column */
9360
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361/*
9362 * Get flags for a search function.
9363 * Possibly sets "p_ws".
9364 * Returns BACKWARD, FORWARD or zero (for an error).
9365 */
9366 static int
9367get_search_arg(typval_T *varp, int *flagsp)
9368{
9369 int dir = FORWARD;
9370 char_u *flags;
9371 char_u nbuf[NUMBUFLEN];
9372 int mask;
9373
9374 if (varp->v_type != VAR_UNKNOWN)
9375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009376 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 if (flags == NULL)
9378 return 0; /* type error; errmsg already given */
9379 while (*flags != NUL)
9380 {
9381 switch (*flags)
9382 {
9383 case 'b': dir = BACKWARD; break;
9384 case 'w': p_ws = TRUE; break;
9385 case 'W': p_ws = FALSE; break;
9386 default: mask = 0;
9387 if (flagsp != NULL)
9388 switch (*flags)
9389 {
9390 case 'c': mask = SP_START; break;
9391 case 'e': mask = SP_END; break;
9392 case 'm': mask = SP_RETCOUNT; break;
9393 case 'n': mask = SP_NOMOVE; break;
9394 case 'p': mask = SP_SUBPAT; break;
9395 case 'r': mask = SP_REPEAT; break;
9396 case 's': mask = SP_SETPCMARK; break;
9397 case 'z': mask = SP_COLUMN; break;
9398 }
9399 if (mask == 0)
9400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009401 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 dir = 0;
9403 }
9404 else
9405 *flagsp |= mask;
9406 }
9407 if (dir == 0)
9408 break;
9409 ++flags;
9410 }
9411 }
9412 return dir;
9413}
9414
9415/*
9416 * Shared by search() and searchpos() functions.
9417 */
9418 static int
9419search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9420{
9421 int flags;
9422 char_u *pat;
9423 pos_T pos;
9424 pos_T save_cursor;
9425 int save_p_ws = p_ws;
9426 int dir;
9427 int retval = 0; /* default: FAIL */
9428 long lnum_stop = 0;
9429 proftime_T tm;
9430#ifdef FEAT_RELTIME
9431 long time_limit = 0;
9432#endif
9433 int options = SEARCH_KEEP;
9434 int subpatnum;
9435
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009436 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9438 if (dir == 0)
9439 goto theend;
9440 flags = *flagsp;
9441 if (flags & SP_START)
9442 options |= SEARCH_START;
9443 if (flags & SP_END)
9444 options |= SEARCH_END;
9445 if (flags & SP_COLUMN)
9446 options |= SEARCH_COL;
9447
9448 /* Optional arguments: line number to stop searching and timeout. */
9449 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9450 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009451 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452 if (lnum_stop < 0)
9453 goto theend;
9454#ifdef FEAT_RELTIME
9455 if (argvars[3].v_type != VAR_UNKNOWN)
9456 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009457 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 if (time_limit < 0)
9459 goto theend;
9460 }
9461#endif
9462 }
9463
9464#ifdef FEAT_RELTIME
9465 /* Set the time limit, if there is one. */
9466 profile_setlimit(time_limit, &tm);
9467#endif
9468
9469 /*
9470 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9471 * Check to make sure only those flags are set.
9472 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9473 * flags cannot be set. Check for that condition also.
9474 */
9475 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9476 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009478 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479 goto theend;
9480 }
9481
9482 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009483 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009484 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485 if (subpatnum != FAIL)
9486 {
9487 if (flags & SP_SUBPAT)
9488 retval = subpatnum;
9489 else
9490 retval = pos.lnum;
9491 if (flags & SP_SETPCMARK)
9492 setpcmark();
9493 curwin->w_cursor = pos;
9494 if (match_pos != NULL)
9495 {
9496 /* Store the match cursor position */
9497 match_pos->lnum = pos.lnum;
9498 match_pos->col = pos.col + 1;
9499 }
9500 /* "/$" will put the cursor after the end of the line, may need to
9501 * correct that here */
9502 check_cursor();
9503 }
9504
9505 /* If 'n' flag is used: restore cursor position. */
9506 if (flags & SP_NOMOVE)
9507 curwin->w_cursor = save_cursor;
9508 else
9509 curwin->w_set_curswant = TRUE;
9510theend:
9511 p_ws = save_p_ws;
9512
9513 return retval;
9514}
9515
9516#ifdef FEAT_FLOAT
9517
9518/*
9519 * round() is not in C90, use ceil() or floor() instead.
9520 */
9521 float_T
9522vim_round(float_T f)
9523{
9524 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9525}
9526
9527/*
9528 * "round({float})" function
9529 */
9530 static void
9531f_round(typval_T *argvars, typval_T *rettv)
9532{
9533 float_T f = 0.0;
9534
9535 rettv->v_type = VAR_FLOAT;
9536 if (get_float_arg(argvars, &f) == OK)
9537 rettv->vval.v_float = vim_round(f);
9538 else
9539 rettv->vval.v_float = 0.0;
9540}
9541#endif
9542
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009543#ifdef FEAT_RUBY
9544/*
9545 * "rubyeval()" function
9546 */
9547 static void
9548f_rubyeval(typval_T *argvars, typval_T *rettv)
9549{
9550 char_u *str;
9551 char_u buf[NUMBUFLEN];
9552
9553 str = tv_get_string_buf(&argvars[0], buf);
9554 do_rubyeval(str, rettv);
9555}
9556#endif
9557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558/*
9559 * "screenattr()" function
9560 */
9561 static void
9562f_screenattr(typval_T *argvars, typval_T *rettv)
9563{
9564 int row;
9565 int col;
9566 int c;
9567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009568 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9569 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 if (row < 0 || row >= screen_Rows
9571 || col < 0 || col >= screen_Columns)
9572 c = -1;
9573 else
9574 c = ScreenAttrs[LineOffset[row] + col];
9575 rettv->vval.v_number = c;
9576}
9577
9578/*
9579 * "screenchar()" function
9580 */
9581 static void
9582f_screenchar(typval_T *argvars, typval_T *rettv)
9583{
9584 int row;
9585 int col;
9586 int off;
9587 int c;
9588
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009589 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9590 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009591 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 c = -1;
9593 else
9594 {
9595 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 if (enc_utf8 && ScreenLinesUC[off] != 0)
9597 c = ScreenLinesUC[off];
9598 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 c = ScreenLines[off];
9600 }
9601 rettv->vval.v_number = c;
9602}
9603
9604/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009605 * "screenchars()" function
9606 */
9607 static void
9608f_screenchars(typval_T *argvars, typval_T *rettv)
9609{
9610 int row;
9611 int col;
9612 int off;
9613 int c;
9614 int i;
9615
9616 if (rettv_list_alloc(rettv) == FAIL)
9617 return;
9618 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9619 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9620 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9621 return;
9622
9623 off = LineOffset[row] + col;
9624 if (enc_utf8 && ScreenLinesUC[off] != 0)
9625 c = ScreenLinesUC[off];
9626 else
9627 c = ScreenLines[off];
9628 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9629
9630 if (enc_utf8)
9631
9632 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9633 list_append_number(rettv->vval.v_list,
9634 (varnumber_T)ScreenLinesC[i][off]);
9635}
9636
9637/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638 * "screencol()" function
9639 *
9640 * First column is 1 to be consistent with virtcol().
9641 */
9642 static void
9643f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9644{
9645 rettv->vval.v_number = screen_screencol() + 1;
9646}
9647
9648/*
9649 * "screenrow()" function
9650 */
9651 static void
9652f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9653{
9654 rettv->vval.v_number = screen_screenrow() + 1;
9655}
9656
9657/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009658 * "screenstring()" function
9659 */
9660 static void
9661f_screenstring(typval_T *argvars, typval_T *rettv)
9662{
9663 int row;
9664 int col;
9665 int off;
9666 int c;
9667 int i;
9668 char_u buf[MB_MAXBYTES + 1];
9669 int buflen = 0;
9670
9671 rettv->vval.v_string = NULL;
9672 rettv->v_type = VAR_STRING;
9673
9674 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9675 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9676 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9677 return;
9678
9679 off = LineOffset[row] + col;
9680 if (enc_utf8 && ScreenLinesUC[off] != 0)
9681 c = ScreenLinesUC[off];
9682 else
9683 c = ScreenLines[off];
9684 buflen += mb_char2bytes(c, buf);
9685
9686 if (enc_utf8)
9687 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9688 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9689
9690 buf[buflen] = NUL;
9691 rettv->vval.v_string = vim_strsave(buf);
9692}
9693
9694/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695 * "search()" function
9696 */
9697 static void
9698f_search(typval_T *argvars, typval_T *rettv)
9699{
9700 int flags = 0;
9701
9702 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9703}
9704
9705/*
9706 * "searchdecl()" function
9707 */
9708 static void
9709f_searchdecl(typval_T *argvars, typval_T *rettv)
9710{
9711 int locally = 1;
9712 int thisblock = 0;
9713 int error = FALSE;
9714 char_u *name;
9715
9716 rettv->vval.v_number = 1; /* default: FAIL */
9717
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009718 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 if (argvars[1].v_type != VAR_UNKNOWN)
9720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009721 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009723 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 }
9725 if (!error && name != NULL)
9726 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9727 locally, thisblock, SEARCH_KEEP) == FAIL;
9728}
9729
9730/*
9731 * Used by searchpair() and searchpairpos()
9732 */
9733 static int
9734searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9735{
9736 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009737 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009738 int save_p_ws = p_ws;
9739 int dir;
9740 int flags = 0;
9741 char_u nbuf1[NUMBUFLEN];
9742 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009743 int retval = 0; /* default: FAIL */
9744 long lnum_stop = 0;
9745 long time_limit = 0;
9746
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009747 /* Get the three pattern arguments: start, middle, end. Will result in an
9748 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009749 spat = tv_get_string_chk(&argvars[0]);
9750 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9751 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 if (spat == NULL || mpat == NULL || epat == NULL)
9753 goto theend; /* type error */
9754
9755 /* Handle the optional fourth argument: flags */
9756 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9757 if (dir == 0)
9758 goto theend;
9759
9760 /* Don't accept SP_END or SP_SUBPAT.
9761 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9762 */
9763 if ((flags & (SP_END | SP_SUBPAT)) != 0
9764 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009766 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767 goto theend;
9768 }
9769
9770 /* Using 'r' implies 'W', otherwise it doesn't work. */
9771 if (flags & SP_REPEAT)
9772 p_ws = FALSE;
9773
9774 /* Optional fifth argument: skip expression */
9775 if (argvars[3].v_type == VAR_UNKNOWN
9776 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009777 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778 else
9779 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009780 skip = &argvars[4];
9781 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9782 && skip->v_type != VAR_STRING)
9783 {
9784 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009785 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009786 goto theend;
9787 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 if (argvars[5].v_type != VAR_UNKNOWN)
9789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009790 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009793 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009795 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796#ifdef FEAT_RELTIME
9797 if (argvars[6].v_type != VAR_UNKNOWN)
9798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009799 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009802 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009804 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805 }
9806#endif
9807 }
9808 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809
9810 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9811 match_pos, lnum_stop, time_limit);
9812
9813theend:
9814 p_ws = save_p_ws;
9815
9816 return retval;
9817}
9818
9819/*
9820 * "searchpair()" function
9821 */
9822 static void
9823f_searchpair(typval_T *argvars, typval_T *rettv)
9824{
9825 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9826}
9827
9828/*
9829 * "searchpairpos()" function
9830 */
9831 static void
9832f_searchpairpos(typval_T *argvars, typval_T *rettv)
9833{
9834 pos_T match_pos;
9835 int lnum = 0;
9836 int col = 0;
9837
9838 if (rettv_list_alloc(rettv) == FAIL)
9839 return;
9840
9841 if (searchpair_cmn(argvars, &match_pos) > 0)
9842 {
9843 lnum = match_pos.lnum;
9844 col = match_pos.col;
9845 }
9846
9847 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9848 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9849}
9850
9851/*
9852 * Search for a start/middle/end thing.
9853 * Used by searchpair(), see its documentation for the details.
9854 * Returns 0 or -1 for no match,
9855 */
9856 long
9857do_searchpair(
9858 char_u *spat, /* start pattern */
9859 char_u *mpat, /* middle pattern */
9860 char_u *epat, /* end pattern */
9861 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009862 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 int flags, /* SP_SETPCMARK and other SP_ values */
9864 pos_T *match_pos,
9865 linenr_T lnum_stop, /* stop at this line if not zero */
9866 long time_limit UNUSED) /* stop after this many msec */
9867{
9868 char_u *save_cpo;
9869 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9870 long retval = 0;
9871 pos_T pos;
9872 pos_T firstpos;
9873 pos_T foundpos;
9874 pos_T save_cursor;
9875 pos_T save_pos;
9876 int n;
9877 int r;
9878 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009879 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 int err;
9881 int options = SEARCH_KEEP;
9882 proftime_T tm;
9883
9884 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9885 save_cpo = p_cpo;
9886 p_cpo = empty_option;
9887
9888#ifdef FEAT_RELTIME
9889 /* Set the time limit, if there is one. */
9890 profile_setlimit(time_limit, &tm);
9891#endif
9892
9893 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9894 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009895 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9896 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 if (pat2 == NULL || pat3 == NULL)
9898 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009899 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900 if (*mpat == NUL)
9901 STRCPY(pat3, pat2);
9902 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009903 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 spat, epat, mpat);
9905 if (flags & SP_START)
9906 options |= SEARCH_START;
9907
Bram Moolenaar48570482017-10-30 21:48:41 +01009908 if (skip != NULL)
9909 {
9910 /* Empty string means to not use the skip expression. */
9911 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9912 use_skip = skip->vval.v_string != NULL
9913 && *skip->vval.v_string != NUL;
9914 }
9915
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 save_cursor = curwin->w_cursor;
9917 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009918 CLEAR_POS(&firstpos);
9919 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 pat = pat3;
9921 for (;;)
9922 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009923 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009924 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009925 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 /* didn't find it or found the first match again: FAIL */
9927 break;
9928
9929 if (firstpos.lnum == 0)
9930 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009931 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009932 {
9933 /* Found the same position again. Can happen with a pattern that
9934 * has "\zs" at the end and searching backwards. Advance one
9935 * character and try again. */
9936 if (dir == BACKWARD)
9937 decl(&pos);
9938 else
9939 incl(&pos);
9940 }
9941 foundpos = pos;
9942
9943 /* clear the start flag to avoid getting stuck here */
9944 options &= ~SEARCH_START;
9945
9946 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009947 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 {
9949 save_pos = curwin->w_cursor;
9950 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009951 err = FALSE;
9952 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 curwin->w_cursor = save_pos;
9954 if (err)
9955 {
9956 /* Evaluating {skip} caused an error, break here. */
9957 curwin->w_cursor = save_cursor;
9958 retval = -1;
9959 break;
9960 }
9961 if (r)
9962 continue;
9963 }
9964
9965 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9966 {
9967 /* Found end when searching backwards or start when searching
9968 * forward: nested pair. */
9969 ++nest;
9970 pat = pat2; /* nested, don't search for middle */
9971 }
9972 else
9973 {
9974 /* Found end when searching forward or start when searching
9975 * backward: end of (nested) pair; or found middle in outer pair. */
9976 if (--nest == 1)
9977 pat = pat3; /* outer level, search for middle */
9978 }
9979
9980 if (nest == 0)
9981 {
9982 /* Found the match: return matchcount or line number. */
9983 if (flags & SP_RETCOUNT)
9984 ++retval;
9985 else
9986 retval = pos.lnum;
9987 if (flags & SP_SETPCMARK)
9988 setpcmark();
9989 curwin->w_cursor = pos;
9990 if (!(flags & SP_REPEAT))
9991 break;
9992 nest = 1; /* search for next unmatched */
9993 }
9994 }
9995
9996 if (match_pos != NULL)
9997 {
9998 /* Store the match cursor position */
9999 match_pos->lnum = curwin->w_cursor.lnum;
10000 match_pos->col = curwin->w_cursor.col + 1;
10001 }
10002
10003 /* If 'n' flag is used or search failed: restore cursor position. */
10004 if ((flags & SP_NOMOVE) || retval == 0)
10005 curwin->w_cursor = save_cursor;
10006
10007theend:
10008 vim_free(pat2);
10009 vim_free(pat3);
10010 if (p_cpo == empty_option)
10011 p_cpo = save_cpo;
10012 else
10013 /* Darn, evaluating the {skip} expression changed the value. */
10014 free_string_option(save_cpo);
10015
10016 return retval;
10017}
10018
10019/*
10020 * "searchpos()" function
10021 */
10022 static void
10023f_searchpos(typval_T *argvars, typval_T *rettv)
10024{
10025 pos_T match_pos;
10026 int lnum = 0;
10027 int col = 0;
10028 int n;
10029 int flags = 0;
10030
10031 if (rettv_list_alloc(rettv) == FAIL)
10032 return;
10033
10034 n = search_cmn(argvars, &match_pos, &flags);
10035 if (n > 0)
10036 {
10037 lnum = match_pos.lnum;
10038 col = match_pos.col;
10039 }
10040
10041 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10042 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10043 if (flags & SP_SUBPAT)
10044 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10045}
10046
10047 static void
10048f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10049{
10050#ifdef FEAT_CLIENTSERVER
10051 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010052 char_u *server = tv_get_string_chk(&argvars[0]);
10053 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054
10055 rettv->vval.v_number = -1;
10056 if (server == NULL || reply == NULL)
10057 return;
10058 if (check_restricted() || check_secure())
10059 return;
10060# ifdef FEAT_X11
10061 if (check_connection() == FAIL)
10062 return;
10063# endif
10064
10065 if (serverSendReply(server, reply) < 0)
10066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010067 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010068 return;
10069 }
10070 rettv->vval.v_number = 0;
10071#else
10072 rettv->vval.v_number = -1;
10073#endif
10074}
10075
10076 static void
10077f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10078{
10079 char_u *r = NULL;
10080
10081#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010082# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 r = serverGetVimNames();
10084# else
10085 make_connection();
10086 if (X_DISPLAY != NULL)
10087 r = serverGetVimNames(X_DISPLAY);
10088# endif
10089#endif
10090 rettv->v_type = VAR_STRING;
10091 rettv->vval.v_string = r;
10092}
10093
10094/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010095 * "setbufline()" function
10096 */
10097 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010098f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010099{
10100 linenr_T lnum;
10101 buf_T *buf;
10102
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010103 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010104 if (buf == NULL)
10105 rettv->vval.v_number = 1; /* FAIL */
10106 else
10107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010108 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010109 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010110 }
10111}
10112
10113/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010114 * "setbufvar()" function
10115 */
10116 static void
10117f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10118{
10119 buf_T *buf;
10120 char_u *varname, *bufvarname;
10121 typval_T *varp;
10122 char_u nbuf[NUMBUFLEN];
10123
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010124 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010125 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010126 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10127 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010128 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 varp = &argvars[2];
10130
10131 if (buf != NULL && varname != NULL && varp != NULL)
10132 {
10133 if (*varname == '&')
10134 {
10135 long numval;
10136 char_u *strval;
10137 int error = FALSE;
10138 aco_save_T aco;
10139
10140 /* set curbuf to be our buf, temporarily */
10141 aucmd_prepbuf(&aco, buf);
10142
10143 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010144 numval = (long)tv_get_number_chk(varp, &error);
10145 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 if (!error && strval != NULL)
10147 set_option_value(varname, numval, strval, OPT_LOCAL);
10148
10149 /* reset notion of buffer */
10150 aucmd_restbuf(&aco);
10151 }
10152 else
10153 {
10154 buf_T *save_curbuf = curbuf;
10155
Bram Moolenaar964b3742019-05-24 18:54:09 +020010156 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 if (bufvarname != NULL)
10158 {
10159 curbuf = buf;
10160 STRCPY(bufvarname, "b:");
10161 STRCPY(bufvarname + 2, varname);
10162 set_var(bufvarname, varp, TRUE);
10163 vim_free(bufvarname);
10164 curbuf = save_curbuf;
10165 }
10166 }
10167 }
10168}
10169
10170 static void
10171f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10172{
10173 dict_T *d;
10174 dictitem_T *di;
10175 char_u *csearch;
10176
10177 if (argvars[0].v_type != VAR_DICT)
10178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010179 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180 return;
10181 }
10182
10183 if ((d = argvars[0].vval.v_dict) != NULL)
10184 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010185 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 if (csearch != NULL)
10187 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010188 if (enc_utf8)
10189 {
10190 int pcc[MAX_MCO];
10191 int c = utfc_ptr2char(csearch, pcc);
10192
10193 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10194 }
10195 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 set_last_csearch(PTR2CHAR(csearch),
10197 csearch, MB_PTR2LEN(csearch));
10198 }
10199
10200 di = dict_find(d, (char_u *)"forward", -1);
10201 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010202 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203 ? FORWARD : BACKWARD);
10204
10205 di = dict_find(d, (char_u *)"until", -1);
10206 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010207 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 }
10209}
10210
10211/*
10212 * "setcmdpos()" function
10213 */
10214 static void
10215f_setcmdpos(typval_T *argvars, typval_T *rettv)
10216{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010217 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010218
10219 if (pos >= 0)
10220 rettv->vval.v_number = set_cmdline_pos(pos);
10221}
10222
10223/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010224 * "setenv()" function
10225 */
10226 static void
10227f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10228{
10229 char_u namebuf[NUMBUFLEN];
10230 char_u valbuf[NUMBUFLEN];
10231 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10232
10233 if (argvars[1].v_type == VAR_SPECIAL
10234 && argvars[1].vval.v_number == VVAL_NULL)
10235 vim_unsetenv(name);
10236 else
10237 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10238}
10239
10240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 * "setfperm({fname}, {mode})" function
10242 */
10243 static void
10244f_setfperm(typval_T *argvars, typval_T *rettv)
10245{
10246 char_u *fname;
10247 char_u modebuf[NUMBUFLEN];
10248 char_u *mode_str;
10249 int i;
10250 int mask;
10251 int mode = 0;
10252
10253 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010254 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 if (fname == NULL)
10256 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010257 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 if (mode_str == NULL)
10259 return;
10260 if (STRLEN(mode_str) != 9)
10261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010262 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 return;
10264 }
10265
10266 mask = 1;
10267 for (i = 8; i >= 0; --i)
10268 {
10269 if (mode_str[i] != '-')
10270 mode |= mask;
10271 mask = mask << 1;
10272 }
10273 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10274}
10275
10276/*
10277 * "setline()" function
10278 */
10279 static void
10280f_setline(typval_T *argvars, typval_T *rettv)
10281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010282 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283
Bram Moolenaarca851592018-06-06 21:04:07 +020010284 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285}
10286
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010287/*
10288 * Used by "setqflist()" and "setloclist()" functions
10289 */
10290 static void
10291set_qf_ll_list(
10292 win_T *wp UNUSED,
10293 typval_T *list_arg UNUSED,
10294 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010295 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010296 typval_T *rettv)
10297{
10298#ifdef FEAT_QUICKFIX
10299 static char *e_invact = N_("E927: Invalid action: '%s'");
10300 char_u *act;
10301 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010302 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303#endif
10304
10305 rettv->vval.v_number = -1;
10306
10307#ifdef FEAT_QUICKFIX
10308 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010309 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010310 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010311 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 else
10313 {
10314 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010315 dict_T *d = NULL;
10316 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317
10318 if (action_arg->v_type == VAR_STRING)
10319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010320 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321 if (act == NULL)
10322 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010323 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10324 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 action = *act;
10326 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010327 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 }
10329 else if (action_arg->v_type == VAR_UNKNOWN)
10330 action = ' ';
10331 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010332 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010333
Bram Moolenaard823fa92016-08-12 16:29:27 +020010334 if (action_arg->v_type != VAR_UNKNOWN
10335 && what_arg->v_type != VAR_UNKNOWN)
10336 {
10337 if (what_arg->v_type == VAR_DICT)
10338 d = what_arg->vval.v_dict;
10339 else
10340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010341 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010342 valid_dict = FALSE;
10343 }
10344 }
10345
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010346 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010347 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010348 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10349 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010350 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010351 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010352 }
10353#endif
10354}
10355
10356/*
10357 * "setloclist()" function
10358 */
10359 static void
10360f_setloclist(typval_T *argvars, typval_T *rettv)
10361{
10362 win_T *win;
10363
10364 rettv->vval.v_number = -1;
10365
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010366 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010367 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010368 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010369}
10370
10371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010372 * "setpos()" function
10373 */
10374 static void
10375f_setpos(typval_T *argvars, typval_T *rettv)
10376{
10377 pos_T pos;
10378 int fnum;
10379 char_u *name;
10380 colnr_T curswant = -1;
10381
10382 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010383 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010384 if (name != NULL)
10385 {
10386 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10387 {
10388 if (--pos.col < 0)
10389 pos.col = 0;
10390 if (name[0] == '.' && name[1] == NUL)
10391 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010392 /* set cursor; "fnum" is ignored */
10393 curwin->w_cursor = pos;
10394 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010395 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010396 curwin->w_curswant = curswant - 1;
10397 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010399 check_cursor();
10400 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010401 }
10402 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10403 {
10404 /* set mark */
10405 if (setmark_pos(name[1], &pos, fnum) == OK)
10406 rettv->vval.v_number = 0;
10407 }
10408 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010409 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 }
10411 }
10412}
10413
10414/*
10415 * "setqflist()" function
10416 */
10417 static void
10418f_setqflist(typval_T *argvars, typval_T *rettv)
10419{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010420 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010421}
10422
10423/*
10424 * "setreg()" function
10425 */
10426 static void
10427f_setreg(typval_T *argvars, typval_T *rettv)
10428{
10429 int regname;
10430 char_u *strregname;
10431 char_u *stropt;
10432 char_u *strval;
10433 int append;
10434 char_u yank_type;
10435 long block_len;
10436
10437 block_len = -1;
10438 yank_type = MAUTO;
10439 append = FALSE;
10440
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010441 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010442 rettv->vval.v_number = 1; /* FAIL is default */
10443
10444 if (strregname == NULL)
10445 return; /* type error; errmsg already given */
10446 regname = *strregname;
10447 if (regname == 0 || regname == '@')
10448 regname = '"';
10449
10450 if (argvars[2].v_type != VAR_UNKNOWN)
10451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010452 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010453 if (stropt == NULL)
10454 return; /* type error */
10455 for (; *stropt != NUL; ++stropt)
10456 switch (*stropt)
10457 {
10458 case 'a': case 'A': /* append */
10459 append = TRUE;
10460 break;
10461 case 'v': case 'c': /* character-wise selection */
10462 yank_type = MCHAR;
10463 break;
10464 case 'V': case 'l': /* line-wise selection */
10465 yank_type = MLINE;
10466 break;
10467 case 'b': case Ctrl_V: /* block-wise selection */
10468 yank_type = MBLOCK;
10469 if (VIM_ISDIGIT(stropt[1]))
10470 {
10471 ++stropt;
10472 block_len = getdigits(&stropt) - 1;
10473 --stropt;
10474 }
10475 break;
10476 }
10477 }
10478
10479 if (argvars[1].v_type == VAR_LIST)
10480 {
10481 char_u **lstval;
10482 char_u **allocval;
10483 char_u buf[NUMBUFLEN];
10484 char_u **curval;
10485 char_u **curallocval;
10486 list_T *ll = argvars[1].vval.v_list;
10487 listitem_T *li;
10488 int len;
10489
10490 /* If the list is NULL handle like an empty list. */
10491 len = ll == NULL ? 0 : ll->lv_len;
10492
10493 /* First half: use for pointers to result lines; second half: use for
10494 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010495 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010496 if (lstval == NULL)
10497 return;
10498 curval = lstval;
10499 allocval = lstval + len + 2;
10500 curallocval = allocval;
10501
10502 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10503 li = li->li_next)
10504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010505 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010506 if (strval == NULL)
10507 goto free_lstval;
10508 if (strval == buf)
10509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010510 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010511 * overwrite the string. */
10512 strval = vim_strsave(buf);
10513 if (strval == NULL)
10514 goto free_lstval;
10515 *curallocval++ = strval;
10516 }
10517 *curval++ = strval;
10518 }
10519 *curval++ = NULL;
10520
10521 write_reg_contents_lst(regname, lstval, -1,
10522 append, yank_type, block_len);
10523free_lstval:
10524 while (curallocval > allocval)
10525 vim_free(*--curallocval);
10526 vim_free(lstval);
10527 }
10528 else
10529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010530 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010531 if (strval == NULL)
10532 return;
10533 write_reg_contents_ex(regname, strval, -1,
10534 append, yank_type, block_len);
10535 }
10536 rettv->vval.v_number = 0;
10537}
10538
10539/*
10540 * "settabvar()" function
10541 */
10542 static void
10543f_settabvar(typval_T *argvars, typval_T *rettv)
10544{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010545 tabpage_T *save_curtab;
10546 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 char_u *varname, *tabvarname;
10548 typval_T *varp;
10549
10550 rettv->vval.v_number = 0;
10551
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010552 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 return;
10554
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010555 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10556 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010557 varp = &argvars[2];
10558
Bram Moolenaar4033c552017-09-16 20:54:51 +020010559 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010560 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010561 save_curtab = curtab;
10562 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010563
Bram Moolenaar964b3742019-05-24 18:54:09 +020010564 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 if (tabvarname != NULL)
10566 {
10567 STRCPY(tabvarname, "t:");
10568 STRCPY(tabvarname + 2, varname);
10569 set_var(tabvarname, varp, TRUE);
10570 vim_free(tabvarname);
10571 }
10572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573 /* Restore current tabpage */
10574 if (valid_tabpage(save_curtab))
10575 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 }
10577}
10578
10579/*
10580 * "settabwinvar()" function
10581 */
10582 static void
10583f_settabwinvar(typval_T *argvars, typval_T *rettv)
10584{
10585 setwinvar(argvars, rettv, 1);
10586}
10587
10588/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010589 * "settagstack()" function
10590 */
10591 static void
10592f_settagstack(typval_T *argvars, typval_T *rettv)
10593{
10594 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10595 win_T *wp;
10596 dict_T *d;
10597 int action = 'r';
10598
10599 rettv->vval.v_number = -1;
10600
10601 // first argument: window number or id
10602 wp = find_win_by_nr_or_id(&argvars[0]);
10603 if (wp == NULL)
10604 return;
10605
10606 // second argument: dict with items to set in the tag stack
10607 if (argvars[1].v_type != VAR_DICT)
10608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010609 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010610 return;
10611 }
10612 d = argvars[1].vval.v_dict;
10613 if (d == NULL)
10614 return;
10615
10616 // third argument: action - 'a' for append and 'r' for replace.
10617 // default is to replace the stack.
10618 if (argvars[2].v_type == VAR_UNKNOWN)
10619 action = 'r';
10620 else if (argvars[2].v_type == VAR_STRING)
10621 {
10622 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010623 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010624 if (actstr == NULL)
10625 return;
10626 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10627 action = *actstr;
10628 else
10629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010630 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010631 return;
10632 }
10633 }
10634 else
10635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010636 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010637 return;
10638 }
10639
10640 if (set_tagstack(wp, d, action) == OK)
10641 rettv->vval.v_number = 0;
10642}
10643
10644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010645 * "setwinvar()" function
10646 */
10647 static void
10648f_setwinvar(typval_T *argvars, typval_T *rettv)
10649{
10650 setwinvar(argvars, rettv, 0);
10651}
10652
10653#ifdef FEAT_CRYPT
10654/*
10655 * "sha256({string})" function
10656 */
10657 static void
10658f_sha256(typval_T *argvars, typval_T *rettv)
10659{
10660 char_u *p;
10661
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010662 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 rettv->vval.v_string = vim_strsave(
10664 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10665 rettv->v_type = VAR_STRING;
10666}
10667#endif /* FEAT_CRYPT */
10668
10669/*
10670 * "shellescape({string})" function
10671 */
10672 static void
10673f_shellescape(typval_T *argvars, typval_T *rettv)
10674{
Bram Moolenaar20615522017-06-05 18:46:26 +020010675 int do_special = non_zero_arg(&argvars[1]);
10676
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010678 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010679 rettv->v_type = VAR_STRING;
10680}
10681
10682/*
10683 * shiftwidth() function
10684 */
10685 static void
10686f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10687{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010688 rettv->vval.v_number = 0;
10689
10690 if (argvars[0].v_type != VAR_UNKNOWN)
10691 {
10692 long col;
10693
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010694 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010695 if (col < 0)
10696 return; // type error; errmsg already given
10697#ifdef FEAT_VARTABS
10698 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10699 return;
10700#endif
10701 }
10702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 rettv->vval.v_number = get_sw_value(curbuf);
10704}
10705
10706/*
10707 * "simplify()" function
10708 */
10709 static void
10710f_simplify(typval_T *argvars, typval_T *rettv)
10711{
10712 char_u *p;
10713
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010714 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010715 rettv->vval.v_string = vim_strsave(p);
10716 simplify_filename(rettv->vval.v_string); /* simplify in place */
10717 rettv->v_type = VAR_STRING;
10718}
10719
10720#ifdef FEAT_FLOAT
10721/*
10722 * "sin()" function
10723 */
10724 static void
10725f_sin(typval_T *argvars, typval_T *rettv)
10726{
10727 float_T f = 0.0;
10728
10729 rettv->v_type = VAR_FLOAT;
10730 if (get_float_arg(argvars, &f) == OK)
10731 rettv->vval.v_float = sin(f);
10732 else
10733 rettv->vval.v_float = 0.0;
10734}
10735
10736/*
10737 * "sinh()" function
10738 */
10739 static void
10740f_sinh(typval_T *argvars, typval_T *rettv)
10741{
10742 float_T f = 0.0;
10743
10744 rettv->v_type = VAR_FLOAT;
10745 if (get_float_arg(argvars, &f) == OK)
10746 rettv->vval.v_float = sinh(f);
10747 else
10748 rettv->vval.v_float = 0.0;
10749}
10750#endif
10751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752/*
10753 * "soundfold({word})" function
10754 */
10755 static void
10756f_soundfold(typval_T *argvars, typval_T *rettv)
10757{
10758 char_u *s;
10759
10760 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010761 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762#ifdef FEAT_SPELL
10763 rettv->vval.v_string = eval_soundfold(s);
10764#else
10765 rettv->vval.v_string = vim_strsave(s);
10766#endif
10767}
10768
10769/*
10770 * "spellbadword()" function
10771 */
10772 static void
10773f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10774{
10775 char_u *word = (char_u *)"";
10776 hlf_T attr = HLF_COUNT;
10777 int len = 0;
10778
10779 if (rettv_list_alloc(rettv) == FAIL)
10780 return;
10781
10782#ifdef FEAT_SPELL
10783 if (argvars[0].v_type == VAR_UNKNOWN)
10784 {
10785 /* Find the start and length of the badly spelled word. */
10786 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10787 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010788 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010789 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010790 curwin->w_set_curswant = TRUE;
10791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 }
10793 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010795 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796 int capcol = -1;
10797
10798 if (str != NULL)
10799 {
10800 /* Check the argument for spelling. */
10801 while (*str != NUL)
10802 {
10803 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10804 if (attr != HLF_COUNT)
10805 {
10806 word = str;
10807 break;
10808 }
10809 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010810 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010811 }
10812 }
10813 }
10814#endif
10815
10816 list_append_string(rettv->vval.v_list, word, len);
10817 list_append_string(rettv->vval.v_list, (char_u *)(
10818 attr == HLF_SPB ? "bad" :
10819 attr == HLF_SPR ? "rare" :
10820 attr == HLF_SPL ? "local" :
10821 attr == HLF_SPC ? "caps" :
10822 ""), -1);
10823}
10824
10825/*
10826 * "spellsuggest()" function
10827 */
10828 static void
10829f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10830{
10831#ifdef FEAT_SPELL
10832 char_u *str;
10833 int typeerr = FALSE;
10834 int maxcount;
10835 garray_T ga;
10836 int i;
10837 listitem_T *li;
10838 int need_capital = FALSE;
10839#endif
10840
10841 if (rettv_list_alloc(rettv) == FAIL)
10842 return;
10843
10844#ifdef FEAT_SPELL
10845 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010847 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 if (argvars[1].v_type != VAR_UNKNOWN)
10849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010850 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 if (maxcount <= 0)
10852 return;
10853 if (argvars[2].v_type != VAR_UNKNOWN)
10854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010855 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010856 if (typeerr)
10857 return;
10858 }
10859 }
10860 else
10861 maxcount = 25;
10862
10863 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10864
10865 for (i = 0; i < ga.ga_len; ++i)
10866 {
10867 str = ((char_u **)ga.ga_data)[i];
10868
10869 li = listitem_alloc();
10870 if (li == NULL)
10871 vim_free(str);
10872 else
10873 {
10874 li->li_tv.v_type = VAR_STRING;
10875 li->li_tv.v_lock = 0;
10876 li->li_tv.vval.v_string = str;
10877 list_append(rettv->vval.v_list, li);
10878 }
10879 }
10880 ga_clear(&ga);
10881 }
10882#endif
10883}
10884
10885 static void
10886f_split(typval_T *argvars, typval_T *rettv)
10887{
10888 char_u *str;
10889 char_u *end;
10890 char_u *pat = NULL;
10891 regmatch_T regmatch;
10892 char_u patbuf[NUMBUFLEN];
10893 char_u *save_cpo;
10894 int match;
10895 colnr_T col = 0;
10896 int keepempty = FALSE;
10897 int typeerr = FALSE;
10898
10899 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10900 save_cpo = p_cpo;
10901 p_cpo = (char_u *)"";
10902
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010903 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 if (argvars[1].v_type != VAR_UNKNOWN)
10905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010906 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010907 if (pat == NULL)
10908 typeerr = TRUE;
10909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010910 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 }
10912 if (pat == NULL || *pat == NUL)
10913 pat = (char_u *)"[\\x01- ]\\+";
10914
10915 if (rettv_list_alloc(rettv) == FAIL)
10916 return;
10917 if (typeerr)
10918 return;
10919
10920 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10921 if (regmatch.regprog != NULL)
10922 {
10923 regmatch.rm_ic = FALSE;
10924 while (*str != NUL || keepempty)
10925 {
10926 if (*str == NUL)
10927 match = FALSE; /* empty item at the end */
10928 else
10929 match = vim_regexec_nl(&regmatch, str, col);
10930 if (match)
10931 end = regmatch.startp[0];
10932 else
10933 end = str + STRLEN(str);
10934 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10935 && *str != NUL && match && end < regmatch.endp[0]))
10936 {
10937 if (list_append_string(rettv->vval.v_list, str,
10938 (int)(end - str)) == FAIL)
10939 break;
10940 }
10941 if (!match)
10942 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010010943 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944 if (regmatch.endp[0] > str)
10945 col = 0;
10946 else
Bram Moolenaar13505972019-01-24 15:04:48 +010010947 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010949 str = regmatch.endp[0];
10950 }
10951
10952 vim_regfree(regmatch.regprog);
10953 }
10954
10955 p_cpo = save_cpo;
10956}
10957
10958#ifdef FEAT_FLOAT
10959/*
10960 * "sqrt()" function
10961 */
10962 static void
10963f_sqrt(typval_T *argvars, typval_T *rettv)
10964{
10965 float_T f = 0.0;
10966
10967 rettv->v_type = VAR_FLOAT;
10968 if (get_float_arg(argvars, &f) == OK)
10969 rettv->vval.v_float = sqrt(f);
10970 else
10971 rettv->vval.v_float = 0.0;
10972}
10973
10974/*
10975 * "str2float()" function
10976 */
10977 static void
10978f_str2float(typval_T *argvars, typval_T *rettv)
10979{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010980 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010981 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982
Bram Moolenaar08243d22017-01-10 16:12:29 +010010983 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 p = skipwhite(p + 1);
10985 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010010986 if (isneg)
10987 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010988 rettv->v_type = VAR_FLOAT;
10989}
10990#endif
10991
10992/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020010993 * "str2list()" function
10994 */
10995 static void
10996f_str2list(typval_T *argvars, typval_T *rettv)
10997{
10998 char_u *p;
10999 int utf8 = FALSE;
11000
11001 if (rettv_list_alloc(rettv) == FAIL)
11002 return;
11003
11004 if (argvars[1].v_type != VAR_UNKNOWN)
11005 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11006
11007 p = tv_get_string(&argvars[0]);
11008
11009 if (has_mbyte || utf8)
11010 {
11011 int (*ptr2len)(char_u *);
11012 int (*ptr2char)(char_u *);
11013
11014 if (utf8 || enc_utf8)
11015 {
11016 ptr2len = utf_ptr2len;
11017 ptr2char = utf_ptr2char;
11018 }
11019 else
11020 {
11021 ptr2len = mb_ptr2len;
11022 ptr2char = mb_ptr2char;
11023 }
11024
11025 for ( ; *p != NUL; p += (*ptr2len)(p))
11026 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11027 }
11028 else
11029 for ( ; *p != NUL; ++p)
11030 list_append_number(rettv->vval.v_list, *p);
11031}
11032
11033/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 * "str2nr()" function
11035 */
11036 static void
11037f_str2nr(typval_T *argvars, typval_T *rettv)
11038{
11039 int base = 10;
11040 char_u *p;
11041 varnumber_T n;
11042 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011043 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044
11045 if (argvars[1].v_type != VAR_UNKNOWN)
11046 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011047 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 if (base != 2 && base != 8 && base != 10 && base != 16)
11049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011050 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051 return;
11052 }
11053 }
11054
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011055 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011056 isneg = (*p == '-');
11057 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 p = skipwhite(p + 1);
11059 switch (base)
11060 {
11061 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11062 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11063 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11064 default: what = 0;
11065 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011066 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11067 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011068 if (isneg)
11069 rettv->vval.v_number = -n;
11070 else
11071 rettv->vval.v_number = n;
11072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073}
11074
11075#ifdef HAVE_STRFTIME
11076/*
11077 * "strftime({format}[, {time}])" function
11078 */
11079 static void
11080f_strftime(typval_T *argvars, typval_T *rettv)
11081{
11082 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011083 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 struct tm *curtime;
11085 time_t seconds;
11086 char_u *p;
11087
11088 rettv->v_type = VAR_STRING;
11089
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011090 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011091 if (argvars[1].v_type == VAR_UNKNOWN)
11092 seconds = time(NULL);
11093 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011094 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011095 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096 /* MSVC returns NULL for an invalid value of seconds. */
11097 if (curtime == NULL)
11098 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11099 else
11100 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 vimconv_T conv;
11102 char_u *enc;
11103
11104 conv.vc_type = CONV_NONE;
11105 enc = enc_locale();
11106 convert_setup(&conv, p_enc, enc);
11107 if (conv.vc_type != CONV_NONE)
11108 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 if (p != NULL)
11110 (void)strftime((char *)result_buf, sizeof(result_buf),
11111 (char *)p, curtime);
11112 else
11113 result_buf[0] = NUL;
11114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 if (conv.vc_type != CONV_NONE)
11116 vim_free(p);
11117 convert_setup(&conv, enc, p_enc);
11118 if (conv.vc_type != CONV_NONE)
11119 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11120 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 rettv->vval.v_string = vim_strsave(result_buf);
11122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 /* Release conversion descriptors */
11124 convert_setup(&conv, NULL, NULL);
11125 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011126 }
11127}
11128#endif
11129
11130/*
11131 * "strgetchar()" function
11132 */
11133 static void
11134f_strgetchar(typval_T *argvars, typval_T *rettv)
11135{
11136 char_u *str;
11137 int len;
11138 int error = FALSE;
11139 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011140 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011141
11142 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011143 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011144 if (str == NULL)
11145 return;
11146 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011147 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148 if (error)
11149 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150
Bram Moolenaar13505972019-01-24 15:04:48 +010011151 while (charidx >= 0 && byteidx < len)
11152 {
11153 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011155 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11156 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011158 --charidx;
11159 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011161}
11162
11163/*
11164 * "stridx()" function
11165 */
11166 static void
11167f_stridx(typval_T *argvars, typval_T *rettv)
11168{
11169 char_u buf[NUMBUFLEN];
11170 char_u *needle;
11171 char_u *haystack;
11172 char_u *save_haystack;
11173 char_u *pos;
11174 int start_idx;
11175
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011176 needle = tv_get_string_chk(&argvars[1]);
11177 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 rettv->vval.v_number = -1;
11179 if (needle == NULL || haystack == NULL)
11180 return; /* type error; errmsg already given */
11181
11182 if (argvars[2].v_type != VAR_UNKNOWN)
11183 {
11184 int error = FALSE;
11185
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011186 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 if (error || start_idx >= (int)STRLEN(haystack))
11188 return;
11189 if (start_idx >= 0)
11190 haystack += start_idx;
11191 }
11192
11193 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11194 if (pos != NULL)
11195 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11196}
11197
11198/*
11199 * "string()" function
11200 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011201 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202f_string(typval_T *argvars, typval_T *rettv)
11203{
11204 char_u *tofree;
11205 char_u numbuf[NUMBUFLEN];
11206
11207 rettv->v_type = VAR_STRING;
11208 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11209 get_copyID());
11210 /* Make a copy if we have a value but it's not in allocated memory. */
11211 if (rettv->vval.v_string != NULL && tofree == NULL)
11212 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11213}
11214
11215/*
11216 * "strlen()" function
11217 */
11218 static void
11219f_strlen(typval_T *argvars, typval_T *rettv)
11220{
11221 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011222 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223}
11224
11225/*
11226 * "strchars()" function
11227 */
11228 static void
11229f_strchars(typval_T *argvars, typval_T *rettv)
11230{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011231 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011232 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011233 varnumber_T len = 0;
11234 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235
11236 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011237 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011238 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011239 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240 else
11241 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011242 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11243 while (*s != NUL)
11244 {
11245 func_mb_ptr2char_adv(&s);
11246 ++len;
11247 }
11248 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 }
11250}
11251
11252/*
11253 * "strdisplaywidth()" function
11254 */
11255 static void
11256f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011258 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011259 int col = 0;
11260
11261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011262 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011263
11264 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11265}
11266
11267/*
11268 * "strwidth()" function
11269 */
11270 static void
11271f_strwidth(typval_T *argvars, typval_T *rettv)
11272{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011273 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011274
Bram Moolenaar13505972019-01-24 15:04:48 +010011275 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011276}
11277
11278/*
11279 * "strcharpart()" function
11280 */
11281 static void
11282f_strcharpart(typval_T *argvars, typval_T *rettv)
11283{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011284 char_u *p;
11285 int nchar;
11286 int nbyte = 0;
11287 int charlen;
11288 int len = 0;
11289 int slen;
11290 int error = FALSE;
11291
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011292 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 slen = (int)STRLEN(p);
11294
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011295 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011296 if (!error)
11297 {
11298 if (nchar > 0)
11299 while (nchar > 0 && nbyte < slen)
11300 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011301 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302 --nchar;
11303 }
11304 else
11305 nbyte = nchar;
11306 if (argvars[2].v_type != VAR_UNKNOWN)
11307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011308 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011309 while (charlen > 0 && nbyte + len < slen)
11310 {
11311 int off = nbyte + len;
11312
11313 if (off < 0)
11314 len += 1;
11315 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011316 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011317 --charlen;
11318 }
11319 }
11320 else
11321 len = slen - nbyte; /* default: all bytes that are available. */
11322 }
11323
11324 /*
11325 * Only return the overlap between the specified part and the actual
11326 * string.
11327 */
11328 if (nbyte < 0)
11329 {
11330 len += nbyte;
11331 nbyte = 0;
11332 }
11333 else if (nbyte > slen)
11334 nbyte = slen;
11335 if (len < 0)
11336 len = 0;
11337 else if (nbyte + len > slen)
11338 len = slen - nbyte;
11339
11340 rettv->v_type = VAR_STRING;
11341 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011342}
11343
11344/*
11345 * "strpart()" function
11346 */
11347 static void
11348f_strpart(typval_T *argvars, typval_T *rettv)
11349{
11350 char_u *p;
11351 int n;
11352 int len;
11353 int slen;
11354 int error = FALSE;
11355
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011356 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011357 slen = (int)STRLEN(p);
11358
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011359 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 if (error)
11361 len = 0;
11362 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011363 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364 else
11365 len = slen - n; /* default len: all bytes that are available. */
11366
11367 /*
11368 * Only return the overlap between the specified part and the actual
11369 * string.
11370 */
11371 if (n < 0)
11372 {
11373 len += n;
11374 n = 0;
11375 }
11376 else if (n > slen)
11377 n = slen;
11378 if (len < 0)
11379 len = 0;
11380 else if (n + len > slen)
11381 len = slen - n;
11382
11383 rettv->v_type = VAR_STRING;
11384 rettv->vval.v_string = vim_strnsave(p + n, len);
11385}
11386
11387/*
11388 * "strridx()" function
11389 */
11390 static void
11391f_strridx(typval_T *argvars, typval_T *rettv)
11392{
11393 char_u buf[NUMBUFLEN];
11394 char_u *needle;
11395 char_u *haystack;
11396 char_u *rest;
11397 char_u *lastmatch = NULL;
11398 int haystack_len, end_idx;
11399
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011400 needle = tv_get_string_chk(&argvars[1]);
11401 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402
11403 rettv->vval.v_number = -1;
11404 if (needle == NULL || haystack == NULL)
11405 return; /* type error; errmsg already given */
11406
11407 haystack_len = (int)STRLEN(haystack);
11408 if (argvars[2].v_type != VAR_UNKNOWN)
11409 {
11410 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011411 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011412 if (end_idx < 0)
11413 return; /* can never find a match */
11414 }
11415 else
11416 end_idx = haystack_len;
11417
11418 if (*needle == NUL)
11419 {
11420 /* Empty string matches past the end. */
11421 lastmatch = haystack + end_idx;
11422 }
11423 else
11424 {
11425 for (rest = haystack; *rest != '\0'; ++rest)
11426 {
11427 rest = (char_u *)strstr((char *)rest, (char *)needle);
11428 if (rest == NULL || rest > haystack + end_idx)
11429 break;
11430 lastmatch = rest;
11431 }
11432 }
11433
11434 if (lastmatch == NULL)
11435 rettv->vval.v_number = -1;
11436 else
11437 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11438}
11439
11440/*
11441 * "strtrans()" function
11442 */
11443 static void
11444f_strtrans(typval_T *argvars, typval_T *rettv)
11445{
11446 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011447 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448}
11449
11450/*
11451 * "submatch()" function
11452 */
11453 static void
11454f_submatch(typval_T *argvars, typval_T *rettv)
11455{
11456 int error = FALSE;
11457 int no;
11458 int retList = 0;
11459
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011460 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011461 if (error)
11462 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011463 if (no < 0 || no >= NSUBEXP)
11464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011465 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011466 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011467 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011469 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 if (error)
11471 return;
11472
11473 if (retList == 0)
11474 {
11475 rettv->v_type = VAR_STRING;
11476 rettv->vval.v_string = reg_submatch(no);
11477 }
11478 else
11479 {
11480 rettv->v_type = VAR_LIST;
11481 rettv->vval.v_list = reg_submatch_list(no);
11482 }
11483}
11484
11485/*
11486 * "substitute()" function
11487 */
11488 static void
11489f_substitute(typval_T *argvars, typval_T *rettv)
11490{
11491 char_u patbuf[NUMBUFLEN];
11492 char_u subbuf[NUMBUFLEN];
11493 char_u flagsbuf[NUMBUFLEN];
11494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011495 char_u *str = tv_get_string_chk(&argvars[0]);
11496 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011497 char_u *sub = NULL;
11498 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011501 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11502 expr = &argvars[2];
11503 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011504 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011506 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011507 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11508 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 rettv->vval.v_string = NULL;
11510 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011511 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512}
11513
11514/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011515 * "swapinfo(swap_filename)" function
11516 */
11517 static void
11518f_swapinfo(typval_T *argvars, typval_T *rettv)
11519{
11520 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011521 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011522}
11523
11524/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011525 * "swapname(expr)" function
11526 */
11527 static void
11528f_swapname(typval_T *argvars, typval_T *rettv)
11529{
11530 buf_T *buf;
11531
11532 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011533 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011534 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11535 || buf->b_ml.ml_mfp->mf_fname == NULL)
11536 rettv->vval.v_string = NULL;
11537 else
11538 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11539}
11540
11541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 * "synID(lnum, col, trans)" function
11543 */
11544 static void
11545f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11546{
11547 int id = 0;
11548#ifdef FEAT_SYN_HL
11549 linenr_T lnum;
11550 colnr_T col;
11551 int trans;
11552 int transerr = FALSE;
11553
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011554 lnum = tv_get_lnum(argvars); /* -1 on type error */
11555 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11556 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557
11558 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11559 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11560 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11561#endif
11562
11563 rettv->vval.v_number = id;
11564}
11565
11566/*
11567 * "synIDattr(id, what [, mode])" function
11568 */
11569 static void
11570f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11571{
11572 char_u *p = NULL;
11573#ifdef FEAT_SYN_HL
11574 int id;
11575 char_u *what;
11576 char_u *mode;
11577 char_u modebuf[NUMBUFLEN];
11578 int modec;
11579
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011580 id = (int)tv_get_number(&argvars[0]);
11581 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011582 if (argvars[2].v_type != VAR_UNKNOWN)
11583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011584 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 modec = TOLOWER_ASC(mode[0]);
11586 if (modec != 't' && modec != 'c' && modec != 'g')
11587 modec = 0; /* replace invalid with current */
11588 }
11589 else
11590 {
11591#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11592 if (USE_24BIT)
11593 modec = 'g';
11594 else
11595#endif
11596 if (t_colors > 1)
11597 modec = 'c';
11598 else
11599 modec = 't';
11600 }
11601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011602 switch (TOLOWER_ASC(what[0]))
11603 {
11604 case 'b':
11605 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11606 p = highlight_color(id, what, modec);
11607 else /* bold */
11608 p = highlight_has_attr(id, HL_BOLD, modec);
11609 break;
11610
11611 case 'f': /* fg[#] or font */
11612 p = highlight_color(id, what, modec);
11613 break;
11614
11615 case 'i':
11616 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11617 p = highlight_has_attr(id, HL_INVERSE, modec);
11618 else /* italic */
11619 p = highlight_has_attr(id, HL_ITALIC, modec);
11620 break;
11621
11622 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011623 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624 break;
11625
11626 case 'r': /* reverse */
11627 p = highlight_has_attr(id, HL_INVERSE, modec);
11628 break;
11629
11630 case 's':
11631 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11632 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011633 /* strikeout */
11634 else if (TOLOWER_ASC(what[1]) == 't' &&
11635 TOLOWER_ASC(what[2]) == 'r')
11636 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637 else /* standout */
11638 p = highlight_has_attr(id, HL_STANDOUT, modec);
11639 break;
11640
11641 case 'u':
11642 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11643 /* underline */
11644 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11645 else
11646 /* undercurl */
11647 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11648 break;
11649 }
11650
11651 if (p != NULL)
11652 p = vim_strsave(p);
11653#endif
11654 rettv->v_type = VAR_STRING;
11655 rettv->vval.v_string = p;
11656}
11657
11658/*
11659 * "synIDtrans(id)" function
11660 */
11661 static void
11662f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11663{
11664 int id;
11665
11666#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011667 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011668
11669 if (id > 0)
11670 id = syn_get_final_id(id);
11671 else
11672#endif
11673 id = 0;
11674
11675 rettv->vval.v_number = id;
11676}
11677
11678/*
11679 * "synconcealed(lnum, col)" function
11680 */
11681 static void
11682f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11683{
11684#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11685 linenr_T lnum;
11686 colnr_T col;
11687 int syntax_flags = 0;
11688 int cchar;
11689 int matchid = 0;
11690 char_u str[NUMBUFLEN];
11691#endif
11692
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011693 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694
11695#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011696 lnum = tv_get_lnum(argvars); /* -1 on type error */
11697 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011698
11699 vim_memset(str, NUL, sizeof(str));
11700
11701 if (rettv_list_alloc(rettv) != FAIL)
11702 {
11703 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11704 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11705 && curwin->w_p_cole > 0)
11706 {
11707 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11708 syntax_flags = get_syntax_info(&matchid);
11709
11710 /* get the conceal character */
11711 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11712 {
11713 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011714 if (cchar == NUL && curwin->w_p_cole == 1)
11715 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011716 if (cchar != NUL)
11717 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 if (has_mbyte)
11719 (*mb_char2bytes)(cchar, str);
11720 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011721 str[0] = cchar;
11722 }
11723 }
11724 }
11725
11726 list_append_number(rettv->vval.v_list,
11727 (syntax_flags & HL_CONCEAL) != 0);
11728 /* -1 to auto-determine strlen */
11729 list_append_string(rettv->vval.v_list, str, -1);
11730 list_append_number(rettv->vval.v_list, matchid);
11731 }
11732#endif
11733}
11734
11735/*
11736 * "synstack(lnum, col)" function
11737 */
11738 static void
11739f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11740{
11741#ifdef FEAT_SYN_HL
11742 linenr_T lnum;
11743 colnr_T col;
11744 int i;
11745 int id;
11746#endif
11747
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011748 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011749
11750#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011751 lnum = tv_get_lnum(argvars); /* -1 on type error */
11752 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011753
11754 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11755 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11756 && rettv_list_alloc(rettv) != FAIL)
11757 {
11758 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11759 for (i = 0; ; ++i)
11760 {
11761 id = syn_get_stack_item(i);
11762 if (id < 0)
11763 break;
11764 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11765 break;
11766 }
11767 }
11768#endif
11769}
11770
11771 static void
11772get_cmd_output_as_rettv(
11773 typval_T *argvars,
11774 typval_T *rettv,
11775 int retlist)
11776{
11777 char_u *res = NULL;
11778 char_u *p;
11779 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 int err = FALSE;
11781 FILE *fd;
11782 list_T *list = NULL;
11783 int flags = SHELL_SILENT;
11784
11785 rettv->v_type = VAR_STRING;
11786 rettv->vval.v_string = NULL;
11787 if (check_restricted() || check_secure())
11788 goto errret;
11789
11790 if (argvars[1].v_type != VAR_UNKNOWN)
11791 {
11792 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011793 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794 * command.
11795 */
11796 if ((infile = vim_tempname('i', TRUE)) == NULL)
11797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011798 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011799 goto errret;
11800 }
11801
11802 fd = mch_fopen((char *)infile, WRITEBIN);
11803 if (fd == NULL)
11804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011805 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806 goto errret;
11807 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011808 if (argvars[1].v_type == VAR_NUMBER)
11809 {
11810 linenr_T lnum;
11811 buf_T *buf;
11812
11813 buf = buflist_findnr(argvars[1].vval.v_number);
11814 if (buf == NULL)
11815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011816 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011817 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011818 goto errret;
11819 }
11820
11821 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11822 {
11823 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11824 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11825 {
11826 err = TRUE;
11827 break;
11828 }
11829 if (putc(NL, fd) == EOF)
11830 {
11831 err = TRUE;
11832 break;
11833 }
11834 }
11835 }
11836 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837 {
11838 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11839 err = TRUE;
11840 }
11841 else
11842 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011843 size_t len;
11844 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011845
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011846 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011847 if (p == NULL)
11848 {
11849 fclose(fd);
11850 goto errret; /* type error; errmsg already given */
11851 }
11852 len = STRLEN(p);
11853 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11854 err = TRUE;
11855 }
11856 if (fclose(fd) != 0)
11857 err = TRUE;
11858 if (err)
11859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011860 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861 goto errret;
11862 }
11863 }
11864
11865 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11866 * echoes typeahead, that messes up the display. */
11867 if (!msg_silent)
11868 flags += SHELL_COOKED;
11869
11870 if (retlist)
11871 {
11872 int len;
11873 listitem_T *li;
11874 char_u *s = NULL;
11875 char_u *start;
11876 char_u *end;
11877 int i;
11878
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011879 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011880 if (res == NULL)
11881 goto errret;
11882
11883 list = list_alloc();
11884 if (list == NULL)
11885 goto errret;
11886
11887 for (i = 0; i < len; ++i)
11888 {
11889 start = res + i;
11890 while (i < len && res[i] != NL)
11891 ++i;
11892 end = res + i;
11893
Bram Moolenaar964b3742019-05-24 18:54:09 +020011894 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011895 if (s == NULL)
11896 goto errret;
11897
11898 for (p = s; start < end; ++p, ++start)
11899 *p = *start == NUL ? NL : *start;
11900 *p = NUL;
11901
11902 li = listitem_alloc();
11903 if (li == NULL)
11904 {
11905 vim_free(s);
11906 goto errret;
11907 }
11908 li->li_tv.v_type = VAR_STRING;
11909 li->li_tv.v_lock = 0;
11910 li->li_tv.vval.v_string = s;
11911 list_append(list, li);
11912 }
11913
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011914 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011915 list = NULL;
11916 }
11917 else
11918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011919 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011920#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921 /* translate <CR><NL> into <NL> */
11922 if (res != NULL)
11923 {
11924 char_u *s, *d;
11925
11926 d = res;
11927 for (s = res; *s; ++s)
11928 {
11929 if (s[0] == CAR && s[1] == NL)
11930 ++s;
11931 *d++ = *s;
11932 }
11933 *d = NUL;
11934 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935#endif
11936 rettv->vval.v_string = res;
11937 res = NULL;
11938 }
11939
11940errret:
11941 if (infile != NULL)
11942 {
11943 mch_remove(infile);
11944 vim_free(infile);
11945 }
11946 if (res != NULL)
11947 vim_free(res);
11948 if (list != NULL)
11949 list_free(list);
11950}
11951
11952/*
11953 * "system()" function
11954 */
11955 static void
11956f_system(typval_T *argvars, typval_T *rettv)
11957{
11958 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11959}
11960
11961/*
11962 * "systemlist()" function
11963 */
11964 static void
11965f_systemlist(typval_T *argvars, typval_T *rettv)
11966{
11967 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11968}
11969
11970/*
11971 * "tabpagebuflist()" function
11972 */
11973 static void
11974f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11975{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011976 tabpage_T *tp;
11977 win_T *wp = NULL;
11978
11979 if (argvars[0].v_type == VAR_UNKNOWN)
11980 wp = firstwin;
11981 else
11982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011983 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011984 if (tp != NULL)
11985 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11986 }
11987 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11988 {
11989 for (; wp != NULL; wp = wp->w_next)
11990 if (list_append_number(rettv->vval.v_list,
11991 wp->w_buffer->b_fnum) == FAIL)
11992 break;
11993 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011994}
11995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011996/*
11997 * "tabpagenr()" function
11998 */
11999 static void
12000f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12001{
12002 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012003 char_u *arg;
12004
12005 if (argvars[0].v_type != VAR_UNKNOWN)
12006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012007 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012008 nr = 0;
12009 if (arg != NULL)
12010 {
12011 if (STRCMP(arg, "$") == 0)
12012 nr = tabpage_index(NULL) - 1;
12013 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012014 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012015 }
12016 }
12017 else
12018 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012019 rettv->vval.v_number = nr;
12020}
12021
12022
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012023/*
12024 * Common code for tabpagewinnr() and winnr().
12025 */
12026 static int
12027get_winnr(tabpage_T *tp, typval_T *argvar)
12028{
12029 win_T *twin;
12030 int nr = 1;
12031 win_T *wp;
12032 char_u *arg;
12033
12034 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12035 if (argvar->v_type != VAR_UNKNOWN)
12036 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012037 int invalid_arg = FALSE;
12038
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012039 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 if (arg == NULL)
12041 nr = 0; /* type error; errmsg already given */
12042 else if (STRCMP(arg, "$") == 0)
12043 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12044 else if (STRCMP(arg, "#") == 0)
12045 {
12046 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12047 if (twin == NULL)
12048 nr = 0;
12049 }
12050 else
12051 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012052 long count;
12053 char_u *endp;
12054
12055 // Extract the window count (if specified). e.g. winnr('3j')
12056 count = strtol((char *)arg, (char **)&endp, 10);
12057 if (count <= 0)
12058 count = 1; // if count is not specified, default to 1
12059 if (endp != NULL && *endp != '\0')
12060 {
12061 if (STRCMP(endp, "j") == 0)
12062 twin = win_vert_neighbor(tp, twin, FALSE, count);
12063 else if (STRCMP(endp, "k") == 0)
12064 twin = win_vert_neighbor(tp, twin, TRUE, count);
12065 else if (STRCMP(endp, "h") == 0)
12066 twin = win_horz_neighbor(tp, twin, TRUE, count);
12067 else if (STRCMP(endp, "l") == 0)
12068 twin = win_horz_neighbor(tp, twin, FALSE, count);
12069 else
12070 invalid_arg = TRUE;
12071 }
12072 else
12073 invalid_arg = TRUE;
12074 }
12075
12076 if (invalid_arg)
12077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012078 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012079 nr = 0;
12080 }
12081 }
12082
12083 if (nr > 0)
12084 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12085 wp != twin; wp = wp->w_next)
12086 {
12087 if (wp == NULL)
12088 {
12089 /* didn't find it in this tabpage */
12090 nr = 0;
12091 break;
12092 }
12093 ++nr;
12094 }
12095 return nr;
12096}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097
12098/*
12099 * "tabpagewinnr()" function
12100 */
12101 static void
12102f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12103{
12104 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012105 tabpage_T *tp;
12106
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012107 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108 if (tp == NULL)
12109 nr = 0;
12110 else
12111 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012112 rettv->vval.v_number = nr;
12113}
12114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012115/*
12116 * "tagfiles()" function
12117 */
12118 static void
12119f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12120{
12121 char_u *fname;
12122 tagname_T tn;
12123 int first;
12124
12125 if (rettv_list_alloc(rettv) == FAIL)
12126 return;
12127 fname = alloc(MAXPATHL);
12128 if (fname == NULL)
12129 return;
12130
12131 for (first = TRUE; ; first = FALSE)
12132 if (get_tagfname(&tn, first, fname) == FAIL
12133 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12134 break;
12135 tagname_free(&tn);
12136 vim_free(fname);
12137}
12138
12139/*
12140 * "taglist()" function
12141 */
12142 static void
12143f_taglist(typval_T *argvars, typval_T *rettv)
12144{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012145 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012146 char_u *tag_pattern;
12147
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012148 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012149
12150 rettv->vval.v_number = FALSE;
12151 if (*tag_pattern == NUL)
12152 return;
12153
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012154 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012155 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012156 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012157 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158}
12159
12160/*
12161 * "tempname()" function
12162 */
12163 static void
12164f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12165{
12166 static int x = 'A';
12167
12168 rettv->v_type = VAR_STRING;
12169 rettv->vval.v_string = vim_tempname(x, FALSE);
12170
12171 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12172 * names. Skip 'I' and 'O', they are used for shell redirection. */
12173 do
12174 {
12175 if (x == 'Z')
12176 x = '0';
12177 else if (x == '9')
12178 x = 'A';
12179 else
12180 {
12181#ifdef EBCDIC
12182 if (x == 'I')
12183 x = 'J';
12184 else if (x == 'R')
12185 x = 'S';
12186 else
12187#endif
12188 ++x;
12189 }
12190 } while (x == 'I' || x == 'O');
12191}
12192
12193#ifdef FEAT_FLOAT
12194/*
12195 * "tan()" function
12196 */
12197 static void
12198f_tan(typval_T *argvars, typval_T *rettv)
12199{
12200 float_T f = 0.0;
12201
12202 rettv->v_type = VAR_FLOAT;
12203 if (get_float_arg(argvars, &f) == OK)
12204 rettv->vval.v_float = tan(f);
12205 else
12206 rettv->vval.v_float = 0.0;
12207}
12208
12209/*
12210 * "tanh()" function
12211 */
12212 static void
12213f_tanh(typval_T *argvars, typval_T *rettv)
12214{
12215 float_T f = 0.0;
12216
12217 rettv->v_type = VAR_FLOAT;
12218 if (get_float_arg(argvars, &f) == OK)
12219 rettv->vval.v_float = tanh(f);
12220 else
12221 rettv->vval.v_float = 0.0;
12222}
12223#endif
12224
12225/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 * Get a callback from "arg". It can be a Funcref or a function name.
12227 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012228 * "cb_name" is not allocated.
12229 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012230 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012231 callback_T
12232get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012233{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012234 callback_T res;
12235
12236 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012237 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12238 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012239 res.cb_partial = arg->vval.v_partial;
12240 ++res.cb_partial->pt_refcount;
12241 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012242 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012243 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012244 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012245 res.cb_partial = NULL;
12246 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12247 {
12248 // Note that we don't make a copy of the string.
12249 res.cb_name = arg->vval.v_string;
12250 func_ref(res.cb_name);
12251 }
12252 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12253 {
12254 res.cb_name = (char_u *)"";
12255 }
12256 else
12257 {
12258 emsg(_("E921: Invalid callback argument"));
12259 res.cb_name = NULL;
12260 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012262 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012263}
12264
12265/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012266 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 */
12268 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012269put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012270{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012271 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012272 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012273 tv->v_type = VAR_PARTIAL;
12274 tv->vval.v_partial = cb->cb_partial;
12275 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012276 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012277 else
12278 {
12279 tv->v_type = VAR_FUNC;
12280 tv->vval.v_string = vim_strsave(cb->cb_name);
12281 func_ref(cb->cb_name);
12282 }
12283}
12284
12285/*
12286 * Make a copy of "src" into "dest", allocating the function name if needed,
12287 * without incrementing the refcount.
12288 */
12289 void
12290set_callback(callback_T *dest, callback_T *src)
12291{
12292 if (src->cb_partial == NULL)
12293 {
12294 // just a function name, make a copy
12295 dest->cb_name = vim_strsave(src->cb_name);
12296 dest->cb_free_name = TRUE;
12297 }
12298 else
12299 {
12300 // cb_name is a pointer into cb_partial
12301 dest->cb_name = src->cb_name;
12302 dest->cb_free_name = FALSE;
12303 }
12304 dest->cb_partial = src->cb_partial;
12305}
12306
12307/*
12308 * Unref/free "callback" returned by get_callback() or set_callback().
12309 */
12310 void
12311free_callback(callback_T *callback)
12312{
12313 if (callback->cb_partial != NULL)
12314 {
12315 partial_unref(callback->cb_partial);
12316 callback->cb_partial = NULL;
12317 }
12318 else if (callback->cb_name != NULL)
12319 func_unref(callback->cb_name);
12320 if (callback->cb_free_name)
12321 {
12322 vim_free(callback->cb_name);
12323 callback->cb_free_name = FALSE;
12324 }
12325 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012326}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012327
12328#ifdef FEAT_TIMERS
12329/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012330 * "timer_info([timer])" function
12331 */
12332 static void
12333f_timer_info(typval_T *argvars, typval_T *rettv)
12334{
12335 timer_T *timer = NULL;
12336
12337 if (rettv_list_alloc(rettv) != OK)
12338 return;
12339 if (argvars[0].v_type != VAR_UNKNOWN)
12340 {
12341 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012342 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012343 else
12344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012345 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012346 if (timer != NULL)
12347 add_timer_info(rettv, timer);
12348 }
12349 }
12350 else
12351 add_timer_info_all(rettv);
12352}
12353
12354/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012355 * "timer_pause(timer, paused)" function
12356 */
12357 static void
12358f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12359{
12360 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012361 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012362
12363 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012364 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012365 else
12366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012367 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012368 if (timer != NULL)
12369 timer->tr_paused = paused;
12370 }
12371}
12372
12373/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012374 * "timer_start(time, callback [, options])" function
12375 */
12376 static void
12377f_timer_start(typval_T *argvars, typval_T *rettv)
12378{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012379 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012380 timer_T *timer;
12381 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012382 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012383 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384
Bram Moolenaar75537a92016-09-05 22:45:28 +020012385 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012386 if (check_secure())
12387 return;
12388 if (argvars[2].v_type != VAR_UNKNOWN)
12389 {
12390 if (argvars[2].v_type != VAR_DICT
12391 || (dict = argvars[2].vval.v_dict) == NULL)
12392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012393 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012394 return;
12395 }
12396 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012397 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012398 }
12399
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012400 callback = get_callback(&argvars[1]);
12401 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012402 return;
12403
12404 timer = create_timer(msec, repeat);
12405 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012406 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407 else
12408 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012409 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012410 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411 }
12412}
12413
12414/*
12415 * "timer_stop(timer)" function
12416 */
12417 static void
12418f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12419{
12420 timer_T *timer;
12421
12422 if (argvars[0].v_type != VAR_NUMBER)
12423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012424 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 return;
12426 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012427 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428 if (timer != NULL)
12429 stop_timer(timer);
12430}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012431
12432/*
12433 * "timer_stopall()" function
12434 */
12435 static void
12436f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12437{
12438 stop_all_timers();
12439}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440#endif
12441
12442/*
12443 * "tolower(string)" function
12444 */
12445 static void
12446f_tolower(typval_T *argvars, typval_T *rettv)
12447{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012448 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450}
12451
12452/*
12453 * "toupper(string)" function
12454 */
12455 static void
12456f_toupper(typval_T *argvars, typval_T *rettv)
12457{
12458 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012459 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460}
12461
12462/*
12463 * "tr(string, fromstr, tostr)" function
12464 */
12465 static void
12466f_tr(typval_T *argvars, typval_T *rettv)
12467{
12468 char_u *in_str;
12469 char_u *fromstr;
12470 char_u *tostr;
12471 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472 int inlen;
12473 int fromlen;
12474 int tolen;
12475 int idx;
12476 char_u *cpstr;
12477 int cplen;
12478 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 char_u buf[NUMBUFLEN];
12480 char_u buf2[NUMBUFLEN];
12481 garray_T ga;
12482
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012483 in_str = tv_get_string(&argvars[0]);
12484 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12485 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012486
12487 /* Default return value: empty string. */
12488 rettv->v_type = VAR_STRING;
12489 rettv->vval.v_string = NULL;
12490 if (fromstr == NULL || tostr == NULL)
12491 return; /* type error; errmsg already given */
12492 ga_init2(&ga, (int)sizeof(char), 80);
12493
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012494 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012495 /* not multi-byte: fromstr and tostr must be the same length */
12496 if (STRLEN(fromstr) != STRLEN(tostr))
12497 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012499 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500 ga_clear(&ga);
12501 return;
12502 }
12503
12504 /* fromstr and tostr have to contain the same number of chars */
12505 while (*in_str != NUL)
12506 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507 if (has_mbyte)
12508 {
12509 inlen = (*mb_ptr2len)(in_str);
12510 cpstr = in_str;
12511 cplen = inlen;
12512 idx = 0;
12513 for (p = fromstr; *p != NUL; p += fromlen)
12514 {
12515 fromlen = (*mb_ptr2len)(p);
12516 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12517 {
12518 for (p = tostr; *p != NUL; p += tolen)
12519 {
12520 tolen = (*mb_ptr2len)(p);
12521 if (idx-- == 0)
12522 {
12523 cplen = tolen;
12524 cpstr = p;
12525 break;
12526 }
12527 }
12528 if (*p == NUL) /* tostr is shorter than fromstr */
12529 goto error;
12530 break;
12531 }
12532 ++idx;
12533 }
12534
12535 if (first && cpstr == in_str)
12536 {
12537 /* Check that fromstr and tostr have the same number of
12538 * (multi-byte) characters. Done only once when a character
12539 * of in_str doesn't appear in fromstr. */
12540 first = FALSE;
12541 for (p = tostr; *p != NUL; p += tolen)
12542 {
12543 tolen = (*mb_ptr2len)(p);
12544 --idx;
12545 }
12546 if (idx != 0)
12547 goto error;
12548 }
12549
12550 (void)ga_grow(&ga, cplen);
12551 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12552 ga.ga_len += cplen;
12553
12554 in_str += inlen;
12555 }
12556 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557 {
12558 /* When not using multi-byte chars we can do it faster. */
12559 p = vim_strchr(fromstr, *in_str);
12560 if (p != NULL)
12561 ga_append(&ga, tostr[p - fromstr]);
12562 else
12563 ga_append(&ga, *in_str);
12564 ++in_str;
12565 }
12566 }
12567
12568 /* add a terminating NUL */
12569 (void)ga_grow(&ga, 1);
12570 ga_append(&ga, NUL);
12571
12572 rettv->vval.v_string = ga.ga_data;
12573}
12574
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012575/*
12576 * "trim({expr})" function
12577 */
12578 static void
12579f_trim(typval_T *argvars, typval_T *rettv)
12580{
12581 char_u buf1[NUMBUFLEN];
12582 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012583 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012584 char_u *mask = NULL;
12585 char_u *tail;
12586 char_u *prev;
12587 char_u *p;
12588 int c1;
12589
12590 rettv->v_type = VAR_STRING;
12591 if (head == NULL)
12592 {
12593 rettv->vval.v_string = NULL;
12594 return;
12595 }
12596
12597 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012598 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012599
12600 while (*head != NUL)
12601 {
12602 c1 = PTR2CHAR(head);
12603 if (mask == NULL)
12604 {
12605 if (c1 > ' ' && c1 != 0xa0)
12606 break;
12607 }
12608 else
12609 {
12610 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12611 if (c1 == PTR2CHAR(p))
12612 break;
12613 if (*p == NUL)
12614 break;
12615 }
12616 MB_PTR_ADV(head);
12617 }
12618
12619 for (tail = head + STRLEN(head); tail > head; tail = prev)
12620 {
12621 prev = tail;
12622 MB_PTR_BACK(head, prev);
12623 c1 = PTR2CHAR(prev);
12624 if (mask == NULL)
12625 {
12626 if (c1 > ' ' && c1 != 0xa0)
12627 break;
12628 }
12629 else
12630 {
12631 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12632 if (c1 == PTR2CHAR(p))
12633 break;
12634 if (*p == NUL)
12635 break;
12636 }
12637 }
12638 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12639}
12640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641#ifdef FEAT_FLOAT
12642/*
12643 * "trunc({float})" function
12644 */
12645 static void
12646f_trunc(typval_T *argvars, typval_T *rettv)
12647{
12648 float_T f = 0.0;
12649
12650 rettv->v_type = VAR_FLOAT;
12651 if (get_float_arg(argvars, &f) == OK)
12652 /* trunc() is not in C90, use floor() or ceil() instead. */
12653 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12654 else
12655 rettv->vval.v_float = 0.0;
12656}
12657#endif
12658
12659/*
12660 * "type(expr)" function
12661 */
12662 static void
12663f_type(typval_T *argvars, typval_T *rettv)
12664{
12665 int n = -1;
12666
12667 switch (argvars[0].v_type)
12668 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012669 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12670 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012671 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012672 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12673 case VAR_LIST: n = VAR_TYPE_LIST; break;
12674 case VAR_DICT: n = VAR_TYPE_DICT; break;
12675 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012676 case VAR_SPECIAL:
12677 if (argvars[0].vval.v_number == VVAL_FALSE
12678 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012679 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012680 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012681 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012683 case VAR_JOB: n = VAR_TYPE_JOB; break;
12684 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012685 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012687 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688 n = -1;
12689 break;
12690 }
12691 rettv->vval.v_number = n;
12692}
12693
12694/*
12695 * "undofile(name)" function
12696 */
12697 static void
12698f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12699{
12700 rettv->v_type = VAR_STRING;
12701#ifdef FEAT_PERSISTENT_UNDO
12702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012703 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704
12705 if (*fname == NUL)
12706 {
12707 /* If there is no file name there will be no undo file. */
12708 rettv->vval.v_string = NULL;
12709 }
12710 else
12711 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012712 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012713
12714 if (ffname != NULL)
12715 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12716 vim_free(ffname);
12717 }
12718 }
12719#else
12720 rettv->vval.v_string = NULL;
12721#endif
12722}
12723
12724/*
12725 * "undotree()" function
12726 */
12727 static void
12728f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12729{
12730 if (rettv_dict_alloc(rettv) == OK)
12731 {
12732 dict_T *dict = rettv->vval.v_dict;
12733 list_T *list;
12734
Bram Moolenaare0be1672018-07-08 16:50:37 +020012735 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12736 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12737 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12738 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12739 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12740 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741
12742 list = list_alloc();
12743 if (list != NULL)
12744 {
12745 u_eval_tree(curbuf->b_u_oldhead, list);
12746 dict_add_list(dict, "entries", list);
12747 }
12748 }
12749}
12750
12751/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 * "virtcol(string)" function
12753 */
12754 static void
12755f_virtcol(typval_T *argvars, typval_T *rettv)
12756{
12757 colnr_T vcol = 0;
12758 pos_T *fp;
12759 int fnum = curbuf->b_fnum;
12760
12761 fp = var2fpos(&argvars[0], FALSE, &fnum);
12762 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12763 && fnum == curbuf->b_fnum)
12764 {
12765 getvvcol(curwin, fp, NULL, NULL, &vcol);
12766 ++vcol;
12767 }
12768
12769 rettv->vval.v_number = vcol;
12770}
12771
12772/*
12773 * "visualmode()" function
12774 */
12775 static void
12776f_visualmode(typval_T *argvars, typval_T *rettv)
12777{
12778 char_u str[2];
12779
12780 rettv->v_type = VAR_STRING;
12781 str[0] = curbuf->b_visual_mode_eval;
12782 str[1] = NUL;
12783 rettv->vval.v_string = vim_strsave(str);
12784
12785 /* A non-zero number or non-empty string argument: reset mode. */
12786 if (non_zero_arg(&argvars[0]))
12787 curbuf->b_visual_mode_eval = NUL;
12788}
12789
12790/*
12791 * "wildmenumode()" function
12792 */
12793 static void
12794f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12795{
12796#ifdef FEAT_WILDMENU
12797 if (wild_menu_showing)
12798 rettv->vval.v_number = 1;
12799#endif
12800}
12801
12802/*
12803 * "winbufnr(nr)" function
12804 */
12805 static void
12806f_winbufnr(typval_T *argvars, typval_T *rettv)
12807{
12808 win_T *wp;
12809
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012810 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012811 if (wp == NULL)
12812 rettv->vval.v_number = -1;
12813 else
12814 rettv->vval.v_number = wp->w_buffer->b_fnum;
12815}
12816
12817/*
12818 * "wincol()" function
12819 */
12820 static void
12821f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12822{
12823 validate_cursor();
12824 rettv->vval.v_number = curwin->w_wcol + 1;
12825}
12826
12827/*
12828 * "winheight(nr)" function
12829 */
12830 static void
12831f_winheight(typval_T *argvars, typval_T *rettv)
12832{
12833 win_T *wp;
12834
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012835 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836 if (wp == NULL)
12837 rettv->vval.v_number = -1;
12838 else
12839 rettv->vval.v_number = wp->w_height;
12840}
12841
12842/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012843 * "winlayout()" function
12844 */
12845 static void
12846f_winlayout(typval_T *argvars, typval_T *rettv)
12847{
12848 tabpage_T *tp;
12849
12850 if (rettv_list_alloc(rettv) != OK)
12851 return;
12852
12853 if (argvars[0].v_type == VAR_UNKNOWN)
12854 tp = curtab;
12855 else
12856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012857 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012858 if (tp == NULL)
12859 return;
12860 }
12861
12862 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12863}
12864
12865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012866 * "winline()" function
12867 */
12868 static void
12869f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12870{
12871 validate_cursor();
12872 rettv->vval.v_number = curwin->w_wrow + 1;
12873}
12874
12875/*
12876 * "winnr()" function
12877 */
12878 static void
12879f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12880{
12881 int nr = 1;
12882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012883 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 rettv->vval.v_number = nr;
12885}
12886
12887/*
12888 * "winrestcmd()" function
12889 */
12890 static void
12891f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12892{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 win_T *wp;
12894 int winnr = 1;
12895 garray_T ga;
12896 char_u buf[50];
12897
12898 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012899 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 {
12901 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12902 ga_concat(&ga, buf);
12903 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12904 ga_concat(&ga, buf);
12905 ++winnr;
12906 }
12907 ga_append(&ga, NUL);
12908
12909 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012910 rettv->v_type = VAR_STRING;
12911}
12912
12913/*
12914 * "winrestview()" function
12915 */
12916 static void
12917f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12918{
12919 dict_T *dict;
12920
12921 if (argvars[0].v_type != VAR_DICT
12922 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012923 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924 else
12925 {
12926 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012927 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012929 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012930 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012931 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12933 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010012934 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 curwin->w_set_curswant = FALSE;
12936 }
12937
12938 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012939 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012940#ifdef FEAT_DIFF
12941 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012942 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943#endif
12944 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012945 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012946 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012947 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948
12949 check_cursor();
12950 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020012951 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952 changed_window_setting();
12953
12954 if (curwin->w_topline <= 0)
12955 curwin->w_topline = 1;
12956 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12957 curwin->w_topline = curbuf->b_ml.ml_line_count;
12958#ifdef FEAT_DIFF
12959 check_topfill(curwin, TRUE);
12960#endif
12961 }
12962}
12963
12964/*
12965 * "winsaveview()" function
12966 */
12967 static void
12968f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12969{
12970 dict_T *dict;
12971
12972 if (rettv_dict_alloc(rettv) == FAIL)
12973 return;
12974 dict = rettv->vval.v_dict;
12975
Bram Moolenaare0be1672018-07-08 16:50:37 +020012976 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
12977 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020012978 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020012980 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012981
Bram Moolenaare0be1672018-07-08 16:50:37 +020012982 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012983#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020012984 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012985#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020012986 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
12987 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988}
12989
12990/*
12991 * "winwidth(nr)" function
12992 */
12993 static void
12994f_winwidth(typval_T *argvars, typval_T *rettv)
12995{
12996 win_T *wp;
12997
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012998 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999 if (wp == NULL)
13000 rettv->vval.v_number = -1;
13001 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013003}
13004
13005/*
13006 * "wordcount()" function
13007 */
13008 static void
13009f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13010{
13011 if (rettv_dict_alloc(rettv) == FAIL)
13012 return;
13013 cursor_pos_info(rettv->vval.v_dict);
13014}
13015
13016/*
13017 * "writefile()" function
13018 */
13019 static void
13020f_writefile(typval_T *argvars, typval_T *rettv)
13021{
13022 int binary = FALSE;
13023 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013024#ifdef HAVE_FSYNC
13025 int do_fsync = p_fs;
13026#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027 char_u *fname;
13028 FILE *fd;
13029 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013030 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013031 list_T *list = NULL;
13032 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013034 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013035 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013036 return;
13037
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013038 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013039 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013040 list = argvars[0].vval.v_list;
13041 if (list == NULL)
13042 return;
13043 for (li = list->lv_first; li != NULL; li = li->li_next)
13044 if (tv_get_string_chk(&li->li_tv) == NULL)
13045 return;
13046 }
13047 else if (argvars[0].v_type == VAR_BLOB)
13048 {
13049 blob = argvars[0].vval.v_blob;
13050 if (blob == NULL)
13051 return;
13052 }
13053 else
13054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013055 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013056 return;
13057 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013058
13059 if (argvars[2].v_type != VAR_UNKNOWN)
13060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013061 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013062
13063 if (arg2 == NULL)
13064 return;
13065 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013067 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013069#ifdef HAVE_FSYNC
13070 if (vim_strchr(arg2, 's') != NULL)
13071 do_fsync = TRUE;
13072 else if (vim_strchr(arg2, 'S') != NULL)
13073 do_fsync = FALSE;
13074#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075 }
13076
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013077 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013078 if (fname == NULL)
13079 return;
13080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013081 /* Always open the file in binary mode, library functions have a mind of
13082 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13084 append ? APPENDBIN : WRITEBIN)) == NULL)
13085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013086 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087 ret = -1;
13088 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013089 else if (blob)
13090 {
13091 if (write_blob(fd, blob) == FAIL)
13092 ret = -1;
13093#ifdef HAVE_FSYNC
13094 else if (do_fsync)
13095 // Ignore the error, the user wouldn't know what to do about it.
13096 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013097 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013098#endif
13099 fclose(fd);
13100 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 else
13102 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013103 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013105#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013106 else if (do_fsync)
13107 /* Ignore the error, the user wouldn't know what to do about it.
13108 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013109 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013111 fclose(fd);
13112 }
13113
13114 rettv->vval.v_number = ret;
13115}
13116
13117/*
13118 * "xor(expr, expr)" function
13119 */
13120 static void
13121f_xor(typval_T *argvars, typval_T *rettv)
13122{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013123 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13124 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125}
13126
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127#endif /* FEAT_EVAL */