blob: 804dbb47d0381e6af5f6b70eaf80e8dfac332eb0 [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);
234#ifdef vim_mkdir
235static void f_mkdir(typval_T *argvars, typval_T *rettv);
236#endif
237static void f_mode(typval_T *argvars, typval_T *rettv);
238#ifdef FEAT_MZSCHEME
239static void f_mzeval(typval_T *argvars, typval_T *rettv);
240#endif
241static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
242static void f_nr2char(typval_T *argvars, typval_T *rettv);
243static void f_or(typval_T *argvars, typval_T *rettv);
244static void f_pathshorten(typval_T *argvars, typval_T *rettv);
245#ifdef FEAT_PERL
246static void f_perleval(typval_T *argvars, typval_T *rettv);
247#endif
248#ifdef FEAT_FLOAT
249static void f_pow(typval_T *argvars, typval_T *rettv);
250#endif
251static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
252static void f_printf(typval_T *argvars, typval_T *rettv);
253static void f_pumvisible(typval_T *argvars, typval_T *rettv);
254#ifdef FEAT_PYTHON3
255static void f_py3eval(typval_T *argvars, typval_T *rettv);
256#endif
257#ifdef FEAT_PYTHON
258static void f_pyeval(typval_T *argvars, typval_T *rettv);
259#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100260#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
261static void f_pyxeval(typval_T *argvars, typval_T *rettv);
262#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200263static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200264static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200265static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200266static void f_reg_executing(typval_T *argvars, typval_T *rettv);
267static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200268static void f_reltime(typval_T *argvars, typval_T *rettv);
269#ifdef FEAT_FLOAT
270static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
271#endif
272static void f_reltimestr(typval_T *argvars, typval_T *rettv);
273static void f_remote_expr(typval_T *argvars, typval_T *rettv);
274static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
275static void f_remote_peek(typval_T *argvars, typval_T *rettv);
276static void f_remote_read(typval_T *argvars, typval_T *rettv);
277static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100278static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200279static void f_remove(typval_T *argvars, typval_T *rettv);
280static void f_rename(typval_T *argvars, typval_T *rettv);
281static void f_repeat(typval_T *argvars, typval_T *rettv);
282static void f_resolve(typval_T *argvars, typval_T *rettv);
283static void f_reverse(typval_T *argvars, typval_T *rettv);
284#ifdef FEAT_FLOAT
285static void f_round(typval_T *argvars, typval_T *rettv);
286#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100287#ifdef FEAT_RUBY
288static void f_rubyeval(typval_T *argvars, typval_T *rettv);
289#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200290static void f_screenattr(typval_T *argvars, typval_T *rettv);
291static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100292static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200293static void f_screencol(typval_T *argvars, typval_T *rettv);
294static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100295static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_search(typval_T *argvars, typval_T *rettv);
297static void f_searchdecl(typval_T *argvars, typval_T *rettv);
298static void f_searchpair(typval_T *argvars, typval_T *rettv);
299static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
300static void f_searchpos(typval_T *argvars, typval_T *rettv);
301static void f_server2client(typval_T *argvars, typval_T *rettv);
302static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200303static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200304static void f_setbufvar(typval_T *argvars, typval_T *rettv);
305static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
306static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200307static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_setfperm(typval_T *argvars, typval_T *rettv);
309static void f_setline(typval_T *argvars, typval_T *rettv);
310static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200311static void f_setpos(typval_T *argvars, typval_T *rettv);
312static void f_setqflist(typval_T *argvars, typval_T *rettv);
313static void f_setreg(typval_T *argvars, typval_T *rettv);
314static void f_settabvar(typval_T *argvars, typval_T *rettv);
315static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100316static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_setwinvar(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_CRYPT
319static void f_sha256(typval_T *argvars, typval_T *rettv);
320#endif /* FEAT_CRYPT */
321static void f_shellescape(typval_T *argvars, typval_T *rettv);
322static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
323static void f_simplify(typval_T *argvars, typval_T *rettv);
324#ifdef FEAT_FLOAT
325static void f_sin(typval_T *argvars, typval_T *rettv);
326static void f_sinh(typval_T *argvars, typval_T *rettv);
327#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_soundfold(typval_T *argvars, typval_T *rettv);
329static void f_spellbadword(typval_T *argvars, typval_T *rettv);
330static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
331static void f_split(typval_T *argvars, typval_T *rettv);
332#ifdef FEAT_FLOAT
333static void f_sqrt(typval_T *argvars, typval_T *rettv);
334static void f_str2float(typval_T *argvars, typval_T *rettv);
335#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200336static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_str2nr(typval_T *argvars, typval_T *rettv);
338static void f_strchars(typval_T *argvars, typval_T *rettv);
339#ifdef HAVE_STRFTIME
340static void f_strftime(typval_T *argvars, typval_T *rettv);
341#endif
342static void f_strgetchar(typval_T *argvars, typval_T *rettv);
343static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200344static void f_strlen(typval_T *argvars, typval_T *rettv);
345static void f_strcharpart(typval_T *argvars, typval_T *rettv);
346static void f_strpart(typval_T *argvars, typval_T *rettv);
347static void f_strridx(typval_T *argvars, typval_T *rettv);
348static void f_strtrans(typval_T *argvars, typval_T *rettv);
349static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
350static void f_strwidth(typval_T *argvars, typval_T *rettv);
351static void f_submatch(typval_T *argvars, typval_T *rettv);
352static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200353static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200354static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200355static void f_synID(typval_T *argvars, typval_T *rettv);
356static void f_synIDattr(typval_T *argvars, typval_T *rettv);
357static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
358static void f_synstack(typval_T *argvars, typval_T *rettv);
359static void f_synconcealed(typval_T *argvars, typval_T *rettv);
360static void f_system(typval_T *argvars, typval_T *rettv);
361static void f_systemlist(typval_T *argvars, typval_T *rettv);
362static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
363static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
364static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
365static void f_taglist(typval_T *argvars, typval_T *rettv);
366static void f_tagfiles(typval_T *argvars, typval_T *rettv);
367static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368#ifdef FEAT_FLOAT
369static void f_tan(typval_T *argvars, typval_T *rettv);
370static void f_tanh(typval_T *argvars, typval_T *rettv);
371#endif
372#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200373static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200374static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_timer_start(typval_T *argvars, typval_T *rettv);
376static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200377static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200378#endif
379static void f_tolower(typval_T *argvars, typval_T *rettv);
380static void f_toupper(typval_T *argvars, typval_T *rettv);
381static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100382static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200383#ifdef FEAT_FLOAT
384static void f_trunc(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_type(typval_T *argvars, typval_T *rettv);
387static void f_undofile(typval_T *argvars, typval_T *rettv);
388static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200389static void f_virtcol(typval_T *argvars, typval_T *rettv);
390static void f_visualmode(typval_T *argvars, typval_T *rettv);
391static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200392static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200393static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
394static void f_win_getid(typval_T *argvars, typval_T *rettv);
395static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
396static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
397static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100398static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200399static void f_winbufnr(typval_T *argvars, typval_T *rettv);
400static void f_wincol(typval_T *argvars, typval_T *rettv);
401static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200402static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_winline(typval_T *argvars, typval_T *rettv);
404static void f_winnr(typval_T *argvars, typval_T *rettv);
405static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
406static void f_winrestview(typval_T *argvars, typval_T *rettv);
407static void f_winsaveview(typval_T *argvars, typval_T *rettv);
408static void f_winwidth(typval_T *argvars, typval_T *rettv);
409static void f_writefile(typval_T *argvars, typval_T *rettv);
410static void f_wordcount(typval_T *argvars, typval_T *rettv);
411static void f_xor(typval_T *argvars, typval_T *rettv);
412
413/*
414 * Array with names and number of arguments of all internal functions
415 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
416 */
417static struct fst
418{
419 char *f_name; /* function name */
420 char f_min_argc; /* minimal number of arguments */
421 char f_max_argc; /* maximal number of arguments */
422 void (*f_func)(typval_T *args, typval_T *rvar);
423 /* implementation of function */
424} functions[] =
425{
426#ifdef FEAT_FLOAT
427 {"abs", 1, 1, f_abs},
428 {"acos", 1, 1, f_acos}, /* WJMc */
429#endif
430 {"add", 2, 2, f_add},
431 {"and", 2, 2, f_and},
432 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200433 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200434 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200435 {"argidx", 0, 0, f_argidx},
436 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200437 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438#ifdef FEAT_FLOAT
439 {"asin", 1, 1, f_asin}, /* WJMc */
440#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100441 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200442 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100443 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200445 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100447 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200448 {"assert_match", 2, 3, f_assert_match},
449 {"assert_notequal", 2, 3, f_assert_notequal},
450 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100451 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452 {"assert_true", 1, 2, f_assert_true},
453#ifdef FEAT_FLOAT
454 {"atan", 1, 1, f_atan},
455 {"atan2", 2, 2, f_atan2},
456#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100457#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200458 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100459 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100460# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100461 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100462# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100463#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200464 {"browse", 4, 4, f_browse},
465 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200466 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200467 {"bufexists", 1, 1, f_bufexists},
468 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
469 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
470 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
471 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200472 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473 {"bufloaded", 1, 1, f_bufloaded},
474 {"bufname", 1, 1, f_bufname},
475 {"bufnr", 1, 2, f_bufnr},
476 {"bufwinid", 1, 1, f_bufwinid},
477 {"bufwinnr", 1, 1, f_bufwinnr},
478 {"byte2line", 1, 1, f_byte2line},
479 {"byteidx", 2, 2, f_byteidx},
480 {"byteidxcomp", 2, 2, f_byteidxcomp},
481 {"call", 2, 3, f_call},
482#ifdef FEAT_FLOAT
483 {"ceil", 1, 1, f_ceil},
484#endif
485#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100486 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200487 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200488 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200489 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
490 {"ch_evalraw", 2, 3, f_ch_evalraw},
491 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
492 {"ch_getjob", 1, 1, f_ch_getjob},
493 {"ch_info", 1, 1, f_ch_info},
494 {"ch_log", 1, 2, f_ch_log},
495 {"ch_logfile", 1, 2, f_ch_logfile},
496 {"ch_open", 1, 2, f_ch_open},
497 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100498 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499 {"ch_readraw", 1, 2, f_ch_readraw},
500 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
501 {"ch_sendraw", 2, 3, f_ch_sendraw},
502 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200503 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504#endif
505 {"changenr", 0, 0, f_changenr},
506 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200507 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100509 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510 {"col", 1, 1, f_col},
511#if defined(FEAT_INS_EXPAND)
512 {"complete", 2, 2, f_complete},
513 {"complete_add", 1, 1, f_complete_add},
514 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100515 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516#endif
517 {"confirm", 1, 4, f_confirm},
518 {"copy", 1, 1, f_copy},
519#ifdef FEAT_FLOAT
520 {"cos", 1, 1, f_cos},
521 {"cosh", 1, 1, f_cosh},
522#endif
523 {"count", 2, 4, f_count},
524 {"cscope_connection",0,3, f_cscope_connection},
525 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100526#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200527 {"debugbreak", 1, 1, f_debugbreak},
528#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"deepcopy", 1, 2, f_deepcopy},
530 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200531 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"did_filetype", 0, 0, f_did_filetype},
533 {"diff_filler", 1, 1, f_diff_filler},
534 {"diff_hlID", 2, 2, f_diff_hlID},
535 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200536 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"escape", 2, 2, f_escape},
538 {"eval", 1, 1, f_eval},
539 {"eventhandler", 0, 0, f_eventhandler},
540 {"executable", 1, 1, f_executable},
541 {"execute", 1, 2, f_execute},
542 {"exepath", 1, 1, f_exepath},
543 {"exists", 1, 1, f_exists},
544#ifdef FEAT_FLOAT
545 {"exp", 1, 1, f_exp},
546#endif
547 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200548 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"extend", 2, 3, f_extend},
550 {"feedkeys", 1, 2, f_feedkeys},
551 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
552 {"filereadable", 1, 1, f_filereadable},
553 {"filewritable", 1, 1, f_filewritable},
554 {"filter", 2, 2, f_filter},
555 {"finddir", 1, 3, f_finddir},
556 {"findfile", 1, 3, f_findfile},
557#ifdef FEAT_FLOAT
558 {"float2nr", 1, 1, f_float2nr},
559 {"floor", 1, 1, f_floor},
560 {"fmod", 2, 2, f_fmod},
561#endif
562 {"fnameescape", 1, 1, f_fnameescape},
563 {"fnamemodify", 2, 2, f_fnamemodify},
564 {"foldclosed", 1, 1, f_foldclosed},
565 {"foldclosedend", 1, 1, f_foldclosedend},
566 {"foldlevel", 1, 1, f_foldlevel},
567 {"foldtext", 0, 0, f_foldtext},
568 {"foldtextresult", 1, 1, f_foldtextresult},
569 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200570 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200571 {"function", 1, 3, f_function},
572 {"garbagecollect", 0, 1, f_garbagecollect},
573 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200574 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200575 {"getbufline", 2, 3, f_getbufline},
576 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100577 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200578 {"getchar", 0, 1, f_getchar},
579 {"getcharmod", 0, 0, f_getcharmod},
580 {"getcharsearch", 0, 0, f_getcharsearch},
581 {"getcmdline", 0, 0, f_getcmdline},
582 {"getcmdpos", 0, 0, f_getcmdpos},
583 {"getcmdtype", 0, 0, f_getcmdtype},
584 {"getcmdwintype", 0, 0, f_getcmdwintype},
585#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200586 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587#endif
588 {"getcurpos", 0, 0, f_getcurpos},
589 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200590 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"getfontname", 0, 1, f_getfontname},
592 {"getfperm", 1, 1, f_getfperm},
593 {"getfsize", 1, 1, f_getfsize},
594 {"getftime", 1, 1, f_getftime},
595 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100596 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200597 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200598 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100599 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"getpid", 0, 0, f_getpid},
601 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200602 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603 {"getreg", 0, 3, f_getreg},
604 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200605 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606 {"gettabvar", 2, 3, f_gettabvar},
607 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100608 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200609 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100610 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"getwinposx", 0, 0, f_getwinposx},
612 {"getwinposy", 0, 0, f_getwinposy},
613 {"getwinvar", 2, 3, f_getwinvar},
614 {"glob", 1, 4, f_glob},
615 {"glob2regpat", 1, 1, f_glob2regpat},
616 {"globpath", 2, 5, f_globpath},
617 {"has", 1, 1, f_has},
618 {"has_key", 2, 2, f_has_key},
619 {"haslocaldir", 0, 2, f_haslocaldir},
620 {"hasmapto", 1, 3, f_hasmapto},
621 {"highlightID", 1, 1, f_hlID}, /* obsolete */
622 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
623 {"histadd", 2, 2, f_histadd},
624 {"histdel", 1, 2, f_histdel},
625 {"histget", 1, 2, f_histget},
626 {"histnr", 1, 1, f_histnr},
627 {"hlID", 1, 1, f_hlID},
628 {"hlexists", 1, 1, f_hlexists},
629 {"hostname", 0, 0, f_hostname},
630 {"iconv", 3, 3, f_iconv},
631 {"indent", 1, 1, f_indent},
632 {"index", 2, 4, f_index},
633 {"input", 1, 3, f_input},
634 {"inputdialog", 1, 3, f_inputdialog},
635 {"inputlist", 1, 1, f_inputlist},
636 {"inputrestore", 0, 0, f_inputrestore},
637 {"inputsave", 0, 0, f_inputsave},
638 {"inputsecret", 1, 2, f_inputsecret},
639 {"insert", 2, 3, f_insert},
640 {"invert", 1, 1, f_invert},
641 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
643 {"isinf", 1, 1, f_isinf},
644#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645 {"islocked", 1, 1, f_islocked},
646#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
647 {"isnan", 1, 1, f_isnan},
648#endif
649 {"items", 1, 1, f_items},
650#ifdef FEAT_JOB_CHANNEL
651 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200652 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"job_setoptions", 2, 2, f_job_setoptions},
654 {"job_start", 1, 2, f_job_start},
655 {"job_status", 1, 1, f_job_status},
656 {"job_stop", 1, 2, f_job_stop},
657#endif
658 {"join", 1, 2, f_join},
659 {"js_decode", 1, 1, f_js_decode},
660 {"js_encode", 1, 1, f_js_encode},
661 {"json_decode", 1, 1, f_json_decode},
662 {"json_encode", 1, 1, f_json_encode},
663 {"keys", 1, 1, f_keys},
664 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
665 {"len", 1, 1, f_len},
666 {"libcall", 3, 3, f_libcall},
667 {"libcallnr", 3, 3, f_libcallnr},
668 {"line", 1, 1, f_line},
669 {"line2byte", 1, 1, f_line2byte},
670 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200671 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200672 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200673 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200674 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675 {"localtime", 0, 0, f_localtime},
676#ifdef FEAT_FLOAT
677 {"log", 1, 1, f_log},
678 {"log10", 1, 1, f_log10},
679#endif
680#ifdef FEAT_LUA
681 {"luaeval", 1, 2, f_luaeval},
682#endif
683 {"map", 2, 2, f_map},
684 {"maparg", 1, 4, f_maparg},
685 {"mapcheck", 1, 3, f_mapcheck},
686 {"match", 2, 4, f_match},
687 {"matchadd", 2, 5, f_matchadd},
688 {"matchaddpos", 2, 5, f_matchaddpos},
689 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100690 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200691 {"matchend", 2, 4, f_matchend},
692 {"matchlist", 2, 4, f_matchlist},
693 {"matchstr", 2, 4, f_matchstr},
694 {"matchstrpos", 2, 4, f_matchstrpos},
695 {"max", 1, 1, f_max},
696 {"min", 1, 1, f_min},
697#ifdef vim_mkdir
698 {"mkdir", 1, 3, f_mkdir},
699#endif
700 {"mode", 0, 1, f_mode},
701#ifdef FEAT_MZSCHEME
702 {"mzeval", 1, 1, f_mzeval},
703#endif
704 {"nextnonblank", 1, 1, f_nextnonblank},
705 {"nr2char", 1, 2, f_nr2char},
706 {"or", 2, 2, f_or},
707 {"pathshorten", 1, 1, f_pathshorten},
708#ifdef FEAT_PERL
709 {"perleval", 1, 1, f_perleval},
710#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200711#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200712 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200713 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200714 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200715 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200716 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200717 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200718 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200719 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200720 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200721 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200722 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200723 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200724 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200725 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200726 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200727 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200728 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200729 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200731#ifdef FEAT_FLOAT
732 {"pow", 2, 2, f_pow},
733#endif
734 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100735 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200736#ifdef FEAT_JOB_CHANNEL
737 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200738 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200739 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
740#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100741#ifdef FEAT_TEXT_PROP
742 {"prop_add", 3, 3, f_prop_add},
743 {"prop_clear", 1, 3, f_prop_clear},
744 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100745 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100746 {"prop_type_add", 2, 2, f_prop_type_add},
747 {"prop_type_change", 2, 2, f_prop_type_change},
748 {"prop_type_delete", 1, 2, f_prop_type_delete},
749 {"prop_type_get", 1, 2, f_prop_type_get},
750 {"prop_type_list", 0, 1, f_prop_type_list},
751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"pumvisible", 0, 0, f_pumvisible},
753#ifdef FEAT_PYTHON3
754 {"py3eval", 1, 1, f_py3eval},
755#endif
756#ifdef FEAT_PYTHON
757 {"pyeval", 1, 1, f_pyeval},
758#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100759#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
760 {"pyxeval", 1, 1, f_pyxeval},
761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200763 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200764 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200765 {"reg_executing", 0, 0, f_reg_executing},
766 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"reltime", 0, 2, f_reltime},
768#ifdef FEAT_FLOAT
769 {"reltimefloat", 1, 1, f_reltimefloat},
770#endif
771 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100772 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"remote_foreground", 1, 1, f_remote_foreground},
774 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100775 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100777 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200778 {"remove", 2, 3, f_remove},
779 {"rename", 2, 2, f_rename},
780 {"repeat", 2, 2, f_repeat},
781 {"resolve", 1, 1, f_resolve},
782 {"reverse", 1, 1, f_reverse},
783#ifdef FEAT_FLOAT
784 {"round", 1, 1, f_round},
785#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100786#ifdef FEAT_RUBY
787 {"rubyeval", 1, 1, f_rubyeval},
788#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"screenattr", 2, 2, f_screenattr},
790 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100791 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200793 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100795 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"search", 1, 4, f_search},
797 {"searchdecl", 1, 3, f_searchdecl},
798 {"searchpair", 3, 7, f_searchpair},
799 {"searchpairpos", 3, 7, f_searchpairpos},
800 {"searchpos", 1, 4, f_searchpos},
801 {"server2client", 2, 2, f_server2client},
802 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200803 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804 {"setbufvar", 3, 3, f_setbufvar},
805 {"setcharsearch", 1, 1, f_setcharsearch},
806 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200807 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808 {"setfperm", 2, 2, f_setfperm},
809 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200810 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100811 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200813 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200814 {"setreg", 2, 3, f_setreg},
815 {"settabvar", 3, 3, f_settabvar},
816 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100817 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setwinvar", 3, 3, f_setwinvar},
819#ifdef FEAT_CRYPT
820 {"sha256", 1, 1, f_sha256},
821#endif
822 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100823 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100824#ifdef FEAT_SIGNS
825 {"sign_define", 1, 2, f_sign_define},
826 {"sign_getdefined", 0, 1, f_sign_getdefined},
827 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100828 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100829 {"sign_place", 4, 5, f_sign_place},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200830 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100831 {"sign_undefine", 0, 1, f_sign_undefine},
832 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200833 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"simplify", 1, 1, f_simplify},
836#ifdef FEAT_FLOAT
837 {"sin", 1, 1, f_sin},
838 {"sinh", 1, 1, f_sinh},
839#endif
840 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200841#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200842 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200843 {"sound_playevent", 1, 2, f_sound_playevent},
844 {"sound_playfile", 1, 2, f_sound_playfile},
845 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200846#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200847 {"soundfold", 1, 1, f_soundfold},
848 {"spellbadword", 0, 1, f_spellbadword},
849 {"spellsuggest", 1, 3, f_spellsuggest},
850 {"split", 1, 3, f_split},
851#ifdef FEAT_FLOAT
852 {"sqrt", 1, 1, f_sqrt},
853 {"str2float", 1, 1, f_str2float},
854#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200855 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"str2nr", 1, 2, f_str2nr},
857 {"strcharpart", 2, 3, f_strcharpart},
858 {"strchars", 1, 2, f_strchars},
859 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
860#ifdef HAVE_STRFTIME
861 {"strftime", 1, 2, f_strftime},
862#endif
863 {"strgetchar", 2, 2, f_strgetchar},
864 {"stridx", 2, 3, f_stridx},
865 {"string", 1, 1, f_string},
866 {"strlen", 1, 1, f_strlen},
867 {"strpart", 2, 3, f_strpart},
868 {"strridx", 2, 3, f_strridx},
869 {"strtrans", 1, 1, f_strtrans},
870 {"strwidth", 1, 1, f_strwidth},
871 {"submatch", 1, 2, f_submatch},
872 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200873 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200874 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"synID", 3, 3, f_synID},
876 {"synIDattr", 2, 3, f_synIDattr},
877 {"synIDtrans", 1, 1, f_synIDtrans},
878 {"synconcealed", 2, 2, f_synconcealed},
879 {"synstack", 2, 2, f_synstack},
880 {"system", 1, 2, f_system},
881 {"systemlist", 1, 2, f_systemlist},
882 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
883 {"tabpagenr", 0, 1, f_tabpagenr},
884 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
885 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100886 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200887#ifdef FEAT_FLOAT
888 {"tan", 1, 1, f_tan},
889 {"tanh", 1, 1, f_tanh},
890#endif
891 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200892#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100893 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
894 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100895 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200896 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200897# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
898 {"term_getansicolors", 1, 1, f_term_getansicolors},
899# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200900 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200901 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200903 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200904 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200905 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200906 {"term_getstatus", 1, 1, f_term_getstatus},
907 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200908 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200909 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200910 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200911 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200912# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
913 {"term_setansicolors", 2, 2, f_term_setansicolors},
914# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100915 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100916 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200917 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200918 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200919 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200920#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
922 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200923 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200925 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200926 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100927 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100928 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929#ifdef FEAT_JOB_CHANNEL
930 {"test_null_channel", 0, 0, f_test_null_channel},
931#endif
932 {"test_null_dict", 0, 0, f_test_null_dict},
933#ifdef FEAT_JOB_CHANNEL
934 {"test_null_job", 0, 0, f_test_null_job},
935#endif
936 {"test_null_list", 0, 0, f_test_null_list},
937 {"test_null_partial", 0, 0, f_test_null_partial},
938 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200939 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100940 {"test_override", 2, 2, f_test_override},
941 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200942#ifdef FEAT_GUI
943 {"test_scrollbar", 3, 3, f_test_scrollbar},
944#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200945#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200946 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200947#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200948 {"test_settime", 1, 1, f_test_settime},
949#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200950 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200951 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"timer_start", 2, 3, f_timer_start},
953 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200954 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200955#endif
956 {"tolower", 1, 1, f_tolower},
957 {"toupper", 1, 1, f_toupper},
958 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100959 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200960#ifdef FEAT_FLOAT
961 {"trunc", 1, 1, f_trunc},
962#endif
963 {"type", 1, 1, f_type},
964 {"undofile", 1, 1, f_undofile},
965 {"undotree", 0, 0, f_undotree},
966 {"uniq", 1, 3, f_uniq},
967 {"values", 1, 1, f_values},
968 {"virtcol", 1, 1, f_virtcol},
969 {"visualmode", 0, 1, f_visualmode},
970 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200971 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200972 {"win_findbuf", 1, 1, f_win_findbuf},
973 {"win_getid", 0, 2, f_win_getid},
974 {"win_gotoid", 1, 1, f_win_gotoid},
975 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
976 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100977 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978 {"winbufnr", 1, 1, f_winbufnr},
979 {"wincol", 0, 0, f_wincol},
980 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200981 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982 {"winline", 0, 0, f_winline},
983 {"winnr", 0, 1, f_winnr},
984 {"winrestcmd", 0, 0, f_winrestcmd},
985 {"winrestview", 1, 1, f_winrestview},
986 {"winsaveview", 0, 0, f_winsaveview},
987 {"winwidth", 1, 1, f_winwidth},
988 {"wordcount", 0, 0, f_wordcount},
989 {"writefile", 2, 3, f_writefile},
990 {"xor", 2, 2, f_xor},
991};
992
993#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
994
995/*
996 * Function given to ExpandGeneric() to obtain the list of internal
997 * or user defined function names.
998 */
999 char_u *
1000get_function_name(expand_T *xp, int idx)
1001{
1002 static int intidx = -1;
1003 char_u *name;
1004
1005 if (idx == 0)
1006 intidx = -1;
1007 if (intidx < 0)
1008 {
1009 name = get_user_func_name(xp, idx);
1010 if (name != NULL)
1011 return name;
1012 }
1013 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1014 {
1015 STRCPY(IObuff, functions[intidx].f_name);
1016 STRCAT(IObuff, "(");
1017 if (functions[intidx].f_max_argc == 0)
1018 STRCAT(IObuff, ")");
1019 return IObuff;
1020 }
1021
1022 return NULL;
1023}
1024
1025/*
1026 * Function given to ExpandGeneric() to obtain the list of internal or
1027 * user defined variable or function names.
1028 */
1029 char_u *
1030get_expr_name(expand_T *xp, int idx)
1031{
1032 static int intidx = -1;
1033 char_u *name;
1034
1035 if (idx == 0)
1036 intidx = -1;
1037 if (intidx < 0)
1038 {
1039 name = get_function_name(xp, idx);
1040 if (name != NULL)
1041 return name;
1042 }
1043 return get_user_var_name(xp, ++intidx);
1044}
1045
1046#endif /* FEAT_CMDL_COMPL */
1047
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048/*
1049 * Find internal function in table above.
1050 * Return index, or -1 if not found
1051 */
1052 int
1053find_internal_func(
1054 char_u *name) /* name of the function */
1055{
1056 int first = 0;
1057 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1058 int cmp;
1059 int x;
1060
1061 /*
1062 * Find the function name in the table. Binary search.
1063 */
1064 while (first <= last)
1065 {
1066 x = first + ((unsigned)(last - first) >> 1);
1067 cmp = STRCMP(name, functions[x].f_name);
1068 if (cmp < 0)
1069 last = x - 1;
1070 else if (cmp > 0)
1071 first = x + 1;
1072 else
1073 return x;
1074 }
1075 return -1;
1076}
1077
1078 int
1079call_internal_func(
1080 char_u *name,
1081 int argcount,
1082 typval_T *argvars,
1083 typval_T *rettv)
1084{
1085 int i;
1086
1087 i = find_internal_func(name);
1088 if (i < 0)
1089 return ERROR_UNKNOWN;
1090 if (argcount < functions[i].f_min_argc)
1091 return ERROR_TOOFEW;
1092 if (argcount > functions[i].f_max_argc)
1093 return ERROR_TOOMANY;
1094 argvars[argcount].v_type = VAR_UNKNOWN;
1095 functions[i].f_func(argvars, rettv);
1096 return ERROR_NONE;
1097}
1098
1099/*
1100 * Return TRUE for a non-zero Number and a non-empty String.
1101 */
1102 static int
1103non_zero_arg(typval_T *argvars)
1104{
1105 return ((argvars[0].v_type == VAR_NUMBER
1106 && argvars[0].vval.v_number != 0)
1107 || (argvars[0].v_type == VAR_SPECIAL
1108 && argvars[0].vval.v_number == VVAL_TRUE)
1109 || (argvars[0].v_type == VAR_STRING
1110 && argvars[0].vval.v_string != NULL
1111 && *argvars[0].vval.v_string != NUL));
1112}
1113
1114/*
1115 * Get the lnum from the first argument.
1116 * Also accepts ".", "$", etc., but that only works for the current buffer.
1117 * Returns -1 on error.
1118 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001119 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001120tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001121{
1122 typval_T rettv;
1123 linenr_T lnum;
1124
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001125 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126 if (lnum == 0) /* no valid number, try using line() */
1127 {
1128 rettv.v_type = VAR_NUMBER;
1129 f_line(argvars, &rettv);
1130 lnum = (linenr_T)rettv.vval.v_number;
1131 clear_tv(&rettv);
1132 }
1133 return lnum;
1134}
1135
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001136/*
1137 * Get the lnum from the first argument.
1138 * Also accepts "$", then "buf" is used.
1139 * Returns 0 on error.
1140 */
1141 static linenr_T
1142tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1143{
1144 if (argvars[0].v_type == VAR_STRING
1145 && argvars[0].vval.v_string != NULL
1146 && argvars[0].vval.v_string[0] == '$'
1147 && buf != NULL)
1148 return buf->b_ml.ml_line_count;
1149 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1150}
1151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001152#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001153/*
1154 * Get the float value of "argvars[0]" into "f".
1155 * Returns FAIL when the argument is not a Number or Float.
1156 */
1157 static int
1158get_float_arg(typval_T *argvars, float_T *f)
1159{
1160 if (argvars[0].v_type == VAR_FLOAT)
1161 {
1162 *f = argvars[0].vval.v_float;
1163 return OK;
1164 }
1165 if (argvars[0].v_type == VAR_NUMBER)
1166 {
1167 *f = (float_T)argvars[0].vval.v_number;
1168 return OK;
1169 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001170 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001171 return FAIL;
1172}
1173
1174/*
1175 * "abs(expr)" function
1176 */
1177 static void
1178f_abs(typval_T *argvars, typval_T *rettv)
1179{
1180 if (argvars[0].v_type == VAR_FLOAT)
1181 {
1182 rettv->v_type = VAR_FLOAT;
1183 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1184 }
1185 else
1186 {
1187 varnumber_T n;
1188 int error = FALSE;
1189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001190 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001191 if (error)
1192 rettv->vval.v_number = -1;
1193 else if (n > 0)
1194 rettv->vval.v_number = n;
1195 else
1196 rettv->vval.v_number = -n;
1197 }
1198}
1199
1200/*
1201 * "acos()" function
1202 */
1203 static void
1204f_acos(typval_T *argvars, typval_T *rettv)
1205{
1206 float_T f = 0.0;
1207
1208 rettv->v_type = VAR_FLOAT;
1209 if (get_float_arg(argvars, &f) == OK)
1210 rettv->vval.v_float = acos(f);
1211 else
1212 rettv->vval.v_float = 0.0;
1213}
1214#endif
1215
1216/*
1217 * "add(list, item)" function
1218 */
1219 static void
1220f_add(typval_T *argvars, typval_T *rettv)
1221{
1222 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001223 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001224
1225 rettv->vval.v_number = 1; /* Default: Failed */
1226 if (argvars[0].v_type == VAR_LIST)
1227 {
1228 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001229 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001230 (char_u *)N_("add() argument"), TRUE)
1231 && list_append_tv(l, &argvars[1]) == OK)
1232 copy_tv(&argvars[0], rettv);
1233 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001234 else if (argvars[0].v_type == VAR_BLOB)
1235 {
1236 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001237 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001238 (char_u *)N_("add() argument"), TRUE))
1239 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001240 int error = FALSE;
1241 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1242
1243 if (!error)
1244 {
1245 ga_append(&b->bv_ga, (int)n);
1246 copy_tv(&argvars[0], rettv);
1247 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001248 }
1249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001251 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252}
1253
1254/*
1255 * "and(expr, expr)" function
1256 */
1257 static void
1258f_and(typval_T *argvars, typval_T *rettv)
1259{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001260 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1261 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001262}
1263
1264/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001265 * If there is a window for "curbuf", make it the current window.
1266 */
1267 static void
1268find_win_for_curbuf(void)
1269{
1270 wininfo_T *wip;
1271
1272 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1273 {
1274 if (wip->wi_win != NULL)
1275 {
1276 curwin = wip->wi_win;
1277 break;
1278 }
1279 }
1280}
1281
1282/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001283 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001284 */
1285 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001286set_buffer_lines(
1287 buf_T *buf,
1288 linenr_T lnum_arg,
1289 int append,
1290 typval_T *lines,
1291 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001292{
Bram Moolenaarca851592018-06-06 21:04:07 +02001293 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1294 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295 list_T *l = NULL;
1296 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001297 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001298 linenr_T append_lnum;
1299 buf_T *curbuf_save = NULL;
1300 win_T *curwin_save = NULL;
1301 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001302
Bram Moolenaarca851592018-06-06 21:04:07 +02001303 /* When using the current buffer ml_mfp will be set if needed. Useful when
1304 * setline() is used on startup. For other buffers the buffer must be
1305 * loaded. */
1306 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001307 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001308 rettv->vval.v_number = 1; /* FAIL */
1309 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310 }
1311
Bram Moolenaarca851592018-06-06 21:04:07 +02001312 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001314 curbuf_save = curbuf;
1315 curwin_save = curwin;
1316 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001317 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001318 }
1319
1320 if (append)
1321 // appendbufline() uses the line number below which we insert
1322 append_lnum = lnum - 1;
1323 else
1324 // setbufline() uses the line number above which we insert, we only
1325 // append if it's below the last line
1326 append_lnum = curbuf->b_ml.ml_line_count;
1327
1328 if (lines->v_type == VAR_LIST)
1329 {
1330 l = lines->vval.v_list;
1331 li = l->lv_first;
1332 }
1333 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001334 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001335
1336 /* default result is zero == OK */
1337 for (;;)
1338 {
1339 if (l != NULL)
1340 {
1341 /* list argument, get next string */
1342 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001344 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 li = li->li_next;
1346 }
1347
Bram Moolenaarca851592018-06-06 21:04:07 +02001348 rettv->vval.v_number = 1; /* FAIL */
1349 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1350 break;
1351
1352 /* When coming here from Insert mode, sync undo, so that this can be
1353 * undone separately from what was previously inserted. */
1354 if (u_sync_once == 2)
1355 {
1356 u_sync_once = 1; /* notify that u_sync() was called */
1357 u_sync(TRUE);
1358 }
1359
1360 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1361 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001362 // Existing line, replace it.
1363 // Removes any existing text properties.
1364 if (u_savesub(lnum) == OK && ml_replace_len(
1365 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001366 {
1367 changed_bytes(lnum, 0);
1368 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1369 check_cursor_col();
1370 rettv->vval.v_number = 0; /* OK */
1371 }
1372 }
1373 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1374 {
1375 /* append the line */
1376 ++added;
1377 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1378 rettv->vval.v_number = 0; /* OK */
1379 }
1380
1381 if (l == NULL) /* only one string argument */
1382 break;
1383 ++lnum;
1384 }
1385
1386 if (added > 0)
1387 {
1388 win_T *wp;
1389 tabpage_T *tp;
1390
1391 appended_lines_mark(append_lnum, added);
1392 FOR_ALL_TAB_WINDOWS(tp, wp)
1393 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1394 wp->w_cursor.lnum += added;
1395 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001396 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001398
1399 if (!is_curbuf)
1400 {
1401 curbuf = curbuf_save;
1402 curwin = curwin_save;
1403 }
1404}
1405
1406/*
1407 * "append(lnum, string/list)" function
1408 */
1409 static void
1410f_append(typval_T *argvars, typval_T *rettv)
1411{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001412 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001413
1414 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1415}
1416
1417/*
1418 * "appendbufline(buf, lnum, string/list)" function
1419 */
1420 static void
1421f_appendbufline(typval_T *argvars, typval_T *rettv)
1422{
1423 linenr_T lnum;
1424 buf_T *buf;
1425
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001426 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001427 if (buf == NULL)
1428 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001429 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001430 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001431 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001432 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1433 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001434}
1435
1436/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001437 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438 */
1439 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001440f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001441{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001442 win_T *wp;
1443
1444 if (argvars[0].v_type == VAR_UNKNOWN)
1445 // use the current window
1446 rettv->vval.v_number = ARGCOUNT;
1447 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001448 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001449 // use the global argument list
1450 rettv->vval.v_number = GARGCOUNT;
1451 else
1452 {
1453 // use the argument list of the specified window
1454 wp = find_win_by_nr_or_id(&argvars[0]);
1455 if (wp != NULL)
1456 rettv->vval.v_number = WARGCOUNT(wp);
1457 else
1458 rettv->vval.v_number = -1;
1459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460}
1461
1462/*
1463 * "argidx()" function
1464 */
1465 static void
1466f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1467{
1468 rettv->vval.v_number = curwin->w_arg_idx;
1469}
1470
1471/*
1472 * "arglistid()" function
1473 */
1474 static void
1475f_arglistid(typval_T *argvars, typval_T *rettv)
1476{
1477 win_T *wp;
1478
1479 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001480 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001481 if (wp != NULL)
1482 rettv->vval.v_number = wp->w_alist->id;
1483}
1484
1485/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001486 * Get the argument list for a given window
1487 */
1488 static void
1489get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1490{
1491 int idx;
1492
1493 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1494 for (idx = 0; idx < argcount; ++idx)
1495 list_append_string(rettv->vval.v_list,
1496 alist_name(&arglist[idx]), -1);
1497}
1498
1499/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001500 * "argv(nr)" function
1501 */
1502 static void
1503f_argv(typval_T *argvars, typval_T *rettv)
1504{
1505 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001506 aentry_T *arglist = NULL;
1507 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508
1509 if (argvars[0].v_type != VAR_UNKNOWN)
1510 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001511 if (argvars[1].v_type == VAR_UNKNOWN)
1512 {
1513 arglist = ARGLIST;
1514 argcount = ARGCOUNT;
1515 }
1516 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001517 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001518 {
1519 arglist = GARGLIST;
1520 argcount = GARGCOUNT;
1521 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001522 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001523 {
1524 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1525
1526 if (wp != NULL)
1527 {
1528 /* Use the argument list of the specified window */
1529 arglist = WARGLIST(wp);
1530 argcount = WARGCOUNT(wp);
1531 }
1532 }
1533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001535 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001536 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 if (arglist != NULL && idx >= 0 && idx < argcount)
1538 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1539 else if (idx == -1)
1540 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001542 else
1543 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001544}
1545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546#ifdef FEAT_FLOAT
1547/*
1548 * "asin()" function
1549 */
1550 static void
1551f_asin(typval_T *argvars, typval_T *rettv)
1552{
1553 float_T f = 0.0;
1554
1555 rettv->v_type = VAR_FLOAT;
1556 if (get_float_arg(argvars, &f) == OK)
1557 rettv->vval.v_float = asin(f);
1558 else
1559 rettv->vval.v_float = 0.0;
1560}
1561
1562/*
1563 * "atan()" function
1564 */
1565 static void
1566f_atan(typval_T *argvars, typval_T *rettv)
1567{
1568 float_T f = 0.0;
1569
1570 rettv->v_type = VAR_FLOAT;
1571 if (get_float_arg(argvars, &f) == OK)
1572 rettv->vval.v_float = atan(f);
1573 else
1574 rettv->vval.v_float = 0.0;
1575}
1576
1577/*
1578 * "atan2()" function
1579 */
1580 static void
1581f_atan2(typval_T *argvars, typval_T *rettv)
1582{
1583 float_T fx = 0.0, fy = 0.0;
1584
1585 rettv->v_type = VAR_FLOAT;
1586 if (get_float_arg(argvars, &fx) == OK
1587 && get_float_arg(&argvars[1], &fy) == OK)
1588 rettv->vval.v_float = atan2(fx, fy);
1589 else
1590 rettv->vval.v_float = 0.0;
1591}
1592#endif
1593
1594/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001595 * "balloon_show()" function
1596 */
1597#ifdef FEAT_BEVAL
1598 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001599f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1600{
1601 rettv->v_type = VAR_STRING;
1602 if (balloonEval != NULL)
1603 {
1604 if (balloonEval->msg == NULL)
1605 rettv->vval.v_string = NULL;
1606 else
1607 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1608 }
1609}
1610
1611 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001612f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1613{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001614 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001615 {
1616 if (argvars[0].v_type == VAR_LIST
1617# ifdef FEAT_GUI
1618 && !gui.in_use
1619# endif
1620 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001621 {
1622 list_T *l = argvars[0].vval.v_list;
1623
1624 // empty list removes the balloon
1625 post_balloon(balloonEval, NULL,
1626 l == NULL || l->lv_len == 0 ? NULL : l);
1627 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001628 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001629 {
1630 char_u *mesg = tv_get_string_chk(&argvars[0]);
1631
1632 if (mesg != NULL)
1633 // empty string removes the balloon
1634 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1635 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001636 }
1637}
1638
Bram Moolenaar669a8282017-11-19 20:13:05 +01001639# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001640 static void
1641f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1642{
1643 if (rettv_list_alloc(rettv) == OK)
1644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001645 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001646
1647 if (msg != NULL)
1648 {
1649 pumitem_T *array;
1650 int size = split_message(msg, &array);
1651 int i;
1652
1653 /* Skip the first and last item, they are always empty. */
1654 for (i = 1; i < size - 1; ++i)
1655 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001656 while (size > 0)
1657 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001658 vim_free(array);
1659 }
1660 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001661}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001662# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001663#endif
1664
1665/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666 * "browse(save, title, initdir, default)" function
1667 */
1668 static void
1669f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1670{
1671#ifdef FEAT_BROWSE
1672 int save;
1673 char_u *title;
1674 char_u *initdir;
1675 char_u *defname;
1676 char_u buf[NUMBUFLEN];
1677 char_u buf2[NUMBUFLEN];
1678 int error = FALSE;
1679
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001680 save = (int)tv_get_number_chk(&argvars[0], &error);
1681 title = tv_get_string_chk(&argvars[1]);
1682 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1683 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684
1685 if (error || title == NULL || initdir == NULL || defname == NULL)
1686 rettv->vval.v_string = NULL;
1687 else
1688 rettv->vval.v_string =
1689 do_browse(save ? BROWSE_SAVE : 0,
1690 title, defname, NULL, initdir, NULL, curbuf);
1691#else
1692 rettv->vval.v_string = NULL;
1693#endif
1694 rettv->v_type = VAR_STRING;
1695}
1696
1697/*
1698 * "browsedir(title, initdir)" function
1699 */
1700 static void
1701f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1702{
1703#ifdef FEAT_BROWSE
1704 char_u *title;
1705 char_u *initdir;
1706 char_u buf[NUMBUFLEN];
1707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001708 title = tv_get_string_chk(&argvars[0]);
1709 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710
1711 if (title == NULL || initdir == NULL)
1712 rettv->vval.v_string = NULL;
1713 else
1714 rettv->vval.v_string = do_browse(BROWSE_DIR,
1715 title, NULL, NULL, initdir, NULL, curbuf);
1716#else
1717 rettv->vval.v_string = NULL;
1718#endif
1719 rettv->v_type = VAR_STRING;
1720}
1721
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001722/*
1723 * Find a buffer by number or exact name.
1724 */
1725 static buf_T *
1726find_buffer(typval_T *avar)
1727{
1728 buf_T *buf = NULL;
1729
1730 if (avar->v_type == VAR_NUMBER)
1731 buf = buflist_findnr((int)avar->vval.v_number);
1732 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1733 {
1734 buf = buflist_findname_exp(avar->vval.v_string);
1735 if (buf == NULL)
1736 {
1737 /* No full path name match, try a match with a URL or a "nofile"
1738 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001739 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001740 if (buf->b_fname != NULL
1741 && (path_with_url(buf->b_fname)
1742#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001743 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744#endif
1745 )
1746 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1747 break;
1748 }
1749 }
1750 return buf;
1751}
1752
1753/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001754 * "bufadd(expr)" function
1755 */
1756 static void
1757f_bufadd(typval_T *argvars, typval_T *rettv)
1758{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001759 char_u *name = tv_get_string(&argvars[0]);
1760
1761 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001762}
1763
1764/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001765 * "bufexists(expr)" function
1766 */
1767 static void
1768f_bufexists(typval_T *argvars, typval_T *rettv)
1769{
1770 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1771}
1772
1773/*
1774 * "buflisted(expr)" function
1775 */
1776 static void
1777f_buflisted(typval_T *argvars, typval_T *rettv)
1778{
1779 buf_T *buf;
1780
1781 buf = find_buffer(&argvars[0]);
1782 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1783}
1784
1785/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001786 * "bufload(expr)" function
1787 */
1788 static void
1789f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1790{
1791 buf_T *buf = get_buf_arg(&argvars[0]);
1792
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001793 if (buf != NULL)
1794 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001795}
1796
1797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001798 * "bufloaded(expr)" function
1799 */
1800 static void
1801f_bufloaded(typval_T *argvars, typval_T *rettv)
1802{
1803 buf_T *buf;
1804
1805 buf = find_buffer(&argvars[0]);
1806 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1807}
1808
1809 buf_T *
1810buflist_find_by_name(char_u *name, int curtab_only)
1811{
1812 int save_magic;
1813 char_u *save_cpo;
1814 buf_T *buf;
1815
1816 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1817 save_magic = p_magic;
1818 p_magic = TRUE;
1819 save_cpo = p_cpo;
1820 p_cpo = (char_u *)"";
1821
1822 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1823 TRUE, FALSE, curtab_only));
1824
1825 p_magic = save_magic;
1826 p_cpo = save_cpo;
1827 return buf;
1828}
1829
1830/*
1831 * Get buffer by number or pattern.
1832 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001833 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001834tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001835{
1836 char_u *name = tv->vval.v_string;
1837 buf_T *buf;
1838
1839 if (tv->v_type == VAR_NUMBER)
1840 return buflist_findnr((int)tv->vval.v_number);
1841 if (tv->v_type != VAR_STRING)
1842 return NULL;
1843 if (name == NULL || *name == NUL)
1844 return curbuf;
1845 if (name[0] == '$' && name[1] == NUL)
1846 return lastbuf;
1847
1848 buf = buflist_find_by_name(name, curtab_only);
1849
1850 /* If not found, try expanding the name, like done for bufexists(). */
1851 if (buf == NULL)
1852 buf = find_buffer(tv);
1853
1854 return buf;
1855}
1856
1857/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001858 * Get the buffer from "arg" and give an error and return NULL if it is not
1859 * valid.
1860 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001861 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001862get_buf_arg(typval_T *arg)
1863{
1864 buf_T *buf;
1865
1866 ++emsg_off;
1867 buf = tv_get_buf(arg, FALSE);
1868 --emsg_off;
1869 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001870 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001871 return buf;
1872}
1873
1874/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 * "bufname(expr)" function
1876 */
1877 static void
1878f_bufname(typval_T *argvars, typval_T *rettv)
1879{
1880 buf_T *buf;
1881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001884 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 rettv->v_type = VAR_STRING;
1886 if (buf != NULL && buf->b_fname != NULL)
1887 rettv->vval.v_string = vim_strsave(buf->b_fname);
1888 else
1889 rettv->vval.v_string = NULL;
1890 --emsg_off;
1891}
1892
1893/*
1894 * "bufnr(expr)" function
1895 */
1896 static void
1897f_bufnr(typval_T *argvars, typval_T *rettv)
1898{
1899 buf_T *buf;
1900 int error = FALSE;
1901 char_u *name;
1902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001903 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001905 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 --emsg_off;
1907
1908 /* If the buffer isn't found and the second argument is not zero create a
1909 * new buffer. */
1910 if (buf == NULL
1911 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001912 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001914 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 && !error)
1916 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1917
1918 if (buf != NULL)
1919 rettv->vval.v_number = buf->b_fnum;
1920 else
1921 rettv->vval.v_number = -1;
1922}
1923
1924 static void
1925buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1926{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 win_T *wp;
1928 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 buf_T *buf;
1930
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001931 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001933 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001934 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 {
1936 ++winnr;
1937 if (wp->w_buffer == buf)
1938 break;
1939 }
1940 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941 --emsg_off;
1942}
1943
1944/*
1945 * "bufwinid(nr)" function
1946 */
1947 static void
1948f_bufwinid(typval_T *argvars, typval_T *rettv)
1949{
1950 buf_win_common(argvars, rettv, FALSE);
1951}
1952
1953/*
1954 * "bufwinnr(nr)" function
1955 */
1956 static void
1957f_bufwinnr(typval_T *argvars, typval_T *rettv)
1958{
1959 buf_win_common(argvars, rettv, TRUE);
1960}
1961
1962/*
1963 * "byte2line(byte)" function
1964 */
1965 static void
1966f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1967{
1968#ifndef FEAT_BYTEOFF
1969 rettv->vval.v_number = -1;
1970#else
1971 long boff = 0;
1972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 if (boff < 0)
1975 rettv->vval.v_number = -1;
1976 else
1977 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1978 (linenr_T)0, &boff);
1979#endif
1980}
1981
1982 static void
1983byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1984{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001985 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 char_u *str;
1987 varnumber_T idx;
1988
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001989 str = tv_get_string_chk(&argvars[0]);
1990 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001991 rettv->vval.v_number = -1;
1992 if (str == NULL || idx < 0)
1993 return;
1994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 t = str;
1996 for ( ; idx > 0; idx--)
1997 {
1998 if (*t == NUL) /* EOL reached */
1999 return;
2000 if (enc_utf8 && comp)
2001 t += utf_ptr2len(t);
2002 else
2003 t += (*mb_ptr2len)(t);
2004 }
2005 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002006}
2007
2008/*
2009 * "byteidx()" function
2010 */
2011 static void
2012f_byteidx(typval_T *argvars, typval_T *rettv)
2013{
2014 byteidx(argvars, rettv, FALSE);
2015}
2016
2017/*
2018 * "byteidxcomp()" function
2019 */
2020 static void
2021f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2022{
2023 byteidx(argvars, rettv, TRUE);
2024}
2025
2026/*
2027 * "call(func, arglist [, dict])" function
2028 */
2029 static void
2030f_call(typval_T *argvars, typval_T *rettv)
2031{
2032 char_u *func;
2033 partial_T *partial = NULL;
2034 dict_T *selfdict = NULL;
2035
2036 if (argvars[1].v_type != VAR_LIST)
2037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002038 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002039 return;
2040 }
2041 if (argvars[1].vval.v_list == NULL)
2042 return;
2043
2044 if (argvars[0].v_type == VAR_FUNC)
2045 func = argvars[0].vval.v_string;
2046 else if (argvars[0].v_type == VAR_PARTIAL)
2047 {
2048 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002049 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 }
2051 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002052 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 if (*func == NUL)
2054 return; /* type error or empty name */
2055
2056 if (argvars[2].v_type != VAR_UNKNOWN)
2057 {
2058 if (argvars[2].v_type != VAR_DICT)
2059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 return;
2062 }
2063 selfdict = argvars[2].vval.v_dict;
2064 }
2065
2066 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2067}
2068
2069#ifdef FEAT_FLOAT
2070/*
2071 * "ceil({float})" function
2072 */
2073 static void
2074f_ceil(typval_T *argvars, typval_T *rettv)
2075{
2076 float_T f = 0.0;
2077
2078 rettv->v_type = VAR_FLOAT;
2079 if (get_float_arg(argvars, &f) == OK)
2080 rettv->vval.v_float = ceil(f);
2081 else
2082 rettv->vval.v_float = 0.0;
2083}
2084#endif
2085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086/*
2087 * "changenr()" function
2088 */
2089 static void
2090f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2091{
2092 rettv->vval.v_number = curbuf->b_u_seq_cur;
2093}
2094
2095/*
2096 * "char2nr(string)" function
2097 */
2098 static void
2099f_char2nr(typval_T *argvars, typval_T *rettv)
2100{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 if (has_mbyte)
2102 {
2103 int utf8 = 0;
2104
2105 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002106 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107
2108 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002109 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002111 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 }
2113 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002114 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115}
2116
2117/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002118 * "chdir(dir)" function
2119 */
2120 static void
2121f_chdir(typval_T *argvars, typval_T *rettv)
2122{
2123 char_u *cwd;
2124 cdscope_T scope = CDSCOPE_GLOBAL;
2125
2126 rettv->v_type = VAR_STRING;
2127 rettv->vval.v_string = NULL;
2128
2129 if (argvars[0].v_type != VAR_STRING)
2130 return;
2131
2132 // Return the current directory
2133 cwd = alloc(MAXPATHL);
2134 if (cwd != NULL)
2135 {
2136 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2137 {
2138#ifdef BACKSLASH_IN_FILENAME
2139 slash_adjust(cwd);
2140#endif
2141 rettv->vval.v_string = vim_strsave(cwd);
2142 }
2143 vim_free(cwd);
2144 }
2145
2146 if (curwin->w_localdir != NULL)
2147 scope = CDSCOPE_WINDOW;
2148 else if (curtab->tp_localdir != NULL)
2149 scope = CDSCOPE_TABPAGE;
2150
2151 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2152 // Directory change failed
2153 VIM_CLEAR(rettv->vval.v_string);
2154}
2155
2156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 * "cindent(lnum)" function
2158 */
2159 static void
2160f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2161{
2162#ifdef FEAT_CINDENT
2163 pos_T pos;
2164 linenr_T lnum;
2165
2166 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002167 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2169 {
2170 curwin->w_cursor.lnum = lnum;
2171 rettv->vval.v_number = get_c_indent();
2172 curwin->w_cursor = pos;
2173 }
2174 else
2175#endif
2176 rettv->vval.v_number = -1;
2177}
2178
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002179 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002180get_optional_window(typval_T *argvars, int idx)
2181{
2182 win_T *win = curwin;
2183
2184 if (argvars[idx].v_type != VAR_UNKNOWN)
2185 {
2186 win = find_win_by_nr_or_id(&argvars[idx]);
2187 if (win == NULL)
2188 {
2189 emsg(_(e_invalwindow));
2190 return NULL;
2191 }
2192 }
2193 return win;
2194}
2195
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002197 * "col(string)" function
2198 */
2199 static void
2200f_col(typval_T *argvars, typval_T *rettv)
2201{
2202 colnr_T col = 0;
2203 pos_T *fp;
2204 int fnum = curbuf->b_fnum;
2205
2206 fp = var2fpos(&argvars[0], FALSE, &fnum);
2207 if (fp != NULL && fnum == curbuf->b_fnum)
2208 {
2209 if (fp->col == MAXCOL)
2210 {
2211 /* '> can be MAXCOL, get the length of the line then */
2212 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2213 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2214 else
2215 col = MAXCOL;
2216 }
2217 else
2218 {
2219 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002220 /* col(".") when the cursor is on the NUL at the end of the line
2221 * because of "coladd" can be seen as an extra column. */
2222 if (virtual_active() && fp == &curwin->w_cursor)
2223 {
2224 char_u *p = ml_get_cursor();
2225
2226 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2227 curwin->w_virtcol - curwin->w_cursor.coladd))
2228 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 int l;
2230
2231 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2232 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 }
2234 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 }
2236 }
2237 rettv->vval.v_number = col;
2238}
2239
2240#if defined(FEAT_INS_EXPAND)
2241/*
2242 * "complete()" function
2243 */
2244 static void
2245f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2246{
2247 int startcol;
2248
2249 if ((State & INSERT) == 0)
2250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002252 return;
2253 }
2254
2255 /* Check for undo allowed here, because if something was already inserted
2256 * the line was already saved for undo and this check isn't done. */
2257 if (!undo_allowed())
2258 return;
2259
2260 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002263 return;
2264 }
2265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002266 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 if (startcol <= 0)
2268 return;
2269
2270 set_completion(startcol - 1, argvars[1].vval.v_list);
2271}
2272
2273/*
2274 * "complete_add()" function
2275 */
2276 static void
2277f_complete_add(typval_T *argvars, typval_T *rettv)
2278{
2279 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2280}
2281
2282/*
2283 * "complete_check()" function
2284 */
2285 static void
2286f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2287{
2288 int saved = RedrawingDisabled;
2289
2290 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002291 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002292 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002293 RedrawingDisabled = saved;
2294}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002295
2296/*
2297 * "complete_info()" function
2298 */
2299 static void
2300f_complete_info(typval_T *argvars, typval_T *rettv)
2301{
2302 list_T *what_list = NULL;
2303
2304 if (rettv_dict_alloc(rettv) != OK)
2305 return;
2306
2307 if (argvars[0].v_type != VAR_UNKNOWN)
2308 {
2309 if (argvars[0].v_type != VAR_LIST)
2310 {
2311 emsg(_(e_listreq));
2312 return;
2313 }
2314 what_list = argvars[0].vval.v_list;
2315 }
2316 get_complete_info(what_list, rettv->vval.v_dict);
2317}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002318#endif
2319
2320/*
2321 * "confirm(message, buttons[, default [, type]])" function
2322 */
2323 static void
2324f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2325{
2326#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2327 char_u *message;
2328 char_u *buttons = NULL;
2329 char_u buf[NUMBUFLEN];
2330 char_u buf2[NUMBUFLEN];
2331 int def = 1;
2332 int type = VIM_GENERIC;
2333 char_u *typestr;
2334 int error = FALSE;
2335
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 if (message == NULL)
2338 error = TRUE;
2339 if (argvars[1].v_type != VAR_UNKNOWN)
2340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002341 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342 if (buttons == NULL)
2343 error = TRUE;
2344 if (argvars[2].v_type != VAR_UNKNOWN)
2345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002346 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002347 if (argvars[3].v_type != VAR_UNKNOWN)
2348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002349 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 if (typestr == NULL)
2351 error = TRUE;
2352 else
2353 {
2354 switch (TOUPPER_ASC(*typestr))
2355 {
2356 case 'E': type = VIM_ERROR; break;
2357 case 'Q': type = VIM_QUESTION; break;
2358 case 'I': type = VIM_INFO; break;
2359 case 'W': type = VIM_WARNING; break;
2360 case 'G': type = VIM_GENERIC; break;
2361 }
2362 }
2363 }
2364 }
2365 }
2366
2367 if (buttons == NULL || *buttons == NUL)
2368 buttons = (char_u *)_("&Ok");
2369
2370 if (!error)
2371 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2372 def, NULL, FALSE);
2373#endif
2374}
2375
2376/*
2377 * "copy()" function
2378 */
2379 static void
2380f_copy(typval_T *argvars, typval_T *rettv)
2381{
2382 item_copy(&argvars[0], rettv, FALSE, 0);
2383}
2384
2385#ifdef FEAT_FLOAT
2386/*
2387 * "cos()" function
2388 */
2389 static void
2390f_cos(typval_T *argvars, typval_T *rettv)
2391{
2392 float_T f = 0.0;
2393
2394 rettv->v_type = VAR_FLOAT;
2395 if (get_float_arg(argvars, &f) == OK)
2396 rettv->vval.v_float = cos(f);
2397 else
2398 rettv->vval.v_float = 0.0;
2399}
2400
2401/*
2402 * "cosh()" function
2403 */
2404 static void
2405f_cosh(typval_T *argvars, typval_T *rettv)
2406{
2407 float_T f = 0.0;
2408
2409 rettv->v_type = VAR_FLOAT;
2410 if (get_float_arg(argvars, &f) == OK)
2411 rettv->vval.v_float = cosh(f);
2412 else
2413 rettv->vval.v_float = 0.0;
2414}
2415#endif
2416
2417/*
2418 * "count()" function
2419 */
2420 static void
2421f_count(typval_T *argvars, typval_T *rettv)
2422{
2423 long n = 0;
2424 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002425 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426
Bram Moolenaar9966b212017-07-28 16:46:57 +02002427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002428 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002429
2430 if (argvars[0].v_type == VAR_STRING)
2431 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002432 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002433 char_u *p = argvars[0].vval.v_string;
2434 char_u *next;
2435
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002436 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002437 {
2438 if (ic)
2439 {
2440 size_t len = STRLEN(expr);
2441
2442 while (*p != NUL)
2443 {
2444 if (MB_STRNICMP(p, expr, len) == 0)
2445 {
2446 ++n;
2447 p += len;
2448 }
2449 else
2450 MB_PTR_ADV(p);
2451 }
2452 }
2453 else
2454 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2455 != NULL)
2456 {
2457 ++n;
2458 p = next + STRLEN(expr);
2459 }
2460 }
2461
2462 }
2463 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 {
2465 listitem_T *li;
2466 list_T *l;
2467 long idx;
2468
2469 if ((l = argvars[0].vval.v_list) != NULL)
2470 {
2471 li = l->lv_first;
2472 if (argvars[2].v_type != VAR_UNKNOWN)
2473 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (argvars[3].v_type != VAR_UNKNOWN)
2475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002476 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477 if (!error)
2478 {
2479 li = list_find(l, idx);
2480 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002481 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002482 }
2483 }
2484 if (error)
2485 li = NULL;
2486 }
2487
2488 for ( ; li != NULL; li = li->li_next)
2489 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2490 ++n;
2491 }
2492 }
2493 else if (argvars[0].v_type == VAR_DICT)
2494 {
2495 int todo;
2496 dict_T *d;
2497 hashitem_T *hi;
2498
2499 if ((d = argvars[0].vval.v_dict) != NULL)
2500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002501 if (argvars[2].v_type != VAR_UNKNOWN)
2502 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002503 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002504 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505 }
2506
2507 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2508 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2509 {
2510 if (!HASHITEM_EMPTY(hi))
2511 {
2512 --todo;
2513 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2514 ++n;
2515 }
2516 }
2517 }
2518 }
2519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002520 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521 rettv->vval.v_number = n;
2522}
2523
2524/*
2525 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2526 *
2527 * Checks the existence of a cscope connection.
2528 */
2529 static void
2530f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2531{
2532#ifdef FEAT_CSCOPE
2533 int num = 0;
2534 char_u *dbpath = NULL;
2535 char_u *prepend = NULL;
2536 char_u buf[NUMBUFLEN];
2537
2538 if (argvars[0].v_type != VAR_UNKNOWN
2539 && argvars[1].v_type != VAR_UNKNOWN)
2540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 num = (int)tv_get_number(&argvars[0]);
2542 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 }
2546
2547 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2548#endif
2549}
2550
2551/*
2552 * "cursor(lnum, col)" function, or
2553 * "cursor(list)"
2554 *
2555 * Moves the cursor to the specified line and column.
2556 * Returns 0 when the position could be set, -1 otherwise.
2557 */
2558 static void
2559f_cursor(typval_T *argvars, typval_T *rettv)
2560{
2561 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 int set_curswant = TRUE;
2564
2565 rettv->vval.v_number = -1;
2566 if (argvars[1].v_type == VAR_UNKNOWN)
2567 {
2568 pos_T pos;
2569 colnr_T curswant = -1;
2570
2571 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002573 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 return;
2575 }
2576 line = pos.lnum;
2577 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002579 if (curswant >= 0)
2580 {
2581 curwin->w_curswant = curswant - 1;
2582 set_curswant = FALSE;
2583 }
2584 }
2585 else
2586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002587 line = tv_get_lnum(argvars);
2588 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002590 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002592 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593 return; /* type error; errmsg already given */
2594 if (line > 0)
2595 curwin->w_cursor.lnum = line;
2596 if (col > 0)
2597 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002599
2600 /* Make sure the cursor is in a valid position. */
2601 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 /* Correct cursor for multi-byte character. */
2603 if (has_mbyte)
2604 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002605
2606 curwin->w_set_curswant = set_curswant;
2607 rettv->vval.v_number = 0;
2608}
2609
Bram Moolenaar4f974752019-02-17 17:44:42 +01002610#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002611/*
2612 * "debugbreak()" function
2613 */
2614 static void
2615f_debugbreak(typval_T *argvars, typval_T *rettv)
2616{
2617 int pid;
2618
2619 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002620 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002621 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002622 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002623 else
2624 {
2625 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2626
2627 if (hProcess != NULL)
2628 {
2629 DebugBreakProcess(hProcess);
2630 CloseHandle(hProcess);
2631 rettv->vval.v_number = OK;
2632 }
2633 }
2634}
2635#endif
2636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637/*
2638 * "deepcopy()" function
2639 */
2640 static void
2641f_deepcopy(typval_T *argvars, typval_T *rettv)
2642{
2643 int noref = 0;
2644 int copyID;
2645
2646 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002647 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002649 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 else
2651 {
2652 copyID = get_copyID();
2653 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2654 }
2655}
2656
2657/*
2658 * "delete()" function
2659 */
2660 static void
2661f_delete(typval_T *argvars, typval_T *rettv)
2662{
2663 char_u nbuf[NUMBUFLEN];
2664 char_u *name;
2665 char_u *flags;
2666
2667 rettv->vval.v_number = -1;
2668 if (check_restricted() || check_secure())
2669 return;
2670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002671 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 if (name == NULL || *name == NUL)
2673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 return;
2676 }
2677
2678 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002679 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 else
2681 flags = (char_u *)"";
2682
2683 if (*flags == NUL)
2684 /* delete a file */
2685 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2686 else if (STRCMP(flags, "d") == 0)
2687 /* delete an empty directory */
2688 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2689 else if (STRCMP(flags, "rf") == 0)
2690 /* delete a directory recursively */
2691 rettv->vval.v_number = delete_recursive(name);
2692 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002693 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694}
2695
2696/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002697 * "deletebufline()" function
2698 */
2699 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002700f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002701{
2702 buf_T *buf;
2703 linenr_T first, last;
2704 linenr_T lnum;
2705 long count;
2706 int is_curbuf;
2707 buf_T *curbuf_save = NULL;
2708 win_T *curwin_save = NULL;
2709 tabpage_T *tp;
2710 win_T *wp;
2711
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002712 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002713 if (buf == NULL)
2714 {
2715 rettv->vval.v_number = 1; /* FAIL */
2716 return;
2717 }
2718 is_curbuf = buf == curbuf;
2719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002720 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002722 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002723 else
2724 last = first;
2725
2726 if (buf->b_ml.ml_mfp == NULL || first < 1
2727 || first > buf->b_ml.ml_line_count || last < first)
2728 {
2729 rettv->vval.v_number = 1; /* FAIL */
2730 return;
2731 }
2732
2733 if (!is_curbuf)
2734 {
2735 curbuf_save = curbuf;
2736 curwin_save = curwin;
2737 curbuf = buf;
2738 find_win_for_curbuf();
2739 }
2740 if (last > curbuf->b_ml.ml_line_count)
2741 last = curbuf->b_ml.ml_line_count;
2742 count = last - first + 1;
2743
2744 // When coming here from Insert mode, sync undo, so that this can be
2745 // undone separately from what was previously inserted.
2746 if (u_sync_once == 2)
2747 {
2748 u_sync_once = 1; // notify that u_sync() was called
2749 u_sync(TRUE);
2750 }
2751
2752 if (u_save(first - 1, last + 1) == FAIL)
2753 {
2754 rettv->vval.v_number = 1; /* FAIL */
2755 return;
2756 }
2757
2758 for (lnum = first; lnum <= last; ++lnum)
2759 ml_delete(first, TRUE);
2760
2761 FOR_ALL_TAB_WINDOWS(tp, wp)
2762 if (wp->w_buffer == buf)
2763 {
2764 if (wp->w_cursor.lnum > last)
2765 wp->w_cursor.lnum -= count;
2766 else if (wp->w_cursor.lnum> first)
2767 wp->w_cursor.lnum = first;
2768 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2769 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2770 }
2771 check_cursor_col();
2772 deleted_lines_mark(first, count);
2773
2774 if (!is_curbuf)
2775 {
2776 curbuf = curbuf_save;
2777 curwin = curwin_save;
2778 }
2779}
2780
2781/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 * "did_filetype()" function
2783 */
2784 static void
2785f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2786{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788}
2789
2790/*
2791 * "diff_filler()" function
2792 */
2793 static void
2794f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2795{
2796#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002797 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798#endif
2799}
2800
2801/*
2802 * "diff_hlID()" function
2803 */
2804 static void
2805f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2806{
2807#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002808 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002810 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 static int fnum = 0;
2812 static int change_start = 0;
2813 static int change_end = 0;
2814 static hlf_T hlID = (hlf_T)0;
2815 int filler_lines;
2816 int col;
2817
2818 if (lnum < 0) /* ignore type error in {lnum} arg */
2819 lnum = 0;
2820 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002821 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 || fnum != curbuf->b_fnum)
2823 {
2824 /* New line, buffer, change: need to get the values. */
2825 filler_lines = diff_check(curwin, lnum);
2826 if (filler_lines < 0)
2827 {
2828 if (filler_lines == -1)
2829 {
2830 change_start = MAXCOL;
2831 change_end = -1;
2832 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2833 hlID = HLF_ADD; /* added line */
2834 else
2835 hlID = HLF_CHD; /* changed line */
2836 }
2837 else
2838 hlID = HLF_ADD; /* added line */
2839 }
2840 else
2841 hlID = (hlf_T)0;
2842 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002843 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002844 fnum = curbuf->b_fnum;
2845 }
2846
2847 if (hlID == HLF_CHD || hlID == HLF_TXD)
2848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002849 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 if (col >= change_start && col <= change_end)
2851 hlID = HLF_TXD; /* changed text */
2852 else
2853 hlID = HLF_CHD; /* changed line */
2854 }
2855 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2856#endif
2857}
2858
2859/*
2860 * "empty({expr})" function
2861 */
2862 static void
2863f_empty(typval_T *argvars, typval_T *rettv)
2864{
2865 int n = FALSE;
2866
2867 switch (argvars[0].v_type)
2868 {
2869 case VAR_STRING:
2870 case VAR_FUNC:
2871 n = argvars[0].vval.v_string == NULL
2872 || *argvars[0].vval.v_string == NUL;
2873 break;
2874 case VAR_PARTIAL:
2875 n = FALSE;
2876 break;
2877 case VAR_NUMBER:
2878 n = argvars[0].vval.v_number == 0;
2879 break;
2880 case VAR_FLOAT:
2881#ifdef FEAT_FLOAT
2882 n = argvars[0].vval.v_float == 0.0;
2883 break;
2884#endif
2885 case VAR_LIST:
2886 n = argvars[0].vval.v_list == NULL
2887 || argvars[0].vval.v_list->lv_first == NULL;
2888 break;
2889 case VAR_DICT:
2890 n = argvars[0].vval.v_dict == NULL
2891 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2892 break;
2893 case VAR_SPECIAL:
2894 n = argvars[0].vval.v_number != VVAL_TRUE;
2895 break;
2896
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002897 case VAR_BLOB:
2898 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002899 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2900 break;
2901
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 case VAR_JOB:
2903#ifdef FEAT_JOB_CHANNEL
2904 n = argvars[0].vval.v_job == NULL
2905 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2906 break;
2907#endif
2908 case VAR_CHANNEL:
2909#ifdef FEAT_JOB_CHANNEL
2910 n = argvars[0].vval.v_channel == NULL
2911 || !channel_is_open(argvars[0].vval.v_channel);
2912 break;
2913#endif
2914 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002915 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 n = TRUE;
2917 break;
2918 }
2919
2920 rettv->vval.v_number = n;
2921}
2922
2923/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002924 * "environ()" function
2925 */
2926 static void
2927f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2928{
2929#if !defined(AMIGA)
2930 int i = 0;
2931 char_u *entry, *value;
2932# ifdef MSWIN
2933 extern wchar_t **_wenviron;
2934# else
2935 extern char **environ;
2936# endif
2937
2938 if (rettv_dict_alloc(rettv) != OK)
2939 return;
2940
2941# ifdef MSWIN
2942 if (*_wenviron == NULL)
2943 return;
2944# else
2945 if (*environ == NULL)
2946 return;
2947# endif
2948
2949 for (i = 0; ; ++i)
2950 {
2951# ifdef MSWIN
2952 short_u *p;
2953
2954 if ((p = (short_u *)_wenviron[i]) == NULL)
2955 return;
2956 entry = utf16_to_enc(p, NULL);
2957# else
2958 if ((entry = (char_u *)environ[i]) == NULL)
2959 return;
2960 entry = vim_strsave(entry);
2961# endif
2962 if (entry == NULL) // out of memory
2963 return;
2964 if ((value = vim_strchr(entry, '=')) == NULL)
2965 {
2966 vim_free(entry);
2967 continue;
2968 }
2969 *value++ = NUL;
2970 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2971 vim_free(entry);
2972 }
2973#endif
2974}
2975
2976/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 * "escape({string}, {chars})" function
2978 */
2979 static void
2980f_escape(typval_T *argvars, typval_T *rettv)
2981{
2982 char_u buf[NUMBUFLEN];
2983
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002984 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2985 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 rettv->v_type = VAR_STRING;
2987}
2988
2989/*
2990 * "eval()" function
2991 */
2992 static void
2993f_eval(typval_T *argvars, typval_T *rettv)
2994{
2995 char_u *s, *p;
2996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 if (s != NULL)
2999 s = skipwhite(s);
3000
3001 p = s;
3002 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3003 {
3004 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003005 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 need_clr_eos = FALSE;
3007 rettv->v_type = VAR_NUMBER;
3008 rettv->vval.v_number = 0;
3009 }
3010 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012}
3013
3014/*
3015 * "eventhandler()" function
3016 */
3017 static void
3018f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3019{
3020 rettv->vval.v_number = vgetc_busy;
3021}
3022
3023/*
3024 * "executable()" function
3025 */
3026 static void
3027f_executable(typval_T *argvars, typval_T *rettv)
3028{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003029 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030
3031 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003032 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033}
3034
3035static garray_T redir_execute_ga;
3036
3037/*
3038 * Append "value[value_len]" to the execute() output.
3039 */
3040 void
3041execute_redir_str(char_u *value, int value_len)
3042{
3043 int len;
3044
3045 if (value_len == -1)
3046 len = (int)STRLEN(value); /* Append the entire string */
3047 else
3048 len = value_len; /* Append only "value_len" characters */
3049 if (ga_grow(&redir_execute_ga, len) == OK)
3050 {
3051 mch_memmove((char *)redir_execute_ga.ga_data
3052 + redir_execute_ga.ga_len, value, len);
3053 redir_execute_ga.ga_len += len;
3054 }
3055}
3056
3057/*
3058 * Get next line from a list.
3059 * Called by do_cmdline() to get the next line.
3060 * Returns allocated string, or NULL for end of function.
3061 */
3062
3063 static char_u *
3064get_list_line(
3065 int c UNUSED,
3066 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003067 int indent UNUSED,
3068 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069{
3070 listitem_T **p = (listitem_T **)cookie;
3071 listitem_T *item = *p;
3072 char_u buf[NUMBUFLEN];
3073 char_u *s;
3074
3075 if (item == NULL)
3076 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003077 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 *p = item->li_next;
3079 return s == NULL ? NULL : vim_strsave(s);
3080}
3081
3082/*
3083 * "execute()" function
3084 */
3085 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003086execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087{
3088 char_u *cmd = NULL;
3089 list_T *list = NULL;
3090 int save_msg_silent = msg_silent;
3091 int save_emsg_silent = emsg_silent;
3092 int save_emsg_noredir = emsg_noredir;
3093 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003094 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003096 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003097 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098
3099 rettv->vval.v_string = NULL;
3100 rettv->v_type = VAR_STRING;
3101
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003102 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003104 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003105 if (list == NULL || list->lv_first == NULL)
3106 /* empty list, no commands, empty output */
3107 return;
3108 ++list->lv_refcount;
3109 }
3110 else
3111 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003112 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 if (cmd == NULL)
3114 return;
3115 }
3116
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003117 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118 {
3119 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003120 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121
3122 if (s == NULL)
3123 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003124 if (*s == NUL)
3125 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 if (STRNCMP(s, "silent", 6) == 0)
3127 ++msg_silent;
3128 if (STRCMP(s, "silent!") == 0)
3129 {
3130 emsg_silent = TRUE;
3131 emsg_noredir = TRUE;
3132 }
3133 }
3134 else
3135 ++msg_silent;
3136
3137 if (redir_execute)
3138 save_ga = redir_execute_ga;
3139 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3140 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003141 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003142 if (!echo_output)
3143 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144
3145 if (cmd != NULL)
3146 do_cmdline_cmd(cmd);
3147 else
3148 {
3149 listitem_T *item = list->lv_first;
3150
3151 do_cmdline(NULL, get_list_line, (void *)&item,
3152 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3153 --list->lv_refcount;
3154 }
3155
Bram Moolenaard297f352017-01-29 20:31:21 +01003156 /* Need to append a NUL to the result. */
3157 if (ga_grow(&redir_execute_ga, 1) == OK)
3158 {
3159 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3160 rettv->vval.v_string = redir_execute_ga.ga_data;
3161 }
3162 else
3163 {
3164 ga_clear(&redir_execute_ga);
3165 rettv->vval.v_string = NULL;
3166 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 msg_silent = save_msg_silent;
3168 emsg_silent = save_emsg_silent;
3169 emsg_noredir = save_emsg_noredir;
3170
3171 redir_execute = save_redir_execute;
3172 if (redir_execute)
3173 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003174 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003176 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003177 if (echo_output)
3178 // When not working silently: put it in column zero. A following
3179 // "echon" will overwrite the message, unavoidably.
3180 msg_col = 0;
3181 else
3182 // When working silently: Put it back where it was, since nothing
3183 // should have been written.
3184 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185}
3186
3187/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003188 * "execute()" function
3189 */
3190 static void
3191f_execute(typval_T *argvars, typval_T *rettv)
3192{
3193 execute_common(argvars, rettv, 0);
3194}
3195
3196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 * "exepath()" function
3198 */
3199 static void
3200f_exepath(typval_T *argvars, typval_T *rettv)
3201{
3202 char_u *p = NULL;
3203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003204 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205 rettv->v_type = VAR_STRING;
3206 rettv->vval.v_string = p;
3207}
3208
3209/*
3210 * "exists()" function
3211 */
3212 static void
3213f_exists(typval_T *argvars, typval_T *rettv)
3214{
3215 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003218 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003219 if (*p == '$') /* environment variable */
3220 {
3221 /* first try "normal" environment variables (fast) */
3222 if (mch_getenv(p + 1) != NULL)
3223 n = TRUE;
3224 else
3225 {
3226 /* try expanding things like $VIM and ${HOME} */
3227 p = expand_env_save(p);
3228 if (p != NULL && *p != '$')
3229 n = TRUE;
3230 vim_free(p);
3231 }
3232 }
3233 else if (*p == '&' || *p == '+') /* option */
3234 {
3235 n = (get_option_tv(&p, NULL, TRUE) == OK);
3236 if (*skipwhite(p) != NUL)
3237 n = FALSE; /* trailing garbage */
3238 }
3239 else if (*p == '*') /* internal or user defined function */
3240 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003241 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242 }
3243 else if (*p == ':')
3244 {
3245 n = cmd_exists(p + 1);
3246 }
3247 else if (*p == '#')
3248 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 if (p[1] == '#')
3250 n = autocmd_supported(p + 2);
3251 else
3252 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253 }
3254 else /* internal variable */
3255 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003256 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 }
3258
3259 rettv->vval.v_number = n;
3260}
3261
3262#ifdef FEAT_FLOAT
3263/*
3264 * "exp()" function
3265 */
3266 static void
3267f_exp(typval_T *argvars, typval_T *rettv)
3268{
3269 float_T f = 0.0;
3270
3271 rettv->v_type = VAR_FLOAT;
3272 if (get_float_arg(argvars, &f) == OK)
3273 rettv->vval.v_float = exp(f);
3274 else
3275 rettv->vval.v_float = 0.0;
3276}
3277#endif
3278
3279/*
3280 * "expand()" function
3281 */
3282 static void
3283f_expand(typval_T *argvars, typval_T *rettv)
3284{
3285 char_u *s;
3286 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3289 expand_T xpc;
3290 int error = FALSE;
3291 char_u *result;
3292
3293 rettv->v_type = VAR_STRING;
3294 if (argvars[1].v_type != VAR_UNKNOWN
3295 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003298 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003300 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 if (*s == '%' || *s == '#' || *s == '<')
3302 {
3303 ++emsg_off;
3304 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3305 --emsg_off;
3306 if (rettv->v_type == VAR_LIST)
3307 {
3308 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3309 list_append_string(rettv->vval.v_list, result, -1);
3310 else
3311 vim_free(result);
3312 }
3313 else
3314 rettv->vval.v_string = result;
3315 }
3316 else
3317 {
3318 /* When the optional second argument is non-zero, don't remove matches
3319 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3320 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 options |= WILD_KEEP_ALL;
3323 if (!error)
3324 {
3325 ExpandInit(&xpc);
3326 xpc.xp_context = EXPAND_FILES;
3327 if (p_wic)
3328 options += WILD_ICASE;
3329 if (rettv->v_type == VAR_STRING)
3330 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3331 options, WILD_ALL);
3332 else if (rettv_list_alloc(rettv) != FAIL)
3333 {
3334 int i;
3335
3336 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3337 for (i = 0; i < xpc.xp_numfiles; i++)
3338 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3339 ExpandCleanup(&xpc);
3340 }
3341 }
3342 else
3343 rettv->vval.v_string = NULL;
3344 }
3345}
3346
3347/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003348 * "expandcmd()" function
3349 * Expand all the special characters in a command string.
3350 */
3351 static void
3352f_expandcmd(typval_T *argvars, typval_T *rettv)
3353{
3354 exarg_T eap;
3355 char_u *cmdstr;
3356 char *errormsg = NULL;
3357
3358 rettv->v_type = VAR_STRING;
3359 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3360
3361 memset(&eap, 0, sizeof(eap));
3362 eap.cmd = cmdstr;
3363 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003364 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003365 eap.usefilter = FALSE;
3366 eap.nextcmd = NULL;
3367 eap.cmdidx = CMD_USER;
3368
3369 expand_filename(&eap, &cmdstr, &errormsg);
3370 if (errormsg != NULL && *errormsg != NUL)
3371 emsg(errormsg);
3372
3373 rettv->vval.v_string = cmdstr;
3374}
3375
3376/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377 * "extend(list, list [, idx])" function
3378 * "extend(dict, dict [, action])" function
3379 */
3380 static void
3381f_extend(typval_T *argvars, typval_T *rettv)
3382{
3383 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3384
3385 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3386 {
3387 list_T *l1, *l2;
3388 listitem_T *item;
3389 long before;
3390 int error = FALSE;
3391
3392 l1 = argvars[0].vval.v_list;
3393 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003394 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395 && l2 != NULL)
3396 {
3397 if (argvars[2].v_type != VAR_UNKNOWN)
3398 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003399 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 if (error)
3401 return; /* type error; errmsg already given */
3402
3403 if (before == l1->lv_len)
3404 item = NULL;
3405 else
3406 {
3407 item = list_find(l1, before);
3408 if (item == NULL)
3409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003410 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003411 return;
3412 }
3413 }
3414 }
3415 else
3416 item = NULL;
3417 list_extend(l1, l2, item);
3418
3419 copy_tv(&argvars[0], rettv);
3420 }
3421 }
3422 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3423 {
3424 dict_T *d1, *d2;
3425 char_u *action;
3426 int i;
3427
3428 d1 = argvars[0].vval.v_dict;
3429 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003430 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 && d2 != NULL)
3432 {
3433 /* Check the third argument. */
3434 if (argvars[2].v_type != VAR_UNKNOWN)
3435 {
3436 static char *(av[]) = {"keep", "force", "error"};
3437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003438 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 if (action == NULL)
3440 return; /* type error; errmsg already given */
3441 for (i = 0; i < 3; ++i)
3442 if (STRCMP(action, av[i]) == 0)
3443 break;
3444 if (i == 3)
3445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003446 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447 return;
3448 }
3449 }
3450 else
3451 action = (char_u *)"force";
3452
3453 dict_extend(d1, d2, action);
3454
3455 copy_tv(&argvars[0], rettv);
3456 }
3457 }
3458 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003459 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460}
3461
3462/*
3463 * "feedkeys()" function
3464 */
3465 static void
3466f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3467{
3468 int remap = TRUE;
3469 int insert = FALSE;
3470 char_u *keys, *flags;
3471 char_u nbuf[NUMBUFLEN];
3472 int typed = FALSE;
3473 int execute = FALSE;
3474 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003475 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003476 char_u *keys_esc;
3477
3478 /* This is not allowed in the sandbox. If the commands would still be
3479 * executed in the sandbox it would be OK, but it probably happens later,
3480 * when "sandbox" is no longer set. */
3481 if (check_secure())
3482 return;
3483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485
3486 if (argvars[1].v_type != VAR_UNKNOWN)
3487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003488 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003489 for ( ; *flags != NUL; ++flags)
3490 {
3491 switch (*flags)
3492 {
3493 case 'n': remap = FALSE; break;
3494 case 'm': remap = TRUE; break;
3495 case 't': typed = TRUE; break;
3496 case 'i': insert = TRUE; break;
3497 case 'x': execute = TRUE; break;
3498 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003499 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500 }
3501 }
3502 }
3503
3504 if (*keys != NUL || execute)
3505 {
3506 /* Need to escape K_SPECIAL and CSI before putting the string in the
3507 * typeahead buffer. */
3508 keys_esc = vim_strsave_escape_csi(keys);
3509 if (keys_esc != NULL)
3510 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003511 if (lowlevel)
3512 {
3513#ifdef USE_INPUT_BUF
3514 add_to_input_buf(keys, (int)STRLEN(keys));
3515#else
3516 emsg(_("E980: lowlevel input not supported"));
3517#endif
3518 }
3519 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003520 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003521 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003523 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003524#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003525 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003526#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003527 )
3528 typebuf_was_filled = TRUE;
3529 }
3530 vim_free(keys_esc);
3531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (execute)
3533 {
3534 int save_msg_scroll = msg_scroll;
3535
3536 /* Avoid a 1 second delay when the keys start Insert mode. */
3537 msg_scroll = FALSE;
3538
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003539 if (!dangerous)
3540 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003541 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003542 if (!dangerous)
3543 --ex_normal_busy;
3544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 msg_scroll |= save_msg_scroll;
3546 }
3547 }
3548 }
3549}
3550
3551/*
3552 * "filereadable()" function
3553 */
3554 static void
3555f_filereadable(typval_T *argvars, typval_T *rettv)
3556{
3557 int fd;
3558 char_u *p;
3559 int n;
3560
3561#ifndef O_NONBLOCK
3562# define O_NONBLOCK 0
3563#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003564 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3566 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3567 {
3568 n = TRUE;
3569 close(fd);
3570 }
3571 else
3572 n = FALSE;
3573
3574 rettv->vval.v_number = n;
3575}
3576
3577/*
3578 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3579 * rights to write into.
3580 */
3581 static void
3582f_filewritable(typval_T *argvars, typval_T *rettv)
3583{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003584 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585}
3586
3587 static void
3588findfilendir(
3589 typval_T *argvars UNUSED,
3590 typval_T *rettv,
3591 int find_what UNUSED)
3592{
3593#ifdef FEAT_SEARCHPATH
3594 char_u *fname;
3595 char_u *fresult = NULL;
3596 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3597 char_u *p;
3598 char_u pathbuf[NUMBUFLEN];
3599 int count = 1;
3600 int first = TRUE;
3601 int error = FALSE;
3602#endif
3603
3604 rettv->vval.v_string = NULL;
3605 rettv->v_type = VAR_STRING;
3606
3607#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003608 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609
3610 if (argvars[1].v_type != VAR_UNKNOWN)
3611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003612 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 if (p == NULL)
3614 error = TRUE;
3615 else
3616 {
3617 if (*p != NUL)
3618 path = p;
3619
3620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003621 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 }
3623 }
3624
3625 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3626 error = TRUE;
3627
3628 if (*fname != NUL && !error)
3629 {
3630 do
3631 {
3632 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3633 vim_free(fresult);
3634 fresult = find_file_in_path_option(first ? fname : NULL,
3635 first ? (int)STRLEN(fname) : 0,
3636 0, first, path,
3637 find_what,
3638 curbuf->b_ffname,
3639 find_what == FINDFILE_DIR
3640 ? (char_u *)"" : curbuf->b_p_sua);
3641 first = FALSE;
3642
3643 if (fresult != NULL && rettv->v_type == VAR_LIST)
3644 list_append_string(rettv->vval.v_list, fresult, -1);
3645
3646 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3647 }
3648
3649 if (rettv->v_type == VAR_STRING)
3650 rettv->vval.v_string = fresult;
3651#endif
3652}
3653
3654/*
3655 * "filter()" function
3656 */
3657 static void
3658f_filter(typval_T *argvars, typval_T *rettv)
3659{
3660 filter_map(argvars, rettv, FALSE);
3661}
3662
3663/*
3664 * "finddir({fname}[, {path}[, {count}]])" function
3665 */
3666 static void
3667f_finddir(typval_T *argvars, typval_T *rettv)
3668{
3669 findfilendir(argvars, rettv, FINDFILE_DIR);
3670}
3671
3672/*
3673 * "findfile({fname}[, {path}[, {count}]])" function
3674 */
3675 static void
3676f_findfile(typval_T *argvars, typval_T *rettv)
3677{
3678 findfilendir(argvars, rettv, FINDFILE_FILE);
3679}
3680
3681#ifdef FEAT_FLOAT
3682/*
3683 * "float2nr({float})" function
3684 */
3685 static void
3686f_float2nr(typval_T *argvars, typval_T *rettv)
3687{
3688 float_T f = 0.0;
3689
3690 if (get_float_arg(argvars, &f) == OK)
3691 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003692 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003693 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003694 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003695 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003696 else
3697 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 }
3699}
3700
3701/*
3702 * "floor({float})" function
3703 */
3704 static void
3705f_floor(typval_T *argvars, typval_T *rettv)
3706{
3707 float_T f = 0.0;
3708
3709 rettv->v_type = VAR_FLOAT;
3710 if (get_float_arg(argvars, &f) == OK)
3711 rettv->vval.v_float = floor(f);
3712 else
3713 rettv->vval.v_float = 0.0;
3714}
3715
3716/*
3717 * "fmod()" function
3718 */
3719 static void
3720f_fmod(typval_T *argvars, typval_T *rettv)
3721{
3722 float_T fx = 0.0, fy = 0.0;
3723
3724 rettv->v_type = VAR_FLOAT;
3725 if (get_float_arg(argvars, &fx) == OK
3726 && get_float_arg(&argvars[1], &fy) == OK)
3727 rettv->vval.v_float = fmod(fx, fy);
3728 else
3729 rettv->vval.v_float = 0.0;
3730}
3731#endif
3732
3733/*
3734 * "fnameescape({string})" function
3735 */
3736 static void
3737f_fnameescape(typval_T *argvars, typval_T *rettv)
3738{
3739 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 rettv->v_type = VAR_STRING;
3742}
3743
3744/*
3745 * "fnamemodify({fname}, {mods})" function
3746 */
3747 static void
3748f_fnamemodify(typval_T *argvars, typval_T *rettv)
3749{
3750 char_u *fname;
3751 char_u *mods;
3752 int usedlen = 0;
3753 int len;
3754 char_u *fbuf = NULL;
3755 char_u buf[NUMBUFLEN];
3756
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003757 fname = tv_get_string_chk(&argvars[0]);
3758 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003759 if (fname == NULL || mods == NULL)
3760 fname = NULL;
3761 else
3762 {
3763 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003764 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 }
3766
3767 rettv->v_type = VAR_STRING;
3768 if (fname == NULL)
3769 rettv->vval.v_string = NULL;
3770 else
3771 rettv->vval.v_string = vim_strnsave(fname, len);
3772 vim_free(fbuf);
3773}
3774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775/*
3776 * "foldclosed()" function
3777 */
3778 static void
3779foldclosed_both(
3780 typval_T *argvars UNUSED,
3781 typval_T *rettv,
3782 int end UNUSED)
3783{
3784#ifdef FEAT_FOLDING
3785 linenr_T lnum;
3786 linenr_T first, last;
3787
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003788 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003789 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3790 {
3791 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3792 {
3793 if (end)
3794 rettv->vval.v_number = (varnumber_T)last;
3795 else
3796 rettv->vval.v_number = (varnumber_T)first;
3797 return;
3798 }
3799 }
3800#endif
3801 rettv->vval.v_number = -1;
3802}
3803
3804/*
3805 * "foldclosed()" function
3806 */
3807 static void
3808f_foldclosed(typval_T *argvars, typval_T *rettv)
3809{
3810 foldclosed_both(argvars, rettv, FALSE);
3811}
3812
3813/*
3814 * "foldclosedend()" function
3815 */
3816 static void
3817f_foldclosedend(typval_T *argvars, typval_T *rettv)
3818{
3819 foldclosed_both(argvars, rettv, TRUE);
3820}
3821
3822/*
3823 * "foldlevel()" function
3824 */
3825 static void
3826f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3827{
3828#ifdef FEAT_FOLDING
3829 linenr_T lnum;
3830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003831 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3833 rettv->vval.v_number = foldLevel(lnum);
3834#endif
3835}
3836
3837/*
3838 * "foldtext()" function
3839 */
3840 static void
3841f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3842{
3843#ifdef FEAT_FOLDING
3844 linenr_T foldstart;
3845 linenr_T foldend;
3846 char_u *dashes;
3847 linenr_T lnum;
3848 char_u *s;
3849 char_u *r;
3850 int len;
3851 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003852 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853#endif
3854
3855 rettv->v_type = VAR_STRING;
3856 rettv->vval.v_string = NULL;
3857#ifdef FEAT_FOLDING
3858 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3859 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3860 dashes = get_vim_var_str(VV_FOLDDASHES);
3861 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3862 && dashes != NULL)
3863 {
3864 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003865 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 if (!linewhite(lnum))
3867 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003868
3869 /* Find interesting text in this line. */
3870 s = skipwhite(ml_get(lnum));
3871 /* skip C comment-start */
3872 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3873 {
3874 s = skipwhite(s + 2);
3875 if (*skipwhite(s) == NUL
3876 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3877 {
3878 s = skipwhite(ml_get(lnum + 1));
3879 if (*s == '*')
3880 s = skipwhite(s + 1);
3881 }
3882 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003883 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003884 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003885 r = alloc(STRLEN(txt)
3886 + STRLEN(dashes) // for %s
3887 + 20 // for %3ld
3888 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 if (r != NULL)
3890 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003891 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003892 len = (int)STRLEN(r);
3893 STRCAT(r, s);
3894 /* remove 'foldmarker' and 'commentstring' */
3895 foldtext_cleanup(r + len);
3896 rettv->vval.v_string = r;
3897 }
3898 }
3899#endif
3900}
3901
3902/*
3903 * "foldtextresult(lnum)" function
3904 */
3905 static void
3906f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3907{
3908#ifdef FEAT_FOLDING
3909 linenr_T lnum;
3910 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003911 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 foldinfo_T foldinfo;
3913 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003914 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003915#endif
3916
3917 rettv->v_type = VAR_STRING;
3918 rettv->vval.v_string = NULL;
3919#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003920 if (entered)
3921 return; /* reject recursive use */
3922 entered = TRUE;
3923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003924 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 /* treat illegal types and illegal string values for {lnum} the same */
3926 if (lnum < 0)
3927 lnum = 0;
3928 fold_count = foldedCount(curwin, lnum, &foldinfo);
3929 if (fold_count > 0)
3930 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003931 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3932 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 if (text == buf)
3934 text = vim_strsave(text);
3935 rettv->vval.v_string = text;
3936 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003937
3938 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939#endif
3940}
3941
3942/*
3943 * "foreground()" function
3944 */
3945 static void
3946f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3947{
3948#ifdef FEAT_GUI
3949 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003950 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003952 return;
3953 }
3954#endif
3955#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957#endif
3958}
3959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003960 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003961common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962{
3963 char_u *s;
3964 char_u *name;
3965 int use_string = FALSE;
3966 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003967 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968
3969 if (argvars[0].v_type == VAR_FUNC)
3970 {
3971 /* function(MyFunc, [arg], dict) */
3972 s = argvars[0].vval.v_string;
3973 }
3974 else if (argvars[0].v_type == VAR_PARTIAL
3975 && argvars[0].vval.v_partial != NULL)
3976 {
3977 /* function(dict.MyFunc, [arg]) */
3978 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003979 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003980 }
3981 else
3982 {
3983 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003984 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003985 use_string = TRUE;
3986 }
3987
Bram Moolenaar843b8842016-08-21 14:36:15 +02003988 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003989 {
3990 name = s;
3991 trans_name = trans_function_name(&name, FALSE,
3992 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3993 if (*name != NUL)
3994 s = NULL;
3995 }
3996
Bram Moolenaar843b8842016-08-21 14:36:15 +02003997 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3998 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003999 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001 else if (trans_name != NULL && (is_funcref
4002 ? find_func(trans_name) == NULL
4003 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004004 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004005 else
4006 {
4007 int dict_idx = 0;
4008 int arg_idx = 0;
4009 list_T *list = NULL;
4010
4011 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4012 {
4013 char sid_buf[25];
4014 int off = *s == 's' ? 2 : 5;
4015
4016 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4017 * also be called from another script. Using trans_function_name()
4018 * would also work, but some plugins depend on the name being
4019 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004020 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004021 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 if (name != NULL)
4023 {
4024 STRCPY(name, sid_buf);
4025 STRCAT(name, s + off);
4026 }
4027 }
4028 else
4029 name = vim_strsave(s);
4030
4031 if (argvars[1].v_type != VAR_UNKNOWN)
4032 {
4033 if (argvars[2].v_type != VAR_UNKNOWN)
4034 {
4035 /* function(name, [args], dict) */
4036 arg_idx = 1;
4037 dict_idx = 2;
4038 }
4039 else if (argvars[1].v_type == VAR_DICT)
4040 /* function(name, dict) */
4041 dict_idx = 1;
4042 else
4043 /* function(name, [args]) */
4044 arg_idx = 1;
4045 if (dict_idx > 0)
4046 {
4047 if (argvars[dict_idx].v_type != VAR_DICT)
4048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004049 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004050 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004051 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 }
4053 if (argvars[dict_idx].vval.v_dict == NULL)
4054 dict_idx = 0;
4055 }
4056 if (arg_idx > 0)
4057 {
4058 if (argvars[arg_idx].v_type != VAR_LIST)
4059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004062 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 }
4064 list = argvars[arg_idx].vval.v_list;
4065 if (list == NULL || list->lv_len == 0)
4066 arg_idx = 0;
4067 }
4068 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004069 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004071 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072
4073 /* result is a VAR_PARTIAL */
4074 if (pt == NULL)
4075 vim_free(name);
4076 else
4077 {
4078 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4079 {
4080 listitem_T *li;
4081 int i = 0;
4082 int arg_len = 0;
4083 int lv_len = 0;
4084
4085 if (arg_pt != NULL)
4086 arg_len = arg_pt->pt_argc;
4087 if (list != NULL)
4088 lv_len = list->lv_len;
4089 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004090 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 if (pt->pt_argv == NULL)
4092 {
4093 vim_free(pt);
4094 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004095 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004097 for (i = 0; i < arg_len; i++)
4098 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4099 if (lv_len > 0)
4100 for (li = list->lv_first; li != NULL;
4101 li = li->li_next)
4102 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103 }
4104
4105 /* For "function(dict.func, [], dict)" and "func" is a partial
4106 * use "dict". That is backwards compatible. */
4107 if (dict_idx > 0)
4108 {
4109 /* The dict is bound explicitly, pt_auto is FALSE. */
4110 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4111 ++pt->pt_dict->dv_refcount;
4112 }
4113 else if (arg_pt != NULL)
4114 {
4115 /* If the dict was bound automatically the result is also
4116 * bound automatically. */
4117 pt->pt_dict = arg_pt->pt_dict;
4118 pt->pt_auto = arg_pt->pt_auto;
4119 if (pt->pt_dict != NULL)
4120 ++pt->pt_dict->dv_refcount;
4121 }
4122
4123 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004124 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4125 {
4126 pt->pt_func = arg_pt->pt_func;
4127 func_ptr_ref(pt->pt_func);
4128 vim_free(name);
4129 }
4130 else if (is_funcref)
4131 {
4132 pt->pt_func = find_func(trans_name);
4133 func_ptr_ref(pt->pt_func);
4134 vim_free(name);
4135 }
4136 else
4137 {
4138 pt->pt_name = name;
4139 func_ref(name);
4140 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141 }
4142 rettv->v_type = VAR_PARTIAL;
4143 rettv->vval.v_partial = pt;
4144 }
4145 else
4146 {
4147 /* result is a VAR_FUNC */
4148 rettv->v_type = VAR_FUNC;
4149 rettv->vval.v_string = name;
4150 func_ref(name);
4151 }
4152 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004153theend:
4154 vim_free(trans_name);
4155}
4156
4157/*
4158 * "funcref()" function
4159 */
4160 static void
4161f_funcref(typval_T *argvars, typval_T *rettv)
4162{
4163 common_function(argvars, rettv, TRUE);
4164}
4165
4166/*
4167 * "function()" function
4168 */
4169 static void
4170f_function(typval_T *argvars, typval_T *rettv)
4171{
4172 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004173}
4174
4175/*
4176 * "garbagecollect()" function
4177 */
4178 static void
4179f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4180{
4181 /* This is postponed until we are back at the toplevel, because we may be
4182 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4183 want_garbage_collect = TRUE;
4184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004185 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 garbage_collect_at_exit = TRUE;
4187}
4188
4189/*
4190 * "get()" function
4191 */
4192 static void
4193f_get(typval_T *argvars, typval_T *rettv)
4194{
4195 listitem_T *li;
4196 list_T *l;
4197 dictitem_T *di;
4198 dict_T *d;
4199 typval_T *tv = NULL;
4200
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004201 if (argvars[0].v_type == VAR_BLOB)
4202 {
4203 int error = FALSE;
4204 int idx = tv_get_number_chk(&argvars[1], &error);
4205
4206 if (!error)
4207 {
4208 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004209 if (idx < 0)
4210 idx = blob_len(argvars[0].vval.v_blob) + idx;
4211 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4212 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004213 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004214 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004215 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004216 tv = rettv;
4217 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004218 }
4219 }
4220 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004221 {
4222 if ((l = argvars[0].vval.v_list) != NULL)
4223 {
4224 int error = FALSE;
4225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004226 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004227 if (!error && li != NULL)
4228 tv = &li->li_tv;
4229 }
4230 }
4231 else if (argvars[0].v_type == VAR_DICT)
4232 {
4233 if ((d = argvars[0].vval.v_dict) != NULL)
4234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004235 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 if (di != NULL)
4237 tv = &di->di_tv;
4238 }
4239 }
4240 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4241 {
4242 partial_T *pt;
4243 partial_T fref_pt;
4244
4245 if (argvars[0].v_type == VAR_PARTIAL)
4246 pt = argvars[0].vval.v_partial;
4247 else
4248 {
4249 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4250 fref_pt.pt_name = argvars[0].vval.v_string;
4251 pt = &fref_pt;
4252 }
4253
4254 if (pt != NULL)
4255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004256 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258
4259 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4260 {
4261 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004262 n = partial_name(pt);
4263 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 rettv->vval.v_string = NULL;
4265 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004266 {
4267 rettv->vval.v_string = vim_strsave(n);
4268 if (rettv->v_type == VAR_FUNC)
4269 func_ref(rettv->vval.v_string);
4270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271 }
4272 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004273 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 else if (STRCMP(what, "args") == 0)
4275 {
4276 rettv->v_type = VAR_LIST;
4277 if (rettv_list_alloc(rettv) == OK)
4278 {
4279 int i;
4280
4281 for (i = 0; i < pt->pt_argc; ++i)
4282 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4283 }
4284 }
4285 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 return;
4288 }
4289 }
4290 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004291 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292
4293 if (tv == NULL)
4294 {
4295 if (argvars[2].v_type != VAR_UNKNOWN)
4296 copy_tv(&argvars[2], rettv);
4297 }
4298 else
4299 copy_tv(tv, rettv);
4300}
4301
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004302/*
4303 * Returns buffer options, variables and other attributes in a dictionary.
4304 */
4305 static dict_T *
4306get_buffer_info(buf_T *buf)
4307{
4308 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004309 tabpage_T *tp;
4310 win_T *wp;
4311 list_T *windows;
4312
4313 dict = dict_alloc();
4314 if (dict == NULL)
4315 return NULL;
4316
Bram Moolenaare0be1672018-07-08 16:50:37 +02004317 dict_add_number(dict, "bufnr", buf->b_fnum);
4318 dict_add_string(dict, "name", buf->b_ffname);
4319 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4320 : buflist_findlnum(buf));
4321 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4322 dict_add_number(dict, "listed", buf->b_p_bl);
4323 dict_add_number(dict, "changed", bufIsChanged(buf));
4324 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4325 dict_add_number(dict, "hidden",
4326 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004327
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004328 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004329 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004330
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004331 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004332 windows = list_alloc();
4333 if (windows != NULL)
4334 {
4335 FOR_ALL_TAB_WINDOWS(tp, wp)
4336 if (wp->w_buffer == buf)
4337 list_append_number(windows, (varnumber_T)wp->w_id);
4338 dict_add_list(dict, "windows", windows);
4339 }
4340
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004341#ifdef FEAT_TEXT_PROP
4342 // List of popup windows displaying this buffer
4343 windows = list_alloc();
4344 if (windows != NULL)
4345 {
4346 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4347 if (wp->w_buffer == buf)
4348 list_append_number(windows, (varnumber_T)wp->w_id);
4349 FOR_ALL_TABPAGES(tp)
4350 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4351 if (wp->w_buffer == buf)
4352 list_append_number(windows, (varnumber_T)wp->w_id);
4353
4354 dict_add_list(dict, "popups", windows);
4355 }
4356#endif
4357
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004358#ifdef FEAT_SIGNS
4359 if (buf->b_signlist != NULL)
4360 {
4361 /* List of signs placed in this buffer */
4362 list_T *signs = list_alloc();
4363 if (signs != NULL)
4364 {
4365 get_buffer_signs(buf, signs);
4366 dict_add_list(dict, "signs", signs);
4367 }
4368 }
4369#endif
4370
4371 return dict;
4372}
4373
4374/*
4375 * "getbufinfo()" function
4376 */
4377 static void
4378f_getbufinfo(typval_T *argvars, typval_T *rettv)
4379{
4380 buf_T *buf = NULL;
4381 buf_T *argbuf = NULL;
4382 dict_T *d;
4383 int filtered = FALSE;
4384 int sel_buflisted = FALSE;
4385 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004386 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004387
4388 if (rettv_list_alloc(rettv) != OK)
4389 return;
4390
4391 /* List of all the buffers or selected buffers */
4392 if (argvars[0].v_type == VAR_DICT)
4393 {
4394 dict_T *sel_d = argvars[0].vval.v_dict;
4395
4396 if (sel_d != NULL)
4397 {
4398 dictitem_T *di;
4399
4400 filtered = TRUE;
4401
4402 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004403 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004404 sel_buflisted = TRUE;
4405
4406 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004407 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004408 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004409
4410 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004411 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004412 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004413 }
4414 }
4415 else if (argvars[0].v_type != VAR_UNKNOWN)
4416 {
4417 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004418 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004419 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004420 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004421 --emsg_off;
4422 if (argbuf == NULL)
4423 return;
4424 }
4425
4426 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004427 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004428 {
4429 if (argbuf != NULL && argbuf != buf)
4430 continue;
4431 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004432 || (sel_buflisted && !buf->b_p_bl)
4433 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004434 continue;
4435
4436 d = get_buffer_info(buf);
4437 if (d != NULL)
4438 list_append_dict(rettv->vval.v_list, d);
4439 if (argbuf != NULL)
4440 return;
4441 }
4442}
4443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004444/*
4445 * Get line or list of lines from buffer "buf" into "rettv".
4446 * Return a range (from start to end) of lines in rettv from the specified
4447 * buffer.
4448 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4449 */
4450 static void
4451get_buffer_lines(
4452 buf_T *buf,
4453 linenr_T start,
4454 linenr_T end,
4455 int retlist,
4456 typval_T *rettv)
4457{
4458 char_u *p;
4459
4460 rettv->v_type = VAR_STRING;
4461 rettv->vval.v_string = NULL;
4462 if (retlist && rettv_list_alloc(rettv) == FAIL)
4463 return;
4464
4465 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4466 return;
4467
4468 if (!retlist)
4469 {
4470 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4471 p = ml_get_buf(buf, start, FALSE);
4472 else
4473 p = (char_u *)"";
4474 rettv->vval.v_string = vim_strsave(p);
4475 }
4476 else
4477 {
4478 if (end < start)
4479 return;
4480
4481 if (start < 1)
4482 start = 1;
4483 if (end > buf->b_ml.ml_line_count)
4484 end = buf->b_ml.ml_line_count;
4485 while (start <= end)
4486 if (list_append_string(rettv->vval.v_list,
4487 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4488 break;
4489 }
4490}
4491
4492/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004493 * "getbufline()" function
4494 */
4495 static void
4496f_getbufline(typval_T *argvars, typval_T *rettv)
4497{
4498 linenr_T lnum;
4499 linenr_T end;
4500 buf_T *buf;
4501
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004502 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004503 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004504 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004505 --emsg_off;
4506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004507 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508 if (argvars[2].v_type == VAR_UNKNOWN)
4509 end = lnum;
4510 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004511 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512
4513 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4514}
4515
4516/*
4517 * "getbufvar()" function
4518 */
4519 static void
4520f_getbufvar(typval_T *argvars, typval_T *rettv)
4521{
4522 buf_T *buf;
4523 buf_T *save_curbuf;
4524 char_u *varname;
4525 dictitem_T *v;
4526 int done = FALSE;
4527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004528 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4529 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004531 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532
4533 rettv->v_type = VAR_STRING;
4534 rettv->vval.v_string = NULL;
4535
4536 if (buf != NULL && varname != NULL)
4537 {
4538 /* set curbuf to be our buf, temporarily */
4539 save_curbuf = curbuf;
4540 curbuf = buf;
4541
Bram Moolenaar30567352016-08-27 21:25:44 +02004542 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004544 if (varname[1] == NUL)
4545 {
4546 /* get all buffer-local options in a dict */
4547 dict_T *opts = get_winbuf_options(TRUE);
4548
4549 if (opts != NULL)
4550 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004551 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004552 done = TRUE;
4553 }
4554 }
4555 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4556 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557 done = TRUE;
4558 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559 else
4560 {
4561 /* Look up the variable. */
4562 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4563 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4564 'b', varname, FALSE);
4565 if (v != NULL)
4566 {
4567 copy_tv(&v->di_tv, rettv);
4568 done = TRUE;
4569 }
4570 }
4571
4572 /* restore previous notion of curbuf */
4573 curbuf = save_curbuf;
4574 }
4575
4576 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4577 /* use the default value */
4578 copy_tv(&argvars[2], rettv);
4579
4580 --emsg_off;
4581}
4582
4583/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004584 * "getchangelist()" function
4585 */
4586 static void
4587f_getchangelist(typval_T *argvars, typval_T *rettv)
4588{
4589#ifdef FEAT_JUMPLIST
4590 buf_T *buf;
4591 int i;
4592 list_T *l;
4593 dict_T *d;
4594#endif
4595
4596 if (rettv_list_alloc(rettv) != OK)
4597 return;
4598
4599#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004600 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004601 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004602 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004603 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004604 if (buf == NULL)
4605 return;
4606
4607 l = list_alloc();
4608 if (l == NULL)
4609 return;
4610
4611 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4612 return;
4613 /*
4614 * The current window change list index tracks only the position in the
4615 * current buffer change list. For other buffers, use the change list
4616 * length as the current index.
4617 */
4618 list_append_number(rettv->vval.v_list,
4619 (varnumber_T)((buf == curwin->w_buffer)
4620 ? curwin->w_changelistidx : buf->b_changelistlen));
4621
4622 for (i = 0; i < buf->b_changelistlen; ++i)
4623 {
4624 if (buf->b_changelist[i].lnum == 0)
4625 continue;
4626 if ((d = dict_alloc()) == NULL)
4627 return;
4628 if (list_append_dict(l, d) == FAIL)
4629 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004630 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4631 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004632 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004633 }
4634#endif
4635}
4636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004637 * "getchar()" function
4638 */
4639 static void
4640f_getchar(typval_T *argvars, typval_T *rettv)
4641{
4642 varnumber_T n;
4643 int error = FALSE;
4644
Bram Moolenaar84d93902018-09-11 20:10:20 +02004645#ifdef MESSAGE_QUEUE
4646 // vpeekc() used to check for messages, but that caused problems, invoking
4647 // a callback where it was not expected. Some plugins use getchar(1) in a
4648 // loop to await a message, therefore make sure we check for messages here.
4649 parse_queued_messages();
4650#endif
4651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652 /* Position the cursor. Needed after a message that ends in a space. */
4653 windgoto(msg_row, msg_col);
4654
4655 ++no_mapping;
4656 ++allow_keys;
4657 for (;;)
4658 {
4659 if (argvars[0].v_type == VAR_UNKNOWN)
4660 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004661 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004662 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663 /* getchar(1): only check if char avail */
4664 n = vpeekc_any();
4665 else if (error || vpeekc_any() == NUL)
4666 /* illegal argument or getchar(0) and no char avail: return zero */
4667 n = 0;
4668 else
4669 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004670 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671
4672 if (n == K_IGNORE)
4673 continue;
4674 break;
4675 }
4676 --no_mapping;
4677 --allow_keys;
4678
4679 set_vim_var_nr(VV_MOUSE_WIN, 0);
4680 set_vim_var_nr(VV_MOUSE_WINID, 0);
4681 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4682 set_vim_var_nr(VV_MOUSE_COL, 0);
4683
4684 rettv->vval.v_number = n;
4685 if (IS_SPECIAL(n) || mod_mask != 0)
4686 {
4687 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4688 int i = 0;
4689
4690 /* Turn a special key into three bytes, plus modifier. */
4691 if (mod_mask != 0)
4692 {
4693 temp[i++] = K_SPECIAL;
4694 temp[i++] = KS_MODIFIER;
4695 temp[i++] = mod_mask;
4696 }
4697 if (IS_SPECIAL(n))
4698 {
4699 temp[i++] = K_SPECIAL;
4700 temp[i++] = K_SECOND(n);
4701 temp[i++] = K_THIRD(n);
4702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004703 else if (has_mbyte)
4704 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004705 else
4706 temp[i++] = n;
4707 temp[i++] = NUL;
4708 rettv->v_type = VAR_STRING;
4709 rettv->vval.v_string = vim_strsave(temp);
4710
4711#ifdef FEAT_MOUSE
4712 if (is_mouse_key(n))
4713 {
4714 int row = mouse_row;
4715 int col = mouse_col;
4716 win_T *win;
4717 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 int winnr = 1;
4720
4721 if (row >= 0 && col >= 0)
4722 {
4723 /* Find the window at the mouse coordinates and compute the
4724 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004725 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004726 if (win == NULL)
4727 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004728 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004729# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004730 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004731 winnr = 0;
4732 else
4733# endif
4734 for (wp = firstwin; wp != win && wp != NULL;
4735 wp = wp->w_next)
4736 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4738 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4739 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4740 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4741 }
4742 }
4743#endif
4744 }
4745}
4746
4747/*
4748 * "getcharmod()" function
4749 */
4750 static void
4751f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4752{
4753 rettv->vval.v_number = mod_mask;
4754}
4755
4756/*
4757 * "getcharsearch()" function
4758 */
4759 static void
4760f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4761{
4762 if (rettv_dict_alloc(rettv) != FAIL)
4763 {
4764 dict_T *dict = rettv->vval.v_dict;
4765
Bram Moolenaare0be1672018-07-08 16:50:37 +02004766 dict_add_string(dict, "char", last_csearch());
4767 dict_add_number(dict, "forward", last_csearch_forward());
4768 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004769 }
4770}
4771
4772/*
4773 * "getcmdline()" function
4774 */
4775 static void
4776f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4777{
4778 rettv->v_type = VAR_STRING;
4779 rettv->vval.v_string = get_cmdline_str();
4780}
4781
4782/*
4783 * "getcmdpos()" function
4784 */
4785 static void
4786f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4787{
4788 rettv->vval.v_number = get_cmdline_pos() + 1;
4789}
4790
4791/*
4792 * "getcmdtype()" function
4793 */
4794 static void
4795f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4796{
4797 rettv->v_type = VAR_STRING;
4798 rettv->vval.v_string = alloc(2);
4799 if (rettv->vval.v_string != NULL)
4800 {
4801 rettv->vval.v_string[0] = get_cmdline_type();
4802 rettv->vval.v_string[1] = NUL;
4803 }
4804}
4805
4806/*
4807 * "getcmdwintype()" function
4808 */
4809 static void
4810f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4811{
4812 rettv->v_type = VAR_STRING;
4813 rettv->vval.v_string = NULL;
4814#ifdef FEAT_CMDWIN
4815 rettv->vval.v_string = alloc(2);
4816 if (rettv->vval.v_string != NULL)
4817 {
4818 rettv->vval.v_string[0] = cmdwin_type;
4819 rettv->vval.v_string[1] = NUL;
4820 }
4821#endif
4822}
4823
4824#if defined(FEAT_CMDL_COMPL)
4825/*
4826 * "getcompletion()" function
4827 */
4828 static void
4829f_getcompletion(typval_T *argvars, typval_T *rettv)
4830{
4831 char_u *pat;
4832 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004833 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004834 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4835 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 if (p_wic)
4841 options |= WILD_ICASE;
4842
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004843 /* For filtered results, 'wildignore' is used */
4844 if (!filtered)
4845 options |= WILD_KEEP_ALL;
4846
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004848 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 if (xpc.xp_context == EXPAND_NOTHING)
4852 {
4853 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004857 return;
4858 }
4859
4860# if defined(FEAT_MENU)
4861 if (xpc.xp_context == EXPAND_MENUS)
4862 {
4863 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4864 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4865 }
4866# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004867#ifdef FEAT_CSCOPE
4868 if (xpc.xp_context == EXPAND_CSCOPE)
4869 {
4870 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4871 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4872 }
4873#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004874#ifdef FEAT_SIGNS
4875 if (xpc.xp_context == EXPAND_SIGN)
4876 {
4877 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4878 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4879 }
4880#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881
4882 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4883 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4884 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004885 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886
4887 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4888
4889 for (i = 0; i < xpc.xp_numfiles; i++)
4890 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4891 }
4892 vim_free(pat);
4893 ExpandCleanup(&xpc);
4894}
4895#endif
4896
4897/*
4898 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004899 *
4900 * Return the current working directory of a window in a tab page.
4901 * First optional argument 'winnr' is the window number or -1 and the second
4902 * optional argument 'tabnr' is the tab page number.
4903 *
4904 * If no arguments are supplied, then return the directory of the current
4905 * window.
4906 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4907 * the specified window.
4908 * If 'winnr' is 0 then return the directory of the current window.
4909 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4910 * directory of the specified tab page. Otherwise return the directory of the
4911 * specified window in the specified tab page.
4912 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 */
4914 static void
4915f_getcwd(typval_T *argvars, typval_T *rettv)
4916{
4917 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004918 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004919 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004920 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921
4922 rettv->v_type = VAR_STRING;
4923 rettv->vval.v_string = NULL;
4924
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004925 if (argvars[0].v_type == VAR_NUMBER
4926 && argvars[0].vval.v_number == -1
4927 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004928 global = TRUE;
4929 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004930 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004931
4932 if (wp != NULL && wp->w_localdir != NULL)
4933 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004934 else if (tp != NULL && tp->tp_localdir != NULL)
4935 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4936 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004938 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 rettv->vval.v_string = vim_strsave(globaldir);
4940 else
4941 {
4942 cwd = alloc(MAXPATHL);
4943 if (cwd != NULL)
4944 {
4945 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4946 rettv->vval.v_string = vim_strsave(cwd);
4947 vim_free(cwd);
4948 }
4949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004951#ifdef BACKSLASH_IN_FILENAME
4952 if (rettv->vval.v_string != NULL)
4953 slash_adjust(rettv->vval.v_string);
4954#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955}
4956
4957/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004958 * "getenv()" function
4959 */
4960 static void
4961f_getenv(typval_T *argvars, typval_T *rettv)
4962{
4963 int mustfree = FALSE;
4964 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4965
4966 if (p == NULL)
4967 {
4968 rettv->v_type = VAR_SPECIAL;
4969 rettv->vval.v_number = VVAL_NULL;
4970 return;
4971 }
4972 if (!mustfree)
4973 p = vim_strsave(p);
4974 rettv->vval.v_string = p;
4975 rettv->v_type = VAR_STRING;
4976}
4977
4978/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004979 * "getfontname()" function
4980 */
4981 static void
4982f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4983{
4984 rettv->v_type = VAR_STRING;
4985 rettv->vval.v_string = NULL;
4986#ifdef FEAT_GUI
4987 if (gui.in_use)
4988 {
4989 GuiFont font;
4990 char_u *name = NULL;
4991
4992 if (argvars[0].v_type == VAR_UNKNOWN)
4993 {
4994 /* Get the "Normal" font. Either the name saved by
4995 * hl_set_font_name() or from the font ID. */
4996 font = gui.norm_font;
4997 name = hl_get_font_name();
4998 }
4999 else
5000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005001 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5003 return;
5004 font = gui_mch_get_font(name, FALSE);
5005 if (font == NOFONT)
5006 return; /* Invalid font name, return empty string. */
5007 }
5008 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5009 if (argvars[0].v_type != VAR_UNKNOWN)
5010 gui_mch_free_font(font);
5011 }
5012#endif
5013}
5014
5015/*
5016 * "getfperm({fname})" function
5017 */
5018 static void
5019f_getfperm(typval_T *argvars, typval_T *rettv)
5020{
5021 char_u *fname;
5022 stat_T st;
5023 char_u *perm = NULL;
5024 char_u flags[] = "rwx";
5025 int i;
5026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005027 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028
5029 rettv->v_type = VAR_STRING;
5030 if (mch_stat((char *)fname, &st) >= 0)
5031 {
5032 perm = vim_strsave((char_u *)"---------");
5033 if (perm != NULL)
5034 {
5035 for (i = 0; i < 9; i++)
5036 {
5037 if (st.st_mode & (1 << (8 - i)))
5038 perm[i] = flags[i % 3];
5039 }
5040 }
5041 }
5042 rettv->vval.v_string = perm;
5043}
5044
5045/*
5046 * "getfsize({fname})" function
5047 */
5048 static void
5049f_getfsize(typval_T *argvars, typval_T *rettv)
5050{
5051 char_u *fname;
5052 stat_T st;
5053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005054 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005055
5056 rettv->v_type = VAR_NUMBER;
5057
5058 if (mch_stat((char *)fname, &st) >= 0)
5059 {
5060 if (mch_isdir(fname))
5061 rettv->vval.v_number = 0;
5062 else
5063 {
5064 rettv->vval.v_number = (varnumber_T)st.st_size;
5065
5066 /* non-perfect check for overflow */
5067 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5068 rettv->vval.v_number = -2;
5069 }
5070 }
5071 else
5072 rettv->vval.v_number = -1;
5073}
5074
5075/*
5076 * "getftime({fname})" function
5077 */
5078 static void
5079f_getftime(typval_T *argvars, typval_T *rettv)
5080{
5081 char_u *fname;
5082 stat_T st;
5083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005084 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085
5086 if (mch_stat((char *)fname, &st) >= 0)
5087 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5088 else
5089 rettv->vval.v_number = -1;
5090}
5091
5092/*
5093 * "getftype({fname})" function
5094 */
5095 static void
5096f_getftype(typval_T *argvars, typval_T *rettv)
5097{
5098 char_u *fname;
5099 stat_T st;
5100 char_u *type = NULL;
5101 char *t;
5102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005103 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104
5105 rettv->v_type = VAR_STRING;
5106 if (mch_lstat((char *)fname, &st) >= 0)
5107 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 if (S_ISREG(st.st_mode))
5109 t = "file";
5110 else if (S_ISDIR(st.st_mode))
5111 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 else if (S_ISLNK(st.st_mode))
5113 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 else if (S_ISBLK(st.st_mode))
5115 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 else if (S_ISCHR(st.st_mode))
5117 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else if (S_ISFIFO(st.st_mode))
5119 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005121 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 else
5123 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 type = vim_strsave((char_u *)t);
5125 }
5126 rettv->vval.v_string = type;
5127}
5128
5129/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005130 * "getjumplist()" function
5131 */
5132 static void
5133f_getjumplist(typval_T *argvars, typval_T *rettv)
5134{
5135#ifdef FEAT_JUMPLIST
5136 win_T *wp;
5137 int i;
5138 list_T *l;
5139 dict_T *d;
5140#endif
5141
5142 if (rettv_list_alloc(rettv) != OK)
5143 return;
5144
5145#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005146 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005147 if (wp == NULL)
5148 return;
5149
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005150 cleanup_jumplist(wp, TRUE);
5151
Bram Moolenaar4f505882018-02-10 21:06:32 +01005152 l = list_alloc();
5153 if (l == NULL)
5154 return;
5155
5156 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5157 return;
5158 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5159
5160 for (i = 0; i < wp->w_jumplistlen; ++i)
5161 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005162 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5163 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005164 if ((d = dict_alloc()) == NULL)
5165 return;
5166 if (list_append_dict(l, d) == FAIL)
5167 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005168 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5169 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005170 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005171 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005172 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005173 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005174 }
5175#endif
5176}
5177
5178/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 * "getline(lnum, [end])" function
5180 */
5181 static void
5182f_getline(typval_T *argvars, typval_T *rettv)
5183{
5184 linenr_T lnum;
5185 linenr_T end;
5186 int retlist;
5187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005188 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 if (argvars[1].v_type == VAR_UNKNOWN)
5190 {
5191 end = 0;
5192 retlist = FALSE;
5193 }
5194 else
5195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005196 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005197 retlist = TRUE;
5198 }
5199
5200 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5201}
5202
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005203#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005204 static void
5205get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5206{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005207 if (what_arg->v_type == VAR_UNKNOWN)
5208 {
5209 if (rettv_list_alloc(rettv) == OK)
5210 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005211 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005212 }
5213 else
5214 {
5215 if (rettv_dict_alloc(rettv) == OK)
5216 if (is_qf || (wp != NULL))
5217 {
5218 if (what_arg->v_type == VAR_DICT)
5219 {
5220 dict_T *d = what_arg->vval.v_dict;
5221
5222 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005223 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005224 }
5225 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005226 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005227 }
5228 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005229}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005230#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005231
5232/*
5233 * "getloclist()" function
5234 */
5235 static void
5236f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5237{
5238#ifdef FEAT_QUICKFIX
5239 win_T *wp;
5240
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005241 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005242 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5243#endif
5244}
5245
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005247 * "getpid()" function
5248 */
5249 static void
5250f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5251{
5252 rettv->vval.v_number = mch_get_pid();
5253}
5254
5255 static void
5256getpos_both(
5257 typval_T *argvars,
5258 typval_T *rettv,
5259 int getcurpos)
5260{
5261 pos_T *fp;
5262 list_T *l;
5263 int fnum = -1;
5264
5265 if (rettv_list_alloc(rettv) == OK)
5266 {
5267 l = rettv->vval.v_list;
5268 if (getcurpos)
5269 fp = &curwin->w_cursor;
5270 else
5271 fp = var2fpos(&argvars[0], TRUE, &fnum);
5272 if (fnum != -1)
5273 list_append_number(l, (varnumber_T)fnum);
5274 else
5275 list_append_number(l, (varnumber_T)0);
5276 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5277 : (varnumber_T)0);
5278 list_append_number(l, (fp != NULL)
5279 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5280 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005281 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 (varnumber_T)0);
5283 if (getcurpos)
5284 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005285 int save_set_curswant = curwin->w_set_curswant;
5286 colnr_T save_curswant = curwin->w_curswant;
5287 colnr_T save_virtcol = curwin->w_virtcol;
5288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289 update_curswant();
5290 list_append_number(l, curwin->w_curswant == MAXCOL ?
5291 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005292
5293 // Do not change "curswant", as it is unexpected that a get
5294 // function has a side effect.
5295 if (save_set_curswant)
5296 {
5297 curwin->w_set_curswant = save_set_curswant;
5298 curwin->w_curswant = save_curswant;
5299 curwin->w_virtcol = save_virtcol;
5300 curwin->w_valid &= ~VALID_VIRTCOL;
5301 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 }
5303 }
5304 else
5305 rettv->vval.v_number = FALSE;
5306}
5307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308/*
5309 * "getcurpos()" function
5310 */
5311 static void
5312f_getcurpos(typval_T *argvars, typval_T *rettv)
5313{
5314 getpos_both(argvars, rettv, TRUE);
5315}
5316
5317/*
5318 * "getpos(string)" function
5319 */
5320 static void
5321f_getpos(typval_T *argvars, typval_T *rettv)
5322{
5323 getpos_both(argvars, rettv, FALSE);
5324}
5325
5326/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005327 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328 */
5329 static void
5330f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5331{
5332#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005333 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334#endif
5335}
5336
5337/*
5338 * "getreg()" function
5339 */
5340 static void
5341f_getreg(typval_T *argvars, typval_T *rettv)
5342{
5343 char_u *strregname;
5344 int regname;
5345 int arg2 = FALSE;
5346 int return_list = FALSE;
5347 int error = FALSE;
5348
5349 if (argvars[0].v_type != VAR_UNKNOWN)
5350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005351 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352 error = strregname == NULL;
5353 if (argvars[1].v_type != VAR_UNKNOWN)
5354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005355 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005357 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 }
5359 }
5360 else
5361 strregname = get_vim_var_str(VV_REG);
5362
5363 if (error)
5364 return;
5365
5366 regname = (strregname == NULL ? '"' : *strregname);
5367 if (regname == 0)
5368 regname = '"';
5369
5370 if (return_list)
5371 {
5372 rettv->v_type = VAR_LIST;
5373 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5374 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5375 if (rettv->vval.v_list == NULL)
5376 (void)rettv_list_alloc(rettv);
5377 else
5378 ++rettv->vval.v_list->lv_refcount;
5379 }
5380 else
5381 {
5382 rettv->v_type = VAR_STRING;
5383 rettv->vval.v_string = get_reg_contents(regname,
5384 arg2 ? GREG_EXPR_SRC : 0);
5385 }
5386}
5387
5388/*
5389 * "getregtype()" function
5390 */
5391 static void
5392f_getregtype(typval_T *argvars, typval_T *rettv)
5393{
5394 char_u *strregname;
5395 int regname;
5396 char_u buf[NUMBUFLEN + 2];
5397 long reglen = 0;
5398
5399 if (argvars[0].v_type != VAR_UNKNOWN)
5400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005401 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005402 if (strregname == NULL) /* type error; errmsg already given */
5403 {
5404 rettv->v_type = VAR_STRING;
5405 rettv->vval.v_string = NULL;
5406 return;
5407 }
5408 }
5409 else
5410 /* Default to v:register */
5411 strregname = get_vim_var_str(VV_REG);
5412
5413 regname = (strregname == NULL ? '"' : *strregname);
5414 if (regname == 0)
5415 regname = '"';
5416
5417 buf[0] = NUL;
5418 buf[1] = NUL;
5419 switch (get_reg_type(regname, &reglen))
5420 {
5421 case MLINE: buf[0] = 'V'; break;
5422 case MCHAR: buf[0] = 'v'; break;
5423 case MBLOCK:
5424 buf[0] = Ctrl_V;
5425 sprintf((char *)buf + 1, "%ld", reglen + 1);
5426 break;
5427 }
5428 rettv->v_type = VAR_STRING;
5429 rettv->vval.v_string = vim_strsave(buf);
5430}
5431
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005432/*
5433 * Returns information (variables, options, etc.) about a tab page
5434 * as a dictionary.
5435 */
5436 static dict_T *
5437get_tabpage_info(tabpage_T *tp, int tp_idx)
5438{
5439 win_T *wp;
5440 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005441 list_T *l;
5442
5443 dict = dict_alloc();
5444 if (dict == NULL)
5445 return NULL;
5446
Bram Moolenaare0be1672018-07-08 16:50:37 +02005447 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005448
5449 l = list_alloc();
5450 if (l != NULL)
5451 {
5452 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005453 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 list_append_number(l, (varnumber_T)wp->w_id);
5455 dict_add_list(dict, "windows", l);
5456 }
5457
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005458 /* Make a reference to tabpage variables */
5459 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005460
5461 return dict;
5462}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005463
5464/*
5465 * "gettabinfo()" function
5466 */
5467 static void
5468f_gettabinfo(typval_T *argvars, typval_T *rettv)
5469{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005470 tabpage_T *tp, *tparg = NULL;
5471 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005472 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005473
5474 if (rettv_list_alloc(rettv) != OK)
5475 return;
5476
5477 if (argvars[0].v_type != VAR_UNKNOWN)
5478 {
5479 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005480 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005481 if (tparg == NULL)
5482 return;
5483 }
5484
5485 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005486 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005487 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005488 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005489 if (tparg != NULL && tp != tparg)
5490 continue;
5491 d = get_tabpage_info(tp, tpnr);
5492 if (d != NULL)
5493 list_append_dict(rettv->vval.v_list, d);
5494 if (tparg != NULL)
5495 return;
5496 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005497}
5498
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499/*
5500 * "gettabvar()" function
5501 */
5502 static void
5503f_gettabvar(typval_T *argvars, typval_T *rettv)
5504{
5505 win_T *oldcurwin;
5506 tabpage_T *tp, *oldtabpage;
5507 dictitem_T *v;
5508 char_u *varname;
5509 int done = FALSE;
5510
5511 rettv->v_type = VAR_STRING;
5512 rettv->vval.v_string = NULL;
5513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005514 varname = tv_get_string_chk(&argvars[1]);
5515 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516 if (tp != NULL && varname != NULL)
5517 {
5518 /* Set tp to be our tabpage, temporarily. Also set the window to the
5519 * first window in the tabpage, otherwise the window is not valid. */
5520 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005521 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5522 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 {
5524 /* look up the variable */
5525 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5526 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5527 if (v != NULL)
5528 {
5529 copy_tv(&v->di_tv, rettv);
5530 done = TRUE;
5531 }
5532 }
5533
5534 /* restore previous notion of curwin */
5535 restore_win(oldcurwin, oldtabpage, TRUE);
5536 }
5537
5538 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5539 copy_tv(&argvars[2], rettv);
5540}
5541
5542/*
5543 * "gettabwinvar()" function
5544 */
5545 static void
5546f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5547{
5548 getwinvar(argvars, rettv, 1);
5549}
5550
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005552 * "gettagstack()" function
5553 */
5554 static void
5555f_gettagstack(typval_T *argvars, typval_T *rettv)
5556{
5557 win_T *wp = curwin; // default is current window
5558
5559 if (rettv_dict_alloc(rettv) != OK)
5560 return;
5561
5562 if (argvars[0].v_type != VAR_UNKNOWN)
5563 {
5564 wp = find_win_by_nr_or_id(&argvars[0]);
5565 if (wp == NULL)
5566 return;
5567 }
5568
5569 get_tagstack(wp, rettv->vval.v_dict);
5570}
5571
5572/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573 * Returns information about a window as a dictionary.
5574 */
5575 static dict_T *
5576get_win_info(win_T *wp, short tpnr, short winnr)
5577{
5578 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005579
5580 dict = dict_alloc();
5581 if (dict == NULL)
5582 return NULL;
5583
Bram Moolenaare0be1672018-07-08 16:50:37 +02005584 dict_add_number(dict, "tabnr", tpnr);
5585 dict_add_number(dict, "winnr", winnr);
5586 dict_add_number(dict, "winid", wp->w_id);
5587 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005588 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005589 dict_add_number(dict, "topline", wp->w_topline);
5590 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005591#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005592 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005593#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005594 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005595 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005596 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005597
Bram Moolenaar69905d12017-08-13 18:14:47 +02005598#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005599 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005600#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005601#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005602 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5603 dict_add_number(dict, "loclist",
5604 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005605#endif
5606
Bram Moolenaar30567352016-08-27 21:25:44 +02005607 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005608 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005609
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005610 return dict;
5611}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005612
5613/*
5614 * "getwininfo()" function
5615 */
5616 static void
5617f_getwininfo(typval_T *argvars, typval_T *rettv)
5618{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005619 tabpage_T *tp;
5620 win_T *wp = NULL, *wparg = NULL;
5621 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005622 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005623
5624 if (rettv_list_alloc(rettv) != OK)
5625 return;
5626
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005627 if (argvars[0].v_type != VAR_UNKNOWN)
5628 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005629 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005630 if (wparg == NULL)
5631 return;
5632 }
5633
5634 /* Collect information about either all the windows across all the tab
5635 * pages or one particular window.
5636 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005637 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005638 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005639 tabnr++;
5640 winnr = 0;
5641 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005642 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005643 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005644 if (wparg != NULL && wp != wparg)
5645 continue;
5646 d = get_win_info(wp, tabnr, winnr);
5647 if (d != NULL)
5648 list_append_dict(rettv->vval.v_list, d);
5649 if (wparg != NULL)
5650 /* found information about a specific window */
5651 return;
5652 }
5653 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654}
5655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005657 * "win_execute()" function
5658 */
5659 static void
5660f_win_execute(typval_T *argvars, typval_T *rettv)
5661{
5662 int id = (int)tv_get_number(argvars);
5663 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005664 win_T *save_curwin;
5665 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005666
5667 if (wp != NULL)
5668 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005669 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5670 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005671 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005672 check_cursor();
5673 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005674 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005675 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005676 }
5677}
5678
5679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 * "win_findbuf()" function
5681 */
5682 static void
5683f_win_findbuf(typval_T *argvars, typval_T *rettv)
5684{
5685 if (rettv_list_alloc(rettv) != FAIL)
5686 win_findbuf(argvars, rettv->vval.v_list);
5687}
5688
5689/*
5690 * "win_getid()" function
5691 */
5692 static void
5693f_win_getid(typval_T *argvars, typval_T *rettv)
5694{
5695 rettv->vval.v_number = win_getid(argvars);
5696}
5697
5698/*
5699 * "win_gotoid()" function
5700 */
5701 static void
5702f_win_gotoid(typval_T *argvars, typval_T *rettv)
5703{
5704 rettv->vval.v_number = win_gotoid(argvars);
5705}
5706
5707/*
5708 * "win_id2tabwin()" function
5709 */
5710 static void
5711f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5712{
5713 if (rettv_list_alloc(rettv) != FAIL)
5714 win_id2tabwin(argvars, rettv->vval.v_list);
5715}
5716
5717/*
5718 * "win_id2win()" function
5719 */
5720 static void
5721f_win_id2win(typval_T *argvars, typval_T *rettv)
5722{
5723 rettv->vval.v_number = win_id2win(argvars);
5724}
5725
5726/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005727 * "win_screenpos()" function
5728 */
5729 static void
5730f_win_screenpos(typval_T *argvars, typval_T *rettv)
5731{
5732 win_T *wp;
5733
5734 if (rettv_list_alloc(rettv) == FAIL)
5735 return;
5736
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005737 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005738 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5739 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5740}
5741
5742/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005743 * "getwinpos({timeout})" function
5744 */
5745 static void
5746f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5747{
5748 int x = -1;
5749 int y = -1;
5750
5751 if (rettv_list_alloc(rettv) == FAIL)
5752 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005753#if defined(FEAT_GUI) \
5754 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5755 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005756 {
5757 varnumber_T timeout = 100;
5758
5759 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005761
5762 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005763 }
5764#endif
5765 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5766 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5767}
5768
5769
5770/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005771 * "getwinposx()" function
5772 */
5773 static void
5774f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5775{
5776 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005777#if defined(FEAT_GUI) \
5778 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5779 || defined(MSWIN)
5780
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005781 {
5782 int x, y;
5783
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005784 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005785 rettv->vval.v_number = x;
5786 }
5787#endif
5788}
5789
5790/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005791 * "getwinposy()" function
5792 */
5793 static void
5794f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5795{
5796 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005797#if defined(FEAT_GUI) \
5798 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5799 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 {
5801 int x, y;
5802
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005803 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005804 rettv->vval.v_number = y;
5805 }
5806#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005807}
5808
5809/*
5810 * "getwinvar()" function
5811 */
5812 static void
5813f_getwinvar(typval_T *argvars, typval_T *rettv)
5814{
5815 getwinvar(argvars, rettv, 0);
5816}
5817
5818/*
5819 * "glob()" function
5820 */
5821 static void
5822f_glob(typval_T *argvars, typval_T *rettv)
5823{
5824 int options = WILD_SILENT|WILD_USE_NL;
5825 expand_T xpc;
5826 int error = FALSE;
5827
5828 /* When the optional second argument is non-zero, don't remove matches
5829 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5830 rettv->v_type = VAR_STRING;
5831 if (argvars[1].v_type != VAR_UNKNOWN)
5832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 options |= WILD_KEEP_ALL;
5835 if (argvars[2].v_type != VAR_UNKNOWN)
5836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005838 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005839 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005840 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005841 options |= WILD_ALLLINKS;
5842 }
5843 }
5844 if (!error)
5845 {
5846 ExpandInit(&xpc);
5847 xpc.xp_context = EXPAND_FILES;
5848 if (p_wic)
5849 options += WILD_ICASE;
5850 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005851 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 NULL, options, WILD_ALL);
5853 else if (rettv_list_alloc(rettv) != FAIL)
5854 {
5855 int i;
5856
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 NULL, options, WILD_ALL_KEEP);
5859 for (i = 0; i < xpc.xp_numfiles; i++)
5860 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5861
5862 ExpandCleanup(&xpc);
5863 }
5864 }
5865 else
5866 rettv->vval.v_string = NULL;
5867}
5868
5869/*
5870 * "globpath()" function
5871 */
5872 static void
5873f_globpath(typval_T *argvars, typval_T *rettv)
5874{
5875 int flags = 0;
5876 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005877 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878 int error = FALSE;
5879 garray_T ga;
5880 int i;
5881
5882 /* When the optional second argument is non-zero, don't remove matches
5883 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5884 rettv->v_type = VAR_STRING;
5885 if (argvars[2].v_type != VAR_UNKNOWN)
5886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005887 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 flags |= WILD_KEEP_ALL;
5889 if (argvars[3].v_type != VAR_UNKNOWN)
5890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005891 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005892 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005893 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005894 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895 flags |= WILD_ALLLINKS;
5896 }
5897 }
5898 if (file != NULL && !error)
5899 {
5900 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005901 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902 if (rettv->v_type == VAR_STRING)
5903 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5904 else if (rettv_list_alloc(rettv) != FAIL)
5905 for (i = 0; i < ga.ga_len; ++i)
5906 list_append_string(rettv->vval.v_list,
5907 ((char_u **)(ga.ga_data))[i], -1);
5908 ga_clear_strings(&ga);
5909 }
5910 else
5911 rettv->vval.v_string = NULL;
5912}
5913
5914/*
5915 * "glob2regpat()" function
5916 */
5917 static void
5918f_glob2regpat(typval_T *argvars, typval_T *rettv)
5919{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005920 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005921
5922 rettv->v_type = VAR_STRING;
5923 rettv->vval.v_string = (pat == NULL)
5924 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5925}
5926
5927/* for VIM_VERSION_ defines */
5928#include "version.h"
5929
5930/*
5931 * "has()" function
5932 */
5933 static void
5934f_has(typval_T *argvars, typval_T *rettv)
5935{
5936 int i;
5937 char_u *name;
5938 int n = FALSE;
5939 static char *(has_list[]) =
5940 {
5941#ifdef AMIGA
5942 "amiga",
5943# ifdef FEAT_ARP
5944 "arp",
5945# endif
5946#endif
5947#ifdef __BEOS__
5948 "beos",
5949#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005950#if defined(BSD) && !defined(MACOS_X)
5951 "bsd",
5952#endif
5953#ifdef hpux
5954 "hpux",
5955#endif
5956#ifdef __linux__
5957 "linux",
5958#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005959#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005960 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5961 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005962# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005963 "macunix", /* Mac OS X, with the darwin feature */
5964 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005965# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966#endif
5967#ifdef __QNX__
5968 "qnx",
5969#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005970#ifdef SUN_SYSTEM
5971 "sun",
5972#else
5973 "moon",
5974#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975#ifdef UNIX
5976 "unix",
5977#endif
5978#ifdef VMS
5979 "vms",
5980#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005981#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 "win32",
5983#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005984#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 "win32unix",
5986#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005987#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 "win64",
5989#endif
5990#ifdef EBCDIC
5991 "ebcdic",
5992#endif
5993#ifndef CASE_INSENSITIVE_FILENAME
5994 "fname_case",
5995#endif
5996#ifdef HAVE_ACL
5997 "acl",
5998#endif
5999#ifdef FEAT_ARABIC
6000 "arabic",
6001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006003#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006004 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006005#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006006#ifdef FEAT_AUTOSERVERNAME
6007 "autoservername",
6008#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006009#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006011# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 "balloon_multiline",
6013# endif
6014#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006015#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006016 "balloon_eval_term",
6017#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6019 "builtin_terms",
6020# ifdef ALL_BUILTIN_TCAPS
6021 "all_builtin_terms",
6022# endif
6023#endif
6024#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006025 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 || defined(FEAT_GUI_MOTIF))
6027 "browsefilter",
6028#endif
6029#ifdef FEAT_BYTEOFF
6030 "byte_offset",
6031#endif
6032#ifdef FEAT_JOB_CHANNEL
6033 "channel",
6034#endif
6035#ifdef FEAT_CINDENT
6036 "cindent",
6037#endif
6038#ifdef FEAT_CLIENTSERVER
6039 "clientserver",
6040#endif
6041#ifdef FEAT_CLIPBOARD
6042 "clipboard",
6043#endif
6044#ifdef FEAT_CMDL_COMPL
6045 "cmdline_compl",
6046#endif
6047#ifdef FEAT_CMDHIST
6048 "cmdline_hist",
6049#endif
6050#ifdef FEAT_COMMENTS
6051 "comments",
6052#endif
6053#ifdef FEAT_CONCEAL
6054 "conceal",
6055#endif
6056#ifdef FEAT_CRYPT
6057 "cryptv",
6058 "crypt-blowfish",
6059 "crypt-blowfish2",
6060#endif
6061#ifdef FEAT_CSCOPE
6062 "cscope",
6063#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006065#ifdef CURSOR_SHAPE
6066 "cursorshape",
6067#endif
6068#ifdef DEBUG
6069 "debug",
6070#endif
6071#ifdef FEAT_CON_DIALOG
6072 "dialog_con",
6073#endif
6074#ifdef FEAT_GUI_DIALOG
6075 "dialog_gui",
6076#endif
6077#ifdef FEAT_DIFF
6078 "diff",
6079#endif
6080#ifdef FEAT_DIGRAPHS
6081 "digraphs",
6082#endif
6083#ifdef FEAT_DIRECTX
6084 "directx",
6085#endif
6086#ifdef FEAT_DND
6087 "dnd",
6088#endif
6089#ifdef FEAT_EMACS_TAGS
6090 "emacs_tags",
6091#endif
6092 "eval", /* always present, of course! */
6093 "ex_extra", /* graduated feature */
6094#ifdef FEAT_SEARCH_EXTRA
6095 "extra_search",
6096#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097#ifdef FEAT_SEARCHPATH
6098 "file_in_path",
6099#endif
6100#ifdef FEAT_FILTERPIPE
6101 "filterpipe",
6102#endif
6103#ifdef FEAT_FIND_ID
6104 "find_in_path",
6105#endif
6106#ifdef FEAT_FLOAT
6107 "float",
6108#endif
6109#ifdef FEAT_FOLDING
6110 "folding",
6111#endif
6112#ifdef FEAT_FOOTER
6113 "footer",
6114#endif
6115#if !defined(USE_SYSTEM) && defined(UNIX)
6116 "fork",
6117#endif
6118#ifdef FEAT_GETTEXT
6119 "gettext",
6120#endif
6121#ifdef FEAT_GUI
6122 "gui",
6123#endif
6124#ifdef FEAT_GUI_ATHENA
6125# ifdef FEAT_GUI_NEXTAW
6126 "gui_neXtaw",
6127# else
6128 "gui_athena",
6129# endif
6130#endif
6131#ifdef FEAT_GUI_GTK
6132 "gui_gtk",
6133# ifdef USE_GTK3
6134 "gui_gtk3",
6135# else
6136 "gui_gtk2",
6137# endif
6138#endif
6139#ifdef FEAT_GUI_GNOME
6140 "gui_gnome",
6141#endif
6142#ifdef FEAT_GUI_MAC
6143 "gui_mac",
6144#endif
6145#ifdef FEAT_GUI_MOTIF
6146 "gui_motif",
6147#endif
6148#ifdef FEAT_GUI_PHOTON
6149 "gui_photon",
6150#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006151#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152 "gui_win32",
6153#endif
6154#ifdef FEAT_HANGULIN
6155 "hangul_input",
6156#endif
6157#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6158 "iconv",
6159#endif
6160#ifdef FEAT_INS_EXPAND
6161 "insert_expand",
6162#endif
6163#ifdef FEAT_JOB_CHANNEL
6164 "job",
6165#endif
6166#ifdef FEAT_JUMPLIST
6167 "jumplist",
6168#endif
6169#ifdef FEAT_KEYMAP
6170 "keymap",
6171#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006172 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173#ifdef FEAT_LANGMAP
6174 "langmap",
6175#endif
6176#ifdef FEAT_LIBCALL
6177 "libcall",
6178#endif
6179#ifdef FEAT_LINEBREAK
6180 "linebreak",
6181#endif
6182#ifdef FEAT_LISP
6183 "lispindent",
6184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006185 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186#ifdef FEAT_LOCALMAP
6187 "localmap",
6188#endif
6189#ifdef FEAT_LUA
6190# ifndef DYNAMIC_LUA
6191 "lua",
6192# endif
6193#endif
6194#ifdef FEAT_MENU
6195 "menu",
6196#endif
6197#ifdef FEAT_SESSION
6198 "mksession",
6199#endif
6200#ifdef FEAT_MODIFY_FNAME
6201 "modify_fname",
6202#endif
6203#ifdef FEAT_MOUSE
6204 "mouse",
6205#endif
6206#ifdef FEAT_MOUSESHAPE
6207 "mouseshape",
6208#endif
6209#if defined(UNIX) || defined(VMS)
6210# ifdef FEAT_MOUSE_DEC
6211 "mouse_dec",
6212# endif
6213# ifdef FEAT_MOUSE_GPM
6214 "mouse_gpm",
6215# endif
6216# ifdef FEAT_MOUSE_JSB
6217 "mouse_jsbterm",
6218# endif
6219# ifdef FEAT_MOUSE_NET
6220 "mouse_netterm",
6221# endif
6222# ifdef FEAT_MOUSE_PTERM
6223 "mouse_pterm",
6224# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006225# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 "mouse_sgr",
6227# endif
6228# ifdef FEAT_SYSMOUSE
6229 "mouse_sysmouse",
6230# endif
6231# ifdef FEAT_MOUSE_URXVT
6232 "mouse_urxvt",
6233# endif
6234# ifdef FEAT_MOUSE_XTERM
6235 "mouse_xterm",
6236# endif
6237#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239#ifdef FEAT_MBYTE_IME
6240 "multi_byte_ime",
6241#endif
6242#ifdef FEAT_MULTI_LANG
6243 "multi_lang",
6244#endif
6245#ifdef FEAT_MZSCHEME
6246#ifndef DYNAMIC_MZSCHEME
6247 "mzscheme",
6248#endif
6249#endif
6250#ifdef FEAT_NUM64
6251 "num64",
6252#endif
6253#ifdef FEAT_OLE
6254 "ole",
6255#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006256#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006258#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259#ifdef FEAT_PATH_EXTRA
6260 "path_extra",
6261#endif
6262#ifdef FEAT_PERL
6263#ifndef DYNAMIC_PERL
6264 "perl",
6265#endif
6266#endif
6267#ifdef FEAT_PERSISTENT_UNDO
6268 "persistent_undo",
6269#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006270#if defined(FEAT_PYTHON)
6271 "python_compiled",
6272# if defined(DYNAMIC_PYTHON)
6273 "python_dynamic",
6274# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006276 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006277# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006279#if defined(FEAT_PYTHON3)
6280 "python3_compiled",
6281# if defined(DYNAMIC_PYTHON3)
6282 "python3_dynamic",
6283# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006285 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006286# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287#endif
6288#ifdef FEAT_POSTSCRIPT
6289 "postscript",
6290#endif
6291#ifdef FEAT_PRINTER
6292 "printer",
6293#endif
6294#ifdef FEAT_PROFILE
6295 "profile",
6296#endif
6297#ifdef FEAT_RELTIME
6298 "reltime",
6299#endif
6300#ifdef FEAT_QUICKFIX
6301 "quickfix",
6302#endif
6303#ifdef FEAT_RIGHTLEFT
6304 "rightleft",
6305#endif
6306#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6307 "ruby",
6308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006310#ifdef FEAT_CMDL_INFO
6311 "showcmd",
6312 "cmdline_info",
6313#endif
6314#ifdef FEAT_SIGNS
6315 "signs",
6316#endif
6317#ifdef FEAT_SMARTINDENT
6318 "smartindent",
6319#endif
6320#ifdef STARTUPTIME
6321 "startuptime",
6322#endif
6323#ifdef FEAT_STL_OPT
6324 "statusline",
6325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006326#ifdef FEAT_NETBEANS_INTG
6327 "netbeans_intg",
6328#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006329#ifdef FEAT_SOUND
6330 "sound",
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_SPELL
6333 "spell",
6334#endif
6335#ifdef FEAT_SYN_HL
6336 "syntax",
6337#endif
6338#if defined(USE_SYSTEM) || !defined(UNIX)
6339 "system",
6340#endif
6341#ifdef FEAT_TAG_BINS
6342 "tag_binary",
6343#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344#ifdef FEAT_TCL
6345# ifndef DYNAMIC_TCL
6346 "tcl",
6347# endif
6348#endif
6349#ifdef FEAT_TERMGUICOLORS
6350 "termguicolors",
6351#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006352#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006353 "terminal",
6354#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355#ifdef TERMINFO
6356 "terminfo",
6357#endif
6358#ifdef FEAT_TERMRESPONSE
6359 "termresponse",
6360#endif
6361#ifdef FEAT_TEXTOBJ
6362 "textobjects",
6363#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006364#ifdef FEAT_TEXT_PROP
6365 "textprop",
6366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367#ifdef HAVE_TGETENT
6368 "tgetent",
6369#endif
6370#ifdef FEAT_TIMERS
6371 "timers",
6372#endif
6373#ifdef FEAT_TITLE
6374 "title",
6375#endif
6376#ifdef FEAT_TOOLBAR
6377 "toolbar",
6378#endif
6379#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6380 "unnamedplus",
6381#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 "user-commands", /* was accidentally included in 5.4 */
6383 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006384#ifdef FEAT_VARTABS
6385 "vartabs",
6386#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006387 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388#ifdef FEAT_VIMINFO
6389 "viminfo",
6390#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006391 "vimscript-1",
6392 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006393 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006398#ifdef FEAT_VTP
6399 "vtp",
6400#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401#ifdef FEAT_WILDIGN
6402 "wildignore",
6403#endif
6404#ifdef FEAT_WILDMENU
6405 "wildmenu",
6406#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#ifdef FEAT_WAK
6409 "winaltkeys",
6410#endif
6411#ifdef FEAT_WRITEBACKUP
6412 "writebackup",
6413#endif
6414#ifdef FEAT_XIM
6415 "xim",
6416#endif
6417#ifdef FEAT_XFONTSET
6418 "xfontset",
6419#endif
6420#ifdef FEAT_XPM_W32
6421 "xpm",
6422 "xpm_w32", /* for backward compatibility */
6423#else
6424# if defined(HAVE_XPM)
6425 "xpm",
6426# endif
6427#endif
6428#ifdef USE_XSMP
6429 "xsmp",
6430#endif
6431#ifdef USE_XSMP_INTERACT
6432 "xsmp_interact",
6433#endif
6434#ifdef FEAT_XCLIPBOARD
6435 "xterm_clipboard",
6436#endif
6437#ifdef FEAT_XTERM_SAVE
6438 "xterm_save",
6439#endif
6440#if defined(UNIX) && defined(FEAT_X11)
6441 "X11",
6442#endif
6443 NULL
6444 };
6445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006446 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 for (i = 0; has_list[i] != NULL; ++i)
6448 if (STRICMP(name, has_list[i]) == 0)
6449 {
6450 n = TRUE;
6451 break;
6452 }
6453
6454 if (n == FALSE)
6455 {
6456 if (STRNICMP(name, "patch", 5) == 0)
6457 {
6458 if (name[5] == '-'
6459 && STRLEN(name) >= 11
6460 && vim_isdigit(name[6])
6461 && vim_isdigit(name[8])
6462 && vim_isdigit(name[10]))
6463 {
6464 int major = atoi((char *)name + 6);
6465 int minor = atoi((char *)name + 8);
6466
6467 /* Expect "patch-9.9.01234". */
6468 n = (major < VIM_VERSION_MAJOR
6469 || (major == VIM_VERSION_MAJOR
6470 && (minor < VIM_VERSION_MINOR
6471 || (minor == VIM_VERSION_MINOR
6472 && has_patch(atoi((char *)name + 10))))));
6473 }
6474 else
6475 n = has_patch(atoi((char *)name + 5));
6476 }
6477 else if (STRICMP(name, "vim_starting") == 0)
6478 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006479 else if (STRICMP(name, "ttyin") == 0)
6480 n = mch_input_isatty();
6481 else if (STRICMP(name, "ttyout") == 0)
6482 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 else if (STRICMP(name, "multi_byte_encoding") == 0)
6484 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006485#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 else if (STRICMP(name, "balloon_multiline") == 0)
6487 n = multiline_balloon_available();
6488#endif
6489#ifdef DYNAMIC_TCL
6490 else if (STRICMP(name, "tcl") == 0)
6491 n = tcl_enabled(FALSE);
6492#endif
6493#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6494 else if (STRICMP(name, "iconv") == 0)
6495 n = iconv_enabled(FALSE);
6496#endif
6497#ifdef DYNAMIC_LUA
6498 else if (STRICMP(name, "lua") == 0)
6499 n = lua_enabled(FALSE);
6500#endif
6501#ifdef DYNAMIC_MZSCHEME
6502 else if (STRICMP(name, "mzscheme") == 0)
6503 n = mzscheme_enabled(FALSE);
6504#endif
6505#ifdef DYNAMIC_RUBY
6506 else if (STRICMP(name, "ruby") == 0)
6507 n = ruby_enabled(FALSE);
6508#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509#ifdef DYNAMIC_PYTHON
6510 else if (STRICMP(name, "python") == 0)
6511 n = python_enabled(FALSE);
6512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef DYNAMIC_PYTHON3
6514 else if (STRICMP(name, "python3") == 0)
6515 n = python3_enabled(FALSE);
6516#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006517#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6518 else if (STRICMP(name, "pythonx") == 0)
6519 {
6520# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6521 if (p_pyx == 0)
6522 n = python3_enabled(FALSE) || python_enabled(FALSE);
6523 else if (p_pyx == 3)
6524 n = python3_enabled(FALSE);
6525 else if (p_pyx == 2)
6526 n = python_enabled(FALSE);
6527# elif defined(DYNAMIC_PYTHON)
6528 n = python_enabled(FALSE);
6529# elif defined(DYNAMIC_PYTHON3)
6530 n = python3_enabled(FALSE);
6531# endif
6532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533#endif
6534#ifdef DYNAMIC_PERL
6535 else if (STRICMP(name, "perl") == 0)
6536 n = perl_enabled(FALSE);
6537#endif
6538#ifdef FEAT_GUI
6539 else if (STRICMP(name, "gui_running") == 0)
6540 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541# ifdef FEAT_BROWSE
6542 else if (STRICMP(name, "browse") == 0)
6543 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6544# endif
6545#endif
6546#ifdef FEAT_SYN_HL
6547 else if (STRICMP(name, "syntax_items") == 0)
6548 n = syntax_present(curwin);
6549#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006550#ifdef FEAT_VTP
6551 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006552 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553#endif
6554#ifdef FEAT_NETBEANS_INTG
6555 else if (STRICMP(name, "netbeans_enabled") == 0)
6556 n = netbeans_active();
6557#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006558#ifdef FEAT_MOUSE_GPM
6559 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6560 n = gpm_enabled();
6561#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006562#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006563 else if (STRICMP(name, "terminal") == 0)
6564 n = terminal_enabled();
6565#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006566#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006567 else if (STRICMP(name, "conpty") == 0)
6568 n = use_conpty();
6569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 }
6571
6572 rettv->vval.v_number = n;
6573}
6574
6575/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 * "haslocaldir()" function
6577 */
6578 static void
6579f_haslocaldir(typval_T *argvars, typval_T *rettv)
6580{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006581 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 win_T *wp = NULL;
6583
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006584 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6585
6586 // Check for window-local and tab-local directories
6587 if (wp != NULL && wp->w_localdir != NULL)
6588 rettv->vval.v_number = 1;
6589 else if (tp != NULL && tp->tp_localdir != NULL)
6590 rettv->vval.v_number = 2;
6591 else
6592 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593}
6594
6595/*
6596 * "hasmapto()" function
6597 */
6598 static void
6599f_hasmapto(typval_T *argvars, typval_T *rettv)
6600{
6601 char_u *name;
6602 char_u *mode;
6603 char_u buf[NUMBUFLEN];
6604 int abbr = FALSE;
6605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006606 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 if (argvars[1].v_type == VAR_UNKNOWN)
6608 mode = (char_u *)"nvo";
6609 else
6610 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006611 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006613 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 }
6615
6616 if (map_to_exists(name, mode, abbr))
6617 rettv->vval.v_number = TRUE;
6618 else
6619 rettv->vval.v_number = FALSE;
6620}
6621
6622/*
6623 * "histadd()" function
6624 */
6625 static void
6626f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6627{
6628#ifdef FEAT_CMDHIST
6629 int histype;
6630 char_u *str;
6631 char_u buf[NUMBUFLEN];
6632#endif
6633
6634 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006635 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 return;
6637#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006638 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 histype = str != NULL ? get_histtype(str) : -1;
6640 if (histype >= 0)
6641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006642 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 if (*str != NUL)
6644 {
6645 init_history();
6646 add_to_history(histype, str, FALSE, NUL);
6647 rettv->vval.v_number = TRUE;
6648 return;
6649 }
6650 }
6651#endif
6652}
6653
6654/*
6655 * "histdel()" function
6656 */
6657 static void
6658f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6659{
6660#ifdef FEAT_CMDHIST
6661 int n;
6662 char_u buf[NUMBUFLEN];
6663 char_u *str;
6664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006665 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666 if (str == NULL)
6667 n = 0;
6668 else if (argvars[1].v_type == VAR_UNKNOWN)
6669 /* only one argument: clear entire history */
6670 n = clr_history(get_histtype(str));
6671 else if (argvars[1].v_type == VAR_NUMBER)
6672 /* index given: remove that entry */
6673 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006674 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 else
6676 /* string given: remove all matching entries */
6677 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006678 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 rettv->vval.v_number = n;
6680#endif
6681}
6682
6683/*
6684 * "histget()" function
6685 */
6686 static void
6687f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6688{
6689#ifdef FEAT_CMDHIST
6690 int type;
6691 int idx;
6692 char_u *str;
6693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006694 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 if (str == NULL)
6696 rettv->vval.v_string = NULL;
6697 else
6698 {
6699 type = get_histtype(str);
6700 if (argvars[1].v_type == VAR_UNKNOWN)
6701 idx = get_history_idx(type);
6702 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006703 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 /* -1 on type error */
6705 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6706 }
6707#else
6708 rettv->vval.v_string = NULL;
6709#endif
6710 rettv->v_type = VAR_STRING;
6711}
6712
6713/*
6714 * "histnr()" function
6715 */
6716 static void
6717f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6718{
6719 int i;
6720
6721#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006722 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723
6724 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6725 if (i >= HIST_CMD && i < HIST_COUNT)
6726 i = get_history_idx(i);
6727 else
6728#endif
6729 i = -1;
6730 rettv->vval.v_number = i;
6731}
6732
6733/*
6734 * "highlightID(name)" function
6735 */
6736 static void
6737f_hlID(typval_T *argvars, typval_T *rettv)
6738{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006739 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740}
6741
6742/*
6743 * "highlight_exists()" function
6744 */
6745 static void
6746f_hlexists(typval_T *argvars, typval_T *rettv)
6747{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006748 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749}
6750
6751/*
6752 * "hostname()" function
6753 */
6754 static void
6755f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6756{
6757 char_u hostname[256];
6758
6759 mch_get_host_name(hostname, 256);
6760 rettv->v_type = VAR_STRING;
6761 rettv->vval.v_string = vim_strsave(hostname);
6762}
6763
6764/*
6765 * iconv() function
6766 */
6767 static void
6768f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6769{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 char_u buf1[NUMBUFLEN];
6771 char_u buf2[NUMBUFLEN];
6772 char_u *from, *to, *str;
6773 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006774
6775 rettv->v_type = VAR_STRING;
6776 rettv->vval.v_string = NULL;
6777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006778 str = tv_get_string(&argvars[0]);
6779 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6780 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781 vimconv.vc_type = CONV_NONE;
6782 convert_setup(&vimconv, from, to);
6783
6784 /* If the encodings are equal, no conversion needed. */
6785 if (vimconv.vc_type == CONV_NONE)
6786 rettv->vval.v_string = vim_strsave(str);
6787 else
6788 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6789
6790 convert_setup(&vimconv, NULL, NULL);
6791 vim_free(from);
6792 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793}
6794
6795/*
6796 * "indent()" function
6797 */
6798 static void
6799f_indent(typval_T *argvars, typval_T *rettv)
6800{
6801 linenr_T lnum;
6802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006803 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6805 rettv->vval.v_number = get_indent_lnum(lnum);
6806 else
6807 rettv->vval.v_number = -1;
6808}
6809
6810/*
6811 * "index()" function
6812 */
6813 static void
6814f_index(typval_T *argvars, typval_T *rettv)
6815{
6816 list_T *l;
6817 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006818 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 long idx = 0;
6820 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006821 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822
6823 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006824 if (argvars[0].v_type == VAR_BLOB)
6825 {
6826 typval_T tv;
6827 int start = 0;
6828
6829 if (argvars[2].v_type != VAR_UNKNOWN)
6830 {
6831 start = tv_get_number_chk(&argvars[2], &error);
6832 if (error)
6833 return;
6834 }
6835 b = argvars[0].vval.v_blob;
6836 if (b == NULL)
6837 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006838 if (start < 0)
6839 {
6840 start = blob_len(b) + start;
6841 if (start < 0)
6842 start = 0;
6843 }
6844
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006845 for (idx = start; idx < blob_len(b); ++idx)
6846 {
6847 tv.v_type = VAR_NUMBER;
6848 tv.vval.v_number = blob_get(b, idx);
6849 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6850 {
6851 rettv->vval.v_number = idx;
6852 return;
6853 }
6854 }
6855 return;
6856 }
6857 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006858 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006859 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860 return;
6861 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863 l = argvars[0].vval.v_list;
6864 if (l != NULL)
6865 {
6866 item = l->lv_first;
6867 if (argvars[2].v_type != VAR_UNKNOWN)
6868 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 /* Start at specified item. Use the cached index that list_find()
6870 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 idx = l->lv_idx;
6873 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006874 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 if (error)
6876 item = NULL;
6877 }
6878
6879 for ( ; item != NULL; item = item->li_next, ++idx)
6880 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6881 {
6882 rettv->vval.v_number = idx;
6883 break;
6884 }
6885 }
6886}
6887
6888static int inputsecret_flag = 0;
6889
6890/*
6891 * "input()" function
6892 * Also handles inputsecret() when inputsecret is set.
6893 */
6894 static void
6895f_input(typval_T *argvars, typval_T *rettv)
6896{
6897 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6898}
6899
6900/*
6901 * "inputdialog()" function
6902 */
6903 static void
6904f_inputdialog(typval_T *argvars, typval_T *rettv)
6905{
6906#if defined(FEAT_GUI_TEXTDIALOG)
6907 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6908 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6909 {
6910 char_u *message;
6911 char_u buf[NUMBUFLEN];
6912 char_u *defstr = (char_u *)"";
6913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006914 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006916 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6918 else
6919 IObuff[0] = NUL;
6920 if (message != NULL && defstr != NULL
6921 && do_dialog(VIM_QUESTION, NULL, message,
6922 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6923 rettv->vval.v_string = vim_strsave(IObuff);
6924 else
6925 {
6926 if (message != NULL && defstr != NULL
6927 && argvars[1].v_type != VAR_UNKNOWN
6928 && argvars[2].v_type != VAR_UNKNOWN)
6929 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006930 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 else
6932 rettv->vval.v_string = NULL;
6933 }
6934 rettv->v_type = VAR_STRING;
6935 }
6936 else
6937#endif
6938 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6939}
6940
6941/*
6942 * "inputlist()" function
6943 */
6944 static void
6945f_inputlist(typval_T *argvars, typval_T *rettv)
6946{
6947 listitem_T *li;
6948 int selected;
6949 int mouse_used;
6950
6951#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006952 /* While starting up, there is no place to enter text. When running tests
6953 * with --not-a-term we assume feedkeys() will be used. */
6954 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006955 return;
6956#endif
6957 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006959 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006960 return;
6961 }
6962
6963 msg_start();
6964 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6965 lines_left = Rows; /* avoid more prompt */
6966 msg_scroll = TRUE;
6967 msg_clr_eos();
6968
6969 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6970 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006971 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 msg_putchar('\n');
6973 }
6974
6975 /* Ask for choice. */
6976 selected = prompt_for_number(&mouse_used);
6977 if (mouse_used)
6978 selected -= lines_left;
6979
6980 rettv->vval.v_number = selected;
6981}
6982
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006983static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6984
6985/*
6986 * "inputrestore()" function
6987 */
6988 static void
6989f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6990{
6991 if (ga_userinput.ga_len > 0)
6992 {
6993 --ga_userinput.ga_len;
6994 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6995 + ga_userinput.ga_len);
6996 /* default return is zero == OK */
6997 }
6998 else if (p_verbose > 1)
6999 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007000 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 rettv->vval.v_number = 1; /* Failed */
7002 }
7003}
7004
7005/*
7006 * "inputsave()" function
7007 */
7008 static void
7009f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7010{
7011 /* Add an entry to the stack of typeahead storage. */
7012 if (ga_grow(&ga_userinput, 1) == OK)
7013 {
7014 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7015 + ga_userinput.ga_len);
7016 ++ga_userinput.ga_len;
7017 /* default return is zero == OK */
7018 }
7019 else
7020 rettv->vval.v_number = 1; /* Failed */
7021}
7022
7023/*
7024 * "inputsecret()" function
7025 */
7026 static void
7027f_inputsecret(typval_T *argvars, typval_T *rettv)
7028{
7029 ++cmdline_star;
7030 ++inputsecret_flag;
7031 f_input(argvars, rettv);
7032 --cmdline_star;
7033 --inputsecret_flag;
7034}
7035
7036/*
7037 * "insert()" function
7038 */
7039 static void
7040f_insert(typval_T *argvars, typval_T *rettv)
7041{
7042 long before = 0;
7043 listitem_T *item;
7044 list_T *l;
7045 int error = FALSE;
7046
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007047 if (argvars[0].v_type == VAR_BLOB)
7048 {
7049 int val, len;
7050 char_u *p;
7051
7052 len = blob_len(argvars[0].vval.v_blob);
7053 if (argvars[2].v_type != VAR_UNKNOWN)
7054 {
7055 before = (long)tv_get_number_chk(&argvars[2], &error);
7056 if (error)
7057 return; // type error; errmsg already given
7058 if (before < 0 || before > len)
7059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007061 return;
7062 }
7063 }
7064 val = tv_get_number_chk(&argvars[1], &error);
7065 if (error)
7066 return;
7067 if (val < 0 || val > 255)
7068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007069 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007070 return;
7071 }
7072
7073 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7074 return;
7075 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7076 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7077 *(p + before) = val;
7078 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7079
7080 copy_tv(&argvars[0], rettv);
7081 }
7082 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007083 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007084 else if ((l = argvars[0].vval.v_list) != NULL
7085 && !var_check_lock(l->lv_lock,
7086 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007087 {
7088 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007089 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 if (error)
7091 return; /* type error; errmsg already given */
7092
7093 if (before == l->lv_len)
7094 item = NULL;
7095 else
7096 {
7097 item = list_find(l, before);
7098 if (item == NULL)
7099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007100 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 l = NULL;
7102 }
7103 }
7104 if (l != NULL)
7105 {
7106 list_insert_tv(l, &argvars[1], item);
7107 copy_tv(&argvars[0], rettv);
7108 }
7109 }
7110}
7111
7112/*
7113 * "invert(expr)" function
7114 */
7115 static void
7116f_invert(typval_T *argvars, typval_T *rettv)
7117{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119}
7120
7121/*
7122 * "isdirectory()" function
7123 */
7124 static void
7125f_isdirectory(typval_T *argvars, typval_T *rettv)
7126{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007127 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128}
7129
7130/*
7131 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7132 * or it refers to a List or Dictionary that is locked.
7133 */
7134 static int
7135tv_islocked(typval_T *tv)
7136{
7137 return (tv->v_lock & VAR_LOCKED)
7138 || (tv->v_type == VAR_LIST
7139 && tv->vval.v_list != NULL
7140 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7141 || (tv->v_type == VAR_DICT
7142 && tv->vval.v_dict != NULL
7143 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7144}
7145
7146/*
7147 * "islocked()" function
7148 */
7149 static void
7150f_islocked(typval_T *argvars, typval_T *rettv)
7151{
7152 lval_T lv;
7153 char_u *end;
7154 dictitem_T *di;
7155
7156 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007157 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007158 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 if (end != NULL && lv.ll_name != NULL)
7160 {
7161 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007162 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 else
7164 {
7165 if (lv.ll_tv == NULL)
7166 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007167 di = find_var(lv.ll_name, NULL, TRUE);
7168 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007170 /* Consider a variable locked when:
7171 * 1. the variable itself is locked
7172 * 2. the value of the variable is locked.
7173 * 3. the List or Dict value is locked.
7174 */
7175 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7176 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177 }
7178 }
7179 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007180 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007182 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007183 else if (lv.ll_list != NULL)
7184 /* List item. */
7185 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7186 else
7187 /* Dictionary item. */
7188 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7189 }
7190 }
7191
7192 clear_lval(&lv);
7193}
7194
7195#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7196/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007197 * "isinf()" function
7198 */
7199 static void
7200f_isinf(typval_T *argvars, typval_T *rettv)
7201{
7202 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7203 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7204}
7205
7206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 * "isnan()" function
7208 */
7209 static void
7210f_isnan(typval_T *argvars, typval_T *rettv)
7211{
7212 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7213 && isnan(argvars[0].vval.v_float);
7214}
7215#endif
7216
7217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 * "last_buffer_nr()" function.
7219 */
7220 static void
7221f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7222{
7223 int n = 0;
7224 buf_T *buf;
7225
Bram Moolenaar29323592016-07-24 22:04:11 +02007226 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 if (n < buf->b_fnum)
7228 n = buf->b_fnum;
7229
7230 rettv->vval.v_number = n;
7231}
7232
7233/*
7234 * "len()" function
7235 */
7236 static void
7237f_len(typval_T *argvars, typval_T *rettv)
7238{
7239 switch (argvars[0].v_type)
7240 {
7241 case VAR_STRING:
7242 case VAR_NUMBER:
7243 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 case VAR_BLOB:
7247 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7248 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007249 case VAR_LIST:
7250 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7251 break;
7252 case VAR_DICT:
7253 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7254 break;
7255 case VAR_UNKNOWN:
7256 case VAR_SPECIAL:
7257 case VAR_FLOAT:
7258 case VAR_FUNC:
7259 case VAR_PARTIAL:
7260 case VAR_JOB:
7261 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007262 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263 break;
7264 }
7265}
7266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007268libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269{
7270#ifdef FEAT_LIBCALL
7271 char_u *string_in;
7272 char_u **string_result;
7273 int nr_result;
7274#endif
7275
7276 rettv->v_type = type;
7277 if (type != VAR_NUMBER)
7278 rettv->vval.v_string = NULL;
7279
7280 if (check_restricted() || check_secure())
7281 return;
7282
7283#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007284 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007285 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7286 {
7287 string_in = NULL;
7288 if (argvars[2].v_type == VAR_STRING)
7289 string_in = argvars[2].vval.v_string;
7290 if (type == VAR_NUMBER)
7291 string_result = NULL;
7292 else
7293 string_result = &rettv->vval.v_string;
7294 if (mch_libcall(argvars[0].vval.v_string,
7295 argvars[1].vval.v_string,
7296 string_in,
7297 argvars[2].vval.v_number,
7298 string_result,
7299 &nr_result) == OK
7300 && type == VAR_NUMBER)
7301 rettv->vval.v_number = nr_result;
7302 }
7303#endif
7304}
7305
7306/*
7307 * "libcall()" function
7308 */
7309 static void
7310f_libcall(typval_T *argvars, typval_T *rettv)
7311{
7312 libcall_common(argvars, rettv, VAR_STRING);
7313}
7314
7315/*
7316 * "libcallnr()" function
7317 */
7318 static void
7319f_libcallnr(typval_T *argvars, typval_T *rettv)
7320{
7321 libcall_common(argvars, rettv, VAR_NUMBER);
7322}
7323
7324/*
7325 * "line(string)" function
7326 */
7327 static void
7328f_line(typval_T *argvars, typval_T *rettv)
7329{
7330 linenr_T lnum = 0;
7331 pos_T *fp;
7332 int fnum;
7333
7334 fp = var2fpos(&argvars[0], TRUE, &fnum);
7335 if (fp != NULL)
7336 lnum = fp->lnum;
7337 rettv->vval.v_number = lnum;
7338}
7339
7340/*
7341 * "line2byte(lnum)" function
7342 */
7343 static void
7344f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7345{
7346#ifndef FEAT_BYTEOFF
7347 rettv->vval.v_number = -1;
7348#else
7349 linenr_T lnum;
7350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7353 rettv->vval.v_number = -1;
7354 else
7355 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7356 if (rettv->vval.v_number >= 0)
7357 ++rettv->vval.v_number;
7358#endif
7359}
7360
7361/*
7362 * "lispindent(lnum)" function
7363 */
7364 static void
7365f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7366{
7367#ifdef FEAT_LISP
7368 pos_T pos;
7369 linenr_T lnum;
7370
7371 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007372 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7374 {
7375 curwin->w_cursor.lnum = lnum;
7376 rettv->vval.v_number = get_lisp_indent();
7377 curwin->w_cursor = pos;
7378 }
7379 else
7380#endif
7381 rettv->vval.v_number = -1;
7382}
7383
7384/*
7385 * "localtime()" function
7386 */
7387 static void
7388f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7389{
7390 rettv->vval.v_number = (varnumber_T)time(NULL);
7391}
7392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 static void
7394get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7395{
7396 char_u *keys;
7397 char_u *which;
7398 char_u buf[NUMBUFLEN];
7399 char_u *keys_buf = NULL;
7400 char_u *rhs;
7401 int mode;
7402 int abbr = FALSE;
7403 int get_dict = FALSE;
7404 mapblock_T *mp;
7405 int buffer_local;
7406
7407 /* return empty string for failure */
7408 rettv->v_type = VAR_STRING;
7409 rettv->vval.v_string = NULL;
7410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007411 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 if (*keys == NUL)
7413 return;
7414
7415 if (argvars[1].v_type != VAR_UNKNOWN)
7416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007417 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418 if (argvars[2].v_type != VAR_UNKNOWN)
7419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007422 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 }
7424 }
7425 else
7426 which = (char_u *)"";
7427 if (which == NULL)
7428 return;
7429
7430 mode = get_map_mode(&which, 0);
7431
7432 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7433 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7434 vim_free(keys_buf);
7435
7436 if (!get_dict)
7437 {
7438 /* Return a string. */
7439 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007440 {
7441 if (*rhs == NUL)
7442 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7443 else
7444 rettv->vval.v_string = str2special_save(rhs, FALSE);
7445 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446
7447 }
7448 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7449 {
7450 /* Return a dictionary. */
7451 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7452 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7453 dict_T *dict = rettv->vval.v_dict;
7454
Bram Moolenaare0be1672018-07-08 16:50:37 +02007455 dict_add_string(dict, "lhs", lhs);
7456 dict_add_string(dict, "rhs", mp->m_orig_str);
7457 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7458 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7459 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007460 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7461 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007462 dict_add_number(dict, "buffer", (long)buffer_local);
7463 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7464 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465
7466 vim_free(lhs);
7467 vim_free(mapmode);
7468 }
7469}
7470
7471#ifdef FEAT_FLOAT
7472/*
7473 * "log()" function
7474 */
7475 static void
7476f_log(typval_T *argvars, typval_T *rettv)
7477{
7478 float_T f = 0.0;
7479
7480 rettv->v_type = VAR_FLOAT;
7481 if (get_float_arg(argvars, &f) == OK)
7482 rettv->vval.v_float = log(f);
7483 else
7484 rettv->vval.v_float = 0.0;
7485}
7486
7487/*
7488 * "log10()" function
7489 */
7490 static void
7491f_log10(typval_T *argvars, typval_T *rettv)
7492{
7493 float_T f = 0.0;
7494
7495 rettv->v_type = VAR_FLOAT;
7496 if (get_float_arg(argvars, &f) == OK)
7497 rettv->vval.v_float = log10(f);
7498 else
7499 rettv->vval.v_float = 0.0;
7500}
7501#endif
7502
7503#ifdef FEAT_LUA
7504/*
7505 * "luaeval()" function
7506 */
7507 static void
7508f_luaeval(typval_T *argvars, typval_T *rettv)
7509{
7510 char_u *str;
7511 char_u buf[NUMBUFLEN];
7512
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007513 if (check_restricted() || check_secure())
7514 return;
7515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007516 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 do_luaeval(str, argvars + 1, rettv);
7518}
7519#endif
7520
7521/*
7522 * "map()" function
7523 */
7524 static void
7525f_map(typval_T *argvars, typval_T *rettv)
7526{
7527 filter_map(argvars, rettv, TRUE);
7528}
7529
7530/*
7531 * "maparg()" function
7532 */
7533 static void
7534f_maparg(typval_T *argvars, typval_T *rettv)
7535{
7536 get_maparg(argvars, rettv, TRUE);
7537}
7538
7539/*
7540 * "mapcheck()" function
7541 */
7542 static void
7543f_mapcheck(typval_T *argvars, typval_T *rettv)
7544{
7545 get_maparg(argvars, rettv, FALSE);
7546}
7547
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007548typedef enum
7549{
7550 MATCH_END, /* matchend() */
7551 MATCH_MATCH, /* match() */
7552 MATCH_STR, /* matchstr() */
7553 MATCH_LIST, /* matchlist() */
7554 MATCH_POS /* matchstrpos() */
7555} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556
7557 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007558find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559{
7560 char_u *str = NULL;
7561 long len = 0;
7562 char_u *expr = NULL;
7563 char_u *pat;
7564 regmatch_T regmatch;
7565 char_u patbuf[NUMBUFLEN];
7566 char_u strbuf[NUMBUFLEN];
7567 char_u *save_cpo;
7568 long start = 0;
7569 long nth = 1;
7570 colnr_T startcol = 0;
7571 int match = 0;
7572 list_T *l = NULL;
7573 listitem_T *li = NULL;
7574 long idx = 0;
7575 char_u *tofree = NULL;
7576
7577 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7578 save_cpo = p_cpo;
7579 p_cpo = (char_u *)"";
7580
7581 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007582 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007584 /* type MATCH_LIST: return empty list when there are no matches.
7585 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586 if (rettv_list_alloc(rettv) == FAIL)
7587 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007588 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 && (list_append_string(rettv->vval.v_list,
7590 (char_u *)"", 0) == FAIL
7591 || list_append_number(rettv->vval.v_list,
7592 (varnumber_T)-1) == FAIL
7593 || list_append_number(rettv->vval.v_list,
7594 (varnumber_T)-1) == FAIL
7595 || list_append_number(rettv->vval.v_list,
7596 (varnumber_T)-1) == FAIL))
7597 {
7598 list_free(rettv->vval.v_list);
7599 rettv->vval.v_list = NULL;
7600 goto theend;
7601 }
7602 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007603 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007604 {
7605 rettv->v_type = VAR_STRING;
7606 rettv->vval.v_string = NULL;
7607 }
7608
7609 if (argvars[0].v_type == VAR_LIST)
7610 {
7611 if ((l = argvars[0].vval.v_list) == NULL)
7612 goto theend;
7613 li = l->lv_first;
7614 }
7615 else
7616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007617 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 len = (long)STRLEN(str);
7619 }
7620
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007621 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 if (pat == NULL)
7623 goto theend;
7624
7625 if (argvars[2].v_type != VAR_UNKNOWN)
7626 {
7627 int error = FALSE;
7628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 if (error)
7631 goto theend;
7632 if (l != NULL)
7633 {
7634 li = list_find(l, start);
7635 if (li == NULL)
7636 goto theend;
7637 idx = l->lv_idx; /* use the cached index */
7638 }
7639 else
7640 {
7641 if (start < 0)
7642 start = 0;
7643 if (start > len)
7644 goto theend;
7645 /* When "count" argument is there ignore matches before "start",
7646 * otherwise skip part of the string. Differs when pattern is "^"
7647 * or "\<". */
7648 if (argvars[3].v_type != VAR_UNKNOWN)
7649 startcol = start;
7650 else
7651 {
7652 str += start;
7653 len -= start;
7654 }
7655 }
7656
7657 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007658 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 if (error)
7660 goto theend;
7661 }
7662
7663 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7664 if (regmatch.regprog != NULL)
7665 {
7666 regmatch.rm_ic = p_ic;
7667
7668 for (;;)
7669 {
7670 if (l != NULL)
7671 {
7672 if (li == NULL)
7673 {
7674 match = FALSE;
7675 break;
7676 }
7677 vim_free(tofree);
7678 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7679 if (str == NULL)
7680 break;
7681 }
7682
7683 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7684
7685 if (match && --nth <= 0)
7686 break;
7687 if (l == NULL && !match)
7688 break;
7689
7690 /* Advance to just after the match. */
7691 if (l != NULL)
7692 {
7693 li = li->li_next;
7694 ++idx;
7695 }
7696 else
7697 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007698 startcol = (colnr_T)(regmatch.startp[0]
7699 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 if (startcol > (colnr_T)len
7701 || str + startcol <= regmatch.startp[0])
7702 {
7703 match = FALSE;
7704 break;
7705 }
7706 }
7707 }
7708
7709 if (match)
7710 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007711 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 {
7713 listitem_T *li1 = rettv->vval.v_list->lv_first;
7714 listitem_T *li2 = li1->li_next;
7715 listitem_T *li3 = li2->li_next;
7716 listitem_T *li4 = li3->li_next;
7717
7718 vim_free(li1->li_tv.vval.v_string);
7719 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7720 (int)(regmatch.endp[0] - regmatch.startp[0]));
7721 li3->li_tv.vval.v_number =
7722 (varnumber_T)(regmatch.startp[0] - expr);
7723 li4->li_tv.vval.v_number =
7724 (varnumber_T)(regmatch.endp[0] - expr);
7725 if (l != NULL)
7726 li2->li_tv.vval.v_number = (varnumber_T)idx;
7727 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007728 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 {
7730 int i;
7731
7732 /* return list with matched string and submatches */
7733 for (i = 0; i < NSUBEXP; ++i)
7734 {
7735 if (regmatch.endp[i] == NULL)
7736 {
7737 if (list_append_string(rettv->vval.v_list,
7738 (char_u *)"", 0) == FAIL)
7739 break;
7740 }
7741 else if (list_append_string(rettv->vval.v_list,
7742 regmatch.startp[i],
7743 (int)(regmatch.endp[i] - regmatch.startp[i]))
7744 == FAIL)
7745 break;
7746 }
7747 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007748 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 {
7750 /* return matched string */
7751 if (l != NULL)
7752 copy_tv(&li->li_tv, rettv);
7753 else
7754 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7755 (int)(regmatch.endp[0] - regmatch.startp[0]));
7756 }
7757 else if (l != NULL)
7758 rettv->vval.v_number = idx;
7759 else
7760 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007761 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 rettv->vval.v_number =
7763 (varnumber_T)(regmatch.startp[0] - str);
7764 else
7765 rettv->vval.v_number =
7766 (varnumber_T)(regmatch.endp[0] - str);
7767 rettv->vval.v_number += (varnumber_T)(str - expr);
7768 }
7769 }
7770 vim_regfree(regmatch.regprog);
7771 }
7772
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007773theend:
7774 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775 /* matchstrpos() without a list: drop the second item. */
7776 listitem_remove(rettv->vval.v_list,
7777 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 vim_free(tofree);
7779 p_cpo = save_cpo;
7780}
7781
7782/*
7783 * "match()" function
7784 */
7785 static void
7786f_match(typval_T *argvars, typval_T *rettv)
7787{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007788 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789}
7790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791/*
7792 * "matchend()" function
7793 */
7794 static void
7795f_matchend(typval_T *argvars, typval_T *rettv)
7796{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007797 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798}
7799
7800/*
7801 * "matchlist()" function
7802 */
7803 static void
7804f_matchlist(typval_T *argvars, typval_T *rettv)
7805{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007806 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807}
7808
7809/*
7810 * "matchstr()" function
7811 */
7812 static void
7813f_matchstr(typval_T *argvars, typval_T *rettv)
7814{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007815 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816}
7817
7818/*
7819 * "matchstrpos()" function
7820 */
7821 static void
7822f_matchstrpos(typval_T *argvars, typval_T *rettv)
7823{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007824 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825}
7826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 static void
7828max_min(typval_T *argvars, typval_T *rettv, int domax)
7829{
7830 varnumber_T n = 0;
7831 varnumber_T i;
7832 int error = FALSE;
7833
7834 if (argvars[0].v_type == VAR_LIST)
7835 {
7836 list_T *l;
7837 listitem_T *li;
7838
7839 l = argvars[0].vval.v_list;
7840 if (l != NULL)
7841 {
7842 li = l->lv_first;
7843 if (li != NULL)
7844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007845 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 for (;;)
7847 {
7848 li = li->li_next;
7849 if (li == NULL)
7850 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 if (domax ? i > n : i < n)
7853 n = i;
7854 }
7855 }
7856 }
7857 }
7858 else if (argvars[0].v_type == VAR_DICT)
7859 {
7860 dict_T *d;
7861 int first = TRUE;
7862 hashitem_T *hi;
7863 int todo;
7864
7865 d = argvars[0].vval.v_dict;
7866 if (d != NULL)
7867 {
7868 todo = (int)d->dv_hashtab.ht_used;
7869 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7870 {
7871 if (!HASHITEM_EMPTY(hi))
7872 {
7873 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007874 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 if (first)
7876 {
7877 n = i;
7878 first = FALSE;
7879 }
7880 else if (domax ? i > n : i < n)
7881 n = i;
7882 }
7883 }
7884 }
7885 }
7886 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007887 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 rettv->vval.v_number = error ? 0 : n;
7889}
7890
7891/*
7892 * "max()" function
7893 */
7894 static void
7895f_max(typval_T *argvars, typval_T *rettv)
7896{
7897 max_min(argvars, rettv, TRUE);
7898}
7899
7900/*
7901 * "min()" function
7902 */
7903 static void
7904f_min(typval_T *argvars, typval_T *rettv)
7905{
7906 max_min(argvars, rettv, FALSE);
7907}
7908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909/*
7910 * Create the directory in which "dir" is located, and higher levels when
7911 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007912 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 */
7914 static int
7915mkdir_recurse(char_u *dir, int prot)
7916{
7917 char_u *p;
7918 char_u *updir;
7919 int r = FAIL;
7920
7921 /* Get end of directory name in "dir".
7922 * We're done when it's "/" or "c:/". */
7923 p = gettail_sep(dir);
7924 if (p <= get_past_head(dir))
7925 return OK;
7926
7927 /* If the directory exists we're done. Otherwise: create it.*/
7928 updir = vim_strnsave(dir, (int)(p - dir));
7929 if (updir == NULL)
7930 return FAIL;
7931 if (mch_isdir(updir))
7932 r = OK;
7933 else if (mkdir_recurse(updir, prot) == OK)
7934 r = vim_mkdir_emsg(updir, prot);
7935 vim_free(updir);
7936 return r;
7937}
7938
7939#ifdef vim_mkdir
7940/*
7941 * "mkdir()" function
7942 */
7943 static void
7944f_mkdir(typval_T *argvars, typval_T *rettv)
7945{
7946 char_u *dir;
7947 char_u buf[NUMBUFLEN];
7948 int prot = 0755;
7949
7950 rettv->vval.v_number = FAIL;
7951 if (check_restricted() || check_secure())
7952 return;
7953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007954 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007956 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007958 if (*gettail(dir) == NUL)
7959 /* remove trailing slashes */
7960 *gettail_sep(dir) = NUL;
7961
7962 if (argvars[1].v_type != VAR_UNKNOWN)
7963 {
7964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007966 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007967 if (prot == -1)
7968 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007970 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007971 {
7972 if (mch_isdir(dir))
7973 {
7974 /* With the "p" flag it's OK if the dir already exists. */
7975 rettv->vval.v_number = OK;
7976 return;
7977 }
7978 mkdir_recurse(dir, prot);
7979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007981 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982}
7983#endif
7984
7985/*
7986 * "mode()" function
7987 */
7988 static void
7989f_mode(typval_T *argvars, typval_T *rettv)
7990{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007991 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992
Bram Moolenaar612cc382018-07-29 15:34:26 +02007993 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994
7995 if (time_for_testing == 93784)
7996 {
7997 /* Testing the two-character code. */
7998 buf[0] = 'x';
7999 buf[1] = '!';
8000 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008001#ifdef FEAT_TERMINAL
8002 else if (term_use_loop())
8003 buf[0] = 't';
8004#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 else if (VIsual_active)
8006 {
8007 if (VIsual_select)
8008 buf[0] = VIsual_mode + 's' - 'v';
8009 else
8010 buf[0] = VIsual_mode;
8011 }
8012 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8013 || State == CONFIRM)
8014 {
8015 buf[0] = 'r';
8016 if (State == ASKMORE)
8017 buf[1] = 'm';
8018 else if (State == CONFIRM)
8019 buf[1] = '?';
8020 }
8021 else if (State == EXTERNCMD)
8022 buf[0] = '!';
8023 else if (State & INSERT)
8024 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 if (State & VREPLACE_FLAG)
8026 {
8027 buf[0] = 'R';
8028 buf[1] = 'v';
8029 }
8030 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008031 {
8032 if (State & REPLACE_FLAG)
8033 buf[0] = 'R';
8034 else
8035 buf[0] = 'i';
8036#ifdef FEAT_INS_EXPAND
8037 if (ins_compl_active())
8038 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008039 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008040 buf[1] = 'x';
8041#endif
8042 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008044 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 {
8046 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008047 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008049 else if (exmode_active == EXMODE_NORMAL)
8050 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 }
8052 else
8053 {
8054 buf[0] = 'n';
8055 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008056 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008058 // to be able to detect force-linewise/blockwise/characterwise operations
8059 buf[2] = motion_force;
8060 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008061 else if (restart_edit == 'I' || restart_edit == 'R'
8062 || restart_edit == 'V')
8063 {
8064 buf[1] = 'i';
8065 buf[2] = restart_edit;
8066 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067 }
8068
8069 /* Clear out the minor mode when the argument is not a non-zero number or
8070 * non-empty string. */
8071 if (!non_zero_arg(&argvars[0]))
8072 buf[1] = NUL;
8073
8074 rettv->vval.v_string = vim_strsave(buf);
8075 rettv->v_type = VAR_STRING;
8076}
8077
8078#if defined(FEAT_MZSCHEME) || defined(PROTO)
8079/*
8080 * "mzeval()" function
8081 */
8082 static void
8083f_mzeval(typval_T *argvars, typval_T *rettv)
8084{
8085 char_u *str;
8086 char_u buf[NUMBUFLEN];
8087
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008088 if (check_restricted() || check_secure())
8089 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008090 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 do_mzeval(str, rettv);
8092}
8093
8094 void
8095mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8096{
8097 typval_T argvars[3];
8098
8099 argvars[0].v_type = VAR_STRING;
8100 argvars[0].vval.v_string = name;
8101 copy_tv(args, &argvars[1]);
8102 argvars[2].v_type = VAR_UNKNOWN;
8103 f_call(argvars, rettv);
8104 clear_tv(&argvars[1]);
8105}
8106#endif
8107
8108/*
8109 * "nextnonblank()" function
8110 */
8111 static void
8112f_nextnonblank(typval_T *argvars, typval_T *rettv)
8113{
8114 linenr_T lnum;
8115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008116 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 {
8118 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8119 {
8120 lnum = 0;
8121 break;
8122 }
8123 if (*skipwhite(ml_get(lnum)) != NUL)
8124 break;
8125 }
8126 rettv->vval.v_number = lnum;
8127}
8128
8129/*
8130 * "nr2char()" function
8131 */
8132 static void
8133f_nr2char(typval_T *argvars, typval_T *rettv)
8134{
8135 char_u buf[NUMBUFLEN];
8136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 if (has_mbyte)
8138 {
8139 int utf8 = 0;
8140
8141 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008142 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008144 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008145 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008146 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 }
8148 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008150 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 buf[1] = NUL;
8152 }
8153 rettv->v_type = VAR_STRING;
8154 rettv->vval.v_string = vim_strsave(buf);
8155}
8156
8157/*
8158 * "or(expr, expr)" function
8159 */
8160 static void
8161f_or(typval_T *argvars, typval_T *rettv)
8162{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008163 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8164 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165}
8166
8167/*
8168 * "pathshorten()" function
8169 */
8170 static void
8171f_pathshorten(typval_T *argvars, typval_T *rettv)
8172{
8173 char_u *p;
8174
8175 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008176 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 if (p == NULL)
8178 rettv->vval.v_string = NULL;
8179 else
8180 {
8181 p = vim_strsave(p);
8182 rettv->vval.v_string = p;
8183 if (p != NULL)
8184 shorten_dir(p);
8185 }
8186}
8187
8188#ifdef FEAT_PERL
8189/*
8190 * "perleval()" function
8191 */
8192 static void
8193f_perleval(typval_T *argvars, typval_T *rettv)
8194{
8195 char_u *str;
8196 char_u buf[NUMBUFLEN];
8197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008198 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 do_perleval(str, rettv);
8200}
8201#endif
8202
8203#ifdef FEAT_FLOAT
8204/*
8205 * "pow()" function
8206 */
8207 static void
8208f_pow(typval_T *argvars, typval_T *rettv)
8209{
8210 float_T fx = 0.0, fy = 0.0;
8211
8212 rettv->v_type = VAR_FLOAT;
8213 if (get_float_arg(argvars, &fx) == OK
8214 && get_float_arg(&argvars[1], &fy) == OK)
8215 rettv->vval.v_float = pow(fx, fy);
8216 else
8217 rettv->vval.v_float = 0.0;
8218}
8219#endif
8220
8221/*
8222 * "prevnonblank()" function
8223 */
8224 static void
8225f_prevnonblank(typval_T *argvars, typval_T *rettv)
8226{
8227 linenr_T lnum;
8228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008229 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8231 lnum = 0;
8232 else
8233 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8234 --lnum;
8235 rettv->vval.v_number = lnum;
8236}
8237
8238/* This dummy va_list is here because:
8239 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8240 * - locally in the function results in a "used before set" warning
8241 * - using va_start() to initialize it gives "function with fixed args" error */
8242static va_list ap;
8243
8244/*
8245 * "printf()" function
8246 */
8247 static void
8248f_printf(typval_T *argvars, typval_T *rettv)
8249{
8250 char_u buf[NUMBUFLEN];
8251 int len;
8252 char_u *s;
8253 int saved_did_emsg = did_emsg;
8254 char *fmt;
8255
8256 rettv->v_type = VAR_STRING;
8257 rettv->vval.v_string = NULL;
8258
8259 /* Get the required length, allocate the buffer and do it for real. */
8260 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008261 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008262 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 if (!did_emsg)
8264 {
8265 s = alloc(len + 1);
8266 if (s != NULL)
8267 {
8268 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008269 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8270 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008271 }
8272 }
8273 did_emsg |= saved_did_emsg;
8274}
8275
8276/*
8277 * "pumvisible()" function
8278 */
8279 static void
8280f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8281{
8282#ifdef FEAT_INS_EXPAND
8283 if (pum_visible())
8284 rettv->vval.v_number = 1;
8285#endif
8286}
8287
8288#ifdef FEAT_PYTHON3
8289/*
8290 * "py3eval()" function
8291 */
8292 static void
8293f_py3eval(typval_T *argvars, typval_T *rettv)
8294{
8295 char_u *str;
8296 char_u buf[NUMBUFLEN];
8297
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008298 if (check_restricted() || check_secure())
8299 return;
8300
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008301 if (p_pyx == 0)
8302 p_pyx = 3;
8303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 do_py3eval(str, rettv);
8306}
8307#endif
8308
8309#ifdef FEAT_PYTHON
8310/*
8311 * "pyeval()" function
8312 */
8313 static void
8314f_pyeval(typval_T *argvars, typval_T *rettv)
8315{
8316 char_u *str;
8317 char_u buf[NUMBUFLEN];
8318
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008319 if (check_restricted() || check_secure())
8320 return;
8321
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008322 if (p_pyx == 0)
8323 p_pyx = 2;
8324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 do_pyeval(str, rettv);
8327}
8328#endif
8329
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008330#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8331/*
8332 * "pyxeval()" function
8333 */
8334 static void
8335f_pyxeval(typval_T *argvars, typval_T *rettv)
8336{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008337 if (check_restricted() || check_secure())
8338 return;
8339
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008340# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8341 init_pyxversion();
8342 if (p_pyx == 2)
8343 f_pyeval(argvars, rettv);
8344 else
8345 f_py3eval(argvars, rettv);
8346# elif defined(FEAT_PYTHON)
8347 f_pyeval(argvars, rettv);
8348# elif defined(FEAT_PYTHON3)
8349 f_py3eval(argvars, rettv);
8350# endif
8351}
8352#endif
8353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354/*
8355 * "range()" function
8356 */
8357 static void
8358f_range(typval_T *argvars, typval_T *rettv)
8359{
8360 varnumber_T start;
8361 varnumber_T end;
8362 varnumber_T stride = 1;
8363 varnumber_T i;
8364 int error = FALSE;
8365
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 if (argvars[1].v_type == VAR_UNKNOWN)
8368 {
8369 end = start - 1;
8370 start = 0;
8371 }
8372 else
8373 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 }
8378
8379 if (error)
8380 return; /* type error; errmsg already given */
8381 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008384 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 else
8386 {
8387 if (rettv_list_alloc(rettv) == OK)
8388 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8389 if (list_append_number(rettv->vval.v_list,
8390 (varnumber_T)i) == FAIL)
8391 break;
8392 }
8393}
8394
8395/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008396 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008397 */
8398 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008399readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008400{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008401 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008402 typval_T save_val;
8403 typval_T rettv;
8404 typval_T argv[2];
8405 int retval = 0;
8406 int error = FALSE;
8407
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008408 if (expr->v_type == VAR_UNKNOWN)
8409 return 1;
8410
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008411 prepare_vimvar(VV_VAL, &save_val);
8412 set_vim_var_string(VV_VAL, name, -1);
8413 argv[0].v_type = VAR_STRING;
8414 argv[0].vval.v_string = name;
8415
8416 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8417 goto theend;
8418
8419 retval = tv_get_number_chk(&rettv, &error);
8420 if (error)
8421 retval = -1;
8422 clear_tv(&rettv);
8423
8424theend:
8425 set_vim_var_string(VV_VAL, NULL, 0);
8426 restore_vimvar(VV_VAL, &save_val);
8427 return retval;
8428}
8429
8430/*
8431 * "readdir()" function
8432 */
8433 static void
8434f_readdir(typval_T *argvars, typval_T *rettv)
8435{
8436 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008437 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008438 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008439 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008440 garray_T ga;
8441 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008442
8443 if (rettv_list_alloc(rettv) == FAIL)
8444 return;
8445 path = tv_get_string(&argvars[0]);
8446 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008447
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008448 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8449 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008450 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008451 for (i = 0; i < ga.ga_len; i++)
8452 {
8453 p = ((char_u **)ga.ga_data)[i];
8454 list_append_string(rettv->vval.v_list, p, -1);
8455 }
8456 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008457 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008458}
8459
8460/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461 * "readfile()" function
8462 */
8463 static void
8464f_readfile(typval_T *argvars, typval_T *rettv)
8465{
8466 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008467 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468 int failed = FALSE;
8469 char_u *fname;
8470 FILE *fd;
8471 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8472 int io_size = sizeof(buf);
8473 int readlen; /* size of last fread() */
8474 char_u *prev = NULL; /* previously read bytes, if any */
8475 long prevlen = 0; /* length of data in prev */
8476 long prevsize = 0; /* size of prev buffer */
8477 long maxline = MAXLNUM;
8478 long cnt = 0;
8479 char_u *p; /* position in buf */
8480 char_u *start; /* start of current line */
8481
8482 if (argvars[1].v_type != VAR_UNKNOWN)
8483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008484 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008486 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8487 blob = TRUE;
8488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008490 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 }
8492
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008493 if (blob)
8494 {
8495 if (rettv_blob_alloc(rettv) == FAIL)
8496 return;
8497 }
8498 else
8499 {
8500 if (rettv_list_alloc(rettv) == FAIL)
8501 return;
8502 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503
8504 /* Always open the file in binary mode, library functions have a mind of
8505 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008506 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008509 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 return;
8511 }
8512
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008513 if (blob)
8514 {
8515 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008517 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008518 blob_free(rettv->vval.v_blob);
8519 }
8520 fclose(fd);
8521 return;
8522 }
8523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 while (cnt < maxline || maxline < 0)
8525 {
8526 readlen = (int)fread(buf, 1, io_size, fd);
8527
8528 /* This for loop processes what was read, but is also entered at end
8529 * of file so that either:
8530 * - an incomplete line gets written
8531 * - a "binary" file gets an empty line at the end if it ends in a
8532 * newline. */
8533 for (p = buf, start = buf;
8534 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8535 ++p)
8536 {
8537 if (*p == '\n' || readlen <= 0)
8538 {
8539 listitem_T *li;
8540 char_u *s = NULL;
8541 long_u len = p - start;
8542
8543 /* Finished a line. Remove CRs before NL. */
8544 if (readlen > 0 && !binary)
8545 {
8546 while (len > 0 && start[len - 1] == '\r')
8547 --len;
8548 /* removal may cross back to the "prev" string */
8549 if (len == 0)
8550 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8551 --prevlen;
8552 }
8553 if (prevlen == 0)
8554 s = vim_strnsave(start, (int)len);
8555 else
8556 {
8557 /* Change "prev" buffer to be the right size. This way
8558 * the bytes are only copied once, and very long lines are
8559 * allocated only once. */
8560 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8561 {
8562 mch_memmove(s + prevlen, start, len);
8563 s[prevlen + len] = NUL;
8564 prev = NULL; /* the list will own the string */
8565 prevlen = prevsize = 0;
8566 }
8567 }
8568 if (s == NULL)
8569 {
8570 do_outofmem_msg((long_u) prevlen + len + 1);
8571 failed = TRUE;
8572 break;
8573 }
8574
8575 if ((li = listitem_alloc()) == NULL)
8576 {
8577 vim_free(s);
8578 failed = TRUE;
8579 break;
8580 }
8581 li->li_tv.v_type = VAR_STRING;
8582 li->li_tv.v_lock = 0;
8583 li->li_tv.vval.v_string = s;
8584 list_append(rettv->vval.v_list, li);
8585
8586 start = p + 1; /* step over newline */
8587 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8588 break;
8589 }
8590 else if (*p == NUL)
8591 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8593 * when finding the BF and check the previous two bytes. */
8594 else if (*p == 0xbf && enc_utf8 && !binary)
8595 {
8596 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8597 * + 1, these may be in the "prev" string. */
8598 char_u back1 = p >= buf + 1 ? p[-1]
8599 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8600 char_u back2 = p >= buf + 2 ? p[-2]
8601 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8602 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8603
8604 if (back2 == 0xef && back1 == 0xbb)
8605 {
8606 char_u *dest = p - 2;
8607
8608 /* Usually a BOM is at the beginning of a file, and so at
8609 * the beginning of a line; then we can just step over it.
8610 */
8611 if (start == dest)
8612 start = p + 1;
8613 else
8614 {
8615 /* have to shuffle buf to close gap */
8616 int adjust_prevlen = 0;
8617
8618 if (dest < buf)
8619 {
8620 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8621 dest = buf;
8622 }
8623 if (readlen > p - buf + 1)
8624 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8625 readlen -= 3 - adjust_prevlen;
8626 prevlen -= adjust_prevlen;
8627 p = dest - 1;
8628 }
8629 }
8630 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 } /* for */
8632
8633 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8634 break;
8635 if (start < p)
8636 {
8637 /* There's part of a line in buf, store it in "prev". */
8638 if (p - start + prevlen >= prevsize)
8639 {
8640 /* need bigger "prev" buffer */
8641 char_u *newprev;
8642
8643 /* A common use case is ordinary text files and "prev" gets a
8644 * fragment of a line, so the first allocation is made
8645 * small, to avoid repeatedly 'allocing' large and
8646 * 'reallocing' small. */
8647 if (prevsize == 0)
8648 prevsize = (long)(p - start);
8649 else
8650 {
8651 long grow50pc = (prevsize * 3) / 2;
8652 long growmin = (long)((p - start) * 2 + prevlen);
8653 prevsize = grow50pc > growmin ? grow50pc : growmin;
8654 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008655 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 if (newprev == NULL)
8657 {
8658 do_outofmem_msg((long_u)prevsize);
8659 failed = TRUE;
8660 break;
8661 }
8662 prev = newprev;
8663 }
8664 /* Add the line part to end of "prev". */
8665 mch_memmove(prev + prevlen, start, p - start);
8666 prevlen += (long)(p - start);
8667 }
8668 } /* while */
8669
8670 /*
8671 * For a negative line count use only the lines at the end of the file,
8672 * free the rest.
8673 */
8674 if (!failed && maxline < 0)
8675 while (cnt > -maxline)
8676 {
8677 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8678 --cnt;
8679 }
8680
8681 if (failed)
8682 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008683 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008685 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 }
8687
8688 vim_free(prev);
8689 fclose(fd);
8690}
8691
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008692 static void
8693return_register(int regname, typval_T *rettv)
8694{
8695 char_u buf[2] = {0, 0};
8696
8697 buf[0] = (char_u)regname;
8698 rettv->v_type = VAR_STRING;
8699 rettv->vval.v_string = vim_strsave(buf);
8700}
8701
8702/*
8703 * "reg_executing()" function
8704 */
8705 static void
8706f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8707{
8708 return_register(reg_executing, rettv);
8709}
8710
8711/*
8712 * "reg_recording()" function
8713 */
8714 static void
8715f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8716{
8717 return_register(reg_recording, rettv);
8718}
8719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721/*
8722 * Convert a List to proftime_T.
8723 * Return FAIL when there is something wrong.
8724 */
8725 static int
8726list2proftime(typval_T *arg, proftime_T *tm)
8727{
8728 long n1, n2;
8729 int error = FALSE;
8730
8731 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8732 || arg->vval.v_list->lv_len != 2)
8733 return FAIL;
8734 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8735 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008736# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 tm->HighPart = n1;
8738 tm->LowPart = n2;
8739# else
8740 tm->tv_sec = n1;
8741 tm->tv_usec = n2;
8742# endif
8743 return error ? FAIL : OK;
8744}
8745#endif /* FEAT_RELTIME */
8746
8747/*
8748 * "reltime()" function
8749 */
8750 static void
8751f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8752{
8753#ifdef FEAT_RELTIME
8754 proftime_T res;
8755 proftime_T start;
8756
8757 if (argvars[0].v_type == VAR_UNKNOWN)
8758 {
8759 /* No arguments: get current time. */
8760 profile_start(&res);
8761 }
8762 else if (argvars[1].v_type == VAR_UNKNOWN)
8763 {
8764 if (list2proftime(&argvars[0], &res) == FAIL)
8765 return;
8766 profile_end(&res);
8767 }
8768 else
8769 {
8770 /* Two arguments: compute the difference. */
8771 if (list2proftime(&argvars[0], &start) == FAIL
8772 || list2proftime(&argvars[1], &res) == FAIL)
8773 return;
8774 profile_sub(&res, &start);
8775 }
8776
8777 if (rettv_list_alloc(rettv) == OK)
8778 {
8779 long n1, n2;
8780
Bram Moolenaar4f974752019-02-17 17:44:42 +01008781# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 n1 = res.HighPart;
8783 n2 = res.LowPart;
8784# else
8785 n1 = res.tv_sec;
8786 n2 = res.tv_usec;
8787# endif
8788 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8789 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8790 }
8791#endif
8792}
8793
8794#ifdef FEAT_FLOAT
8795/*
8796 * "reltimefloat()" function
8797 */
8798 static void
8799f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8800{
8801# ifdef FEAT_RELTIME
8802 proftime_T tm;
8803# endif
8804
8805 rettv->v_type = VAR_FLOAT;
8806 rettv->vval.v_float = 0;
8807# ifdef FEAT_RELTIME
8808 if (list2proftime(&argvars[0], &tm) == OK)
8809 rettv->vval.v_float = profile_float(&tm);
8810# endif
8811}
8812#endif
8813
8814/*
8815 * "reltimestr()" function
8816 */
8817 static void
8818f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8819{
8820#ifdef FEAT_RELTIME
8821 proftime_T tm;
8822#endif
8823
8824 rettv->v_type = VAR_STRING;
8825 rettv->vval.v_string = NULL;
8826#ifdef FEAT_RELTIME
8827 if (list2proftime(&argvars[0], &tm) == OK)
8828 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8829#endif
8830}
8831
8832#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 static void
8834make_connection(void)
8835{
8836 if (X_DISPLAY == NULL
8837# ifdef FEAT_GUI
8838 && !gui.in_use
8839# endif
8840 )
8841 {
8842 x_force_connect = TRUE;
8843 setup_term_clip();
8844 x_force_connect = FALSE;
8845 }
8846}
8847
8848 static int
8849check_connection(void)
8850{
8851 make_connection();
8852 if (X_DISPLAY == NULL)
8853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008854 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 return FAIL;
8856 }
8857 return OK;
8858}
8859#endif
8860
8861#ifdef FEAT_CLIENTSERVER
8862 static void
8863remote_common(typval_T *argvars, typval_T *rettv, int expr)
8864{
8865 char_u *server_name;
8866 char_u *keys;
8867 char_u *r = NULL;
8868 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008869 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008870# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871 HWND w;
8872# else
8873 Window w;
8874# endif
8875
8876 if (check_restricted() || check_secure())
8877 return;
8878
8879# ifdef FEAT_X11
8880 if (check_connection() == FAIL)
8881 return;
8882# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008883 if (argvars[2].v_type != VAR_UNKNOWN
8884 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008885 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008887 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 if (server_name == NULL)
8889 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008891# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008892 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008894 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8895 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896# endif
8897 {
8898 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008900 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008901 vim_free(r);
8902 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008904 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 return;
8906 }
8907
8908 rettv->vval.v_string = r;
8909
8910 if (argvars[2].v_type != VAR_UNKNOWN)
8911 {
8912 dictitem_T v;
8913 char_u str[30];
8914 char_u *idvar;
8915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008916 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008917 if (idvar != NULL && *idvar != NUL)
8918 {
8919 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8920 v.di_tv.v_type = VAR_STRING;
8921 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008923 vim_free(v.di_tv.vval.v_string);
8924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 }
8926}
8927#endif
8928
8929/*
8930 * "remote_expr()" function
8931 */
8932 static void
8933f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8934{
8935 rettv->v_type = VAR_STRING;
8936 rettv->vval.v_string = NULL;
8937#ifdef FEAT_CLIENTSERVER
8938 remote_common(argvars, rettv, TRUE);
8939#endif
8940}
8941
8942/*
8943 * "remote_foreground()" function
8944 */
8945 static void
8946f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8947{
8948#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008949# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 /* On Win32 it's done in this application. */
8951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008952 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953
8954 if (server_name != NULL)
8955 serverForeground(server_name);
8956 }
8957# else
8958 /* Send a foreground() expression to the server. */
8959 argvars[1].v_type = VAR_STRING;
8960 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8961 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008962 rettv->v_type = VAR_STRING;
8963 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 remote_common(argvars, rettv, TRUE);
8965 vim_free(argvars[1].vval.v_string);
8966# endif
8967#endif
8968}
8969
8970 static void
8971f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8972{
8973#ifdef FEAT_CLIENTSERVER
8974 dictitem_T v;
8975 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008976# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 long_u n = 0;
8978# endif
8979 char_u *serverid;
8980
8981 if (check_restricted() || check_secure())
8982 {
8983 rettv->vval.v_number = -1;
8984 return;
8985 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008986 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987 if (serverid == NULL)
8988 {
8989 rettv->vval.v_number = -1;
8990 return; /* type error; errmsg already given */
8991 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008992# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8994 if (n == 0)
8995 rettv->vval.v_number = -1;
8996 else
8997 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008998 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 rettv->vval.v_number = (s != NULL);
9000 }
9001# else
9002 if (check_connection() == FAIL)
9003 return;
9004
9005 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9006 serverStrToWin(serverid), &s);
9007# endif
9008
9009 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9010 {
9011 char_u *retvar;
9012
9013 v.di_tv.v_type = VAR_STRING;
9014 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009015 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 if (retvar != NULL)
9017 set_var(retvar, &v.di_tv, FALSE);
9018 vim_free(v.di_tv.vval.v_string);
9019 }
9020#else
9021 rettv->vval.v_number = -1;
9022#endif
9023}
9024
9025 static void
9026f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9027{
9028 char_u *r = NULL;
9029
9030#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009031 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032
9033 if (serverid != NULL && !check_restricted() && !check_secure())
9034 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009035 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009036# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009037 /* The server's HWND is encoded in the 'id' parameter */
9038 long_u n = 0;
9039# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009040
9041 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009042 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009043
Bram Moolenaar4f974752019-02-17 17:44:42 +01009044# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9046 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009047 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 if (r == NULL)
9049# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009050 if (check_connection() == FAIL
9051 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9052 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009054 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 }
9056#endif
9057 rettv->v_type = VAR_STRING;
9058 rettv->vval.v_string = r;
9059}
9060
9061/*
9062 * "remote_send()" function
9063 */
9064 static void
9065f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9066{
9067 rettv->v_type = VAR_STRING;
9068 rettv->vval.v_string = NULL;
9069#ifdef FEAT_CLIENTSERVER
9070 remote_common(argvars, rettv, FALSE);
9071#endif
9072}
9073
9074/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009075 * "remote_startserver()" function
9076 */
9077 static void
9078f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9079{
9080#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009082
9083 if (server == NULL)
9084 return; /* type error; errmsg already given */
9085 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009086 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009087 else
9088 {
9089# ifdef FEAT_X11
9090 if (check_connection() == OK)
9091 serverRegisterName(X_DISPLAY, server);
9092# else
9093 serverSetName(server);
9094# endif
9095 }
9096#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009098#endif
9099}
9100
9101/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 * "remove()" function
9103 */
9104 static void
9105f_remove(typval_T *argvars, typval_T *rettv)
9106{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9108
9109 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009110 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009111 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009112 blob_remove(argvars, rettv);
9113 else if (argvars[0].v_type == VAR_LIST)
9114 list_remove(argvars, rettv, arg_errmsg);
9115 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009116 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117}
9118
9119/*
9120 * "rename({from}, {to})" function
9121 */
9122 static void
9123f_rename(typval_T *argvars, typval_T *rettv)
9124{
9125 char_u buf[NUMBUFLEN];
9126
9127 if (check_restricted() || check_secure())
9128 rettv->vval.v_number = -1;
9129 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009130 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9131 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132}
9133
9134/*
9135 * "repeat()" function
9136 */
9137 static void
9138f_repeat(typval_T *argvars, typval_T *rettv)
9139{
9140 char_u *p;
9141 int n;
9142 int slen;
9143 int len;
9144 char_u *r;
9145 int i;
9146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009147 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 if (argvars[0].v_type == VAR_LIST)
9149 {
9150 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9151 while (n-- > 0)
9152 if (list_extend(rettv->vval.v_list,
9153 argvars[0].vval.v_list, NULL) == FAIL)
9154 break;
9155 }
9156 else
9157 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009158 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159 rettv->v_type = VAR_STRING;
9160 rettv->vval.v_string = NULL;
9161
9162 slen = (int)STRLEN(p);
9163 len = slen * n;
9164 if (len <= 0)
9165 return;
9166
9167 r = alloc(len + 1);
9168 if (r != NULL)
9169 {
9170 for (i = 0; i < n; i++)
9171 mch_memmove(r + i * slen, p, (size_t)slen);
9172 r[len] = NUL;
9173 }
9174
9175 rettv->vval.v_string = r;
9176 }
9177}
9178
9179/*
9180 * "resolve()" function
9181 */
9182 static void
9183f_resolve(typval_T *argvars, typval_T *rettv)
9184{
9185 char_u *p;
9186#ifdef HAVE_READLINK
9187 char_u *buf = NULL;
9188#endif
9189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009190 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191#ifdef FEAT_SHORTCUT
9192 {
9193 char_u *v = NULL;
9194
Bram Moolenaardce1e892019-02-10 23:18:53 +01009195 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 if (v != NULL)
9197 rettv->vval.v_string = v;
9198 else
9199 rettv->vval.v_string = vim_strsave(p);
9200 }
9201#else
9202# ifdef HAVE_READLINK
9203 {
9204 char_u *cpy;
9205 int len;
9206 char_u *remain = NULL;
9207 char_u *q;
9208 int is_relative_to_current = FALSE;
9209 int has_trailing_pathsep = FALSE;
9210 int limit = 100;
9211
9212 p = vim_strsave(p);
9213
9214 if (p[0] == '.' && (vim_ispathsep(p[1])
9215 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9216 is_relative_to_current = TRUE;
9217
9218 len = STRLEN(p);
9219 if (len > 0 && after_pathsep(p, p + len))
9220 {
9221 has_trailing_pathsep = TRUE;
9222 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9223 }
9224
9225 q = getnextcomp(p);
9226 if (*q != NUL)
9227 {
9228 /* Separate the first path component in "p", and keep the
9229 * remainder (beginning with the path separator). */
9230 remain = vim_strsave(q - 1);
9231 q[-1] = NUL;
9232 }
9233
9234 buf = alloc(MAXPATHL + 1);
9235 if (buf == NULL)
9236 goto fail;
9237
9238 for (;;)
9239 {
9240 for (;;)
9241 {
9242 len = readlink((char *)p, (char *)buf, MAXPATHL);
9243 if (len <= 0)
9244 break;
9245 buf[len] = NUL;
9246
9247 if (limit-- == 0)
9248 {
9249 vim_free(p);
9250 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009251 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 rettv->vval.v_string = NULL;
9253 goto fail;
9254 }
9255
9256 /* Ensure that the result will have a trailing path separator
9257 * if the argument has one. */
9258 if (remain == NULL && has_trailing_pathsep)
9259 add_pathsep(buf);
9260
9261 /* Separate the first path component in the link value and
9262 * concatenate the remainders. */
9263 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9264 if (*q != NUL)
9265 {
9266 if (remain == NULL)
9267 remain = vim_strsave(q - 1);
9268 else
9269 {
9270 cpy = concat_str(q - 1, remain);
9271 if (cpy != NULL)
9272 {
9273 vim_free(remain);
9274 remain = cpy;
9275 }
9276 }
9277 q[-1] = NUL;
9278 }
9279
9280 q = gettail(p);
9281 if (q > p && *q == NUL)
9282 {
9283 /* Ignore trailing path separator. */
9284 q[-1] = NUL;
9285 q = gettail(p);
9286 }
9287 if (q > p && !mch_isFullName(buf))
9288 {
9289 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009290 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 if (cpy != NULL)
9292 {
9293 STRCPY(cpy, p);
9294 STRCPY(gettail(cpy), buf);
9295 vim_free(p);
9296 p = cpy;
9297 }
9298 }
9299 else
9300 {
9301 vim_free(p);
9302 p = vim_strsave(buf);
9303 }
9304 }
9305
9306 if (remain == NULL)
9307 break;
9308
9309 /* Append the first path component of "remain" to "p". */
9310 q = getnextcomp(remain + 1);
9311 len = q - remain - (*q != NUL);
9312 cpy = vim_strnsave(p, STRLEN(p) + len);
9313 if (cpy != NULL)
9314 {
9315 STRNCAT(cpy, remain, len);
9316 vim_free(p);
9317 p = cpy;
9318 }
9319 /* Shorten "remain". */
9320 if (*q != NUL)
9321 STRMOVE(remain, q - 1);
9322 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009323 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 }
9325
9326 /* If the result is a relative path name, make it explicitly relative to
9327 * the current directory if and only if the argument had this form. */
9328 if (!vim_ispathsep(*p))
9329 {
9330 if (is_relative_to_current
9331 && *p != NUL
9332 && !(p[0] == '.'
9333 && (p[1] == NUL
9334 || vim_ispathsep(p[1])
9335 || (p[1] == '.'
9336 && (p[2] == NUL
9337 || vim_ispathsep(p[2]))))))
9338 {
9339 /* Prepend "./". */
9340 cpy = concat_str((char_u *)"./", p);
9341 if (cpy != NULL)
9342 {
9343 vim_free(p);
9344 p = cpy;
9345 }
9346 }
9347 else if (!is_relative_to_current)
9348 {
9349 /* Strip leading "./". */
9350 q = p;
9351 while (q[0] == '.' && vim_ispathsep(q[1]))
9352 q += 2;
9353 if (q > p)
9354 STRMOVE(p, p + 2);
9355 }
9356 }
9357
9358 /* Ensure that the result will have no trailing path separator
9359 * if the argument had none. But keep "/" or "//". */
9360 if (!has_trailing_pathsep)
9361 {
9362 q = p + STRLEN(p);
9363 if (after_pathsep(p, q))
9364 *gettail_sep(p) = NUL;
9365 }
9366
9367 rettv->vval.v_string = p;
9368 }
9369# else
9370 rettv->vval.v_string = vim_strsave(p);
9371# endif
9372#endif
9373
9374 simplify_filename(rettv->vval.v_string);
9375
9376#ifdef HAVE_READLINK
9377fail:
9378 vim_free(buf);
9379#endif
9380 rettv->v_type = VAR_STRING;
9381}
9382
9383/*
9384 * "reverse({list})" function
9385 */
9386 static void
9387f_reverse(typval_T *argvars, typval_T *rettv)
9388{
9389 list_T *l;
9390 listitem_T *li, *ni;
9391
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009392 if (argvars[0].v_type == VAR_BLOB)
9393 {
9394 blob_T *b = argvars[0].vval.v_blob;
9395 int i, len = blob_len(b);
9396
9397 for (i = 0; i < len / 2; i++)
9398 {
9399 int tmp = blob_get(b, i);
9400
9401 blob_set(b, i, blob_get(b, len - i - 1));
9402 blob_set(b, len - i - 1, tmp);
9403 }
9404 rettv_blob_set(rettv, b);
9405 return;
9406 }
9407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009409 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009411 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 (char_u *)N_("reverse() argument"), TRUE))
9413 {
9414 li = l->lv_last;
9415 l->lv_first = l->lv_last = NULL;
9416 l->lv_len = 0;
9417 while (li != NULL)
9418 {
9419 ni = li->li_prev;
9420 list_append(l, li);
9421 li = ni;
9422 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009423 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 l->lv_idx = l->lv_len - l->lv_idx - 1;
9425 }
9426}
9427
9428#define SP_NOMOVE 0x01 /* don't move cursor */
9429#define SP_REPEAT 0x02 /* repeat to find outer pair */
9430#define SP_RETCOUNT 0x04 /* return matchcount */
9431#define SP_SETPCMARK 0x08 /* set previous context mark */
9432#define SP_START 0x10 /* accept match at start position */
9433#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9434#define SP_END 0x40 /* leave cursor at end of match */
9435#define SP_COLUMN 0x80 /* start at cursor column */
9436
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437/*
9438 * Get flags for a search function.
9439 * Possibly sets "p_ws".
9440 * Returns BACKWARD, FORWARD or zero (for an error).
9441 */
9442 static int
9443get_search_arg(typval_T *varp, int *flagsp)
9444{
9445 int dir = FORWARD;
9446 char_u *flags;
9447 char_u nbuf[NUMBUFLEN];
9448 int mask;
9449
9450 if (varp->v_type != VAR_UNKNOWN)
9451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 if (flags == NULL)
9454 return 0; /* type error; errmsg already given */
9455 while (*flags != NUL)
9456 {
9457 switch (*flags)
9458 {
9459 case 'b': dir = BACKWARD; break;
9460 case 'w': p_ws = TRUE; break;
9461 case 'W': p_ws = FALSE; break;
9462 default: mask = 0;
9463 if (flagsp != NULL)
9464 switch (*flags)
9465 {
9466 case 'c': mask = SP_START; break;
9467 case 'e': mask = SP_END; break;
9468 case 'm': mask = SP_RETCOUNT; break;
9469 case 'n': mask = SP_NOMOVE; break;
9470 case 'p': mask = SP_SUBPAT; break;
9471 case 'r': mask = SP_REPEAT; break;
9472 case 's': mask = SP_SETPCMARK; break;
9473 case 'z': mask = SP_COLUMN; break;
9474 }
9475 if (mask == 0)
9476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009477 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 dir = 0;
9479 }
9480 else
9481 *flagsp |= mask;
9482 }
9483 if (dir == 0)
9484 break;
9485 ++flags;
9486 }
9487 }
9488 return dir;
9489}
9490
9491/*
9492 * Shared by search() and searchpos() functions.
9493 */
9494 static int
9495search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9496{
9497 int flags;
9498 char_u *pat;
9499 pos_T pos;
9500 pos_T save_cursor;
9501 int save_p_ws = p_ws;
9502 int dir;
9503 int retval = 0; /* default: FAIL */
9504 long lnum_stop = 0;
9505 proftime_T tm;
9506#ifdef FEAT_RELTIME
9507 long time_limit = 0;
9508#endif
9509 int options = SEARCH_KEEP;
9510 int subpatnum;
9511
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009512 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9514 if (dir == 0)
9515 goto theend;
9516 flags = *flagsp;
9517 if (flags & SP_START)
9518 options |= SEARCH_START;
9519 if (flags & SP_END)
9520 options |= SEARCH_END;
9521 if (flags & SP_COLUMN)
9522 options |= SEARCH_COL;
9523
9524 /* Optional arguments: line number to stop searching and timeout. */
9525 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 if (lnum_stop < 0)
9529 goto theend;
9530#ifdef FEAT_RELTIME
9531 if (argvars[3].v_type != VAR_UNKNOWN)
9532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009533 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 if (time_limit < 0)
9535 goto theend;
9536 }
9537#endif
9538 }
9539
9540#ifdef FEAT_RELTIME
9541 /* Set the time limit, if there is one. */
9542 profile_setlimit(time_limit, &tm);
9543#endif
9544
9545 /*
9546 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9547 * Check to make sure only those flags are set.
9548 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9549 * flags cannot be set. Check for that condition also.
9550 */
9551 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9552 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009554 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 goto theend;
9556 }
9557
9558 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009559 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009560 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 if (subpatnum != FAIL)
9562 {
9563 if (flags & SP_SUBPAT)
9564 retval = subpatnum;
9565 else
9566 retval = pos.lnum;
9567 if (flags & SP_SETPCMARK)
9568 setpcmark();
9569 curwin->w_cursor = pos;
9570 if (match_pos != NULL)
9571 {
9572 /* Store the match cursor position */
9573 match_pos->lnum = pos.lnum;
9574 match_pos->col = pos.col + 1;
9575 }
9576 /* "/$" will put the cursor after the end of the line, may need to
9577 * correct that here */
9578 check_cursor();
9579 }
9580
9581 /* If 'n' flag is used: restore cursor position. */
9582 if (flags & SP_NOMOVE)
9583 curwin->w_cursor = save_cursor;
9584 else
9585 curwin->w_set_curswant = TRUE;
9586theend:
9587 p_ws = save_p_ws;
9588
9589 return retval;
9590}
9591
9592#ifdef FEAT_FLOAT
9593
9594/*
9595 * round() is not in C90, use ceil() or floor() instead.
9596 */
9597 float_T
9598vim_round(float_T f)
9599{
9600 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9601}
9602
9603/*
9604 * "round({float})" function
9605 */
9606 static void
9607f_round(typval_T *argvars, typval_T *rettv)
9608{
9609 float_T f = 0.0;
9610
9611 rettv->v_type = VAR_FLOAT;
9612 if (get_float_arg(argvars, &f) == OK)
9613 rettv->vval.v_float = vim_round(f);
9614 else
9615 rettv->vval.v_float = 0.0;
9616}
9617#endif
9618
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009619#ifdef FEAT_RUBY
9620/*
9621 * "rubyeval()" function
9622 */
9623 static void
9624f_rubyeval(typval_T *argvars, typval_T *rettv)
9625{
9626 char_u *str;
9627 char_u buf[NUMBUFLEN];
9628
9629 str = tv_get_string_buf(&argvars[0], buf);
9630 do_rubyeval(str, rettv);
9631}
9632#endif
9633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634/*
9635 * "screenattr()" function
9636 */
9637 static void
9638f_screenattr(typval_T *argvars, typval_T *rettv)
9639{
9640 int row;
9641 int col;
9642 int c;
9643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009644 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9645 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 if (row < 0 || row >= screen_Rows
9647 || col < 0 || col >= screen_Columns)
9648 c = -1;
9649 else
9650 c = ScreenAttrs[LineOffset[row] + col];
9651 rettv->vval.v_number = c;
9652}
9653
9654/*
9655 * "screenchar()" function
9656 */
9657 static void
9658f_screenchar(typval_T *argvars, typval_T *rettv)
9659{
9660 int row;
9661 int col;
9662 int off;
9663 int c;
9664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009665 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9666 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009667 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 c = -1;
9669 else
9670 {
9671 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672 if (enc_utf8 && ScreenLinesUC[off] != 0)
9673 c = ScreenLinesUC[off];
9674 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675 c = ScreenLines[off];
9676 }
9677 rettv->vval.v_number = c;
9678}
9679
9680/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009681 * "screenchars()" function
9682 */
9683 static void
9684f_screenchars(typval_T *argvars, typval_T *rettv)
9685{
9686 int row;
9687 int col;
9688 int off;
9689 int c;
9690 int i;
9691
9692 if (rettv_list_alloc(rettv) == FAIL)
9693 return;
9694 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9695 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9696 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9697 return;
9698
9699 off = LineOffset[row] + col;
9700 if (enc_utf8 && ScreenLinesUC[off] != 0)
9701 c = ScreenLinesUC[off];
9702 else
9703 c = ScreenLines[off];
9704 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9705
9706 if (enc_utf8)
9707
9708 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9709 list_append_number(rettv->vval.v_list,
9710 (varnumber_T)ScreenLinesC[i][off]);
9711}
9712
9713/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 * "screencol()" function
9715 *
9716 * First column is 1 to be consistent with virtcol().
9717 */
9718 static void
9719f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9720{
9721 rettv->vval.v_number = screen_screencol() + 1;
9722}
9723
9724/*
9725 * "screenrow()" function
9726 */
9727 static void
9728f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9729{
9730 rettv->vval.v_number = screen_screenrow() + 1;
9731}
9732
9733/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009734 * "screenstring()" function
9735 */
9736 static void
9737f_screenstring(typval_T *argvars, typval_T *rettv)
9738{
9739 int row;
9740 int col;
9741 int off;
9742 int c;
9743 int i;
9744 char_u buf[MB_MAXBYTES + 1];
9745 int buflen = 0;
9746
9747 rettv->vval.v_string = NULL;
9748 rettv->v_type = VAR_STRING;
9749
9750 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9751 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9752 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9753 return;
9754
9755 off = LineOffset[row] + col;
9756 if (enc_utf8 && ScreenLinesUC[off] != 0)
9757 c = ScreenLinesUC[off];
9758 else
9759 c = ScreenLines[off];
9760 buflen += mb_char2bytes(c, buf);
9761
9762 if (enc_utf8)
9763 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9764 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9765
9766 buf[buflen] = NUL;
9767 rettv->vval.v_string = vim_strsave(buf);
9768}
9769
9770/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 * "search()" function
9772 */
9773 static void
9774f_search(typval_T *argvars, typval_T *rettv)
9775{
9776 int flags = 0;
9777
9778 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9779}
9780
9781/*
9782 * "searchdecl()" function
9783 */
9784 static void
9785f_searchdecl(typval_T *argvars, typval_T *rettv)
9786{
9787 int locally = 1;
9788 int thisblock = 0;
9789 int error = FALSE;
9790 char_u *name;
9791
9792 rettv->vval.v_number = 1; /* default: FAIL */
9793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009794 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 if (argvars[1].v_type != VAR_UNKNOWN)
9796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009797 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009799 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 }
9801 if (!error && name != NULL)
9802 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9803 locally, thisblock, SEARCH_KEEP) == FAIL;
9804}
9805
9806/*
9807 * Used by searchpair() and searchpairpos()
9808 */
9809 static int
9810searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9811{
9812 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009813 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 int save_p_ws = p_ws;
9815 int dir;
9816 int flags = 0;
9817 char_u nbuf1[NUMBUFLEN];
9818 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 int retval = 0; /* default: FAIL */
9820 long lnum_stop = 0;
9821 long time_limit = 0;
9822
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009823 /* Get the three pattern arguments: start, middle, end. Will result in an
9824 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009825 spat = tv_get_string_chk(&argvars[0]);
9826 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9827 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 if (spat == NULL || mpat == NULL || epat == NULL)
9829 goto theend; /* type error */
9830
9831 /* Handle the optional fourth argument: flags */
9832 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9833 if (dir == 0)
9834 goto theend;
9835
9836 /* Don't accept SP_END or SP_SUBPAT.
9837 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9838 */
9839 if ((flags & (SP_END | SP_SUBPAT)) != 0
9840 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009842 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 goto theend;
9844 }
9845
9846 /* Using 'r' implies 'W', otherwise it doesn't work. */
9847 if (flags & SP_REPEAT)
9848 p_ws = FALSE;
9849
9850 /* Optional fifth argument: skip expression */
9851 if (argvars[3].v_type == VAR_UNKNOWN
9852 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009853 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854 else
9855 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009856 skip = &argvars[4];
9857 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9858 && skip->v_type != VAR_STRING)
9859 {
9860 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009861 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009862 goto theend;
9863 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864 if (argvars[5].v_type != VAR_UNKNOWN)
9865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009866 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009869 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009871 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872#ifdef FEAT_RELTIME
9873 if (argvars[6].v_type != VAR_UNKNOWN)
9874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009875 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009878 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009880 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881 }
9882#endif
9883 }
9884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885
9886 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9887 match_pos, lnum_stop, time_limit);
9888
9889theend:
9890 p_ws = save_p_ws;
9891
9892 return retval;
9893}
9894
9895/*
9896 * "searchpair()" function
9897 */
9898 static void
9899f_searchpair(typval_T *argvars, typval_T *rettv)
9900{
9901 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9902}
9903
9904/*
9905 * "searchpairpos()" function
9906 */
9907 static void
9908f_searchpairpos(typval_T *argvars, typval_T *rettv)
9909{
9910 pos_T match_pos;
9911 int lnum = 0;
9912 int col = 0;
9913
9914 if (rettv_list_alloc(rettv) == FAIL)
9915 return;
9916
9917 if (searchpair_cmn(argvars, &match_pos) > 0)
9918 {
9919 lnum = match_pos.lnum;
9920 col = match_pos.col;
9921 }
9922
9923 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9924 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9925}
9926
9927/*
9928 * Search for a start/middle/end thing.
9929 * Used by searchpair(), see its documentation for the details.
9930 * Returns 0 or -1 for no match,
9931 */
9932 long
9933do_searchpair(
9934 char_u *spat, /* start pattern */
9935 char_u *mpat, /* middle pattern */
9936 char_u *epat, /* end pattern */
9937 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009938 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 int flags, /* SP_SETPCMARK and other SP_ values */
9940 pos_T *match_pos,
9941 linenr_T lnum_stop, /* stop at this line if not zero */
9942 long time_limit UNUSED) /* stop after this many msec */
9943{
9944 char_u *save_cpo;
9945 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9946 long retval = 0;
9947 pos_T pos;
9948 pos_T firstpos;
9949 pos_T foundpos;
9950 pos_T save_cursor;
9951 pos_T save_pos;
9952 int n;
9953 int r;
9954 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009955 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 int err;
9957 int options = SEARCH_KEEP;
9958 proftime_T tm;
9959
9960 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9961 save_cpo = p_cpo;
9962 p_cpo = empty_option;
9963
9964#ifdef FEAT_RELTIME
9965 /* Set the time limit, if there is one. */
9966 profile_setlimit(time_limit, &tm);
9967#endif
9968
9969 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9970 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009971 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9972 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 if (pat2 == NULL || pat3 == NULL)
9974 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009975 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009976 if (*mpat == NUL)
9977 STRCPY(pat3, pat2);
9978 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009979 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 spat, epat, mpat);
9981 if (flags & SP_START)
9982 options |= SEARCH_START;
9983
Bram Moolenaar48570482017-10-30 21:48:41 +01009984 if (skip != NULL)
9985 {
9986 /* Empty string means to not use the skip expression. */
9987 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9988 use_skip = skip->vval.v_string != NULL
9989 && *skip->vval.v_string != NUL;
9990 }
9991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 save_cursor = curwin->w_cursor;
9993 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009994 CLEAR_POS(&firstpos);
9995 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 pat = pat3;
9997 for (;;)
9998 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009999 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010000 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010001 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 /* didn't find it or found the first match again: FAIL */
10003 break;
10004
10005 if (firstpos.lnum == 0)
10006 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010007 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008 {
10009 /* Found the same position again. Can happen with a pattern that
10010 * has "\zs" at the end and searching backwards. Advance one
10011 * character and try again. */
10012 if (dir == BACKWARD)
10013 decl(&pos);
10014 else
10015 incl(&pos);
10016 }
10017 foundpos = pos;
10018
10019 /* clear the start flag to avoid getting stuck here */
10020 options &= ~SEARCH_START;
10021
10022 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010023 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010024 {
10025 save_pos = curwin->w_cursor;
10026 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010027 err = FALSE;
10028 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010029 curwin->w_cursor = save_pos;
10030 if (err)
10031 {
10032 /* Evaluating {skip} caused an error, break here. */
10033 curwin->w_cursor = save_cursor;
10034 retval = -1;
10035 break;
10036 }
10037 if (r)
10038 continue;
10039 }
10040
10041 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10042 {
10043 /* Found end when searching backwards or start when searching
10044 * forward: nested pair. */
10045 ++nest;
10046 pat = pat2; /* nested, don't search for middle */
10047 }
10048 else
10049 {
10050 /* Found end when searching forward or start when searching
10051 * backward: end of (nested) pair; or found middle in outer pair. */
10052 if (--nest == 1)
10053 pat = pat3; /* outer level, search for middle */
10054 }
10055
10056 if (nest == 0)
10057 {
10058 /* Found the match: return matchcount or line number. */
10059 if (flags & SP_RETCOUNT)
10060 ++retval;
10061 else
10062 retval = pos.lnum;
10063 if (flags & SP_SETPCMARK)
10064 setpcmark();
10065 curwin->w_cursor = pos;
10066 if (!(flags & SP_REPEAT))
10067 break;
10068 nest = 1; /* search for next unmatched */
10069 }
10070 }
10071
10072 if (match_pos != NULL)
10073 {
10074 /* Store the match cursor position */
10075 match_pos->lnum = curwin->w_cursor.lnum;
10076 match_pos->col = curwin->w_cursor.col + 1;
10077 }
10078
10079 /* If 'n' flag is used or search failed: restore cursor position. */
10080 if ((flags & SP_NOMOVE) || retval == 0)
10081 curwin->w_cursor = save_cursor;
10082
10083theend:
10084 vim_free(pat2);
10085 vim_free(pat3);
10086 if (p_cpo == empty_option)
10087 p_cpo = save_cpo;
10088 else
10089 /* Darn, evaluating the {skip} expression changed the value. */
10090 free_string_option(save_cpo);
10091
10092 return retval;
10093}
10094
10095/*
10096 * "searchpos()" function
10097 */
10098 static void
10099f_searchpos(typval_T *argvars, typval_T *rettv)
10100{
10101 pos_T match_pos;
10102 int lnum = 0;
10103 int col = 0;
10104 int n;
10105 int flags = 0;
10106
10107 if (rettv_list_alloc(rettv) == FAIL)
10108 return;
10109
10110 n = search_cmn(argvars, &match_pos, &flags);
10111 if (n > 0)
10112 {
10113 lnum = match_pos.lnum;
10114 col = match_pos.col;
10115 }
10116
10117 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10118 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10119 if (flags & SP_SUBPAT)
10120 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10121}
10122
10123 static void
10124f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10125{
10126#ifdef FEAT_CLIENTSERVER
10127 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010128 char_u *server = tv_get_string_chk(&argvars[0]);
10129 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010130
10131 rettv->vval.v_number = -1;
10132 if (server == NULL || reply == NULL)
10133 return;
10134 if (check_restricted() || check_secure())
10135 return;
10136# ifdef FEAT_X11
10137 if (check_connection() == FAIL)
10138 return;
10139# endif
10140
10141 if (serverSendReply(server, reply) < 0)
10142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010143 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 return;
10145 }
10146 rettv->vval.v_number = 0;
10147#else
10148 rettv->vval.v_number = -1;
10149#endif
10150}
10151
10152 static void
10153f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10154{
10155 char_u *r = NULL;
10156
10157#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010158# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 r = serverGetVimNames();
10160# else
10161 make_connection();
10162 if (X_DISPLAY != NULL)
10163 r = serverGetVimNames(X_DISPLAY);
10164# endif
10165#endif
10166 rettv->v_type = VAR_STRING;
10167 rettv->vval.v_string = r;
10168}
10169
10170/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010171 * "setbufline()" function
10172 */
10173 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010174f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010175{
10176 linenr_T lnum;
10177 buf_T *buf;
10178
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010179 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010180 if (buf == NULL)
10181 rettv->vval.v_number = 1; /* FAIL */
10182 else
10183 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010184 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010185 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010186 }
10187}
10188
10189/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 * "setbufvar()" function
10191 */
10192 static void
10193f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10194{
10195 buf_T *buf;
10196 char_u *varname, *bufvarname;
10197 typval_T *varp;
10198 char_u nbuf[NUMBUFLEN];
10199
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010200 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010202 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10203 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010204 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 varp = &argvars[2];
10206
10207 if (buf != NULL && varname != NULL && varp != NULL)
10208 {
10209 if (*varname == '&')
10210 {
10211 long numval;
10212 char_u *strval;
10213 int error = FALSE;
10214 aco_save_T aco;
10215
10216 /* set curbuf to be our buf, temporarily */
10217 aucmd_prepbuf(&aco, buf);
10218
10219 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010220 numval = (long)tv_get_number_chk(varp, &error);
10221 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010222 if (!error && strval != NULL)
10223 set_option_value(varname, numval, strval, OPT_LOCAL);
10224
10225 /* reset notion of buffer */
10226 aucmd_restbuf(&aco);
10227 }
10228 else
10229 {
10230 buf_T *save_curbuf = curbuf;
10231
Bram Moolenaar964b3742019-05-24 18:54:09 +020010232 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233 if (bufvarname != NULL)
10234 {
10235 curbuf = buf;
10236 STRCPY(bufvarname, "b:");
10237 STRCPY(bufvarname + 2, varname);
10238 set_var(bufvarname, varp, TRUE);
10239 vim_free(bufvarname);
10240 curbuf = save_curbuf;
10241 }
10242 }
10243 }
10244}
10245
10246 static void
10247f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10248{
10249 dict_T *d;
10250 dictitem_T *di;
10251 char_u *csearch;
10252
10253 if (argvars[0].v_type != VAR_DICT)
10254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010255 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 return;
10257 }
10258
10259 if ((d = argvars[0].vval.v_dict) != NULL)
10260 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010261 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 if (csearch != NULL)
10263 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 if (enc_utf8)
10265 {
10266 int pcc[MAX_MCO];
10267 int c = utfc_ptr2char(csearch, pcc);
10268
10269 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10270 }
10271 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 set_last_csearch(PTR2CHAR(csearch),
10273 csearch, MB_PTR2LEN(csearch));
10274 }
10275
10276 di = dict_find(d, (char_u *)"forward", -1);
10277 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010278 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010279 ? FORWARD : BACKWARD);
10280
10281 di = dict_find(d, (char_u *)"until", -1);
10282 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010283 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010284 }
10285}
10286
10287/*
10288 * "setcmdpos()" function
10289 */
10290 static void
10291f_setcmdpos(typval_T *argvars, typval_T *rettv)
10292{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010293 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294
10295 if (pos >= 0)
10296 rettv->vval.v_number = set_cmdline_pos(pos);
10297}
10298
10299/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010300 * "setenv()" function
10301 */
10302 static void
10303f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10304{
10305 char_u namebuf[NUMBUFLEN];
10306 char_u valbuf[NUMBUFLEN];
10307 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10308
10309 if (argvars[1].v_type == VAR_SPECIAL
10310 && argvars[1].vval.v_number == VVAL_NULL)
10311 vim_unsetenv(name);
10312 else
10313 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10314}
10315
10316/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317 * "setfperm({fname}, {mode})" function
10318 */
10319 static void
10320f_setfperm(typval_T *argvars, typval_T *rettv)
10321{
10322 char_u *fname;
10323 char_u modebuf[NUMBUFLEN];
10324 char_u *mode_str;
10325 int i;
10326 int mask;
10327 int mode = 0;
10328
10329 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010330 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331 if (fname == NULL)
10332 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010333 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010334 if (mode_str == NULL)
10335 return;
10336 if (STRLEN(mode_str) != 9)
10337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010338 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 return;
10340 }
10341
10342 mask = 1;
10343 for (i = 8; i >= 0; --i)
10344 {
10345 if (mode_str[i] != '-')
10346 mode |= mask;
10347 mask = mask << 1;
10348 }
10349 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10350}
10351
10352/*
10353 * "setline()" function
10354 */
10355 static void
10356f_setline(typval_T *argvars, typval_T *rettv)
10357{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010358 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010359
Bram Moolenaarca851592018-06-06 21:04:07 +020010360 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361}
10362
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010363/*
10364 * Used by "setqflist()" and "setloclist()" functions
10365 */
10366 static void
10367set_qf_ll_list(
10368 win_T *wp UNUSED,
10369 typval_T *list_arg UNUSED,
10370 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010371 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010372 typval_T *rettv)
10373{
10374#ifdef FEAT_QUICKFIX
10375 static char *e_invact = N_("E927: Invalid action: '%s'");
10376 char_u *act;
10377 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010378 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379#endif
10380
10381 rettv->vval.v_number = -1;
10382
10383#ifdef FEAT_QUICKFIX
10384 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010385 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010386 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010387 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010388 else
10389 {
10390 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010391 dict_T *d = NULL;
10392 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010393
10394 if (action_arg->v_type == VAR_STRING)
10395 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010396 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397 if (act == NULL)
10398 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010399 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10400 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010401 action = *act;
10402 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010403 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010404 }
10405 else if (action_arg->v_type == VAR_UNKNOWN)
10406 action = ' ';
10407 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010408 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010409
Bram Moolenaard823fa92016-08-12 16:29:27 +020010410 if (action_arg->v_type != VAR_UNKNOWN
10411 && what_arg->v_type != VAR_UNKNOWN)
10412 {
10413 if (what_arg->v_type == VAR_DICT)
10414 d = what_arg->vval.v_dict;
10415 else
10416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010417 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010418 valid_dict = FALSE;
10419 }
10420 }
10421
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010422 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010423 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010424 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10425 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010426 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010427 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 }
10429#endif
10430}
10431
10432/*
10433 * "setloclist()" function
10434 */
10435 static void
10436f_setloclist(typval_T *argvars, typval_T *rettv)
10437{
10438 win_T *win;
10439
10440 rettv->vval.v_number = -1;
10441
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010442 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010443 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010444 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010445}
10446
10447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010448 * "setpos()" function
10449 */
10450 static void
10451f_setpos(typval_T *argvars, typval_T *rettv)
10452{
10453 pos_T pos;
10454 int fnum;
10455 char_u *name;
10456 colnr_T curswant = -1;
10457
10458 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010459 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010460 if (name != NULL)
10461 {
10462 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10463 {
10464 if (--pos.col < 0)
10465 pos.col = 0;
10466 if (name[0] == '.' && name[1] == NUL)
10467 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010468 /* set cursor; "fnum" is ignored */
10469 curwin->w_cursor = pos;
10470 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010471 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010472 curwin->w_curswant = curswant - 1;
10473 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010475 check_cursor();
10476 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 }
10478 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10479 {
10480 /* set mark */
10481 if (setmark_pos(name[1], &pos, fnum) == OK)
10482 rettv->vval.v_number = 0;
10483 }
10484 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010485 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010486 }
10487 }
10488}
10489
10490/*
10491 * "setqflist()" function
10492 */
10493 static void
10494f_setqflist(typval_T *argvars, typval_T *rettv)
10495{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010496 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010497}
10498
10499/*
10500 * "setreg()" function
10501 */
10502 static void
10503f_setreg(typval_T *argvars, typval_T *rettv)
10504{
10505 int regname;
10506 char_u *strregname;
10507 char_u *stropt;
10508 char_u *strval;
10509 int append;
10510 char_u yank_type;
10511 long block_len;
10512
10513 block_len = -1;
10514 yank_type = MAUTO;
10515 append = FALSE;
10516
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010517 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 rettv->vval.v_number = 1; /* FAIL is default */
10519
10520 if (strregname == NULL)
10521 return; /* type error; errmsg already given */
10522 regname = *strregname;
10523 if (regname == 0 || regname == '@')
10524 regname = '"';
10525
10526 if (argvars[2].v_type != VAR_UNKNOWN)
10527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010528 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 if (stropt == NULL)
10530 return; /* type error */
10531 for (; *stropt != NUL; ++stropt)
10532 switch (*stropt)
10533 {
10534 case 'a': case 'A': /* append */
10535 append = TRUE;
10536 break;
10537 case 'v': case 'c': /* character-wise selection */
10538 yank_type = MCHAR;
10539 break;
10540 case 'V': case 'l': /* line-wise selection */
10541 yank_type = MLINE;
10542 break;
10543 case 'b': case Ctrl_V: /* block-wise selection */
10544 yank_type = MBLOCK;
10545 if (VIM_ISDIGIT(stropt[1]))
10546 {
10547 ++stropt;
10548 block_len = getdigits(&stropt) - 1;
10549 --stropt;
10550 }
10551 break;
10552 }
10553 }
10554
10555 if (argvars[1].v_type == VAR_LIST)
10556 {
10557 char_u **lstval;
10558 char_u **allocval;
10559 char_u buf[NUMBUFLEN];
10560 char_u **curval;
10561 char_u **curallocval;
10562 list_T *ll = argvars[1].vval.v_list;
10563 listitem_T *li;
10564 int len;
10565
10566 /* If the list is NULL handle like an empty list. */
10567 len = ll == NULL ? 0 : ll->lv_len;
10568
10569 /* First half: use for pointers to result lines; second half: use for
10570 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010571 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010572 if (lstval == NULL)
10573 return;
10574 curval = lstval;
10575 allocval = lstval + len + 2;
10576 curallocval = allocval;
10577
10578 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10579 li = li->li_next)
10580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010581 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 if (strval == NULL)
10583 goto free_lstval;
10584 if (strval == buf)
10585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010586 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587 * overwrite the string. */
10588 strval = vim_strsave(buf);
10589 if (strval == NULL)
10590 goto free_lstval;
10591 *curallocval++ = strval;
10592 }
10593 *curval++ = strval;
10594 }
10595 *curval++ = NULL;
10596
10597 write_reg_contents_lst(regname, lstval, -1,
10598 append, yank_type, block_len);
10599free_lstval:
10600 while (curallocval > allocval)
10601 vim_free(*--curallocval);
10602 vim_free(lstval);
10603 }
10604 else
10605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010606 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607 if (strval == NULL)
10608 return;
10609 write_reg_contents_ex(regname, strval, -1,
10610 append, yank_type, block_len);
10611 }
10612 rettv->vval.v_number = 0;
10613}
10614
10615/*
10616 * "settabvar()" function
10617 */
10618 static void
10619f_settabvar(typval_T *argvars, typval_T *rettv)
10620{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 tabpage_T *save_curtab;
10622 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 char_u *varname, *tabvarname;
10624 typval_T *varp;
10625
10626 rettv->vval.v_number = 0;
10627
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010628 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 return;
10630
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010631 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10632 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 varp = &argvars[2];
10634
Bram Moolenaar4033c552017-09-16 20:54:51 +020010635 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 save_curtab = curtab;
10638 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639
Bram Moolenaar964b3742019-05-24 18:54:09 +020010640 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010641 if (tabvarname != NULL)
10642 {
10643 STRCPY(tabvarname, "t:");
10644 STRCPY(tabvarname + 2, varname);
10645 set_var(tabvarname, varp, TRUE);
10646 vim_free(tabvarname);
10647 }
10648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 /* Restore current tabpage */
10650 if (valid_tabpage(save_curtab))
10651 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 }
10653}
10654
10655/*
10656 * "settabwinvar()" function
10657 */
10658 static void
10659f_settabwinvar(typval_T *argvars, typval_T *rettv)
10660{
10661 setwinvar(argvars, rettv, 1);
10662}
10663
10664/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010665 * "settagstack()" function
10666 */
10667 static void
10668f_settagstack(typval_T *argvars, typval_T *rettv)
10669{
10670 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10671 win_T *wp;
10672 dict_T *d;
10673 int action = 'r';
10674
10675 rettv->vval.v_number = -1;
10676
10677 // first argument: window number or id
10678 wp = find_win_by_nr_or_id(&argvars[0]);
10679 if (wp == NULL)
10680 return;
10681
10682 // second argument: dict with items to set in the tag stack
10683 if (argvars[1].v_type != VAR_DICT)
10684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010685 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010686 return;
10687 }
10688 d = argvars[1].vval.v_dict;
10689 if (d == NULL)
10690 return;
10691
10692 // third argument: action - 'a' for append and 'r' for replace.
10693 // default is to replace the stack.
10694 if (argvars[2].v_type == VAR_UNKNOWN)
10695 action = 'r';
10696 else if (argvars[2].v_type == VAR_STRING)
10697 {
10698 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010699 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010700 if (actstr == NULL)
10701 return;
10702 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10703 action = *actstr;
10704 else
10705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010706 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010707 return;
10708 }
10709 }
10710 else
10711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010712 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010713 return;
10714 }
10715
10716 if (set_tagstack(wp, d, action) == OK)
10717 rettv->vval.v_number = 0;
10718}
10719
10720/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010721 * "setwinvar()" function
10722 */
10723 static void
10724f_setwinvar(typval_T *argvars, typval_T *rettv)
10725{
10726 setwinvar(argvars, rettv, 0);
10727}
10728
10729#ifdef FEAT_CRYPT
10730/*
10731 * "sha256({string})" function
10732 */
10733 static void
10734f_sha256(typval_T *argvars, typval_T *rettv)
10735{
10736 char_u *p;
10737
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010738 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739 rettv->vval.v_string = vim_strsave(
10740 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10741 rettv->v_type = VAR_STRING;
10742}
10743#endif /* FEAT_CRYPT */
10744
10745/*
10746 * "shellescape({string})" function
10747 */
10748 static void
10749f_shellescape(typval_T *argvars, typval_T *rettv)
10750{
Bram Moolenaar20615522017-06-05 18:46:26 +020010751 int do_special = non_zero_arg(&argvars[1]);
10752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010754 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755 rettv->v_type = VAR_STRING;
10756}
10757
10758/*
10759 * shiftwidth() function
10760 */
10761 static void
10762f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10763{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010764 rettv->vval.v_number = 0;
10765
10766 if (argvars[0].v_type != VAR_UNKNOWN)
10767 {
10768 long col;
10769
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010770 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010771 if (col < 0)
10772 return; // type error; errmsg already given
10773#ifdef FEAT_VARTABS
10774 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10775 return;
10776#endif
10777 }
10778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 rettv->vval.v_number = get_sw_value(curbuf);
10780}
10781
10782/*
10783 * "simplify()" function
10784 */
10785 static void
10786f_simplify(typval_T *argvars, typval_T *rettv)
10787{
10788 char_u *p;
10789
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010790 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010791 rettv->vval.v_string = vim_strsave(p);
10792 simplify_filename(rettv->vval.v_string); /* simplify in place */
10793 rettv->v_type = VAR_STRING;
10794}
10795
10796#ifdef FEAT_FLOAT
10797/*
10798 * "sin()" function
10799 */
10800 static void
10801f_sin(typval_T *argvars, typval_T *rettv)
10802{
10803 float_T f = 0.0;
10804
10805 rettv->v_type = VAR_FLOAT;
10806 if (get_float_arg(argvars, &f) == OK)
10807 rettv->vval.v_float = sin(f);
10808 else
10809 rettv->vval.v_float = 0.0;
10810}
10811
10812/*
10813 * "sinh()" function
10814 */
10815 static void
10816f_sinh(typval_T *argvars, typval_T *rettv)
10817{
10818 float_T f = 0.0;
10819
10820 rettv->v_type = VAR_FLOAT;
10821 if (get_float_arg(argvars, &f) == OK)
10822 rettv->vval.v_float = sinh(f);
10823 else
10824 rettv->vval.v_float = 0.0;
10825}
10826#endif
10827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010828/*
10829 * "soundfold({word})" function
10830 */
10831 static void
10832f_soundfold(typval_T *argvars, typval_T *rettv)
10833{
10834 char_u *s;
10835
10836 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010837 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838#ifdef FEAT_SPELL
10839 rettv->vval.v_string = eval_soundfold(s);
10840#else
10841 rettv->vval.v_string = vim_strsave(s);
10842#endif
10843}
10844
10845/*
10846 * "spellbadword()" function
10847 */
10848 static void
10849f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10850{
10851 char_u *word = (char_u *)"";
10852 hlf_T attr = HLF_COUNT;
10853 int len = 0;
10854
10855 if (rettv_list_alloc(rettv) == FAIL)
10856 return;
10857
10858#ifdef FEAT_SPELL
10859 if (argvars[0].v_type == VAR_UNKNOWN)
10860 {
10861 /* Find the start and length of the badly spelled word. */
10862 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10863 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010864 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010865 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010866 curwin->w_set_curswant = TRUE;
10867 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010868 }
10869 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010871 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010872 int capcol = -1;
10873
10874 if (str != NULL)
10875 {
10876 /* Check the argument for spelling. */
10877 while (*str != NUL)
10878 {
10879 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10880 if (attr != HLF_COUNT)
10881 {
10882 word = str;
10883 break;
10884 }
10885 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010886 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010887 }
10888 }
10889 }
10890#endif
10891
10892 list_append_string(rettv->vval.v_list, word, len);
10893 list_append_string(rettv->vval.v_list, (char_u *)(
10894 attr == HLF_SPB ? "bad" :
10895 attr == HLF_SPR ? "rare" :
10896 attr == HLF_SPL ? "local" :
10897 attr == HLF_SPC ? "caps" :
10898 ""), -1);
10899}
10900
10901/*
10902 * "spellsuggest()" function
10903 */
10904 static void
10905f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10906{
10907#ifdef FEAT_SPELL
10908 char_u *str;
10909 int typeerr = FALSE;
10910 int maxcount;
10911 garray_T ga;
10912 int i;
10913 listitem_T *li;
10914 int need_capital = FALSE;
10915#endif
10916
10917 if (rettv_list_alloc(rettv) == FAIL)
10918 return;
10919
10920#ifdef FEAT_SPELL
10921 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010923 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 if (argvars[1].v_type != VAR_UNKNOWN)
10925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 if (maxcount <= 0)
10928 return;
10929 if (argvars[2].v_type != VAR_UNKNOWN)
10930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 if (typeerr)
10933 return;
10934 }
10935 }
10936 else
10937 maxcount = 25;
10938
10939 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10940
10941 for (i = 0; i < ga.ga_len; ++i)
10942 {
10943 str = ((char_u **)ga.ga_data)[i];
10944
10945 li = listitem_alloc();
10946 if (li == NULL)
10947 vim_free(str);
10948 else
10949 {
10950 li->li_tv.v_type = VAR_STRING;
10951 li->li_tv.v_lock = 0;
10952 li->li_tv.vval.v_string = str;
10953 list_append(rettv->vval.v_list, li);
10954 }
10955 }
10956 ga_clear(&ga);
10957 }
10958#endif
10959}
10960
10961 static void
10962f_split(typval_T *argvars, typval_T *rettv)
10963{
10964 char_u *str;
10965 char_u *end;
10966 char_u *pat = NULL;
10967 regmatch_T regmatch;
10968 char_u patbuf[NUMBUFLEN];
10969 char_u *save_cpo;
10970 int match;
10971 colnr_T col = 0;
10972 int keepempty = FALSE;
10973 int typeerr = FALSE;
10974
10975 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10976 save_cpo = p_cpo;
10977 p_cpo = (char_u *)"";
10978
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 if (argvars[1].v_type != VAR_UNKNOWN)
10981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010982 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 if (pat == NULL)
10984 typeerr = TRUE;
10985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010986 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987 }
10988 if (pat == NULL || *pat == NUL)
10989 pat = (char_u *)"[\\x01- ]\\+";
10990
10991 if (rettv_list_alloc(rettv) == FAIL)
10992 return;
10993 if (typeerr)
10994 return;
10995
10996 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10997 if (regmatch.regprog != NULL)
10998 {
10999 regmatch.rm_ic = FALSE;
11000 while (*str != NUL || keepempty)
11001 {
11002 if (*str == NUL)
11003 match = FALSE; /* empty item at the end */
11004 else
11005 match = vim_regexec_nl(&regmatch, str, col);
11006 if (match)
11007 end = regmatch.startp[0];
11008 else
11009 end = str + STRLEN(str);
11010 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11011 && *str != NUL && match && end < regmatch.endp[0]))
11012 {
11013 if (list_append_string(rettv->vval.v_list, str,
11014 (int)(end - str)) == FAIL)
11015 break;
11016 }
11017 if (!match)
11018 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011019 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 if (regmatch.endp[0] > str)
11021 col = 0;
11022 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011023 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 str = regmatch.endp[0];
11026 }
11027
11028 vim_regfree(regmatch.regprog);
11029 }
11030
11031 p_cpo = save_cpo;
11032}
11033
11034#ifdef FEAT_FLOAT
11035/*
11036 * "sqrt()" function
11037 */
11038 static void
11039f_sqrt(typval_T *argvars, typval_T *rettv)
11040{
11041 float_T f = 0.0;
11042
11043 rettv->v_type = VAR_FLOAT;
11044 if (get_float_arg(argvars, &f) == OK)
11045 rettv->vval.v_float = sqrt(f);
11046 else
11047 rettv->vval.v_float = 0.0;
11048}
11049
11050/*
11051 * "str2float()" function
11052 */
11053 static void
11054f_str2float(typval_T *argvars, typval_T *rettv)
11055{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011056 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011057 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058
Bram Moolenaar08243d22017-01-10 16:12:29 +010011059 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 p = skipwhite(p + 1);
11061 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011062 if (isneg)
11063 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 rettv->v_type = VAR_FLOAT;
11065}
11066#endif
11067
11068/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011069 * "str2list()" function
11070 */
11071 static void
11072f_str2list(typval_T *argvars, typval_T *rettv)
11073{
11074 char_u *p;
11075 int utf8 = FALSE;
11076
11077 if (rettv_list_alloc(rettv) == FAIL)
11078 return;
11079
11080 if (argvars[1].v_type != VAR_UNKNOWN)
11081 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11082
11083 p = tv_get_string(&argvars[0]);
11084
11085 if (has_mbyte || utf8)
11086 {
11087 int (*ptr2len)(char_u *);
11088 int (*ptr2char)(char_u *);
11089
11090 if (utf8 || enc_utf8)
11091 {
11092 ptr2len = utf_ptr2len;
11093 ptr2char = utf_ptr2char;
11094 }
11095 else
11096 {
11097 ptr2len = mb_ptr2len;
11098 ptr2char = mb_ptr2char;
11099 }
11100
11101 for ( ; *p != NUL; p += (*ptr2len)(p))
11102 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11103 }
11104 else
11105 for ( ; *p != NUL; ++p)
11106 list_append_number(rettv->vval.v_list, *p);
11107}
11108
11109/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110 * "str2nr()" function
11111 */
11112 static void
11113f_str2nr(typval_T *argvars, typval_T *rettv)
11114{
11115 int base = 10;
11116 char_u *p;
11117 varnumber_T n;
11118 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011119 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120
11121 if (argvars[1].v_type != VAR_UNKNOWN)
11122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011123 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 if (base != 2 && base != 8 && base != 10 && base != 16)
11125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011126 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 return;
11128 }
11129 }
11130
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011131 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011132 isneg = (*p == '-');
11133 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 p = skipwhite(p + 1);
11135 switch (base)
11136 {
11137 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11138 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11139 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11140 default: what = 0;
11141 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011142 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11143 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011144 if (isneg)
11145 rettv->vval.v_number = -n;
11146 else
11147 rettv->vval.v_number = n;
11148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011149}
11150
11151#ifdef HAVE_STRFTIME
11152/*
11153 * "strftime({format}[, {time}])" function
11154 */
11155 static void
11156f_strftime(typval_T *argvars, typval_T *rettv)
11157{
11158 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011159 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 struct tm *curtime;
11161 time_t seconds;
11162 char_u *p;
11163
11164 rettv->v_type = VAR_STRING;
11165
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011166 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 if (argvars[1].v_type == VAR_UNKNOWN)
11168 seconds = time(NULL);
11169 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011170 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011171 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011172 /* MSVC returns NULL for an invalid value of seconds. */
11173 if (curtime == NULL)
11174 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11175 else
11176 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177 vimconv_T conv;
11178 char_u *enc;
11179
11180 conv.vc_type = CONV_NONE;
11181 enc = enc_locale();
11182 convert_setup(&conv, p_enc, enc);
11183 if (conv.vc_type != CONV_NONE)
11184 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011185 if (p != NULL)
11186 (void)strftime((char *)result_buf, sizeof(result_buf),
11187 (char *)p, curtime);
11188 else
11189 result_buf[0] = NUL;
11190
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011191 if (conv.vc_type != CONV_NONE)
11192 vim_free(p);
11193 convert_setup(&conv, enc, p_enc);
11194 if (conv.vc_type != CONV_NONE)
11195 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11196 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011197 rettv->vval.v_string = vim_strsave(result_buf);
11198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011199 /* Release conversion descriptors */
11200 convert_setup(&conv, NULL, NULL);
11201 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202 }
11203}
11204#endif
11205
11206/*
11207 * "strgetchar()" function
11208 */
11209 static void
11210f_strgetchar(typval_T *argvars, typval_T *rettv)
11211{
11212 char_u *str;
11213 int len;
11214 int error = FALSE;
11215 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011216 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011217
11218 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011219 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011220 if (str == NULL)
11221 return;
11222 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011223 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224 if (error)
11225 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011226
Bram Moolenaar13505972019-01-24 15:04:48 +010011227 while (charidx >= 0 && byteidx < len)
11228 {
11229 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011231 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11232 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011233 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011234 --charidx;
11235 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237}
11238
11239/*
11240 * "stridx()" function
11241 */
11242 static void
11243f_stridx(typval_T *argvars, typval_T *rettv)
11244{
11245 char_u buf[NUMBUFLEN];
11246 char_u *needle;
11247 char_u *haystack;
11248 char_u *save_haystack;
11249 char_u *pos;
11250 int start_idx;
11251
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011252 needle = tv_get_string_chk(&argvars[1]);
11253 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011254 rettv->vval.v_number = -1;
11255 if (needle == NULL || haystack == NULL)
11256 return; /* type error; errmsg already given */
11257
11258 if (argvars[2].v_type != VAR_UNKNOWN)
11259 {
11260 int error = FALSE;
11261
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011262 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011263 if (error || start_idx >= (int)STRLEN(haystack))
11264 return;
11265 if (start_idx >= 0)
11266 haystack += start_idx;
11267 }
11268
11269 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11270 if (pos != NULL)
11271 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11272}
11273
11274/*
11275 * "string()" function
11276 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011277 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011278f_string(typval_T *argvars, typval_T *rettv)
11279{
11280 char_u *tofree;
11281 char_u numbuf[NUMBUFLEN];
11282
11283 rettv->v_type = VAR_STRING;
11284 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11285 get_copyID());
11286 /* Make a copy if we have a value but it's not in allocated memory. */
11287 if (rettv->vval.v_string != NULL && tofree == NULL)
11288 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11289}
11290
11291/*
11292 * "strlen()" function
11293 */
11294 static void
11295f_strlen(typval_T *argvars, typval_T *rettv)
11296{
11297 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011298 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299}
11300
11301/*
11302 * "strchars()" function
11303 */
11304 static void
11305f_strchars(typval_T *argvars, typval_T *rettv)
11306{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011307 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011308 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011309 varnumber_T len = 0;
11310 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011311
11312 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011313 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011315 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011316 else
11317 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011318 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11319 while (*s != NUL)
11320 {
11321 func_mb_ptr2char_adv(&s);
11322 ++len;
11323 }
11324 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011325 }
11326}
11327
11328/*
11329 * "strdisplaywidth()" function
11330 */
11331 static void
11332f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11333{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011334 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 int col = 0;
11336
11337 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011338 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339
11340 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11341}
11342
11343/*
11344 * "strwidth()" function
11345 */
11346 static void
11347f_strwidth(typval_T *argvars, typval_T *rettv)
11348{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011349 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350
Bram Moolenaar13505972019-01-24 15:04:48 +010011351 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011352}
11353
11354/*
11355 * "strcharpart()" function
11356 */
11357 static void
11358f_strcharpart(typval_T *argvars, typval_T *rettv)
11359{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 char_u *p;
11361 int nchar;
11362 int nbyte = 0;
11363 int charlen;
11364 int len = 0;
11365 int slen;
11366 int error = FALSE;
11367
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011368 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369 slen = (int)STRLEN(p);
11370
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011371 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 if (!error)
11373 {
11374 if (nchar > 0)
11375 while (nchar > 0 && nbyte < slen)
11376 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011377 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 --nchar;
11379 }
11380 else
11381 nbyte = nchar;
11382 if (argvars[2].v_type != VAR_UNKNOWN)
11383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011384 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 while (charlen > 0 && nbyte + len < slen)
11386 {
11387 int off = nbyte + len;
11388
11389 if (off < 0)
11390 len += 1;
11391 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011392 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 --charlen;
11394 }
11395 }
11396 else
11397 len = slen - nbyte; /* default: all bytes that are available. */
11398 }
11399
11400 /*
11401 * Only return the overlap between the specified part and the actual
11402 * string.
11403 */
11404 if (nbyte < 0)
11405 {
11406 len += nbyte;
11407 nbyte = 0;
11408 }
11409 else if (nbyte > slen)
11410 nbyte = slen;
11411 if (len < 0)
11412 len = 0;
11413 else if (nbyte + len > slen)
11414 len = slen - nbyte;
11415
11416 rettv->v_type = VAR_STRING;
11417 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011418}
11419
11420/*
11421 * "strpart()" function
11422 */
11423 static void
11424f_strpart(typval_T *argvars, typval_T *rettv)
11425{
11426 char_u *p;
11427 int n;
11428 int len;
11429 int slen;
11430 int error = FALSE;
11431
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011432 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 slen = (int)STRLEN(p);
11434
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011435 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436 if (error)
11437 len = 0;
11438 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011439 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 else
11441 len = slen - n; /* default len: all bytes that are available. */
11442
11443 /*
11444 * Only return the overlap between the specified part and the actual
11445 * string.
11446 */
11447 if (n < 0)
11448 {
11449 len += n;
11450 n = 0;
11451 }
11452 else if (n > slen)
11453 n = slen;
11454 if (len < 0)
11455 len = 0;
11456 else if (n + len > slen)
11457 len = slen - n;
11458
11459 rettv->v_type = VAR_STRING;
11460 rettv->vval.v_string = vim_strnsave(p + n, len);
11461}
11462
11463/*
11464 * "strridx()" function
11465 */
11466 static void
11467f_strridx(typval_T *argvars, typval_T *rettv)
11468{
11469 char_u buf[NUMBUFLEN];
11470 char_u *needle;
11471 char_u *haystack;
11472 char_u *rest;
11473 char_u *lastmatch = NULL;
11474 int haystack_len, end_idx;
11475
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011476 needle = tv_get_string_chk(&argvars[1]);
11477 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011478
11479 rettv->vval.v_number = -1;
11480 if (needle == NULL || haystack == NULL)
11481 return; /* type error; errmsg already given */
11482
11483 haystack_len = (int)STRLEN(haystack);
11484 if (argvars[2].v_type != VAR_UNKNOWN)
11485 {
11486 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011487 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488 if (end_idx < 0)
11489 return; /* can never find a match */
11490 }
11491 else
11492 end_idx = haystack_len;
11493
11494 if (*needle == NUL)
11495 {
11496 /* Empty string matches past the end. */
11497 lastmatch = haystack + end_idx;
11498 }
11499 else
11500 {
11501 for (rest = haystack; *rest != '\0'; ++rest)
11502 {
11503 rest = (char_u *)strstr((char *)rest, (char *)needle);
11504 if (rest == NULL || rest > haystack + end_idx)
11505 break;
11506 lastmatch = rest;
11507 }
11508 }
11509
11510 if (lastmatch == NULL)
11511 rettv->vval.v_number = -1;
11512 else
11513 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11514}
11515
11516/*
11517 * "strtrans()" function
11518 */
11519 static void
11520f_strtrans(typval_T *argvars, typval_T *rettv)
11521{
11522 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011523 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011524}
11525
11526/*
11527 * "submatch()" function
11528 */
11529 static void
11530f_submatch(typval_T *argvars, typval_T *rettv)
11531{
11532 int error = FALSE;
11533 int no;
11534 int retList = 0;
11535
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011536 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537 if (error)
11538 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011539 if (no < 0 || no >= NSUBEXP)
11540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011541 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011542 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011543 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011545 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011546 if (error)
11547 return;
11548
11549 if (retList == 0)
11550 {
11551 rettv->v_type = VAR_STRING;
11552 rettv->vval.v_string = reg_submatch(no);
11553 }
11554 else
11555 {
11556 rettv->v_type = VAR_LIST;
11557 rettv->vval.v_list = reg_submatch_list(no);
11558 }
11559}
11560
11561/*
11562 * "substitute()" function
11563 */
11564 static void
11565f_substitute(typval_T *argvars, typval_T *rettv)
11566{
11567 char_u patbuf[NUMBUFLEN];
11568 char_u subbuf[NUMBUFLEN];
11569 char_u flagsbuf[NUMBUFLEN];
11570
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011571 char_u *str = tv_get_string_chk(&argvars[0]);
11572 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011573 char_u *sub = NULL;
11574 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011575 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011577 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11578 expr = &argvars[2];
11579 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011580 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011582 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011583 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11584 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 rettv->vval.v_string = NULL;
11586 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011587 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011588}
11589
11590/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011591 * "swapinfo(swap_filename)" function
11592 */
11593 static void
11594f_swapinfo(typval_T *argvars, typval_T *rettv)
11595{
11596 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011597 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011598}
11599
11600/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011601 * "swapname(expr)" function
11602 */
11603 static void
11604f_swapname(typval_T *argvars, typval_T *rettv)
11605{
11606 buf_T *buf;
11607
11608 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011609 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011610 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11611 || buf->b_ml.ml_mfp->mf_fname == NULL)
11612 rettv->vval.v_string = NULL;
11613 else
11614 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11615}
11616
11617/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 * "synID(lnum, col, trans)" function
11619 */
11620 static void
11621f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11622{
11623 int id = 0;
11624#ifdef FEAT_SYN_HL
11625 linenr_T lnum;
11626 colnr_T col;
11627 int trans;
11628 int transerr = FALSE;
11629
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011630 lnum = tv_get_lnum(argvars); /* -1 on type error */
11631 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11632 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011633
11634 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11635 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11636 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11637#endif
11638
11639 rettv->vval.v_number = id;
11640}
11641
11642/*
11643 * "synIDattr(id, what [, mode])" function
11644 */
11645 static void
11646f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11647{
11648 char_u *p = NULL;
11649#ifdef FEAT_SYN_HL
11650 int id;
11651 char_u *what;
11652 char_u *mode;
11653 char_u modebuf[NUMBUFLEN];
11654 int modec;
11655
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011656 id = (int)tv_get_number(&argvars[0]);
11657 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658 if (argvars[2].v_type != VAR_UNKNOWN)
11659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011660 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011661 modec = TOLOWER_ASC(mode[0]);
11662 if (modec != 't' && modec != 'c' && modec != 'g')
11663 modec = 0; /* replace invalid with current */
11664 }
11665 else
11666 {
11667#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11668 if (USE_24BIT)
11669 modec = 'g';
11670 else
11671#endif
11672 if (t_colors > 1)
11673 modec = 'c';
11674 else
11675 modec = 't';
11676 }
11677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 switch (TOLOWER_ASC(what[0]))
11679 {
11680 case 'b':
11681 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11682 p = highlight_color(id, what, modec);
11683 else /* bold */
11684 p = highlight_has_attr(id, HL_BOLD, modec);
11685 break;
11686
11687 case 'f': /* fg[#] or font */
11688 p = highlight_color(id, what, modec);
11689 break;
11690
11691 case 'i':
11692 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11693 p = highlight_has_attr(id, HL_INVERSE, modec);
11694 else /* italic */
11695 p = highlight_has_attr(id, HL_ITALIC, modec);
11696 break;
11697
11698 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011699 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011700 break;
11701
11702 case 'r': /* reverse */
11703 p = highlight_has_attr(id, HL_INVERSE, modec);
11704 break;
11705
11706 case 's':
11707 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11708 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011709 /* strikeout */
11710 else if (TOLOWER_ASC(what[1]) == 't' &&
11711 TOLOWER_ASC(what[2]) == 'r')
11712 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011713 else /* standout */
11714 p = highlight_has_attr(id, HL_STANDOUT, modec);
11715 break;
11716
11717 case 'u':
11718 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11719 /* underline */
11720 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11721 else
11722 /* undercurl */
11723 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11724 break;
11725 }
11726
11727 if (p != NULL)
11728 p = vim_strsave(p);
11729#endif
11730 rettv->v_type = VAR_STRING;
11731 rettv->vval.v_string = p;
11732}
11733
11734/*
11735 * "synIDtrans(id)" function
11736 */
11737 static void
11738f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11739{
11740 int id;
11741
11742#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011743 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011744
11745 if (id > 0)
11746 id = syn_get_final_id(id);
11747 else
11748#endif
11749 id = 0;
11750
11751 rettv->vval.v_number = id;
11752}
11753
11754/*
11755 * "synconcealed(lnum, col)" function
11756 */
11757 static void
11758f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11759{
11760#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11761 linenr_T lnum;
11762 colnr_T col;
11763 int syntax_flags = 0;
11764 int cchar;
11765 int matchid = 0;
11766 char_u str[NUMBUFLEN];
11767#endif
11768
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011769 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770
11771#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011772 lnum = tv_get_lnum(argvars); /* -1 on type error */
11773 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011774
11775 vim_memset(str, NUL, sizeof(str));
11776
11777 if (rettv_list_alloc(rettv) != FAIL)
11778 {
11779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11780 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11781 && curwin->w_p_cole > 0)
11782 {
11783 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11784 syntax_flags = get_syntax_info(&matchid);
11785
11786 /* get the conceal character */
11787 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11788 {
11789 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011790 if (cchar == NUL && curwin->w_p_cole == 1)
11791 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011792 if (cchar != NUL)
11793 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794 if (has_mbyte)
11795 (*mb_char2bytes)(cchar, str);
11796 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011797 str[0] = cchar;
11798 }
11799 }
11800 }
11801
11802 list_append_number(rettv->vval.v_list,
11803 (syntax_flags & HL_CONCEAL) != 0);
11804 /* -1 to auto-determine strlen */
11805 list_append_string(rettv->vval.v_list, str, -1);
11806 list_append_number(rettv->vval.v_list, matchid);
11807 }
11808#endif
11809}
11810
11811/*
11812 * "synstack(lnum, col)" function
11813 */
11814 static void
11815f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11816{
11817#ifdef FEAT_SYN_HL
11818 linenr_T lnum;
11819 colnr_T col;
11820 int i;
11821 int id;
11822#endif
11823
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011824 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011825
11826#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011827 lnum = tv_get_lnum(argvars); /* -1 on type error */
11828 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829
11830 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11831 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11832 && rettv_list_alloc(rettv) != FAIL)
11833 {
11834 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11835 for (i = 0; ; ++i)
11836 {
11837 id = syn_get_stack_item(i);
11838 if (id < 0)
11839 break;
11840 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11841 break;
11842 }
11843 }
11844#endif
11845}
11846
11847 static void
11848get_cmd_output_as_rettv(
11849 typval_T *argvars,
11850 typval_T *rettv,
11851 int retlist)
11852{
11853 char_u *res = NULL;
11854 char_u *p;
11855 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 int err = FALSE;
11857 FILE *fd;
11858 list_T *list = NULL;
11859 int flags = SHELL_SILENT;
11860
11861 rettv->v_type = VAR_STRING;
11862 rettv->vval.v_string = NULL;
11863 if (check_restricted() || check_secure())
11864 goto errret;
11865
11866 if (argvars[1].v_type != VAR_UNKNOWN)
11867 {
11868 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011869 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870 * command.
11871 */
11872 if ((infile = vim_tempname('i', TRUE)) == NULL)
11873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011874 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 goto errret;
11876 }
11877
11878 fd = mch_fopen((char *)infile, WRITEBIN);
11879 if (fd == NULL)
11880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011881 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882 goto errret;
11883 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011884 if (argvars[1].v_type == VAR_NUMBER)
11885 {
11886 linenr_T lnum;
11887 buf_T *buf;
11888
11889 buf = buflist_findnr(argvars[1].vval.v_number);
11890 if (buf == NULL)
11891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011892 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011893 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011894 goto errret;
11895 }
11896
11897 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11898 {
11899 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11900 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11901 {
11902 err = TRUE;
11903 break;
11904 }
11905 if (putc(NL, fd) == EOF)
11906 {
11907 err = TRUE;
11908 break;
11909 }
11910 }
11911 }
11912 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011913 {
11914 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11915 err = TRUE;
11916 }
11917 else
11918 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011919 size_t len;
11920 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011922 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923 if (p == NULL)
11924 {
11925 fclose(fd);
11926 goto errret; /* type error; errmsg already given */
11927 }
11928 len = STRLEN(p);
11929 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11930 err = TRUE;
11931 }
11932 if (fclose(fd) != 0)
11933 err = TRUE;
11934 if (err)
11935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011936 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011937 goto errret;
11938 }
11939 }
11940
11941 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11942 * echoes typeahead, that messes up the display. */
11943 if (!msg_silent)
11944 flags += SHELL_COOKED;
11945
11946 if (retlist)
11947 {
11948 int len;
11949 listitem_T *li;
11950 char_u *s = NULL;
11951 char_u *start;
11952 char_u *end;
11953 int i;
11954
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011955 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011956 if (res == NULL)
11957 goto errret;
11958
11959 list = list_alloc();
11960 if (list == NULL)
11961 goto errret;
11962
11963 for (i = 0; i < len; ++i)
11964 {
11965 start = res + i;
11966 while (i < len && res[i] != NL)
11967 ++i;
11968 end = res + i;
11969
Bram Moolenaar964b3742019-05-24 18:54:09 +020011970 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 if (s == NULL)
11972 goto errret;
11973
11974 for (p = s; start < end; ++p, ++start)
11975 *p = *start == NUL ? NL : *start;
11976 *p = NUL;
11977
11978 li = listitem_alloc();
11979 if (li == NULL)
11980 {
11981 vim_free(s);
11982 goto errret;
11983 }
11984 li->li_tv.v_type = VAR_STRING;
11985 li->li_tv.v_lock = 0;
11986 li->li_tv.vval.v_string = s;
11987 list_append(list, li);
11988 }
11989
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011990 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011991 list = NULL;
11992 }
11993 else
11994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011995 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011996#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011997 /* translate <CR><NL> into <NL> */
11998 if (res != NULL)
11999 {
12000 char_u *s, *d;
12001
12002 d = res;
12003 for (s = res; *s; ++s)
12004 {
12005 if (s[0] == CAR && s[1] == NL)
12006 ++s;
12007 *d++ = *s;
12008 }
12009 *d = NUL;
12010 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012011#endif
12012 rettv->vval.v_string = res;
12013 res = NULL;
12014 }
12015
12016errret:
12017 if (infile != NULL)
12018 {
12019 mch_remove(infile);
12020 vim_free(infile);
12021 }
12022 if (res != NULL)
12023 vim_free(res);
12024 if (list != NULL)
12025 list_free(list);
12026}
12027
12028/*
12029 * "system()" function
12030 */
12031 static void
12032f_system(typval_T *argvars, typval_T *rettv)
12033{
12034 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12035}
12036
12037/*
12038 * "systemlist()" function
12039 */
12040 static void
12041f_systemlist(typval_T *argvars, typval_T *rettv)
12042{
12043 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12044}
12045
12046/*
12047 * "tabpagebuflist()" function
12048 */
12049 static void
12050f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12051{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012052 tabpage_T *tp;
12053 win_T *wp = NULL;
12054
12055 if (argvars[0].v_type == VAR_UNKNOWN)
12056 wp = firstwin;
12057 else
12058 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012059 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012060 if (tp != NULL)
12061 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12062 }
12063 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12064 {
12065 for (; wp != NULL; wp = wp->w_next)
12066 if (list_append_number(rettv->vval.v_list,
12067 wp->w_buffer->b_fnum) == FAIL)
12068 break;
12069 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012070}
12071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012072/*
12073 * "tabpagenr()" function
12074 */
12075 static void
12076f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12077{
12078 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012079 char_u *arg;
12080
12081 if (argvars[0].v_type != VAR_UNKNOWN)
12082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012083 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012084 nr = 0;
12085 if (arg != NULL)
12086 {
12087 if (STRCMP(arg, "$") == 0)
12088 nr = tabpage_index(NULL) - 1;
12089 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012090 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091 }
12092 }
12093 else
12094 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095 rettv->vval.v_number = nr;
12096}
12097
12098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012099/*
12100 * Common code for tabpagewinnr() and winnr().
12101 */
12102 static int
12103get_winnr(tabpage_T *tp, typval_T *argvar)
12104{
12105 win_T *twin;
12106 int nr = 1;
12107 win_T *wp;
12108 char_u *arg;
12109
12110 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12111 if (argvar->v_type != VAR_UNKNOWN)
12112 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012113 int invalid_arg = FALSE;
12114
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012115 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012116 if (arg == NULL)
12117 nr = 0; /* type error; errmsg already given */
12118 else if (STRCMP(arg, "$") == 0)
12119 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12120 else if (STRCMP(arg, "#") == 0)
12121 {
12122 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12123 if (twin == NULL)
12124 nr = 0;
12125 }
12126 else
12127 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012128 long count;
12129 char_u *endp;
12130
12131 // Extract the window count (if specified). e.g. winnr('3j')
12132 count = strtol((char *)arg, (char **)&endp, 10);
12133 if (count <= 0)
12134 count = 1; // if count is not specified, default to 1
12135 if (endp != NULL && *endp != '\0')
12136 {
12137 if (STRCMP(endp, "j") == 0)
12138 twin = win_vert_neighbor(tp, twin, FALSE, count);
12139 else if (STRCMP(endp, "k") == 0)
12140 twin = win_vert_neighbor(tp, twin, TRUE, count);
12141 else if (STRCMP(endp, "h") == 0)
12142 twin = win_horz_neighbor(tp, twin, TRUE, count);
12143 else if (STRCMP(endp, "l") == 0)
12144 twin = win_horz_neighbor(tp, twin, FALSE, count);
12145 else
12146 invalid_arg = TRUE;
12147 }
12148 else
12149 invalid_arg = TRUE;
12150 }
12151
12152 if (invalid_arg)
12153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012154 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012155 nr = 0;
12156 }
12157 }
12158
12159 if (nr > 0)
12160 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12161 wp != twin; wp = wp->w_next)
12162 {
12163 if (wp == NULL)
12164 {
12165 /* didn't find it in this tabpage */
12166 nr = 0;
12167 break;
12168 }
12169 ++nr;
12170 }
12171 return nr;
12172}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012173
12174/*
12175 * "tabpagewinnr()" function
12176 */
12177 static void
12178f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12179{
12180 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012181 tabpage_T *tp;
12182
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012183 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012184 if (tp == NULL)
12185 nr = 0;
12186 else
12187 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012188 rettv->vval.v_number = nr;
12189}
12190
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012191/*
12192 * "tagfiles()" function
12193 */
12194 static void
12195f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12196{
12197 char_u *fname;
12198 tagname_T tn;
12199 int first;
12200
12201 if (rettv_list_alloc(rettv) == FAIL)
12202 return;
12203 fname = alloc(MAXPATHL);
12204 if (fname == NULL)
12205 return;
12206
12207 for (first = TRUE; ; first = FALSE)
12208 if (get_tagfname(&tn, first, fname) == FAIL
12209 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12210 break;
12211 tagname_free(&tn);
12212 vim_free(fname);
12213}
12214
12215/*
12216 * "taglist()" function
12217 */
12218 static void
12219f_taglist(typval_T *argvars, typval_T *rettv)
12220{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012221 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012222 char_u *tag_pattern;
12223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012224 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012225
12226 rettv->vval.v_number = FALSE;
12227 if (*tag_pattern == NUL)
12228 return;
12229
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012230 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012231 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012232 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012233 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012234}
12235
12236/*
12237 * "tempname()" function
12238 */
12239 static void
12240f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12241{
12242 static int x = 'A';
12243
12244 rettv->v_type = VAR_STRING;
12245 rettv->vval.v_string = vim_tempname(x, FALSE);
12246
12247 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12248 * names. Skip 'I' and 'O', they are used for shell redirection. */
12249 do
12250 {
12251 if (x == 'Z')
12252 x = '0';
12253 else if (x == '9')
12254 x = 'A';
12255 else
12256 {
12257#ifdef EBCDIC
12258 if (x == 'I')
12259 x = 'J';
12260 else if (x == 'R')
12261 x = 'S';
12262 else
12263#endif
12264 ++x;
12265 }
12266 } while (x == 'I' || x == 'O');
12267}
12268
12269#ifdef FEAT_FLOAT
12270/*
12271 * "tan()" function
12272 */
12273 static void
12274f_tan(typval_T *argvars, typval_T *rettv)
12275{
12276 float_T f = 0.0;
12277
12278 rettv->v_type = VAR_FLOAT;
12279 if (get_float_arg(argvars, &f) == OK)
12280 rettv->vval.v_float = tan(f);
12281 else
12282 rettv->vval.v_float = 0.0;
12283}
12284
12285/*
12286 * "tanh()" function
12287 */
12288 static void
12289f_tanh(typval_T *argvars, typval_T *rettv)
12290{
12291 float_T f = 0.0;
12292
12293 rettv->v_type = VAR_FLOAT;
12294 if (get_float_arg(argvars, &f) == OK)
12295 rettv->vval.v_float = tanh(f);
12296 else
12297 rettv->vval.v_float = 0.0;
12298}
12299#endif
12300
12301/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012302 * Get a callback from "arg". It can be a Funcref or a function name.
12303 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012304 * "cb_name" is not allocated.
12305 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012307 callback_T
12308get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012310 callback_T res;
12311
12312 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12314 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012315 res.cb_partial = arg->vval.v_partial;
12316 ++res.cb_partial->pt_refcount;
12317 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012318 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012319 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012320 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012321 res.cb_partial = NULL;
12322 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12323 {
12324 // Note that we don't make a copy of the string.
12325 res.cb_name = arg->vval.v_string;
12326 func_ref(res.cb_name);
12327 }
12328 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12329 {
12330 res.cb_name = (char_u *)"";
12331 }
12332 else
12333 {
12334 emsg(_("E921: Invalid callback argument"));
12335 res.cb_name = NULL;
12336 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012338 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012339}
12340
12341/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012342 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012343 */
12344 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012345put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012347 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012348 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012349 tv->v_type = VAR_PARTIAL;
12350 tv->vval.v_partial = cb->cb_partial;
12351 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012353 else
12354 {
12355 tv->v_type = VAR_FUNC;
12356 tv->vval.v_string = vim_strsave(cb->cb_name);
12357 func_ref(cb->cb_name);
12358 }
12359}
12360
12361/*
12362 * Make a copy of "src" into "dest", allocating the function name if needed,
12363 * without incrementing the refcount.
12364 */
12365 void
12366set_callback(callback_T *dest, callback_T *src)
12367{
12368 if (src->cb_partial == NULL)
12369 {
12370 // just a function name, make a copy
12371 dest->cb_name = vim_strsave(src->cb_name);
12372 dest->cb_free_name = TRUE;
12373 }
12374 else
12375 {
12376 // cb_name is a pointer into cb_partial
12377 dest->cb_name = src->cb_name;
12378 dest->cb_free_name = FALSE;
12379 }
12380 dest->cb_partial = src->cb_partial;
12381}
12382
12383/*
12384 * Unref/free "callback" returned by get_callback() or set_callback().
12385 */
12386 void
12387free_callback(callback_T *callback)
12388{
12389 if (callback->cb_partial != NULL)
12390 {
12391 partial_unref(callback->cb_partial);
12392 callback->cb_partial = NULL;
12393 }
12394 else if (callback->cb_name != NULL)
12395 func_unref(callback->cb_name);
12396 if (callback->cb_free_name)
12397 {
12398 vim_free(callback->cb_name);
12399 callback->cb_free_name = FALSE;
12400 }
12401 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012403
12404#ifdef FEAT_TIMERS
12405/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012406 * "timer_info([timer])" function
12407 */
12408 static void
12409f_timer_info(typval_T *argvars, typval_T *rettv)
12410{
12411 timer_T *timer = NULL;
12412
12413 if (rettv_list_alloc(rettv) != OK)
12414 return;
12415 if (argvars[0].v_type != VAR_UNKNOWN)
12416 {
12417 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012418 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012419 else
12420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012421 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012422 if (timer != NULL)
12423 add_timer_info(rettv, timer);
12424 }
12425 }
12426 else
12427 add_timer_info_all(rettv);
12428}
12429
12430/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012431 * "timer_pause(timer, paused)" function
12432 */
12433 static void
12434f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12435{
12436 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012437 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012438
12439 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012440 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012441 else
12442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012443 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012444 if (timer != NULL)
12445 timer->tr_paused = paused;
12446 }
12447}
12448
12449/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 * "timer_start(time, callback [, options])" function
12451 */
12452 static void
12453f_timer_start(typval_T *argvars, typval_T *rettv)
12454{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012455 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012456 timer_T *timer;
12457 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012458 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012459 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460
Bram Moolenaar75537a92016-09-05 22:45:28 +020012461 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462 if (check_secure())
12463 return;
12464 if (argvars[2].v_type != VAR_UNKNOWN)
12465 {
12466 if (argvars[2].v_type != VAR_DICT
12467 || (dict = argvars[2].vval.v_dict) == NULL)
12468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012469 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012470 return;
12471 }
12472 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012473 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474 }
12475
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012476 callback = get_callback(&argvars[1]);
12477 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012478 return;
12479
12480 timer = create_timer(msec, repeat);
12481 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012482 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483 else
12484 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012485 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012486 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487 }
12488}
12489
12490/*
12491 * "timer_stop(timer)" function
12492 */
12493 static void
12494f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12495{
12496 timer_T *timer;
12497
12498 if (argvars[0].v_type != VAR_NUMBER)
12499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012500 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501 return;
12502 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012503 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504 if (timer != NULL)
12505 stop_timer(timer);
12506}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012507
12508/*
12509 * "timer_stopall()" function
12510 */
12511 static void
12512f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12513{
12514 stop_all_timers();
12515}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516#endif
12517
12518/*
12519 * "tolower(string)" function
12520 */
12521 static void
12522f_tolower(typval_T *argvars, typval_T *rettv)
12523{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012524 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012525 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526}
12527
12528/*
12529 * "toupper(string)" function
12530 */
12531 static void
12532f_toupper(typval_T *argvars, typval_T *rettv)
12533{
12534 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012535 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536}
12537
12538/*
12539 * "tr(string, fromstr, tostr)" function
12540 */
12541 static void
12542f_tr(typval_T *argvars, typval_T *rettv)
12543{
12544 char_u *in_str;
12545 char_u *fromstr;
12546 char_u *tostr;
12547 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012548 int inlen;
12549 int fromlen;
12550 int tolen;
12551 int idx;
12552 char_u *cpstr;
12553 int cplen;
12554 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555 char_u buf[NUMBUFLEN];
12556 char_u buf2[NUMBUFLEN];
12557 garray_T ga;
12558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012559 in_str = tv_get_string(&argvars[0]);
12560 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12561 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562
12563 /* Default return value: empty string. */
12564 rettv->v_type = VAR_STRING;
12565 rettv->vval.v_string = NULL;
12566 if (fromstr == NULL || tostr == NULL)
12567 return; /* type error; errmsg already given */
12568 ga_init2(&ga, (int)sizeof(char), 80);
12569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012571 /* not multi-byte: fromstr and tostr must be the same length */
12572 if (STRLEN(fromstr) != STRLEN(tostr))
12573 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012575 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012576 ga_clear(&ga);
12577 return;
12578 }
12579
12580 /* fromstr and tostr have to contain the same number of chars */
12581 while (*in_str != NUL)
12582 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 if (has_mbyte)
12584 {
12585 inlen = (*mb_ptr2len)(in_str);
12586 cpstr = in_str;
12587 cplen = inlen;
12588 idx = 0;
12589 for (p = fromstr; *p != NUL; p += fromlen)
12590 {
12591 fromlen = (*mb_ptr2len)(p);
12592 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12593 {
12594 for (p = tostr; *p != NUL; p += tolen)
12595 {
12596 tolen = (*mb_ptr2len)(p);
12597 if (idx-- == 0)
12598 {
12599 cplen = tolen;
12600 cpstr = p;
12601 break;
12602 }
12603 }
12604 if (*p == NUL) /* tostr is shorter than fromstr */
12605 goto error;
12606 break;
12607 }
12608 ++idx;
12609 }
12610
12611 if (first && cpstr == in_str)
12612 {
12613 /* Check that fromstr and tostr have the same number of
12614 * (multi-byte) characters. Done only once when a character
12615 * of in_str doesn't appear in fromstr. */
12616 first = FALSE;
12617 for (p = tostr; *p != NUL; p += tolen)
12618 {
12619 tolen = (*mb_ptr2len)(p);
12620 --idx;
12621 }
12622 if (idx != 0)
12623 goto error;
12624 }
12625
12626 (void)ga_grow(&ga, cplen);
12627 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12628 ga.ga_len += cplen;
12629
12630 in_str += inlen;
12631 }
12632 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012633 {
12634 /* When not using multi-byte chars we can do it faster. */
12635 p = vim_strchr(fromstr, *in_str);
12636 if (p != NULL)
12637 ga_append(&ga, tostr[p - fromstr]);
12638 else
12639 ga_append(&ga, *in_str);
12640 ++in_str;
12641 }
12642 }
12643
12644 /* add a terminating NUL */
12645 (void)ga_grow(&ga, 1);
12646 ga_append(&ga, NUL);
12647
12648 rettv->vval.v_string = ga.ga_data;
12649}
12650
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012651/*
12652 * "trim({expr})" function
12653 */
12654 static void
12655f_trim(typval_T *argvars, typval_T *rettv)
12656{
12657 char_u buf1[NUMBUFLEN];
12658 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012659 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012660 char_u *mask = NULL;
12661 char_u *tail;
12662 char_u *prev;
12663 char_u *p;
12664 int c1;
12665
12666 rettv->v_type = VAR_STRING;
12667 if (head == NULL)
12668 {
12669 rettv->vval.v_string = NULL;
12670 return;
12671 }
12672
12673 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012674 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012675
12676 while (*head != NUL)
12677 {
12678 c1 = PTR2CHAR(head);
12679 if (mask == NULL)
12680 {
12681 if (c1 > ' ' && c1 != 0xa0)
12682 break;
12683 }
12684 else
12685 {
12686 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12687 if (c1 == PTR2CHAR(p))
12688 break;
12689 if (*p == NUL)
12690 break;
12691 }
12692 MB_PTR_ADV(head);
12693 }
12694
12695 for (tail = head + STRLEN(head); tail > head; tail = prev)
12696 {
12697 prev = tail;
12698 MB_PTR_BACK(head, prev);
12699 c1 = PTR2CHAR(prev);
12700 if (mask == NULL)
12701 {
12702 if (c1 > ' ' && c1 != 0xa0)
12703 break;
12704 }
12705 else
12706 {
12707 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12708 if (c1 == PTR2CHAR(p))
12709 break;
12710 if (*p == NUL)
12711 break;
12712 }
12713 }
12714 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12715}
12716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012717#ifdef FEAT_FLOAT
12718/*
12719 * "trunc({float})" function
12720 */
12721 static void
12722f_trunc(typval_T *argvars, typval_T *rettv)
12723{
12724 float_T f = 0.0;
12725
12726 rettv->v_type = VAR_FLOAT;
12727 if (get_float_arg(argvars, &f) == OK)
12728 /* trunc() is not in C90, use floor() or ceil() instead. */
12729 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12730 else
12731 rettv->vval.v_float = 0.0;
12732}
12733#endif
12734
12735/*
12736 * "type(expr)" function
12737 */
12738 static void
12739f_type(typval_T *argvars, typval_T *rettv)
12740{
12741 int n = -1;
12742
12743 switch (argvars[0].v_type)
12744 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012745 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12746 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012748 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12749 case VAR_LIST: n = VAR_TYPE_LIST; break;
12750 case VAR_DICT: n = VAR_TYPE_DICT; break;
12751 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 case VAR_SPECIAL:
12753 if (argvars[0].vval.v_number == VVAL_FALSE
12754 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012755 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012757 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012758 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012759 case VAR_JOB: n = VAR_TYPE_JOB; break;
12760 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012761 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012763 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012764 n = -1;
12765 break;
12766 }
12767 rettv->vval.v_number = n;
12768}
12769
12770/*
12771 * "undofile(name)" function
12772 */
12773 static void
12774f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12775{
12776 rettv->v_type = VAR_STRING;
12777#ifdef FEAT_PERSISTENT_UNDO
12778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012779 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012780
12781 if (*fname == NUL)
12782 {
12783 /* If there is no file name there will be no undo file. */
12784 rettv->vval.v_string = NULL;
12785 }
12786 else
12787 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012788 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789
12790 if (ffname != NULL)
12791 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12792 vim_free(ffname);
12793 }
12794 }
12795#else
12796 rettv->vval.v_string = NULL;
12797#endif
12798}
12799
12800/*
12801 * "undotree()" function
12802 */
12803 static void
12804f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12805{
12806 if (rettv_dict_alloc(rettv) == OK)
12807 {
12808 dict_T *dict = rettv->vval.v_dict;
12809 list_T *list;
12810
Bram Moolenaare0be1672018-07-08 16:50:37 +020012811 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12812 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12813 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12814 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12815 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12816 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012817
12818 list = list_alloc();
12819 if (list != NULL)
12820 {
12821 u_eval_tree(curbuf->b_u_oldhead, list);
12822 dict_add_list(dict, "entries", list);
12823 }
12824 }
12825}
12826
12827/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 * "virtcol(string)" function
12829 */
12830 static void
12831f_virtcol(typval_T *argvars, typval_T *rettv)
12832{
12833 colnr_T vcol = 0;
12834 pos_T *fp;
12835 int fnum = curbuf->b_fnum;
12836
12837 fp = var2fpos(&argvars[0], FALSE, &fnum);
12838 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12839 && fnum == curbuf->b_fnum)
12840 {
12841 getvvcol(curwin, fp, NULL, NULL, &vcol);
12842 ++vcol;
12843 }
12844
12845 rettv->vval.v_number = vcol;
12846}
12847
12848/*
12849 * "visualmode()" function
12850 */
12851 static void
12852f_visualmode(typval_T *argvars, typval_T *rettv)
12853{
12854 char_u str[2];
12855
12856 rettv->v_type = VAR_STRING;
12857 str[0] = curbuf->b_visual_mode_eval;
12858 str[1] = NUL;
12859 rettv->vval.v_string = vim_strsave(str);
12860
12861 /* A non-zero number or non-empty string argument: reset mode. */
12862 if (non_zero_arg(&argvars[0]))
12863 curbuf->b_visual_mode_eval = NUL;
12864}
12865
12866/*
12867 * "wildmenumode()" function
12868 */
12869 static void
12870f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12871{
12872#ifdef FEAT_WILDMENU
12873 if (wild_menu_showing)
12874 rettv->vval.v_number = 1;
12875#endif
12876}
12877
12878/*
12879 * "winbufnr(nr)" function
12880 */
12881 static void
12882f_winbufnr(typval_T *argvars, typval_T *rettv)
12883{
12884 win_T *wp;
12885
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012886 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 if (wp == NULL)
12888 rettv->vval.v_number = -1;
12889 else
12890 rettv->vval.v_number = wp->w_buffer->b_fnum;
12891}
12892
12893/*
12894 * "wincol()" function
12895 */
12896 static void
12897f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12898{
12899 validate_cursor();
12900 rettv->vval.v_number = curwin->w_wcol + 1;
12901}
12902
12903/*
12904 * "winheight(nr)" function
12905 */
12906 static void
12907f_winheight(typval_T *argvars, typval_T *rettv)
12908{
12909 win_T *wp;
12910
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012911 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012912 if (wp == NULL)
12913 rettv->vval.v_number = -1;
12914 else
12915 rettv->vval.v_number = wp->w_height;
12916}
12917
12918/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012919 * "winlayout()" function
12920 */
12921 static void
12922f_winlayout(typval_T *argvars, typval_T *rettv)
12923{
12924 tabpage_T *tp;
12925
12926 if (rettv_list_alloc(rettv) != OK)
12927 return;
12928
12929 if (argvars[0].v_type == VAR_UNKNOWN)
12930 tp = curtab;
12931 else
12932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012933 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012934 if (tp == NULL)
12935 return;
12936 }
12937
12938 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12939}
12940
12941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942 * "winline()" function
12943 */
12944 static void
12945f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12946{
12947 validate_cursor();
12948 rettv->vval.v_number = curwin->w_wrow + 1;
12949}
12950
12951/*
12952 * "winnr()" function
12953 */
12954 static void
12955f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12956{
12957 int nr = 1;
12958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012959 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960 rettv->vval.v_number = nr;
12961}
12962
12963/*
12964 * "winrestcmd()" function
12965 */
12966 static void
12967f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12968{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969 win_T *wp;
12970 int winnr = 1;
12971 garray_T ga;
12972 char_u buf[50];
12973
12974 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012975 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012976 {
12977 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12978 ga_concat(&ga, buf);
12979 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12980 ga_concat(&ga, buf);
12981 ++winnr;
12982 }
12983 ga_append(&ga, NUL);
12984
12985 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012986 rettv->v_type = VAR_STRING;
12987}
12988
12989/*
12990 * "winrestview()" function
12991 */
12992 static void
12993f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12994{
12995 dict_T *dict;
12996
12997 if (argvars[0].v_type != VAR_DICT
12998 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012999 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000 else
13001 {
13002 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013003 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013005 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013007 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13009 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010013010 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011 curwin->w_set_curswant = FALSE;
13012 }
13013
13014 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013015 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016#ifdef FEAT_DIFF
13017 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013018 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013019#endif
13020 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013021 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013022 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013023 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013024
13025 check_cursor();
13026 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013027 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028 changed_window_setting();
13029
13030 if (curwin->w_topline <= 0)
13031 curwin->w_topline = 1;
13032 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13033 curwin->w_topline = curbuf->b_ml.ml_line_count;
13034#ifdef FEAT_DIFF
13035 check_topfill(curwin, TRUE);
13036#endif
13037 }
13038}
13039
13040/*
13041 * "winsaveview()" function
13042 */
13043 static void
13044f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13045{
13046 dict_T *dict;
13047
13048 if (rettv_dict_alloc(rettv) == FAIL)
13049 return;
13050 dict = rettv->vval.v_dict;
13051
Bram Moolenaare0be1672018-07-08 16:50:37 +020013052 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13053 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013054 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013056 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013057
Bram Moolenaare0be1672018-07-08 16:50:37 +020013058 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013060 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013061#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013062 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13063 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064}
13065
13066/*
13067 * "winwidth(nr)" function
13068 */
13069 static void
13070f_winwidth(typval_T *argvars, typval_T *rettv)
13071{
13072 win_T *wp;
13073
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013074 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075 if (wp == NULL)
13076 rettv->vval.v_number = -1;
13077 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079}
13080
13081/*
13082 * "wordcount()" function
13083 */
13084 static void
13085f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13086{
13087 if (rettv_dict_alloc(rettv) == FAIL)
13088 return;
13089 cursor_pos_info(rettv->vval.v_dict);
13090}
13091
13092/*
13093 * "writefile()" function
13094 */
13095 static void
13096f_writefile(typval_T *argvars, typval_T *rettv)
13097{
13098 int binary = FALSE;
13099 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013100#ifdef HAVE_FSYNC
13101 int do_fsync = p_fs;
13102#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103 char_u *fname;
13104 FILE *fd;
13105 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013106 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013107 list_T *list = NULL;
13108 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013109
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013110 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013111 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112 return;
13113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013114 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013116 list = argvars[0].vval.v_list;
13117 if (list == NULL)
13118 return;
13119 for (li = list->lv_first; li != NULL; li = li->li_next)
13120 if (tv_get_string_chk(&li->li_tv) == NULL)
13121 return;
13122 }
13123 else if (argvars[0].v_type == VAR_BLOB)
13124 {
13125 blob = argvars[0].vval.v_blob;
13126 if (blob == NULL)
13127 return;
13128 }
13129 else
13130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013131 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132 return;
13133 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013134
13135 if (argvars[2].v_type != VAR_UNKNOWN)
13136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013137 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013138
13139 if (arg2 == NULL)
13140 return;
13141 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013143 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013145#ifdef HAVE_FSYNC
13146 if (vim_strchr(arg2, 's') != NULL)
13147 do_fsync = TRUE;
13148 else if (vim_strchr(arg2, 'S') != NULL)
13149 do_fsync = FALSE;
13150#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 }
13152
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013153 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013154 if (fname == NULL)
13155 return;
13156
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013157 /* Always open the file in binary mode, library functions have a mind of
13158 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13160 append ? APPENDBIN : WRITEBIN)) == NULL)
13161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013162 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 ret = -1;
13164 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013165 else if (blob)
13166 {
13167 if (write_blob(fd, blob) == FAIL)
13168 ret = -1;
13169#ifdef HAVE_FSYNC
13170 else if (do_fsync)
13171 // Ignore the error, the user wouldn't know what to do about it.
13172 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013173 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013174#endif
13175 fclose(fd);
13176 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177 else
13178 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013179 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013181#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013182 else if (do_fsync)
13183 /* Ignore the error, the user wouldn't know what to do about it.
13184 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013185 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 fclose(fd);
13188 }
13189
13190 rettv->vval.v_number = ret;
13191}
13192
13193/*
13194 * "xor(expr, expr)" function
13195 */
13196 static void
13197f_xor(typval_T *argvars, typval_T *rettv)
13198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013199 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13200 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201}
13202
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013203#endif /* FEAT_EVAL */