blob: 73efa79f2cdb93211f35b72f8bbd8661f93dc141 [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);
183static void f_has_key(typval_T *argvars, typval_T *rettv);
184static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
185static void f_hasmapto(typval_T *argvars, typval_T *rettv);
186static void f_histadd(typval_T *argvars, typval_T *rettv);
187static void f_histdel(typval_T *argvars, typval_T *rettv);
188static void f_histget(typval_T *argvars, typval_T *rettv);
189static void f_histnr(typval_T *argvars, typval_T *rettv);
190static void f_hlID(typval_T *argvars, typval_T *rettv);
191static void f_hlexists(typval_T *argvars, typval_T *rettv);
192static void f_hostname(typval_T *argvars, typval_T *rettv);
193static void f_iconv(typval_T *argvars, typval_T *rettv);
194static void f_indent(typval_T *argvars, typval_T *rettv);
195static void f_index(typval_T *argvars, typval_T *rettv);
196static void f_input(typval_T *argvars, typval_T *rettv);
197static void f_inputdialog(typval_T *argvars, typval_T *rettv);
198static void f_inputlist(typval_T *argvars, typval_T *rettv);
199static void f_inputrestore(typval_T *argvars, typval_T *rettv);
200static void f_inputsave(typval_T *argvars, typval_T *rettv);
201static void f_inputsecret(typval_T *argvars, typval_T *rettv);
202static void f_insert(typval_T *argvars, typval_T *rettv);
203static void f_invert(typval_T *argvars, typval_T *rettv);
204static void f_isdirectory(typval_T *argvars, typval_T *rettv);
205static void f_islocked(typval_T *argvars, typval_T *rettv);
206#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200207static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_isnan(typval_T *argvars, typval_T *rettv);
209#endif
210static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200211static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200212static void f_keys(typval_T *argvars, typval_T *rettv);
213static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
214static void f_len(typval_T *argvars, typval_T *rettv);
215static void f_libcall(typval_T *argvars, typval_T *rettv);
216static void f_libcallnr(typval_T *argvars, typval_T *rettv);
217static void f_line(typval_T *argvars, typval_T *rettv);
218static void f_line2byte(typval_T *argvars, typval_T *rettv);
219static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200220static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200221static void f_localtime(typval_T *argvars, typval_T *rettv);
222#ifdef FEAT_FLOAT
223static void f_log(typval_T *argvars, typval_T *rettv);
224static void f_log10(typval_T *argvars, typval_T *rettv);
225#endif
226#ifdef FEAT_LUA
227static void f_luaeval(typval_T *argvars, typval_T *rettv);
228#endif
229static void f_map(typval_T *argvars, typval_T *rettv);
230static void f_maparg(typval_T *argvars, typval_T *rettv);
231static void f_mapcheck(typval_T *argvars, typval_T *rettv);
232static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_matchend(typval_T *argvars, typval_T *rettv);
234static void f_matchlist(typval_T *argvars, typval_T *rettv);
235static void f_matchstr(typval_T *argvars, typval_T *rettv);
236static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
237static void f_max(typval_T *argvars, typval_T *rettv);
238static void f_min(typval_T *argvars, typval_T *rettv);
239#ifdef vim_mkdir
240static void f_mkdir(typval_T *argvars, typval_T *rettv);
241#endif
242static void f_mode(typval_T *argvars, typval_T *rettv);
243#ifdef FEAT_MZSCHEME
244static void f_mzeval(typval_T *argvars, typval_T *rettv);
245#endif
246static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
247static void f_nr2char(typval_T *argvars, typval_T *rettv);
248static void f_or(typval_T *argvars, typval_T *rettv);
249static void f_pathshorten(typval_T *argvars, typval_T *rettv);
250#ifdef FEAT_PERL
251static void f_perleval(typval_T *argvars, typval_T *rettv);
252#endif
253#ifdef FEAT_FLOAT
254static void f_pow(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
257static void f_printf(typval_T *argvars, typval_T *rettv);
258static void f_pumvisible(typval_T *argvars, typval_T *rettv);
259#ifdef FEAT_PYTHON3
260static void f_py3eval(typval_T *argvars, typval_T *rettv);
261#endif
262#ifdef FEAT_PYTHON
263static void f_pyeval(typval_T *argvars, typval_T *rettv);
264#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100265#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
266static void f_pyxeval(typval_T *argvars, typval_T *rettv);
267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200268static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200269static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200270static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200271static void f_reg_executing(typval_T *argvars, typval_T *rettv);
272static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200273static void f_reltime(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_FLOAT
275static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_reltimestr(typval_T *argvars, typval_T *rettv);
278static void f_remote_expr(typval_T *argvars, typval_T *rettv);
279static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
280static void f_remote_peek(typval_T *argvars, typval_T *rettv);
281static void f_remote_read(typval_T *argvars, typval_T *rettv);
282static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100283static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200284static void f_remove(typval_T *argvars, typval_T *rettv);
285static void f_rename(typval_T *argvars, typval_T *rettv);
286static void f_repeat(typval_T *argvars, typval_T *rettv);
287static void f_resolve(typval_T *argvars, typval_T *rettv);
288static void f_reverse(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_FLOAT
290static void f_round(typval_T *argvars, typval_T *rettv);
291#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100292#ifdef FEAT_RUBY
293static void f_rubyeval(typval_T *argvars, typval_T *rettv);
294#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200295static void f_screenattr(typval_T *argvars, typval_T *rettv);
296static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100297static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200298static void f_screencol(typval_T *argvars, typval_T *rettv);
299static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100300static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200301static void f_search(typval_T *argvars, typval_T *rettv);
302static void f_searchdecl(typval_T *argvars, typval_T *rettv);
303static void f_searchpair(typval_T *argvars, typval_T *rettv);
304static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
305static void f_searchpos(typval_T *argvars, typval_T *rettv);
306static void f_server2client(typval_T *argvars, typval_T *rettv);
307static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200308static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200309static void f_setbufvar(typval_T *argvars, typval_T *rettv);
310static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
311static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200312static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200313static void f_setfperm(typval_T *argvars, typval_T *rettv);
314static void f_setline(typval_T *argvars, typval_T *rettv);
315static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_setpos(typval_T *argvars, typval_T *rettv);
317static void f_setqflist(typval_T *argvars, typval_T *rettv);
318static void f_setreg(typval_T *argvars, typval_T *rettv);
319static void f_settabvar(typval_T *argvars, typval_T *rettv);
320static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100321static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_setwinvar(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_CRYPT
324static void f_sha256(typval_T *argvars, typval_T *rettv);
325#endif /* FEAT_CRYPT */
326static void f_shellescape(typval_T *argvars, typval_T *rettv);
327static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
328static void f_simplify(typval_T *argvars, typval_T *rettv);
329#ifdef FEAT_FLOAT
330static void f_sin(typval_T *argvars, typval_T *rettv);
331static void f_sinh(typval_T *argvars, typval_T *rettv);
332#endif
333static void f_sort(typval_T *argvars, typval_T *rettv);
334static void f_soundfold(typval_T *argvars, typval_T *rettv);
335static void f_spellbadword(typval_T *argvars, typval_T *rettv);
336static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
337static void f_split(typval_T *argvars, typval_T *rettv);
338#ifdef FEAT_FLOAT
339static void f_sqrt(typval_T *argvars, typval_T *rettv);
340static void f_str2float(typval_T *argvars, typval_T *rettv);
341#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200342static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_str2nr(typval_T *argvars, typval_T *rettv);
344static void f_strchars(typval_T *argvars, typval_T *rettv);
345#ifdef HAVE_STRFTIME
346static void f_strftime(typval_T *argvars, typval_T *rettv);
347#endif
348static void f_strgetchar(typval_T *argvars, typval_T *rettv);
349static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200350static void f_strlen(typval_T *argvars, typval_T *rettv);
351static void f_strcharpart(typval_T *argvars, typval_T *rettv);
352static void f_strpart(typval_T *argvars, typval_T *rettv);
353static void f_strridx(typval_T *argvars, typval_T *rettv);
354static void f_strtrans(typval_T *argvars, typval_T *rettv);
355static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
356static void f_strwidth(typval_T *argvars, typval_T *rettv);
357static void f_submatch(typval_T *argvars, typval_T *rettv);
358static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200359static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200360static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200361static void f_synID(typval_T *argvars, typval_T *rettv);
362static void f_synIDattr(typval_T *argvars, typval_T *rettv);
363static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
364static void f_synstack(typval_T *argvars, typval_T *rettv);
365static void f_synconcealed(typval_T *argvars, typval_T *rettv);
366static void f_system(typval_T *argvars, typval_T *rettv);
367static void f_systemlist(typval_T *argvars, typval_T *rettv);
368static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
369static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
370static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
371static void f_taglist(typval_T *argvars, typval_T *rettv);
372static void f_tagfiles(typval_T *argvars, typval_T *rettv);
373static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200374#ifdef FEAT_FLOAT
375static void f_tan(typval_T *argvars, typval_T *rettv);
376static void f_tanh(typval_T *argvars, typval_T *rettv);
377#endif
378#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200379static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200380static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_timer_start(typval_T *argvars, typval_T *rettv);
382static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200383static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200384#endif
385static void f_tolower(typval_T *argvars, typval_T *rettv);
386static void f_toupper(typval_T *argvars, typval_T *rettv);
387static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100388static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200389#ifdef FEAT_FLOAT
390static void f_trunc(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_type(typval_T *argvars, typval_T *rettv);
393static void f_undofile(typval_T *argvars, typval_T *rettv);
394static void f_undotree(typval_T *argvars, typval_T *rettv);
395static void f_uniq(typval_T *argvars, typval_T *rettv);
396static void f_values(typval_T *argvars, typval_T *rettv);
397static void f_virtcol(typval_T *argvars, typval_T *rettv);
398static void f_visualmode(typval_T *argvars, typval_T *rettv);
399static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200400static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
402static void f_win_getid(typval_T *argvars, typval_T *rettv);
403static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
404static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
405static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100406static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407static void f_winbufnr(typval_T *argvars, typval_T *rettv);
408static void f_wincol(typval_T *argvars, typval_T *rettv);
409static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200410static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_winline(typval_T *argvars, typval_T *rettv);
412static void f_winnr(typval_T *argvars, typval_T *rettv);
413static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
414static void f_winrestview(typval_T *argvars, typval_T *rettv);
415static void f_winsaveview(typval_T *argvars, typval_T *rettv);
416static void f_winwidth(typval_T *argvars, typval_T *rettv);
417static void f_writefile(typval_T *argvars, typval_T *rettv);
418static void f_wordcount(typval_T *argvars, typval_T *rettv);
419static void f_xor(typval_T *argvars, typval_T *rettv);
420
421/*
422 * Array with names and number of arguments of all internal functions
423 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
424 */
425static struct fst
426{
427 char *f_name; /* function name */
428 char f_min_argc; /* minimal number of arguments */
429 char f_max_argc; /* maximal number of arguments */
430 void (*f_func)(typval_T *args, typval_T *rvar);
431 /* implementation of function */
432} functions[] =
433{
434#ifdef FEAT_FLOAT
435 {"abs", 1, 1, f_abs},
436 {"acos", 1, 1, f_acos}, /* WJMc */
437#endif
438 {"add", 2, 2, f_add},
439 {"and", 2, 2, f_and},
440 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200441 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200442 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443 {"argidx", 0, 0, f_argidx},
444 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200445 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
447 {"asin", 1, 1, f_asin}, /* WJMc */
448#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100449 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100451 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200453 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100455 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200456 {"assert_match", 2, 3, f_assert_match},
457 {"assert_notequal", 2, 3, f_assert_notequal},
458 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100459 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200460 {"assert_true", 1, 2, f_assert_true},
461#ifdef FEAT_FLOAT
462 {"atan", 1, 1, f_atan},
463 {"atan2", 2, 2, f_atan2},
464#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100465#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200466 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100467 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100468# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100469 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100470# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100471#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200472 {"browse", 4, 4, f_browse},
473 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200474 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475 {"bufexists", 1, 1, f_bufexists},
476 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
477 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
478 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
479 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200480 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200481 {"bufloaded", 1, 1, f_bufloaded},
482 {"bufname", 1, 1, f_bufname},
483 {"bufnr", 1, 2, f_bufnr},
484 {"bufwinid", 1, 1, f_bufwinid},
485 {"bufwinnr", 1, 1, f_bufwinnr},
486 {"byte2line", 1, 1, f_byte2line},
487 {"byteidx", 2, 2, f_byteidx},
488 {"byteidxcomp", 2, 2, f_byteidxcomp},
489 {"call", 2, 3, f_call},
490#ifdef FEAT_FLOAT
491 {"ceil", 1, 1, f_ceil},
492#endif
493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100494 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200496 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
498 {"ch_evalraw", 2, 3, f_ch_evalraw},
499 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
500 {"ch_getjob", 1, 1, f_ch_getjob},
501 {"ch_info", 1, 1, f_ch_info},
502 {"ch_log", 1, 2, f_ch_log},
503 {"ch_logfile", 1, 2, f_ch_logfile},
504 {"ch_open", 1, 2, f_ch_open},
505 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100506 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"ch_readraw", 1, 2, f_ch_readraw},
508 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
509 {"ch_sendraw", 2, 3, f_ch_sendraw},
510 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200511 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512#endif
513 {"changenr", 0, 0, f_changenr},
514 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200515 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100517 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"col", 1, 1, f_col},
519#if defined(FEAT_INS_EXPAND)
520 {"complete", 2, 2, f_complete},
521 {"complete_add", 1, 1, f_complete_add},
522 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100523 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200524#endif
525 {"confirm", 1, 4, f_confirm},
526 {"copy", 1, 1, f_copy},
527#ifdef FEAT_FLOAT
528 {"cos", 1, 1, f_cos},
529 {"cosh", 1, 1, f_cosh},
530#endif
531 {"count", 2, 4, f_count},
532 {"cscope_connection",0,3, f_cscope_connection},
533 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100534#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200535 {"debugbreak", 1, 1, f_debugbreak},
536#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"deepcopy", 1, 2, f_deepcopy},
538 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200539 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"did_filetype", 0, 0, f_did_filetype},
541 {"diff_filler", 1, 1, f_diff_filler},
542 {"diff_hlID", 2, 2, f_diff_hlID},
543 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200544 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"escape", 2, 2, f_escape},
546 {"eval", 1, 1, f_eval},
547 {"eventhandler", 0, 0, f_eventhandler},
548 {"executable", 1, 1, f_executable},
549 {"execute", 1, 2, f_execute},
550 {"exepath", 1, 1, f_exepath},
551 {"exists", 1, 1, f_exists},
552#ifdef FEAT_FLOAT
553 {"exp", 1, 1, f_exp},
554#endif
555 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200556 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200557 {"extend", 2, 3, f_extend},
558 {"feedkeys", 1, 2, f_feedkeys},
559 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
560 {"filereadable", 1, 1, f_filereadable},
561 {"filewritable", 1, 1, f_filewritable},
562 {"filter", 2, 2, f_filter},
563 {"finddir", 1, 3, f_finddir},
564 {"findfile", 1, 3, f_findfile},
565#ifdef FEAT_FLOAT
566 {"float2nr", 1, 1, f_float2nr},
567 {"floor", 1, 1, f_floor},
568 {"fmod", 2, 2, f_fmod},
569#endif
570 {"fnameescape", 1, 1, f_fnameescape},
571 {"fnamemodify", 2, 2, f_fnamemodify},
572 {"foldclosed", 1, 1, f_foldclosed},
573 {"foldclosedend", 1, 1, f_foldclosedend},
574 {"foldlevel", 1, 1, f_foldlevel},
575 {"foldtext", 0, 0, f_foldtext},
576 {"foldtextresult", 1, 1, f_foldtextresult},
577 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200578 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200579 {"function", 1, 3, f_function},
580 {"garbagecollect", 0, 1, f_garbagecollect},
581 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200582 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200583 {"getbufline", 2, 3, f_getbufline},
584 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100585 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200586 {"getchar", 0, 1, f_getchar},
587 {"getcharmod", 0, 0, f_getcharmod},
588 {"getcharsearch", 0, 0, f_getcharsearch},
589 {"getcmdline", 0, 0, f_getcmdline},
590 {"getcmdpos", 0, 0, f_getcmdpos},
591 {"getcmdtype", 0, 0, f_getcmdtype},
592 {"getcmdwintype", 0, 0, f_getcmdwintype},
593#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200594 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595#endif
596 {"getcurpos", 0, 0, f_getcurpos},
597 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200598 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"getfontname", 0, 1, f_getfontname},
600 {"getfperm", 1, 1, f_getfperm},
601 {"getfsize", 1, 1, f_getfsize},
602 {"getftime", 1, 1, f_getftime},
603 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100604 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200606 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100607 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"getpid", 0, 0, f_getpid},
609 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200610 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"getreg", 0, 3, f_getreg},
612 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200613 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200614 {"gettabvar", 2, 3, f_gettabvar},
615 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100616 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200617 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100618 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200619 {"getwinposx", 0, 0, f_getwinposx},
620 {"getwinposy", 0, 0, f_getwinposy},
621 {"getwinvar", 2, 3, f_getwinvar},
622 {"glob", 1, 4, f_glob},
623 {"glob2regpat", 1, 1, f_glob2regpat},
624 {"globpath", 2, 5, f_globpath},
625 {"has", 1, 1, f_has},
626 {"has_key", 2, 2, f_has_key},
627 {"haslocaldir", 0, 2, f_haslocaldir},
628 {"hasmapto", 1, 3, f_hasmapto},
629 {"highlightID", 1, 1, f_hlID}, /* obsolete */
630 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
631 {"histadd", 2, 2, f_histadd},
632 {"histdel", 1, 2, f_histdel},
633 {"histget", 1, 2, f_histget},
634 {"histnr", 1, 1, f_histnr},
635 {"hlID", 1, 1, f_hlID},
636 {"hlexists", 1, 1, f_hlexists},
637 {"hostname", 0, 0, f_hostname},
638 {"iconv", 3, 3, f_iconv},
639 {"indent", 1, 1, f_indent},
640 {"index", 2, 4, f_index},
641 {"input", 1, 3, f_input},
642 {"inputdialog", 1, 3, f_inputdialog},
643 {"inputlist", 1, 1, f_inputlist},
644 {"inputrestore", 0, 0, f_inputrestore},
645 {"inputsave", 0, 0, f_inputsave},
646 {"inputsecret", 1, 2, f_inputsecret},
647 {"insert", 2, 3, f_insert},
648 {"invert", 1, 1, f_invert},
649 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200650#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
651 {"isinf", 1, 1, f_isinf},
652#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"islocked", 1, 1, f_islocked},
654#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
655 {"isnan", 1, 1, f_isnan},
656#endif
657 {"items", 1, 1, f_items},
658#ifdef FEAT_JOB_CHANNEL
659 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200660 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"job_setoptions", 2, 2, f_job_setoptions},
662 {"job_start", 1, 2, f_job_start},
663 {"job_status", 1, 1, f_job_status},
664 {"job_stop", 1, 2, f_job_stop},
665#endif
666 {"join", 1, 2, f_join},
667 {"js_decode", 1, 1, f_js_decode},
668 {"js_encode", 1, 1, f_js_encode},
669 {"json_decode", 1, 1, f_json_decode},
670 {"json_encode", 1, 1, f_json_encode},
671 {"keys", 1, 1, f_keys},
672 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
673 {"len", 1, 1, f_len},
674 {"libcall", 3, 3, f_libcall},
675 {"libcallnr", 3, 3, f_libcallnr},
676 {"line", 1, 1, f_line},
677 {"line2byte", 1, 1, f_line2byte},
678 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200679 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200680 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200681 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200682 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683 {"localtime", 0, 0, f_localtime},
684#ifdef FEAT_FLOAT
685 {"log", 1, 1, f_log},
686 {"log10", 1, 1, f_log10},
687#endif
688#ifdef FEAT_LUA
689 {"luaeval", 1, 2, f_luaeval},
690#endif
691 {"map", 2, 2, f_map},
692 {"maparg", 1, 4, f_maparg},
693 {"mapcheck", 1, 3, f_mapcheck},
694 {"match", 2, 4, f_match},
695 {"matchadd", 2, 5, f_matchadd},
696 {"matchaddpos", 2, 5, f_matchaddpos},
697 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100698 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"matchend", 2, 4, f_matchend},
700 {"matchlist", 2, 4, f_matchlist},
701 {"matchstr", 2, 4, f_matchstr},
702 {"matchstrpos", 2, 4, f_matchstrpos},
703 {"max", 1, 1, f_max},
704 {"min", 1, 1, f_min},
705#ifdef vim_mkdir
706 {"mkdir", 1, 3, f_mkdir},
707#endif
708 {"mode", 0, 1, f_mode},
709#ifdef FEAT_MZSCHEME
710 {"mzeval", 1, 1, f_mzeval},
711#endif
712 {"nextnonblank", 1, 1, f_nextnonblank},
713 {"nr2char", 1, 2, f_nr2char},
714 {"or", 2, 2, f_or},
715 {"pathshorten", 1, 1, f_pathshorten},
716#ifdef FEAT_PERL
717 {"perleval", 1, 1, f_perleval},
718#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200719#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200720 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200721 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200722 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200723 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200724 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200725 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200726 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200727 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200728 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200729 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200730 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200731 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200732 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200733 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200734 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200735 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200736 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200737 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200739#ifdef FEAT_FLOAT
740 {"pow", 2, 2, f_pow},
741#endif
742 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100743 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200744#ifdef FEAT_JOB_CHANNEL
745 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200746 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200747 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
748#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100749#ifdef FEAT_TEXT_PROP
750 {"prop_add", 3, 3, f_prop_add},
751 {"prop_clear", 1, 3, f_prop_clear},
752 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100753 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100754 {"prop_type_add", 2, 2, f_prop_type_add},
755 {"prop_type_change", 2, 2, f_prop_type_change},
756 {"prop_type_delete", 1, 2, f_prop_type_delete},
757 {"prop_type_get", 1, 2, f_prop_type_get},
758 {"prop_type_list", 0, 1, f_prop_type_list},
759#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200760 {"pumvisible", 0, 0, f_pumvisible},
761#ifdef FEAT_PYTHON3
762 {"py3eval", 1, 1, f_py3eval},
763#endif
764#ifdef FEAT_PYTHON
765 {"pyeval", 1, 1, f_pyeval},
766#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100767#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
768 {"pyxeval", 1, 1, f_pyxeval},
769#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200770 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200771 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200772 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200773 {"reg_executing", 0, 0, f_reg_executing},
774 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"reltime", 0, 2, f_reltime},
776#ifdef FEAT_FLOAT
777 {"reltimefloat", 1, 1, f_reltimefloat},
778#endif
779 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100780 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"remote_foreground", 1, 1, f_remote_foreground},
782 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100783 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100785 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"remove", 2, 3, f_remove},
787 {"rename", 2, 2, f_rename},
788 {"repeat", 2, 2, f_repeat},
789 {"resolve", 1, 1, f_resolve},
790 {"reverse", 1, 1, f_reverse},
791#ifdef FEAT_FLOAT
792 {"round", 1, 1, f_round},
793#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100794#ifdef FEAT_RUBY
795 {"rubyeval", 1, 1, f_rubyeval},
796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"screenattr", 2, 2, f_screenattr},
798 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100799 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200801 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200802 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100803 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804 {"search", 1, 4, f_search},
805 {"searchdecl", 1, 3, f_searchdecl},
806 {"searchpair", 3, 7, f_searchpair},
807 {"searchpairpos", 3, 7, f_searchpairpos},
808 {"searchpos", 1, 4, f_searchpos},
809 {"server2client", 2, 2, f_server2client},
810 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200811 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setbufvar", 3, 3, f_setbufvar},
813 {"setcharsearch", 1, 1, f_setcharsearch},
814 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200815 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816 {"setfperm", 2, 2, f_setfperm},
817 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200818 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100819 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200821 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"setreg", 2, 3, f_setreg},
823 {"settabvar", 3, 3, f_settabvar},
824 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100825 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"setwinvar", 3, 3, f_setwinvar},
827#ifdef FEAT_CRYPT
828 {"sha256", 1, 1, f_sha256},
829#endif
830 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100831 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100832#ifdef FEAT_SIGNS
833 {"sign_define", 1, 2, f_sign_define},
834 {"sign_getdefined", 0, 1, f_sign_getdefined},
835 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100836 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100837 {"sign_place", 4, 5, f_sign_place},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200838 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100839 {"sign_undefine", 0, 1, f_sign_undefine},
840 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200841 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100842#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"simplify", 1, 1, f_simplify},
844#ifdef FEAT_FLOAT
845 {"sin", 1, 1, f_sin},
846 {"sinh", 1, 1, f_sinh},
847#endif
848 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200849#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200850 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200851 {"sound_playevent", 1, 2, f_sound_playevent},
852 {"sound_playfile", 1, 2, f_sound_playfile},
853 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200854#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"soundfold", 1, 1, f_soundfold},
856 {"spellbadword", 0, 1, f_spellbadword},
857 {"spellsuggest", 1, 3, f_spellsuggest},
858 {"split", 1, 3, f_split},
859#ifdef FEAT_FLOAT
860 {"sqrt", 1, 1, f_sqrt},
861 {"str2float", 1, 1, f_str2float},
862#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200863 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"str2nr", 1, 2, f_str2nr},
865 {"strcharpart", 2, 3, f_strcharpart},
866 {"strchars", 1, 2, f_strchars},
867 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
868#ifdef HAVE_STRFTIME
869 {"strftime", 1, 2, f_strftime},
870#endif
871 {"strgetchar", 2, 2, f_strgetchar},
872 {"stridx", 2, 3, f_stridx},
873 {"string", 1, 1, f_string},
874 {"strlen", 1, 1, f_strlen},
875 {"strpart", 2, 3, f_strpart},
876 {"strridx", 2, 3, f_strridx},
877 {"strtrans", 1, 1, f_strtrans},
878 {"strwidth", 1, 1, f_strwidth},
879 {"submatch", 1, 2, f_submatch},
880 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200881 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200882 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883 {"synID", 3, 3, f_synID},
884 {"synIDattr", 2, 3, f_synIDattr},
885 {"synIDtrans", 1, 1, f_synIDtrans},
886 {"synconcealed", 2, 2, f_synconcealed},
887 {"synstack", 2, 2, f_synstack},
888 {"system", 1, 2, f_system},
889 {"systemlist", 1, 2, f_systemlist},
890 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
891 {"tabpagenr", 0, 1, f_tabpagenr},
892 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
893 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100894 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895#ifdef FEAT_FLOAT
896 {"tan", 1, 1, f_tan},
897 {"tanh", 1, 1, f_tanh},
898#endif
899 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200900#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100901 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
902 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100903 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200904 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200905# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
906 {"term_getansicolors", 1, 1, f_term_getansicolors},
907# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200909 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200911 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200912 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200914 {"term_getstatus", 1, 1, f_term_getstatus},
915 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200916 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200918 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200919 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200920# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
921 {"term_setansicolors", 2, 2, f_term_setansicolors},
922# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100923 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100924 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200925 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200926 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200927 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
930 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200931 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200932 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200933 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200934 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100935 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100936 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200937#ifdef FEAT_JOB_CHANNEL
938 {"test_null_channel", 0, 0, f_test_null_channel},
939#endif
940 {"test_null_dict", 0, 0, f_test_null_dict},
941#ifdef FEAT_JOB_CHANNEL
942 {"test_null_job", 0, 0, f_test_null_job},
943#endif
944 {"test_null_list", 0, 0, f_test_null_list},
945 {"test_null_partial", 0, 0, f_test_null_partial},
946 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200947 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100948 {"test_override", 2, 2, f_test_override},
949 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200950#ifdef FEAT_GUI
951 {"test_scrollbar", 3, 3, f_test_scrollbar},
952#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200953#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200954 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200955#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956 {"test_settime", 1, 1, f_test_settime},
957#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200958 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200959 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200960 {"timer_start", 2, 3, f_timer_start},
961 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200962 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200963#endif
964 {"tolower", 1, 1, f_tolower},
965 {"toupper", 1, 1, f_toupper},
966 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100967 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968#ifdef FEAT_FLOAT
969 {"trunc", 1, 1, f_trunc},
970#endif
971 {"type", 1, 1, f_type},
972 {"undofile", 1, 1, f_undofile},
973 {"undotree", 0, 0, f_undotree},
974 {"uniq", 1, 3, f_uniq},
975 {"values", 1, 1, f_values},
976 {"virtcol", 1, 1, f_virtcol},
977 {"visualmode", 0, 1, f_visualmode},
978 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200979 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"win_findbuf", 1, 1, f_win_findbuf},
981 {"win_getid", 0, 2, f_win_getid},
982 {"win_gotoid", 1, 1, f_win_gotoid},
983 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
984 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100985 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200986 {"winbufnr", 1, 1, f_winbufnr},
987 {"wincol", 0, 0, f_wincol},
988 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200989 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200990 {"winline", 0, 0, f_winline},
991 {"winnr", 0, 1, f_winnr},
992 {"winrestcmd", 0, 0, f_winrestcmd},
993 {"winrestview", 1, 1, f_winrestview},
994 {"winsaveview", 0, 0, f_winsaveview},
995 {"winwidth", 1, 1, f_winwidth},
996 {"wordcount", 0, 0, f_wordcount},
997 {"writefile", 2, 3, f_writefile},
998 {"xor", 2, 2, f_xor},
999};
1000
1001#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1002
1003/*
1004 * Function given to ExpandGeneric() to obtain the list of internal
1005 * or user defined function names.
1006 */
1007 char_u *
1008get_function_name(expand_T *xp, int idx)
1009{
1010 static int intidx = -1;
1011 char_u *name;
1012
1013 if (idx == 0)
1014 intidx = -1;
1015 if (intidx < 0)
1016 {
1017 name = get_user_func_name(xp, idx);
1018 if (name != NULL)
1019 return name;
1020 }
1021 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1022 {
1023 STRCPY(IObuff, functions[intidx].f_name);
1024 STRCAT(IObuff, "(");
1025 if (functions[intidx].f_max_argc == 0)
1026 STRCAT(IObuff, ")");
1027 return IObuff;
1028 }
1029
1030 return NULL;
1031}
1032
1033/*
1034 * Function given to ExpandGeneric() to obtain the list of internal or
1035 * user defined variable or function names.
1036 */
1037 char_u *
1038get_expr_name(expand_T *xp, int idx)
1039{
1040 static int intidx = -1;
1041 char_u *name;
1042
1043 if (idx == 0)
1044 intidx = -1;
1045 if (intidx < 0)
1046 {
1047 name = get_function_name(xp, idx);
1048 if (name != NULL)
1049 return name;
1050 }
1051 return get_user_var_name(xp, ++intidx);
1052}
1053
1054#endif /* FEAT_CMDL_COMPL */
1055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001056/*
1057 * Find internal function in table above.
1058 * Return index, or -1 if not found
1059 */
1060 int
1061find_internal_func(
1062 char_u *name) /* name of the function */
1063{
1064 int first = 0;
1065 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1066 int cmp;
1067 int x;
1068
1069 /*
1070 * Find the function name in the table. Binary search.
1071 */
1072 while (first <= last)
1073 {
1074 x = first + ((unsigned)(last - first) >> 1);
1075 cmp = STRCMP(name, functions[x].f_name);
1076 if (cmp < 0)
1077 last = x - 1;
1078 else if (cmp > 0)
1079 first = x + 1;
1080 else
1081 return x;
1082 }
1083 return -1;
1084}
1085
1086 int
1087call_internal_func(
1088 char_u *name,
1089 int argcount,
1090 typval_T *argvars,
1091 typval_T *rettv)
1092{
1093 int i;
1094
1095 i = find_internal_func(name);
1096 if (i < 0)
1097 return ERROR_UNKNOWN;
1098 if (argcount < functions[i].f_min_argc)
1099 return ERROR_TOOFEW;
1100 if (argcount > functions[i].f_max_argc)
1101 return ERROR_TOOMANY;
1102 argvars[argcount].v_type = VAR_UNKNOWN;
1103 functions[i].f_func(argvars, rettv);
1104 return ERROR_NONE;
1105}
1106
1107/*
1108 * Return TRUE for a non-zero Number and a non-empty String.
1109 */
1110 static int
1111non_zero_arg(typval_T *argvars)
1112{
1113 return ((argvars[0].v_type == VAR_NUMBER
1114 && argvars[0].vval.v_number != 0)
1115 || (argvars[0].v_type == VAR_SPECIAL
1116 && argvars[0].vval.v_number == VVAL_TRUE)
1117 || (argvars[0].v_type == VAR_STRING
1118 && argvars[0].vval.v_string != NULL
1119 && *argvars[0].vval.v_string != NUL));
1120}
1121
1122/*
1123 * Get the lnum from the first argument.
1124 * Also accepts ".", "$", etc., but that only works for the current buffer.
1125 * Returns -1 on error.
1126 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001127 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001128tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001129{
1130 typval_T rettv;
1131 linenr_T lnum;
1132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001133 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001134 if (lnum == 0) /* no valid number, try using line() */
1135 {
1136 rettv.v_type = VAR_NUMBER;
1137 f_line(argvars, &rettv);
1138 lnum = (linenr_T)rettv.vval.v_number;
1139 clear_tv(&rettv);
1140 }
1141 return lnum;
1142}
1143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144/*
1145 * Get the lnum from the first argument.
1146 * Also accepts "$", then "buf" is used.
1147 * Returns 0 on error.
1148 */
1149 static linenr_T
1150tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1151{
1152 if (argvars[0].v_type == VAR_STRING
1153 && argvars[0].vval.v_string != NULL
1154 && argvars[0].vval.v_string[0] == '$'
1155 && buf != NULL)
1156 return buf->b_ml.ml_line_count;
1157 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1158}
1159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001160#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001161/*
1162 * Get the float value of "argvars[0]" into "f".
1163 * Returns FAIL when the argument is not a Number or Float.
1164 */
1165 static int
1166get_float_arg(typval_T *argvars, float_T *f)
1167{
1168 if (argvars[0].v_type == VAR_FLOAT)
1169 {
1170 *f = argvars[0].vval.v_float;
1171 return OK;
1172 }
1173 if (argvars[0].v_type == VAR_NUMBER)
1174 {
1175 *f = (float_T)argvars[0].vval.v_number;
1176 return OK;
1177 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001178 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001179 return FAIL;
1180}
1181
1182/*
1183 * "abs(expr)" function
1184 */
1185 static void
1186f_abs(typval_T *argvars, typval_T *rettv)
1187{
1188 if (argvars[0].v_type == VAR_FLOAT)
1189 {
1190 rettv->v_type = VAR_FLOAT;
1191 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1192 }
1193 else
1194 {
1195 varnumber_T n;
1196 int error = FALSE;
1197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001198 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199 if (error)
1200 rettv->vval.v_number = -1;
1201 else if (n > 0)
1202 rettv->vval.v_number = n;
1203 else
1204 rettv->vval.v_number = -n;
1205 }
1206}
1207
1208/*
1209 * "acos()" function
1210 */
1211 static void
1212f_acos(typval_T *argvars, typval_T *rettv)
1213{
1214 float_T f = 0.0;
1215
1216 rettv->v_type = VAR_FLOAT;
1217 if (get_float_arg(argvars, &f) == OK)
1218 rettv->vval.v_float = acos(f);
1219 else
1220 rettv->vval.v_float = 0.0;
1221}
1222#endif
1223
1224/*
1225 * "add(list, item)" function
1226 */
1227 static void
1228f_add(typval_T *argvars, typval_T *rettv)
1229{
1230 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001232
1233 rettv->vval.v_number = 1; /* Default: Failed */
1234 if (argvars[0].v_type == VAR_LIST)
1235 {
1236 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001237 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238 (char_u *)N_("add() argument"), TRUE)
1239 && list_append_tv(l, &argvars[1]) == OK)
1240 copy_tv(&argvars[0], rettv);
1241 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001242 else if (argvars[0].v_type == VAR_BLOB)
1243 {
1244 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001245 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001246 (char_u *)N_("add() argument"), TRUE))
1247 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001248 int error = FALSE;
1249 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1250
1251 if (!error)
1252 {
1253 ga_append(&b->bv_ga, (int)n);
1254 copy_tv(&argvars[0], rettv);
1255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001256 }
1257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001258 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001259 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260}
1261
1262/*
1263 * "and(expr, expr)" function
1264 */
1265 static void
1266f_and(typval_T *argvars, typval_T *rettv)
1267{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001268 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1269 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001270}
1271
1272/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001273 * If there is a window for "curbuf", make it the current window.
1274 */
1275 static void
1276find_win_for_curbuf(void)
1277{
1278 wininfo_T *wip;
1279
1280 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1281 {
1282 if (wip->wi_win != NULL)
1283 {
1284 curwin = wip->wi_win;
1285 break;
1286 }
1287 }
1288}
1289
1290/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001291 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001292 */
1293 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001294set_buffer_lines(
1295 buf_T *buf,
1296 linenr_T lnum_arg,
1297 int append,
1298 typval_T *lines,
1299 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300{
Bram Moolenaarca851592018-06-06 21:04:07 +02001301 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1302 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303 list_T *l = NULL;
1304 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 linenr_T append_lnum;
1307 buf_T *curbuf_save = NULL;
1308 win_T *curwin_save = NULL;
1309 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310
Bram Moolenaarca851592018-06-06 21:04:07 +02001311 /* When using the current buffer ml_mfp will be set if needed. Useful when
1312 * setline() is used on startup. For other buffers the buffer must be
1313 * loaded. */
1314 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 rettv->vval.v_number = 1; /* FAIL */
1317 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 }
1319
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001321 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001322 curbuf_save = curbuf;
1323 curwin_save = curwin;
1324 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001325 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001326 }
1327
1328 if (append)
1329 // appendbufline() uses the line number below which we insert
1330 append_lnum = lnum - 1;
1331 else
1332 // setbufline() uses the line number above which we insert, we only
1333 // append if it's below the last line
1334 append_lnum = curbuf->b_ml.ml_line_count;
1335
1336 if (lines->v_type == VAR_LIST)
1337 {
1338 l = lines->vval.v_list;
1339 li = l->lv_first;
1340 }
1341 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001343
1344 /* default result is zero == OK */
1345 for (;;)
1346 {
1347 if (l != NULL)
1348 {
1349 /* list argument, get next string */
1350 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001352 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 li = li->li_next;
1354 }
1355
Bram Moolenaarca851592018-06-06 21:04:07 +02001356 rettv->vval.v_number = 1; /* FAIL */
1357 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1358 break;
1359
1360 /* When coming here from Insert mode, sync undo, so that this can be
1361 * undone separately from what was previously inserted. */
1362 if (u_sync_once == 2)
1363 {
1364 u_sync_once = 1; /* notify that u_sync() was called */
1365 u_sync(TRUE);
1366 }
1367
1368 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1369 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001370 // Existing line, replace it.
1371 // Removes any existing text properties.
1372 if (u_savesub(lnum) == OK && ml_replace_len(
1373 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 {
1375 changed_bytes(lnum, 0);
1376 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1377 check_cursor_col();
1378 rettv->vval.v_number = 0; /* OK */
1379 }
1380 }
1381 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1382 {
1383 /* append the line */
1384 ++added;
1385 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1386 rettv->vval.v_number = 0; /* OK */
1387 }
1388
1389 if (l == NULL) /* only one string argument */
1390 break;
1391 ++lnum;
1392 }
1393
1394 if (added > 0)
1395 {
1396 win_T *wp;
1397 tabpage_T *tp;
1398
1399 appended_lines_mark(append_lnum, added);
1400 FOR_ALL_TAB_WINDOWS(tp, wp)
1401 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1402 wp->w_cursor.lnum += added;
1403 check_cursor_col();
1404
Bram Moolenaarf2732452018-06-03 14:47:35 +02001405#ifdef FEAT_JOB_CHANNEL
1406 if (bt_prompt(curbuf) && (State & INSERT))
1407 // show the line with the prompt
1408 update_topline();
1409#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001410 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001411
1412 if (!is_curbuf)
1413 {
1414 curbuf = curbuf_save;
1415 curwin = curwin_save;
1416 }
1417}
1418
1419/*
1420 * "append(lnum, string/list)" function
1421 */
1422 static void
1423f_append(typval_T *argvars, typval_T *rettv)
1424{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001425 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001426
1427 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1428}
1429
1430/*
1431 * "appendbufline(buf, lnum, string/list)" function
1432 */
1433 static void
1434f_appendbufline(typval_T *argvars, typval_T *rettv)
1435{
1436 linenr_T lnum;
1437 buf_T *buf;
1438
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001439 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001440 if (buf == NULL)
1441 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001442 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001444 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001445 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1446 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001447}
1448
1449/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001450 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001451 */
1452 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001453f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001454{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001455 win_T *wp;
1456
1457 if (argvars[0].v_type == VAR_UNKNOWN)
1458 // use the current window
1459 rettv->vval.v_number = ARGCOUNT;
1460 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001461 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001462 // use the global argument list
1463 rettv->vval.v_number = GARGCOUNT;
1464 else
1465 {
1466 // use the argument list of the specified window
1467 wp = find_win_by_nr_or_id(&argvars[0]);
1468 if (wp != NULL)
1469 rettv->vval.v_number = WARGCOUNT(wp);
1470 else
1471 rettv->vval.v_number = -1;
1472 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001473}
1474
1475/*
1476 * "argidx()" function
1477 */
1478 static void
1479f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1480{
1481 rettv->vval.v_number = curwin->w_arg_idx;
1482}
1483
1484/*
1485 * "arglistid()" function
1486 */
1487 static void
1488f_arglistid(typval_T *argvars, typval_T *rettv)
1489{
1490 win_T *wp;
1491
1492 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001493 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494 if (wp != NULL)
1495 rettv->vval.v_number = wp->w_alist->id;
1496}
1497
1498/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001499 * Get the argument list for a given window
1500 */
1501 static void
1502get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1503{
1504 int idx;
1505
1506 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1507 for (idx = 0; idx < argcount; ++idx)
1508 list_append_string(rettv->vval.v_list,
1509 alist_name(&arglist[idx]), -1);
1510}
1511
1512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513 * "argv(nr)" function
1514 */
1515 static void
1516f_argv(typval_T *argvars, typval_T *rettv)
1517{
1518 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519 aentry_T *arglist = NULL;
1520 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521
1522 if (argvars[0].v_type != VAR_UNKNOWN)
1523 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001524 if (argvars[1].v_type == VAR_UNKNOWN)
1525 {
1526 arglist = ARGLIST;
1527 argcount = ARGCOUNT;
1528 }
1529 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001530 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001531 {
1532 arglist = GARGLIST;
1533 argcount = GARGCOUNT;
1534 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001536 {
1537 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1538
1539 if (wp != NULL)
1540 {
1541 /* Use the argument list of the specified window */
1542 arglist = WARGLIST(wp);
1543 argcount = WARGCOUNT(wp);
1544 }
1545 }
1546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001547 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001548 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 if (arglist != NULL && idx >= 0 && idx < argcount)
1551 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1552 else if (idx == -1)
1553 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001555 else
1556 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557}
1558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001559#ifdef FEAT_FLOAT
1560/*
1561 * "asin()" function
1562 */
1563 static void
1564f_asin(typval_T *argvars, typval_T *rettv)
1565{
1566 float_T f = 0.0;
1567
1568 rettv->v_type = VAR_FLOAT;
1569 if (get_float_arg(argvars, &f) == OK)
1570 rettv->vval.v_float = asin(f);
1571 else
1572 rettv->vval.v_float = 0.0;
1573}
1574
1575/*
1576 * "atan()" function
1577 */
1578 static void
1579f_atan(typval_T *argvars, typval_T *rettv)
1580{
1581 float_T f = 0.0;
1582
1583 rettv->v_type = VAR_FLOAT;
1584 if (get_float_arg(argvars, &f) == OK)
1585 rettv->vval.v_float = atan(f);
1586 else
1587 rettv->vval.v_float = 0.0;
1588}
1589
1590/*
1591 * "atan2()" function
1592 */
1593 static void
1594f_atan2(typval_T *argvars, typval_T *rettv)
1595{
1596 float_T fx = 0.0, fy = 0.0;
1597
1598 rettv->v_type = VAR_FLOAT;
1599 if (get_float_arg(argvars, &fx) == OK
1600 && get_float_arg(&argvars[1], &fy) == OK)
1601 rettv->vval.v_float = atan2(fx, fy);
1602 else
1603 rettv->vval.v_float = 0.0;
1604}
1605#endif
1606
1607/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001608 * "balloon_show()" function
1609 */
1610#ifdef FEAT_BEVAL
1611 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001612f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1613{
1614 rettv->v_type = VAR_STRING;
1615 if (balloonEval != NULL)
1616 {
1617 if (balloonEval->msg == NULL)
1618 rettv->vval.v_string = NULL;
1619 else
1620 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1621 }
1622}
1623
1624 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001625f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1626{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001627 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001628 {
1629 if (argvars[0].v_type == VAR_LIST
1630# ifdef FEAT_GUI
1631 && !gui.in_use
1632# endif
1633 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001634 {
1635 list_T *l = argvars[0].vval.v_list;
1636
1637 // empty list removes the balloon
1638 post_balloon(balloonEval, NULL,
1639 l == NULL || l->lv_len == 0 ? NULL : l);
1640 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001641 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001642 {
1643 char_u *mesg = tv_get_string_chk(&argvars[0]);
1644
1645 if (mesg != NULL)
1646 // empty string removes the balloon
1647 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1648 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001649 }
1650}
1651
Bram Moolenaar669a8282017-11-19 20:13:05 +01001652# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001653 static void
1654f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1655{
1656 if (rettv_list_alloc(rettv) == OK)
1657 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001658 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001659
1660 if (msg != NULL)
1661 {
1662 pumitem_T *array;
1663 int size = split_message(msg, &array);
1664 int i;
1665
1666 /* Skip the first and last item, they are always empty. */
1667 for (i = 1; i < size - 1; ++i)
1668 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001669 while (size > 0)
1670 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001671 vim_free(array);
1672 }
1673 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001674}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001675# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001676#endif
1677
1678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679 * "browse(save, title, initdir, default)" function
1680 */
1681 static void
1682f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1683{
1684#ifdef FEAT_BROWSE
1685 int save;
1686 char_u *title;
1687 char_u *initdir;
1688 char_u *defname;
1689 char_u buf[NUMBUFLEN];
1690 char_u buf2[NUMBUFLEN];
1691 int error = FALSE;
1692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001693 save = (int)tv_get_number_chk(&argvars[0], &error);
1694 title = tv_get_string_chk(&argvars[1]);
1695 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1696 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001697
1698 if (error || title == NULL || initdir == NULL || defname == NULL)
1699 rettv->vval.v_string = NULL;
1700 else
1701 rettv->vval.v_string =
1702 do_browse(save ? BROWSE_SAVE : 0,
1703 title, defname, NULL, initdir, NULL, curbuf);
1704#else
1705 rettv->vval.v_string = NULL;
1706#endif
1707 rettv->v_type = VAR_STRING;
1708}
1709
1710/*
1711 * "browsedir(title, initdir)" function
1712 */
1713 static void
1714f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1715{
1716#ifdef FEAT_BROWSE
1717 char_u *title;
1718 char_u *initdir;
1719 char_u buf[NUMBUFLEN];
1720
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001721 title = tv_get_string_chk(&argvars[0]);
1722 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723
1724 if (title == NULL || initdir == NULL)
1725 rettv->vval.v_string = NULL;
1726 else
1727 rettv->vval.v_string = do_browse(BROWSE_DIR,
1728 title, NULL, NULL, initdir, NULL, curbuf);
1729#else
1730 rettv->vval.v_string = NULL;
1731#endif
1732 rettv->v_type = VAR_STRING;
1733}
1734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735/*
1736 * Find a buffer by number or exact name.
1737 */
1738 static buf_T *
1739find_buffer(typval_T *avar)
1740{
1741 buf_T *buf = NULL;
1742
1743 if (avar->v_type == VAR_NUMBER)
1744 buf = buflist_findnr((int)avar->vval.v_number);
1745 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1746 {
1747 buf = buflist_findname_exp(avar->vval.v_string);
1748 if (buf == NULL)
1749 {
1750 /* No full path name match, try a match with a URL or a "nofile"
1751 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001752 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001753 if (buf->b_fname != NULL
1754 && (path_with_url(buf->b_fname)
1755#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001756 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001757#endif
1758 )
1759 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1760 break;
1761 }
1762 }
1763 return buf;
1764}
1765
1766/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001767 * "bufadd(expr)" function
1768 */
1769 static void
1770f_bufadd(typval_T *argvars, typval_T *rettv)
1771{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001772 char_u *name = tv_get_string(&argvars[0]);
1773
1774 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001775}
1776
1777/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001778 * "bufexists(expr)" function
1779 */
1780 static void
1781f_bufexists(typval_T *argvars, typval_T *rettv)
1782{
1783 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1784}
1785
1786/*
1787 * "buflisted(expr)" function
1788 */
1789 static void
1790f_buflisted(typval_T *argvars, typval_T *rettv)
1791{
1792 buf_T *buf;
1793
1794 buf = find_buffer(&argvars[0]);
1795 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1796}
1797
1798/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001799 * "bufload(expr)" function
1800 */
1801 static void
1802f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1803{
1804 buf_T *buf = get_buf_arg(&argvars[0]);
1805
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001806 if (buf != NULL)
1807 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001808}
1809
1810/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001811 * "bufloaded(expr)" function
1812 */
1813 static void
1814f_bufloaded(typval_T *argvars, typval_T *rettv)
1815{
1816 buf_T *buf;
1817
1818 buf = find_buffer(&argvars[0]);
1819 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1820}
1821
1822 buf_T *
1823buflist_find_by_name(char_u *name, int curtab_only)
1824{
1825 int save_magic;
1826 char_u *save_cpo;
1827 buf_T *buf;
1828
1829 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1830 save_magic = p_magic;
1831 p_magic = TRUE;
1832 save_cpo = p_cpo;
1833 p_cpo = (char_u *)"";
1834
1835 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1836 TRUE, FALSE, curtab_only));
1837
1838 p_magic = save_magic;
1839 p_cpo = save_cpo;
1840 return buf;
1841}
1842
1843/*
1844 * Get buffer by number or pattern.
1845 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001846 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001847tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001848{
1849 char_u *name = tv->vval.v_string;
1850 buf_T *buf;
1851
1852 if (tv->v_type == VAR_NUMBER)
1853 return buflist_findnr((int)tv->vval.v_number);
1854 if (tv->v_type != VAR_STRING)
1855 return NULL;
1856 if (name == NULL || *name == NUL)
1857 return curbuf;
1858 if (name[0] == '$' && name[1] == NUL)
1859 return lastbuf;
1860
1861 buf = buflist_find_by_name(name, curtab_only);
1862
1863 /* If not found, try expanding the name, like done for bufexists(). */
1864 if (buf == NULL)
1865 buf = find_buffer(tv);
1866
1867 return buf;
1868}
1869
1870/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001871 * Get the buffer from "arg" and give an error and return NULL if it is not
1872 * valid.
1873 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001874 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001875get_buf_arg(typval_T *arg)
1876{
1877 buf_T *buf;
1878
1879 ++emsg_off;
1880 buf = tv_get_buf(arg, FALSE);
1881 --emsg_off;
1882 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001883 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001884 return buf;
1885}
1886
1887/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 * "bufname(expr)" function
1889 */
1890 static void
1891f_bufname(typval_T *argvars, typval_T *rettv)
1892{
1893 buf_T *buf;
1894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001897 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 rettv->v_type = VAR_STRING;
1899 if (buf != NULL && buf->b_fname != NULL)
1900 rettv->vval.v_string = vim_strsave(buf->b_fname);
1901 else
1902 rettv->vval.v_string = NULL;
1903 --emsg_off;
1904}
1905
1906/*
1907 * "bufnr(expr)" function
1908 */
1909 static void
1910f_bufnr(typval_T *argvars, typval_T *rettv)
1911{
1912 buf_T *buf;
1913 int error = FALSE;
1914 char_u *name;
1915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001918 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 --emsg_off;
1920
1921 /* If the buffer isn't found and the second argument is not zero create a
1922 * new buffer. */
1923 if (buf == NULL
1924 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001925 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001927 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 && !error)
1929 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1930
1931 if (buf != NULL)
1932 rettv->vval.v_number = buf->b_fnum;
1933 else
1934 rettv->vval.v_number = -1;
1935}
1936
1937 static void
1938buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1939{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001940 win_T *wp;
1941 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001942 buf_T *buf;
1943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001944 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001946 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 {
1949 ++winnr;
1950 if (wp->w_buffer == buf)
1951 break;
1952 }
1953 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001954 --emsg_off;
1955}
1956
1957/*
1958 * "bufwinid(nr)" function
1959 */
1960 static void
1961f_bufwinid(typval_T *argvars, typval_T *rettv)
1962{
1963 buf_win_common(argvars, rettv, FALSE);
1964}
1965
1966/*
1967 * "bufwinnr(nr)" function
1968 */
1969 static void
1970f_bufwinnr(typval_T *argvars, typval_T *rettv)
1971{
1972 buf_win_common(argvars, rettv, TRUE);
1973}
1974
1975/*
1976 * "byte2line(byte)" function
1977 */
1978 static void
1979f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1980{
1981#ifndef FEAT_BYTEOFF
1982 rettv->vval.v_number = -1;
1983#else
1984 long boff = 0;
1985
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001986 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001987 if (boff < 0)
1988 rettv->vval.v_number = -1;
1989 else
1990 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1991 (linenr_T)0, &boff);
1992#endif
1993}
1994
1995 static void
1996byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1997{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001998 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 char_u *str;
2000 varnumber_T idx;
2001
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002002 str = tv_get_string_chk(&argvars[0]);
2003 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004 rettv->vval.v_number = -1;
2005 if (str == NULL || idx < 0)
2006 return;
2007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 t = str;
2009 for ( ; idx > 0; idx--)
2010 {
2011 if (*t == NUL) /* EOL reached */
2012 return;
2013 if (enc_utf8 && comp)
2014 t += utf_ptr2len(t);
2015 else
2016 t += (*mb_ptr2len)(t);
2017 }
2018 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002019}
2020
2021/*
2022 * "byteidx()" function
2023 */
2024 static void
2025f_byteidx(typval_T *argvars, typval_T *rettv)
2026{
2027 byteidx(argvars, rettv, FALSE);
2028}
2029
2030/*
2031 * "byteidxcomp()" function
2032 */
2033 static void
2034f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2035{
2036 byteidx(argvars, rettv, TRUE);
2037}
2038
2039/*
2040 * "call(func, arglist [, dict])" function
2041 */
2042 static void
2043f_call(typval_T *argvars, typval_T *rettv)
2044{
2045 char_u *func;
2046 partial_T *partial = NULL;
2047 dict_T *selfdict = NULL;
2048
2049 if (argvars[1].v_type != VAR_LIST)
2050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002051 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 return;
2053 }
2054 if (argvars[1].vval.v_list == NULL)
2055 return;
2056
2057 if (argvars[0].v_type == VAR_FUNC)
2058 func = argvars[0].vval.v_string;
2059 else if (argvars[0].v_type == VAR_PARTIAL)
2060 {
2061 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002062 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 }
2064 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002065 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002066 if (*func == NUL)
2067 return; /* type error or empty name */
2068
2069 if (argvars[2].v_type != VAR_UNKNOWN)
2070 {
2071 if (argvars[2].v_type != VAR_DICT)
2072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 return;
2075 }
2076 selfdict = argvars[2].vval.v_dict;
2077 }
2078
2079 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2080}
2081
2082#ifdef FEAT_FLOAT
2083/*
2084 * "ceil({float})" function
2085 */
2086 static void
2087f_ceil(typval_T *argvars, typval_T *rettv)
2088{
2089 float_T f = 0.0;
2090
2091 rettv->v_type = VAR_FLOAT;
2092 if (get_float_arg(argvars, &f) == OK)
2093 rettv->vval.v_float = ceil(f);
2094 else
2095 rettv->vval.v_float = 0.0;
2096}
2097#endif
2098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099/*
2100 * "changenr()" function
2101 */
2102 static void
2103f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2104{
2105 rettv->vval.v_number = curbuf->b_u_seq_cur;
2106}
2107
2108/*
2109 * "char2nr(string)" function
2110 */
2111 static void
2112f_char2nr(typval_T *argvars, typval_T *rettv)
2113{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 if (has_mbyte)
2115 {
2116 int utf8 = 0;
2117
2118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002119 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120
2121 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002122 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002123 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002124 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125 }
2126 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002127 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128}
2129
2130/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002131 * "chdir(dir)" function
2132 */
2133 static void
2134f_chdir(typval_T *argvars, typval_T *rettv)
2135{
2136 char_u *cwd;
2137 cdscope_T scope = CDSCOPE_GLOBAL;
2138
2139 rettv->v_type = VAR_STRING;
2140 rettv->vval.v_string = NULL;
2141
2142 if (argvars[0].v_type != VAR_STRING)
2143 return;
2144
2145 // Return the current directory
2146 cwd = alloc(MAXPATHL);
2147 if (cwd != NULL)
2148 {
2149 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2150 {
2151#ifdef BACKSLASH_IN_FILENAME
2152 slash_adjust(cwd);
2153#endif
2154 rettv->vval.v_string = vim_strsave(cwd);
2155 }
2156 vim_free(cwd);
2157 }
2158
2159 if (curwin->w_localdir != NULL)
2160 scope = CDSCOPE_WINDOW;
2161 else if (curtab->tp_localdir != NULL)
2162 scope = CDSCOPE_TABPAGE;
2163
2164 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2165 // Directory change failed
2166 VIM_CLEAR(rettv->vval.v_string);
2167}
2168
2169/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 * "cindent(lnum)" function
2171 */
2172 static void
2173f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2174{
2175#ifdef FEAT_CINDENT
2176 pos_T pos;
2177 linenr_T lnum;
2178
2179 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2182 {
2183 curwin->w_cursor.lnum = lnum;
2184 rettv->vval.v_number = get_c_indent();
2185 curwin->w_cursor = pos;
2186 }
2187 else
2188#endif
2189 rettv->vval.v_number = -1;
2190}
2191
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002192 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002193get_optional_window(typval_T *argvars, int idx)
2194{
2195 win_T *win = curwin;
2196
2197 if (argvars[idx].v_type != VAR_UNKNOWN)
2198 {
2199 win = find_win_by_nr_or_id(&argvars[idx]);
2200 if (win == NULL)
2201 {
2202 emsg(_(e_invalwindow));
2203 return NULL;
2204 }
2205 }
2206 return win;
2207}
2208
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002210 * "col(string)" function
2211 */
2212 static void
2213f_col(typval_T *argvars, typval_T *rettv)
2214{
2215 colnr_T col = 0;
2216 pos_T *fp;
2217 int fnum = curbuf->b_fnum;
2218
2219 fp = var2fpos(&argvars[0], FALSE, &fnum);
2220 if (fp != NULL && fnum == curbuf->b_fnum)
2221 {
2222 if (fp->col == MAXCOL)
2223 {
2224 /* '> can be MAXCOL, get the length of the line then */
2225 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2226 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2227 else
2228 col = MAXCOL;
2229 }
2230 else
2231 {
2232 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 /* col(".") when the cursor is on the NUL at the end of the line
2234 * because of "coladd" can be seen as an extra column. */
2235 if (virtual_active() && fp == &curwin->w_cursor)
2236 {
2237 char_u *p = ml_get_cursor();
2238
2239 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2240 curwin->w_virtcol - curwin->w_cursor.coladd))
2241 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242 int l;
2243
2244 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2245 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 }
2247 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002248 }
2249 }
2250 rettv->vval.v_number = col;
2251}
2252
2253#if defined(FEAT_INS_EXPAND)
2254/*
2255 * "complete()" function
2256 */
2257 static void
2258f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2259{
2260 int startcol;
2261
2262 if ((State & INSERT) == 0)
2263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002264 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002265 return;
2266 }
2267
2268 /* Check for undo allowed here, because if something was already inserted
2269 * the line was already saved for undo and this check isn't done. */
2270 if (!undo_allowed())
2271 return;
2272
2273 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002275 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276 return;
2277 }
2278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002279 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 if (startcol <= 0)
2281 return;
2282
2283 set_completion(startcol - 1, argvars[1].vval.v_list);
2284}
2285
2286/*
2287 * "complete_add()" function
2288 */
2289 static void
2290f_complete_add(typval_T *argvars, typval_T *rettv)
2291{
2292 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2293}
2294
2295/*
2296 * "complete_check()" function
2297 */
2298 static void
2299f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2300{
2301 int saved = RedrawingDisabled;
2302
2303 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002304 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002305 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 RedrawingDisabled = saved;
2307}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002308
2309/*
2310 * "complete_info()" function
2311 */
2312 static void
2313f_complete_info(typval_T *argvars, typval_T *rettv)
2314{
2315 list_T *what_list = NULL;
2316
2317 if (rettv_dict_alloc(rettv) != OK)
2318 return;
2319
2320 if (argvars[0].v_type != VAR_UNKNOWN)
2321 {
2322 if (argvars[0].v_type != VAR_LIST)
2323 {
2324 emsg(_(e_listreq));
2325 return;
2326 }
2327 what_list = argvars[0].vval.v_list;
2328 }
2329 get_complete_info(what_list, rettv->vval.v_dict);
2330}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331#endif
2332
2333/*
2334 * "confirm(message, buttons[, default [, type]])" function
2335 */
2336 static void
2337f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2338{
2339#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2340 char_u *message;
2341 char_u *buttons = NULL;
2342 char_u buf[NUMBUFLEN];
2343 char_u buf2[NUMBUFLEN];
2344 int def = 1;
2345 int type = VIM_GENERIC;
2346 char_u *typestr;
2347 int error = FALSE;
2348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002349 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 if (message == NULL)
2351 error = TRUE;
2352 if (argvars[1].v_type != VAR_UNKNOWN)
2353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002354 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355 if (buttons == NULL)
2356 error = TRUE;
2357 if (argvars[2].v_type != VAR_UNKNOWN)
2358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002359 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360 if (argvars[3].v_type != VAR_UNKNOWN)
2361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002362 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 if (typestr == NULL)
2364 error = TRUE;
2365 else
2366 {
2367 switch (TOUPPER_ASC(*typestr))
2368 {
2369 case 'E': type = VIM_ERROR; break;
2370 case 'Q': type = VIM_QUESTION; break;
2371 case 'I': type = VIM_INFO; break;
2372 case 'W': type = VIM_WARNING; break;
2373 case 'G': type = VIM_GENERIC; break;
2374 }
2375 }
2376 }
2377 }
2378 }
2379
2380 if (buttons == NULL || *buttons == NUL)
2381 buttons = (char_u *)_("&Ok");
2382
2383 if (!error)
2384 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2385 def, NULL, FALSE);
2386#endif
2387}
2388
2389/*
2390 * "copy()" function
2391 */
2392 static void
2393f_copy(typval_T *argvars, typval_T *rettv)
2394{
2395 item_copy(&argvars[0], rettv, FALSE, 0);
2396}
2397
2398#ifdef FEAT_FLOAT
2399/*
2400 * "cos()" function
2401 */
2402 static void
2403f_cos(typval_T *argvars, typval_T *rettv)
2404{
2405 float_T f = 0.0;
2406
2407 rettv->v_type = VAR_FLOAT;
2408 if (get_float_arg(argvars, &f) == OK)
2409 rettv->vval.v_float = cos(f);
2410 else
2411 rettv->vval.v_float = 0.0;
2412}
2413
2414/*
2415 * "cosh()" function
2416 */
2417 static void
2418f_cosh(typval_T *argvars, typval_T *rettv)
2419{
2420 float_T f = 0.0;
2421
2422 rettv->v_type = VAR_FLOAT;
2423 if (get_float_arg(argvars, &f) == OK)
2424 rettv->vval.v_float = cosh(f);
2425 else
2426 rettv->vval.v_float = 0.0;
2427}
2428#endif
2429
2430/*
2431 * "count()" function
2432 */
2433 static void
2434f_count(typval_T *argvars, typval_T *rettv)
2435{
2436 long n = 0;
2437 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002438 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439
Bram Moolenaar9966b212017-07-28 16:46:57 +02002440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002441 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002442
2443 if (argvars[0].v_type == VAR_STRING)
2444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002445 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002446 char_u *p = argvars[0].vval.v_string;
2447 char_u *next;
2448
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002449 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002450 {
2451 if (ic)
2452 {
2453 size_t len = STRLEN(expr);
2454
2455 while (*p != NUL)
2456 {
2457 if (MB_STRNICMP(p, expr, len) == 0)
2458 {
2459 ++n;
2460 p += len;
2461 }
2462 else
2463 MB_PTR_ADV(p);
2464 }
2465 }
2466 else
2467 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2468 != NULL)
2469 {
2470 ++n;
2471 p = next + STRLEN(expr);
2472 }
2473 }
2474
2475 }
2476 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477 {
2478 listitem_T *li;
2479 list_T *l;
2480 long idx;
2481
2482 if ((l = argvars[0].vval.v_list) != NULL)
2483 {
2484 li = l->lv_first;
2485 if (argvars[2].v_type != VAR_UNKNOWN)
2486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487 if (argvars[3].v_type != VAR_UNKNOWN)
2488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002489 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002490 if (!error)
2491 {
2492 li = list_find(l, idx);
2493 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002494 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495 }
2496 }
2497 if (error)
2498 li = NULL;
2499 }
2500
2501 for ( ; li != NULL; li = li->li_next)
2502 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2503 ++n;
2504 }
2505 }
2506 else if (argvars[0].v_type == VAR_DICT)
2507 {
2508 int todo;
2509 dict_T *d;
2510 hashitem_T *hi;
2511
2512 if ((d = argvars[0].vval.v_dict) != NULL)
2513 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (argvars[2].v_type != VAR_UNKNOWN)
2515 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 }
2519
2520 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2521 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2522 {
2523 if (!HASHITEM_EMPTY(hi))
2524 {
2525 --todo;
2526 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2527 ++n;
2528 }
2529 }
2530 }
2531 }
2532 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 rettv->vval.v_number = n;
2535}
2536
2537/*
2538 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2539 *
2540 * Checks the existence of a cscope connection.
2541 */
2542 static void
2543f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2544{
2545#ifdef FEAT_CSCOPE
2546 int num = 0;
2547 char_u *dbpath = NULL;
2548 char_u *prepend = NULL;
2549 char_u buf[NUMBUFLEN];
2550
2551 if (argvars[0].v_type != VAR_UNKNOWN
2552 && argvars[1].v_type != VAR_UNKNOWN)
2553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002554 num = (int)tv_get_number(&argvars[0]);
2555 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002557 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002558 }
2559
2560 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2561#endif
2562}
2563
2564/*
2565 * "cursor(lnum, col)" function, or
2566 * "cursor(list)"
2567 *
2568 * Moves the cursor to the specified line and column.
2569 * Returns 0 when the position could be set, -1 otherwise.
2570 */
2571 static void
2572f_cursor(typval_T *argvars, typval_T *rettv)
2573{
2574 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002575 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 int set_curswant = TRUE;
2577
2578 rettv->vval.v_number = -1;
2579 if (argvars[1].v_type == VAR_UNKNOWN)
2580 {
2581 pos_T pos;
2582 colnr_T curswant = -1;
2583
2584 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002586 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002587 return;
2588 }
2589 line = pos.lnum;
2590 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 if (curswant >= 0)
2593 {
2594 curwin->w_curswant = curswant - 1;
2595 set_curswant = FALSE;
2596 }
2597 }
2598 else
2599 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002600 line = tv_get_lnum(argvars);
2601 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002603 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002604 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002605 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 return; /* type error; errmsg already given */
2607 if (line > 0)
2608 curwin->w_cursor.lnum = line;
2609 if (col > 0)
2610 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612
2613 /* Make sure the cursor is in a valid position. */
2614 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002615 /* Correct cursor for multi-byte character. */
2616 if (has_mbyte)
2617 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618
2619 curwin->w_set_curswant = set_curswant;
2620 rettv->vval.v_number = 0;
2621}
2622
Bram Moolenaar4f974752019-02-17 17:44:42 +01002623#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002624/*
2625 * "debugbreak()" function
2626 */
2627 static void
2628f_debugbreak(typval_T *argvars, typval_T *rettv)
2629{
2630 int pid;
2631
2632 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002633 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002634 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002636 else
2637 {
2638 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2639
2640 if (hProcess != NULL)
2641 {
2642 DebugBreakProcess(hProcess);
2643 CloseHandle(hProcess);
2644 rettv->vval.v_number = OK;
2645 }
2646 }
2647}
2648#endif
2649
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650/*
2651 * "deepcopy()" function
2652 */
2653 static void
2654f_deepcopy(typval_T *argvars, typval_T *rettv)
2655{
2656 int noref = 0;
2657 int copyID;
2658
2659 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002662 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 else
2664 {
2665 copyID = get_copyID();
2666 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2667 }
2668}
2669
2670/*
2671 * "delete()" function
2672 */
2673 static void
2674f_delete(typval_T *argvars, typval_T *rettv)
2675{
2676 char_u nbuf[NUMBUFLEN];
2677 char_u *name;
2678 char_u *flags;
2679
2680 rettv->vval.v_number = -1;
2681 if (check_restricted() || check_secure())
2682 return;
2683
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002684 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 if (name == NULL || *name == NUL)
2686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 return;
2689 }
2690
2691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002692 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 else
2694 flags = (char_u *)"";
2695
2696 if (*flags == NUL)
2697 /* delete a file */
2698 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2699 else if (STRCMP(flags, "d") == 0)
2700 /* delete an empty directory */
2701 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2702 else if (STRCMP(flags, "rf") == 0)
2703 /* delete a directory recursively */
2704 rettv->vval.v_number = delete_recursive(name);
2705 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002706 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707}
2708
2709/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002710 * "deletebufline()" function
2711 */
2712 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002713f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002714{
2715 buf_T *buf;
2716 linenr_T first, last;
2717 linenr_T lnum;
2718 long count;
2719 int is_curbuf;
2720 buf_T *curbuf_save = NULL;
2721 win_T *curwin_save = NULL;
2722 tabpage_T *tp;
2723 win_T *wp;
2724
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002725 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002726 if (buf == NULL)
2727 {
2728 rettv->vval.v_number = 1; /* FAIL */
2729 return;
2730 }
2731 is_curbuf = buf == curbuf;
2732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002734 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002735 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002736 else
2737 last = first;
2738
2739 if (buf->b_ml.ml_mfp == NULL || first < 1
2740 || first > buf->b_ml.ml_line_count || last < first)
2741 {
2742 rettv->vval.v_number = 1; /* FAIL */
2743 return;
2744 }
2745
2746 if (!is_curbuf)
2747 {
2748 curbuf_save = curbuf;
2749 curwin_save = curwin;
2750 curbuf = buf;
2751 find_win_for_curbuf();
2752 }
2753 if (last > curbuf->b_ml.ml_line_count)
2754 last = curbuf->b_ml.ml_line_count;
2755 count = last - first + 1;
2756
2757 // When coming here from Insert mode, sync undo, so that this can be
2758 // undone separately from what was previously inserted.
2759 if (u_sync_once == 2)
2760 {
2761 u_sync_once = 1; // notify that u_sync() was called
2762 u_sync(TRUE);
2763 }
2764
2765 if (u_save(first - 1, last + 1) == FAIL)
2766 {
2767 rettv->vval.v_number = 1; /* FAIL */
2768 return;
2769 }
2770
2771 for (lnum = first; lnum <= last; ++lnum)
2772 ml_delete(first, TRUE);
2773
2774 FOR_ALL_TAB_WINDOWS(tp, wp)
2775 if (wp->w_buffer == buf)
2776 {
2777 if (wp->w_cursor.lnum > last)
2778 wp->w_cursor.lnum -= count;
2779 else if (wp->w_cursor.lnum> first)
2780 wp->w_cursor.lnum = first;
2781 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2782 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2783 }
2784 check_cursor_col();
2785 deleted_lines_mark(first, count);
2786
2787 if (!is_curbuf)
2788 {
2789 curbuf = curbuf_save;
2790 curwin = curwin_save;
2791 }
2792}
2793
2794/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 * "did_filetype()" function
2796 */
2797 static void
2798f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2799{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002801}
2802
2803/*
2804 * "diff_filler()" function
2805 */
2806 static void
2807f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2808{
2809#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002810 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811#endif
2812}
2813
2814/*
2815 * "diff_hlID()" function
2816 */
2817 static void
2818f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2819{
2820#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002823 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002824 static int fnum = 0;
2825 static int change_start = 0;
2826 static int change_end = 0;
2827 static hlf_T hlID = (hlf_T)0;
2828 int filler_lines;
2829 int col;
2830
2831 if (lnum < 0) /* ignore type error in {lnum} arg */
2832 lnum = 0;
2833 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002834 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 || fnum != curbuf->b_fnum)
2836 {
2837 /* New line, buffer, change: need to get the values. */
2838 filler_lines = diff_check(curwin, lnum);
2839 if (filler_lines < 0)
2840 {
2841 if (filler_lines == -1)
2842 {
2843 change_start = MAXCOL;
2844 change_end = -1;
2845 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2846 hlID = HLF_ADD; /* added line */
2847 else
2848 hlID = HLF_CHD; /* changed line */
2849 }
2850 else
2851 hlID = HLF_ADD; /* added line */
2852 }
2853 else
2854 hlID = (hlf_T)0;
2855 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002856 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 fnum = curbuf->b_fnum;
2858 }
2859
2860 if (hlID == HLF_CHD || hlID == HLF_TXD)
2861 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002862 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 if (col >= change_start && col <= change_end)
2864 hlID = HLF_TXD; /* changed text */
2865 else
2866 hlID = HLF_CHD; /* changed line */
2867 }
2868 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2869#endif
2870}
2871
2872/*
2873 * "empty({expr})" function
2874 */
2875 static void
2876f_empty(typval_T *argvars, typval_T *rettv)
2877{
2878 int n = FALSE;
2879
2880 switch (argvars[0].v_type)
2881 {
2882 case VAR_STRING:
2883 case VAR_FUNC:
2884 n = argvars[0].vval.v_string == NULL
2885 || *argvars[0].vval.v_string == NUL;
2886 break;
2887 case VAR_PARTIAL:
2888 n = FALSE;
2889 break;
2890 case VAR_NUMBER:
2891 n = argvars[0].vval.v_number == 0;
2892 break;
2893 case VAR_FLOAT:
2894#ifdef FEAT_FLOAT
2895 n = argvars[0].vval.v_float == 0.0;
2896 break;
2897#endif
2898 case VAR_LIST:
2899 n = argvars[0].vval.v_list == NULL
2900 || argvars[0].vval.v_list->lv_first == NULL;
2901 break;
2902 case VAR_DICT:
2903 n = argvars[0].vval.v_dict == NULL
2904 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2905 break;
2906 case VAR_SPECIAL:
2907 n = argvars[0].vval.v_number != VVAL_TRUE;
2908 break;
2909
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 case VAR_BLOB:
2911 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002912 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2913 break;
2914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002915 case VAR_JOB:
2916#ifdef FEAT_JOB_CHANNEL
2917 n = argvars[0].vval.v_job == NULL
2918 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2919 break;
2920#endif
2921 case VAR_CHANNEL:
2922#ifdef FEAT_JOB_CHANNEL
2923 n = argvars[0].vval.v_channel == NULL
2924 || !channel_is_open(argvars[0].vval.v_channel);
2925 break;
2926#endif
2927 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002928 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 n = TRUE;
2930 break;
2931 }
2932
2933 rettv->vval.v_number = n;
2934}
2935
2936/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002937 * "environ()" function
2938 */
2939 static void
2940f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2941{
2942#if !defined(AMIGA)
2943 int i = 0;
2944 char_u *entry, *value;
2945# ifdef MSWIN
2946 extern wchar_t **_wenviron;
2947# else
2948 extern char **environ;
2949# endif
2950
2951 if (rettv_dict_alloc(rettv) != OK)
2952 return;
2953
2954# ifdef MSWIN
2955 if (*_wenviron == NULL)
2956 return;
2957# else
2958 if (*environ == NULL)
2959 return;
2960# endif
2961
2962 for (i = 0; ; ++i)
2963 {
2964# ifdef MSWIN
2965 short_u *p;
2966
2967 if ((p = (short_u *)_wenviron[i]) == NULL)
2968 return;
2969 entry = utf16_to_enc(p, NULL);
2970# else
2971 if ((entry = (char_u *)environ[i]) == NULL)
2972 return;
2973 entry = vim_strsave(entry);
2974# endif
2975 if (entry == NULL) // out of memory
2976 return;
2977 if ((value = vim_strchr(entry, '=')) == NULL)
2978 {
2979 vim_free(entry);
2980 continue;
2981 }
2982 *value++ = NUL;
2983 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2984 vim_free(entry);
2985 }
2986#endif
2987}
2988
2989/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990 * "escape({string}, {chars})" function
2991 */
2992 static void
2993f_escape(typval_T *argvars, typval_T *rettv)
2994{
2995 char_u buf[NUMBUFLEN];
2996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2998 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999 rettv->v_type = VAR_STRING;
3000}
3001
3002/*
3003 * "eval()" function
3004 */
3005 static void
3006f_eval(typval_T *argvars, typval_T *rettv)
3007{
3008 char_u *s, *p;
3009
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003010 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 if (s != NULL)
3012 s = skipwhite(s);
3013
3014 p = s;
3015 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3016 {
3017 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003018 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 need_clr_eos = FALSE;
3020 rettv->v_type = VAR_NUMBER;
3021 rettv->vval.v_number = 0;
3022 }
3023 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003024 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025}
3026
3027/*
3028 * "eventhandler()" function
3029 */
3030 static void
3031f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3032{
3033 rettv->vval.v_number = vgetc_busy;
3034}
3035
3036/*
3037 * "executable()" function
3038 */
3039 static void
3040f_executable(typval_T *argvars, typval_T *rettv)
3041{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003042 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043
3044 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003045 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046}
3047
3048static garray_T redir_execute_ga;
3049
3050/*
3051 * Append "value[value_len]" to the execute() output.
3052 */
3053 void
3054execute_redir_str(char_u *value, int value_len)
3055{
3056 int len;
3057
3058 if (value_len == -1)
3059 len = (int)STRLEN(value); /* Append the entire string */
3060 else
3061 len = value_len; /* Append only "value_len" characters */
3062 if (ga_grow(&redir_execute_ga, len) == OK)
3063 {
3064 mch_memmove((char *)redir_execute_ga.ga_data
3065 + redir_execute_ga.ga_len, value, len);
3066 redir_execute_ga.ga_len += len;
3067 }
3068}
3069
3070/*
3071 * Get next line from a list.
3072 * Called by do_cmdline() to get the next line.
3073 * Returns allocated string, or NULL for end of function.
3074 */
3075
3076 static char_u *
3077get_list_line(
3078 int c UNUSED,
3079 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003080 int indent UNUSED,
3081 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082{
3083 listitem_T **p = (listitem_T **)cookie;
3084 listitem_T *item = *p;
3085 char_u buf[NUMBUFLEN];
3086 char_u *s;
3087
3088 if (item == NULL)
3089 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 *p = item->li_next;
3092 return s == NULL ? NULL : vim_strsave(s);
3093}
3094
3095/*
3096 * "execute()" function
3097 */
3098 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003099execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100{
3101 char_u *cmd = NULL;
3102 list_T *list = NULL;
3103 int save_msg_silent = msg_silent;
3104 int save_emsg_silent = emsg_silent;
3105 int save_emsg_noredir = emsg_noredir;
3106 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003107 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003109 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003110 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111
3112 rettv->vval.v_string = NULL;
3113 rettv->v_type = VAR_STRING;
3114
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003115 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003117 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118 if (list == NULL || list->lv_first == NULL)
3119 /* empty list, no commands, empty output */
3120 return;
3121 ++list->lv_refcount;
3122 }
3123 else
3124 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003125 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 if (cmd == NULL)
3127 return;
3128 }
3129
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003130 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003131 {
3132 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003133 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134
3135 if (s == NULL)
3136 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003137 if (*s == NUL)
3138 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 if (STRNCMP(s, "silent", 6) == 0)
3140 ++msg_silent;
3141 if (STRCMP(s, "silent!") == 0)
3142 {
3143 emsg_silent = TRUE;
3144 emsg_noredir = TRUE;
3145 }
3146 }
3147 else
3148 ++msg_silent;
3149
3150 if (redir_execute)
3151 save_ga = redir_execute_ga;
3152 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3153 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003154 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003155 if (!echo_output)
3156 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157
3158 if (cmd != NULL)
3159 do_cmdline_cmd(cmd);
3160 else
3161 {
3162 listitem_T *item = list->lv_first;
3163
3164 do_cmdline(NULL, get_list_line, (void *)&item,
3165 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3166 --list->lv_refcount;
3167 }
3168
Bram Moolenaard297f352017-01-29 20:31:21 +01003169 /* Need to append a NUL to the result. */
3170 if (ga_grow(&redir_execute_ga, 1) == OK)
3171 {
3172 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3173 rettv->vval.v_string = redir_execute_ga.ga_data;
3174 }
3175 else
3176 {
3177 ga_clear(&redir_execute_ga);
3178 rettv->vval.v_string = NULL;
3179 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003180 msg_silent = save_msg_silent;
3181 emsg_silent = save_emsg_silent;
3182 emsg_noredir = save_emsg_noredir;
3183
3184 redir_execute = save_redir_execute;
3185 if (redir_execute)
3186 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003187 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003188
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003189 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003190 if (echo_output)
3191 // When not working silently: put it in column zero. A following
3192 // "echon" will overwrite the message, unavoidably.
3193 msg_col = 0;
3194 else
3195 // When working silently: Put it back where it was, since nothing
3196 // should have been written.
3197 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003198}
3199
3200/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003201 * "execute()" function
3202 */
3203 static void
3204f_execute(typval_T *argvars, typval_T *rettv)
3205{
3206 execute_common(argvars, rettv, 0);
3207}
3208
3209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003210 * "exepath()" function
3211 */
3212 static void
3213f_exepath(typval_T *argvars, typval_T *rettv)
3214{
3215 char_u *p = NULL;
3216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003217 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218 rettv->v_type = VAR_STRING;
3219 rettv->vval.v_string = p;
3220}
3221
3222/*
3223 * "exists()" function
3224 */
3225 static void
3226f_exists(typval_T *argvars, typval_T *rettv)
3227{
3228 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003229 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003231 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232 if (*p == '$') /* environment variable */
3233 {
3234 /* first try "normal" environment variables (fast) */
3235 if (mch_getenv(p + 1) != NULL)
3236 n = TRUE;
3237 else
3238 {
3239 /* try expanding things like $VIM and ${HOME} */
3240 p = expand_env_save(p);
3241 if (p != NULL && *p != '$')
3242 n = TRUE;
3243 vim_free(p);
3244 }
3245 }
3246 else if (*p == '&' || *p == '+') /* option */
3247 {
3248 n = (get_option_tv(&p, NULL, TRUE) == OK);
3249 if (*skipwhite(p) != NUL)
3250 n = FALSE; /* trailing garbage */
3251 }
3252 else if (*p == '*') /* internal or user defined function */
3253 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003254 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255 }
3256 else if (*p == ':')
3257 {
3258 n = cmd_exists(p + 1);
3259 }
3260 else if (*p == '#')
3261 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 if (p[1] == '#')
3263 n = autocmd_supported(p + 2);
3264 else
3265 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 }
3267 else /* internal variable */
3268 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003269 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 }
3271
3272 rettv->vval.v_number = n;
3273}
3274
3275#ifdef FEAT_FLOAT
3276/*
3277 * "exp()" function
3278 */
3279 static void
3280f_exp(typval_T *argvars, typval_T *rettv)
3281{
3282 float_T f = 0.0;
3283
3284 rettv->v_type = VAR_FLOAT;
3285 if (get_float_arg(argvars, &f) == OK)
3286 rettv->vval.v_float = exp(f);
3287 else
3288 rettv->vval.v_float = 0.0;
3289}
3290#endif
3291
3292/*
3293 * "expand()" function
3294 */
3295 static void
3296f_expand(typval_T *argvars, typval_T *rettv)
3297{
3298 char_u *s;
3299 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003300 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3302 expand_T xpc;
3303 int error = FALSE;
3304 char_u *result;
3305
3306 rettv->v_type = VAR_STRING;
3307 if (argvars[1].v_type != VAR_UNKNOWN
3308 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003309 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003311 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003313 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 if (*s == '%' || *s == '#' || *s == '<')
3315 {
3316 ++emsg_off;
3317 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3318 --emsg_off;
3319 if (rettv->v_type == VAR_LIST)
3320 {
3321 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3322 list_append_string(rettv->vval.v_list, result, -1);
3323 else
3324 vim_free(result);
3325 }
3326 else
3327 rettv->vval.v_string = result;
3328 }
3329 else
3330 {
3331 /* When the optional second argument is non-zero, don't remove matches
3332 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3333 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003334 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335 options |= WILD_KEEP_ALL;
3336 if (!error)
3337 {
3338 ExpandInit(&xpc);
3339 xpc.xp_context = EXPAND_FILES;
3340 if (p_wic)
3341 options += WILD_ICASE;
3342 if (rettv->v_type == VAR_STRING)
3343 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3344 options, WILD_ALL);
3345 else if (rettv_list_alloc(rettv) != FAIL)
3346 {
3347 int i;
3348
3349 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3350 for (i = 0; i < xpc.xp_numfiles; i++)
3351 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3352 ExpandCleanup(&xpc);
3353 }
3354 }
3355 else
3356 rettv->vval.v_string = NULL;
3357 }
3358}
3359
3360/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003361 * "expandcmd()" function
3362 * Expand all the special characters in a command string.
3363 */
3364 static void
3365f_expandcmd(typval_T *argvars, typval_T *rettv)
3366{
3367 exarg_T eap;
3368 char_u *cmdstr;
3369 char *errormsg = NULL;
3370
3371 rettv->v_type = VAR_STRING;
3372 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3373
3374 memset(&eap, 0, sizeof(eap));
3375 eap.cmd = cmdstr;
3376 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003377 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003378 eap.usefilter = FALSE;
3379 eap.nextcmd = NULL;
3380 eap.cmdidx = CMD_USER;
3381
3382 expand_filename(&eap, &cmdstr, &errormsg);
3383 if (errormsg != NULL && *errormsg != NUL)
3384 emsg(errormsg);
3385
3386 rettv->vval.v_string = cmdstr;
3387}
3388
3389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 * "extend(list, list [, idx])" function
3391 * "extend(dict, dict [, action])" function
3392 */
3393 static void
3394f_extend(typval_T *argvars, typval_T *rettv)
3395{
3396 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3397
3398 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3399 {
3400 list_T *l1, *l2;
3401 listitem_T *item;
3402 long before;
3403 int error = FALSE;
3404
3405 l1 = argvars[0].vval.v_list;
3406 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003407 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003408 && l2 != NULL)
3409 {
3410 if (argvars[2].v_type != VAR_UNKNOWN)
3411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003412 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413 if (error)
3414 return; /* type error; errmsg already given */
3415
3416 if (before == l1->lv_len)
3417 item = NULL;
3418 else
3419 {
3420 item = list_find(l1, before);
3421 if (item == NULL)
3422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003423 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 return;
3425 }
3426 }
3427 }
3428 else
3429 item = NULL;
3430 list_extend(l1, l2, item);
3431
3432 copy_tv(&argvars[0], rettv);
3433 }
3434 }
3435 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3436 {
3437 dict_T *d1, *d2;
3438 char_u *action;
3439 int i;
3440
3441 d1 = argvars[0].vval.v_dict;
3442 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003443 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 && d2 != NULL)
3445 {
3446 /* Check the third argument. */
3447 if (argvars[2].v_type != VAR_UNKNOWN)
3448 {
3449 static char *(av[]) = {"keep", "force", "error"};
3450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003451 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452 if (action == NULL)
3453 return; /* type error; errmsg already given */
3454 for (i = 0; i < 3; ++i)
3455 if (STRCMP(action, av[i]) == 0)
3456 break;
3457 if (i == 3)
3458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003459 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 return;
3461 }
3462 }
3463 else
3464 action = (char_u *)"force";
3465
3466 dict_extend(d1, d2, action);
3467
3468 copy_tv(&argvars[0], rettv);
3469 }
3470 }
3471 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003472 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473}
3474
3475/*
3476 * "feedkeys()" function
3477 */
3478 static void
3479f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3480{
3481 int remap = TRUE;
3482 int insert = FALSE;
3483 char_u *keys, *flags;
3484 char_u nbuf[NUMBUFLEN];
3485 int typed = FALSE;
3486 int execute = FALSE;
3487 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003488 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003489 char_u *keys_esc;
3490
3491 /* This is not allowed in the sandbox. If the commands would still be
3492 * executed in the sandbox it would be OK, but it probably happens later,
3493 * when "sandbox" is no longer set. */
3494 if (check_secure())
3495 return;
3496
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003497 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498
3499 if (argvars[1].v_type != VAR_UNKNOWN)
3500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003501 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 for ( ; *flags != NUL; ++flags)
3503 {
3504 switch (*flags)
3505 {
3506 case 'n': remap = FALSE; break;
3507 case 'm': remap = TRUE; break;
3508 case 't': typed = TRUE; break;
3509 case 'i': insert = TRUE; break;
3510 case 'x': execute = TRUE; break;
3511 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003512 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513 }
3514 }
3515 }
3516
3517 if (*keys != NUL || execute)
3518 {
3519 /* Need to escape K_SPECIAL and CSI before putting the string in the
3520 * typeahead buffer. */
3521 keys_esc = vim_strsave_escape_csi(keys);
3522 if (keys_esc != NULL)
3523 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003524 if (lowlevel)
3525 {
3526#ifdef USE_INPUT_BUF
3527 add_to_input_buf(keys, (int)STRLEN(keys));
3528#else
3529 emsg(_("E980: lowlevel input not supported"));
3530#endif
3531 }
3532 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003533 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003534 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003536 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003537#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003538 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003539#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003540 )
3541 typebuf_was_filled = TRUE;
3542 }
3543 vim_free(keys_esc);
3544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 if (execute)
3546 {
3547 int save_msg_scroll = msg_scroll;
3548
3549 /* Avoid a 1 second delay when the keys start Insert mode. */
3550 msg_scroll = FALSE;
3551
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003552 if (!dangerous)
3553 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003554 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003555 if (!dangerous)
3556 --ex_normal_busy;
3557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 msg_scroll |= save_msg_scroll;
3559 }
3560 }
3561 }
3562}
3563
3564/*
3565 * "filereadable()" function
3566 */
3567 static void
3568f_filereadable(typval_T *argvars, typval_T *rettv)
3569{
3570 int fd;
3571 char_u *p;
3572 int n;
3573
3574#ifndef O_NONBLOCK
3575# define O_NONBLOCK 0
3576#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003577 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3579 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3580 {
3581 n = TRUE;
3582 close(fd);
3583 }
3584 else
3585 n = FALSE;
3586
3587 rettv->vval.v_number = n;
3588}
3589
3590/*
3591 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3592 * rights to write into.
3593 */
3594 static void
3595f_filewritable(typval_T *argvars, typval_T *rettv)
3596{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598}
3599
3600 static void
3601findfilendir(
3602 typval_T *argvars UNUSED,
3603 typval_T *rettv,
3604 int find_what UNUSED)
3605{
3606#ifdef FEAT_SEARCHPATH
3607 char_u *fname;
3608 char_u *fresult = NULL;
3609 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3610 char_u *p;
3611 char_u pathbuf[NUMBUFLEN];
3612 int count = 1;
3613 int first = TRUE;
3614 int error = FALSE;
3615#endif
3616
3617 rettv->vval.v_string = NULL;
3618 rettv->v_type = VAR_STRING;
3619
3620#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003621 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622
3623 if (argvars[1].v_type != VAR_UNKNOWN)
3624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003625 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 if (p == NULL)
3627 error = TRUE;
3628 else
3629 {
3630 if (*p != NUL)
3631 path = p;
3632
3633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003634 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 }
3636 }
3637
3638 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3639 error = TRUE;
3640
3641 if (*fname != NUL && !error)
3642 {
3643 do
3644 {
3645 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3646 vim_free(fresult);
3647 fresult = find_file_in_path_option(first ? fname : NULL,
3648 first ? (int)STRLEN(fname) : 0,
3649 0, first, path,
3650 find_what,
3651 curbuf->b_ffname,
3652 find_what == FINDFILE_DIR
3653 ? (char_u *)"" : curbuf->b_p_sua);
3654 first = FALSE;
3655
3656 if (fresult != NULL && rettv->v_type == VAR_LIST)
3657 list_append_string(rettv->vval.v_list, fresult, -1);
3658
3659 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3660 }
3661
3662 if (rettv->v_type == VAR_STRING)
3663 rettv->vval.v_string = fresult;
3664#endif
3665}
3666
3667/*
3668 * "filter()" function
3669 */
3670 static void
3671f_filter(typval_T *argvars, typval_T *rettv)
3672{
3673 filter_map(argvars, rettv, FALSE);
3674}
3675
3676/*
3677 * "finddir({fname}[, {path}[, {count}]])" function
3678 */
3679 static void
3680f_finddir(typval_T *argvars, typval_T *rettv)
3681{
3682 findfilendir(argvars, rettv, FINDFILE_DIR);
3683}
3684
3685/*
3686 * "findfile({fname}[, {path}[, {count}]])" function
3687 */
3688 static void
3689f_findfile(typval_T *argvars, typval_T *rettv)
3690{
3691 findfilendir(argvars, rettv, FINDFILE_FILE);
3692}
3693
3694#ifdef FEAT_FLOAT
3695/*
3696 * "float2nr({float})" function
3697 */
3698 static void
3699f_float2nr(typval_T *argvars, typval_T *rettv)
3700{
3701 float_T f = 0.0;
3702
3703 if (get_float_arg(argvars, &f) == OK)
3704 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003705 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003706 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003707 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003708 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 else
3710 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 }
3712}
3713
3714/*
3715 * "floor({float})" function
3716 */
3717 static void
3718f_floor(typval_T *argvars, typval_T *rettv)
3719{
3720 float_T f = 0.0;
3721
3722 rettv->v_type = VAR_FLOAT;
3723 if (get_float_arg(argvars, &f) == OK)
3724 rettv->vval.v_float = floor(f);
3725 else
3726 rettv->vval.v_float = 0.0;
3727}
3728
3729/*
3730 * "fmod()" function
3731 */
3732 static void
3733f_fmod(typval_T *argvars, typval_T *rettv)
3734{
3735 float_T fx = 0.0, fy = 0.0;
3736
3737 rettv->v_type = VAR_FLOAT;
3738 if (get_float_arg(argvars, &fx) == OK
3739 && get_float_arg(&argvars[1], &fy) == OK)
3740 rettv->vval.v_float = fmod(fx, fy);
3741 else
3742 rettv->vval.v_float = 0.0;
3743}
3744#endif
3745
3746/*
3747 * "fnameescape({string})" function
3748 */
3749 static void
3750f_fnameescape(typval_T *argvars, typval_T *rettv)
3751{
3752 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 rettv->v_type = VAR_STRING;
3755}
3756
3757/*
3758 * "fnamemodify({fname}, {mods})" function
3759 */
3760 static void
3761f_fnamemodify(typval_T *argvars, typval_T *rettv)
3762{
3763 char_u *fname;
3764 char_u *mods;
3765 int usedlen = 0;
3766 int len;
3767 char_u *fbuf = NULL;
3768 char_u buf[NUMBUFLEN];
3769
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003770 fname = tv_get_string_chk(&argvars[0]);
3771 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 if (fname == NULL || mods == NULL)
3773 fname = NULL;
3774 else
3775 {
3776 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003777 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003778 }
3779
3780 rettv->v_type = VAR_STRING;
3781 if (fname == NULL)
3782 rettv->vval.v_string = NULL;
3783 else
3784 rettv->vval.v_string = vim_strnsave(fname, len);
3785 vim_free(fbuf);
3786}
3787
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003788/*
3789 * "foldclosed()" function
3790 */
3791 static void
3792foldclosed_both(
3793 typval_T *argvars UNUSED,
3794 typval_T *rettv,
3795 int end UNUSED)
3796{
3797#ifdef FEAT_FOLDING
3798 linenr_T lnum;
3799 linenr_T first, last;
3800
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003801 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3803 {
3804 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3805 {
3806 if (end)
3807 rettv->vval.v_number = (varnumber_T)last;
3808 else
3809 rettv->vval.v_number = (varnumber_T)first;
3810 return;
3811 }
3812 }
3813#endif
3814 rettv->vval.v_number = -1;
3815}
3816
3817/*
3818 * "foldclosed()" function
3819 */
3820 static void
3821f_foldclosed(typval_T *argvars, typval_T *rettv)
3822{
3823 foldclosed_both(argvars, rettv, FALSE);
3824}
3825
3826/*
3827 * "foldclosedend()" function
3828 */
3829 static void
3830f_foldclosedend(typval_T *argvars, typval_T *rettv)
3831{
3832 foldclosed_both(argvars, rettv, TRUE);
3833}
3834
3835/*
3836 * "foldlevel()" function
3837 */
3838 static void
3839f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3840{
3841#ifdef FEAT_FOLDING
3842 linenr_T lnum;
3843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003844 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3846 rettv->vval.v_number = foldLevel(lnum);
3847#endif
3848}
3849
3850/*
3851 * "foldtext()" function
3852 */
3853 static void
3854f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3855{
3856#ifdef FEAT_FOLDING
3857 linenr_T foldstart;
3858 linenr_T foldend;
3859 char_u *dashes;
3860 linenr_T lnum;
3861 char_u *s;
3862 char_u *r;
3863 int len;
3864 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003865 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866#endif
3867
3868 rettv->v_type = VAR_STRING;
3869 rettv->vval.v_string = NULL;
3870#ifdef FEAT_FOLDING
3871 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3872 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3873 dashes = get_vim_var_str(VV_FOLDDASHES);
3874 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3875 && dashes != NULL)
3876 {
3877 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003878 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 if (!linewhite(lnum))
3880 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003881
3882 /* Find interesting text in this line. */
3883 s = skipwhite(ml_get(lnum));
3884 /* skip C comment-start */
3885 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3886 {
3887 s = skipwhite(s + 2);
3888 if (*skipwhite(s) == NUL
3889 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3890 {
3891 s = skipwhite(ml_get(lnum + 1));
3892 if (*s == '*')
3893 s = skipwhite(s + 1);
3894 }
3895 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003896 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003897 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003898 r = alloc(STRLEN(txt)
3899 + STRLEN(dashes) // for %s
3900 + 20 // for %3ld
3901 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 if (r != NULL)
3903 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003904 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003905 len = (int)STRLEN(r);
3906 STRCAT(r, s);
3907 /* remove 'foldmarker' and 'commentstring' */
3908 foldtext_cleanup(r + len);
3909 rettv->vval.v_string = r;
3910 }
3911 }
3912#endif
3913}
3914
3915/*
3916 * "foldtextresult(lnum)" function
3917 */
3918 static void
3919f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3920{
3921#ifdef FEAT_FOLDING
3922 linenr_T lnum;
3923 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003924 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 foldinfo_T foldinfo;
3926 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003927 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928#endif
3929
3930 rettv->v_type = VAR_STRING;
3931 rettv->vval.v_string = NULL;
3932#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003933 if (entered)
3934 return; /* reject recursive use */
3935 entered = TRUE;
3936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003937 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 /* treat illegal types and illegal string values for {lnum} the same */
3939 if (lnum < 0)
3940 lnum = 0;
3941 fold_count = foldedCount(curwin, lnum, &foldinfo);
3942 if (fold_count > 0)
3943 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003944 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3945 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946 if (text == buf)
3947 text = vim_strsave(text);
3948 rettv->vval.v_string = text;
3949 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003950
3951 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952#endif
3953}
3954
3955/*
3956 * "foreground()" function
3957 */
3958 static void
3959f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3960{
3961#ifdef FEAT_GUI
3962 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003963 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003965 return;
3966 }
3967#endif
3968#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003970#endif
3971}
3972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003974common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975{
3976 char_u *s;
3977 char_u *name;
3978 int use_string = FALSE;
3979 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003980 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981
3982 if (argvars[0].v_type == VAR_FUNC)
3983 {
3984 /* function(MyFunc, [arg], dict) */
3985 s = argvars[0].vval.v_string;
3986 }
3987 else if (argvars[0].v_type == VAR_PARTIAL
3988 && argvars[0].vval.v_partial != NULL)
3989 {
3990 /* function(dict.MyFunc, [arg]) */
3991 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003992 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 }
3994 else
3995 {
3996 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003997 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998 use_string = TRUE;
3999 }
4000
Bram Moolenaar843b8842016-08-21 14:36:15 +02004001 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004002 {
4003 name = s;
4004 trans_name = trans_function_name(&name, FALSE,
4005 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4006 if (*name != NUL)
4007 s = NULL;
4008 }
4009
Bram Moolenaar843b8842016-08-21 14:36:15 +02004010 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4011 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004012 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004014 else if (trans_name != NULL && (is_funcref
4015 ? find_func(trans_name) == NULL
4016 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004017 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 else
4019 {
4020 int dict_idx = 0;
4021 int arg_idx = 0;
4022 list_T *list = NULL;
4023
4024 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4025 {
4026 char sid_buf[25];
4027 int off = *s == 's' ? 2 : 5;
4028
4029 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4030 * also be called from another script. Using trans_function_name()
4031 * would also work, but some plugins depend on the name being
4032 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004033 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004034 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004035 if (name != NULL)
4036 {
4037 STRCPY(name, sid_buf);
4038 STRCAT(name, s + off);
4039 }
4040 }
4041 else
4042 name = vim_strsave(s);
4043
4044 if (argvars[1].v_type != VAR_UNKNOWN)
4045 {
4046 if (argvars[2].v_type != VAR_UNKNOWN)
4047 {
4048 /* function(name, [args], dict) */
4049 arg_idx = 1;
4050 dict_idx = 2;
4051 }
4052 else if (argvars[1].v_type == VAR_DICT)
4053 /* function(name, dict) */
4054 dict_idx = 1;
4055 else
4056 /* function(name, [args]) */
4057 arg_idx = 1;
4058 if (dict_idx > 0)
4059 {
4060 if (argvars[dict_idx].v_type != VAR_DICT)
4061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004062 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004064 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004065 }
4066 if (argvars[dict_idx].vval.v_dict == NULL)
4067 dict_idx = 0;
4068 }
4069 if (arg_idx > 0)
4070 {
4071 if (argvars[arg_idx].v_type != VAR_LIST)
4072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004073 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004074 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004075 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076 }
4077 list = argvars[arg_idx].vval.v_list;
4078 if (list == NULL || list->lv_len == 0)
4079 arg_idx = 0;
4080 }
4081 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004082 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004083 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004084 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004085
4086 /* result is a VAR_PARTIAL */
4087 if (pt == NULL)
4088 vim_free(name);
4089 else
4090 {
4091 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4092 {
4093 listitem_T *li;
4094 int i = 0;
4095 int arg_len = 0;
4096 int lv_len = 0;
4097
4098 if (arg_pt != NULL)
4099 arg_len = arg_pt->pt_argc;
4100 if (list != NULL)
4101 lv_len = list->lv_len;
4102 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004103 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004104 if (pt->pt_argv == NULL)
4105 {
4106 vim_free(pt);
4107 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004108 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 for (i = 0; i < arg_len; i++)
4111 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4112 if (lv_len > 0)
4113 for (li = list->lv_first; li != NULL;
4114 li = li->li_next)
4115 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004116 }
4117
4118 /* For "function(dict.func, [], dict)" and "func" is a partial
4119 * use "dict". That is backwards compatible. */
4120 if (dict_idx > 0)
4121 {
4122 /* The dict is bound explicitly, pt_auto is FALSE. */
4123 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4124 ++pt->pt_dict->dv_refcount;
4125 }
4126 else if (arg_pt != NULL)
4127 {
4128 /* If the dict was bound automatically the result is also
4129 * bound automatically. */
4130 pt->pt_dict = arg_pt->pt_dict;
4131 pt->pt_auto = arg_pt->pt_auto;
4132 if (pt->pt_dict != NULL)
4133 ++pt->pt_dict->dv_refcount;
4134 }
4135
4136 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004137 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4138 {
4139 pt->pt_func = arg_pt->pt_func;
4140 func_ptr_ref(pt->pt_func);
4141 vim_free(name);
4142 }
4143 else if (is_funcref)
4144 {
4145 pt->pt_func = find_func(trans_name);
4146 func_ptr_ref(pt->pt_func);
4147 vim_free(name);
4148 }
4149 else
4150 {
4151 pt->pt_name = name;
4152 func_ref(name);
4153 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154 }
4155 rettv->v_type = VAR_PARTIAL;
4156 rettv->vval.v_partial = pt;
4157 }
4158 else
4159 {
4160 /* result is a VAR_FUNC */
4161 rettv->v_type = VAR_FUNC;
4162 rettv->vval.v_string = name;
4163 func_ref(name);
4164 }
4165 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004166theend:
4167 vim_free(trans_name);
4168}
4169
4170/*
4171 * "funcref()" function
4172 */
4173 static void
4174f_funcref(typval_T *argvars, typval_T *rettv)
4175{
4176 common_function(argvars, rettv, TRUE);
4177}
4178
4179/*
4180 * "function()" function
4181 */
4182 static void
4183f_function(typval_T *argvars, typval_T *rettv)
4184{
4185 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186}
4187
4188/*
4189 * "garbagecollect()" function
4190 */
4191 static void
4192f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4193{
4194 /* This is postponed until we are back at the toplevel, because we may be
4195 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4196 want_garbage_collect = TRUE;
4197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004198 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004199 garbage_collect_at_exit = TRUE;
4200}
4201
4202/*
4203 * "get()" function
4204 */
4205 static void
4206f_get(typval_T *argvars, typval_T *rettv)
4207{
4208 listitem_T *li;
4209 list_T *l;
4210 dictitem_T *di;
4211 dict_T *d;
4212 typval_T *tv = NULL;
4213
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004214 if (argvars[0].v_type == VAR_BLOB)
4215 {
4216 int error = FALSE;
4217 int idx = tv_get_number_chk(&argvars[1], &error);
4218
4219 if (!error)
4220 {
4221 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004222 if (idx < 0)
4223 idx = blob_len(argvars[0].vval.v_blob) + idx;
4224 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4225 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004226 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004227 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004228 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004229 tv = rettv;
4230 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004231 }
4232 }
4233 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 {
4235 if ((l = argvars[0].vval.v_list) != NULL)
4236 {
4237 int error = FALSE;
4238
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004239 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240 if (!error && li != NULL)
4241 tv = &li->li_tv;
4242 }
4243 }
4244 else if (argvars[0].v_type == VAR_DICT)
4245 {
4246 if ((d = argvars[0].vval.v_dict) != NULL)
4247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004248 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 if (di != NULL)
4250 tv = &di->di_tv;
4251 }
4252 }
4253 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4254 {
4255 partial_T *pt;
4256 partial_T fref_pt;
4257
4258 if (argvars[0].v_type == VAR_PARTIAL)
4259 pt = argvars[0].vval.v_partial;
4260 else
4261 {
4262 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4263 fref_pt.pt_name = argvars[0].vval.v_string;
4264 pt = &fref_pt;
4265 }
4266
4267 if (pt != NULL)
4268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004269 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004270 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271
4272 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4273 {
4274 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004275 n = partial_name(pt);
4276 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 rettv->vval.v_string = NULL;
4278 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 {
4280 rettv->vval.v_string = vim_strsave(n);
4281 if (rettv->v_type == VAR_FUNC)
4282 func_ref(rettv->vval.v_string);
4283 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 }
4285 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004286 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 else if (STRCMP(what, "args") == 0)
4288 {
4289 rettv->v_type = VAR_LIST;
4290 if (rettv_list_alloc(rettv) == OK)
4291 {
4292 int i;
4293
4294 for (i = 0; i < pt->pt_argc; ++i)
4295 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4296 }
4297 }
4298 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004299 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 return;
4301 }
4302 }
4303 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004304 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305
4306 if (tv == NULL)
4307 {
4308 if (argvars[2].v_type != VAR_UNKNOWN)
4309 copy_tv(&argvars[2], rettv);
4310 }
4311 else
4312 copy_tv(tv, rettv);
4313}
4314
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004315/*
4316 * Returns buffer options, variables and other attributes in a dictionary.
4317 */
4318 static dict_T *
4319get_buffer_info(buf_T *buf)
4320{
4321 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004322 tabpage_T *tp;
4323 win_T *wp;
4324 list_T *windows;
4325
4326 dict = dict_alloc();
4327 if (dict == NULL)
4328 return NULL;
4329
Bram Moolenaare0be1672018-07-08 16:50:37 +02004330 dict_add_number(dict, "bufnr", buf->b_fnum);
4331 dict_add_string(dict, "name", buf->b_ffname);
4332 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4333 : buflist_findlnum(buf));
4334 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4335 dict_add_number(dict, "listed", buf->b_p_bl);
4336 dict_add_number(dict, "changed", bufIsChanged(buf));
4337 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4338 dict_add_number(dict, "hidden",
4339 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004340
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004341 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004342 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004343
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004344 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004345 windows = list_alloc();
4346 if (windows != NULL)
4347 {
4348 FOR_ALL_TAB_WINDOWS(tp, wp)
4349 if (wp->w_buffer == buf)
4350 list_append_number(windows, (varnumber_T)wp->w_id);
4351 dict_add_list(dict, "windows", windows);
4352 }
4353
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004354#ifdef FEAT_TEXT_PROP
4355 // List of popup windows displaying this buffer
4356 windows = list_alloc();
4357 if (windows != NULL)
4358 {
4359 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4360 if (wp->w_buffer == buf)
4361 list_append_number(windows, (varnumber_T)wp->w_id);
4362 FOR_ALL_TABPAGES(tp)
4363 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4364 if (wp->w_buffer == buf)
4365 list_append_number(windows, (varnumber_T)wp->w_id);
4366
4367 dict_add_list(dict, "popups", windows);
4368 }
4369#endif
4370
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004371#ifdef FEAT_SIGNS
4372 if (buf->b_signlist != NULL)
4373 {
4374 /* List of signs placed in this buffer */
4375 list_T *signs = list_alloc();
4376 if (signs != NULL)
4377 {
4378 get_buffer_signs(buf, signs);
4379 dict_add_list(dict, "signs", signs);
4380 }
4381 }
4382#endif
4383
4384 return dict;
4385}
4386
4387/*
4388 * "getbufinfo()" function
4389 */
4390 static void
4391f_getbufinfo(typval_T *argvars, typval_T *rettv)
4392{
4393 buf_T *buf = NULL;
4394 buf_T *argbuf = NULL;
4395 dict_T *d;
4396 int filtered = FALSE;
4397 int sel_buflisted = FALSE;
4398 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004399 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004400
4401 if (rettv_list_alloc(rettv) != OK)
4402 return;
4403
4404 /* List of all the buffers or selected buffers */
4405 if (argvars[0].v_type == VAR_DICT)
4406 {
4407 dict_T *sel_d = argvars[0].vval.v_dict;
4408
4409 if (sel_d != NULL)
4410 {
4411 dictitem_T *di;
4412
4413 filtered = TRUE;
4414
4415 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004416 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004417 sel_buflisted = TRUE;
4418
4419 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004420 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004421 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004422
4423 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004424 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004425 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004426 }
4427 }
4428 else if (argvars[0].v_type != VAR_UNKNOWN)
4429 {
4430 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004431 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004432 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004433 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004434 --emsg_off;
4435 if (argbuf == NULL)
4436 return;
4437 }
4438
4439 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004440 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004441 {
4442 if (argbuf != NULL && argbuf != buf)
4443 continue;
4444 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004445 || (sel_buflisted && !buf->b_p_bl)
4446 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004447 continue;
4448
4449 d = get_buffer_info(buf);
4450 if (d != NULL)
4451 list_append_dict(rettv->vval.v_list, d);
4452 if (argbuf != NULL)
4453 return;
4454 }
4455}
4456
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457/*
4458 * Get line or list of lines from buffer "buf" into "rettv".
4459 * Return a range (from start to end) of lines in rettv from the specified
4460 * buffer.
4461 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4462 */
4463 static void
4464get_buffer_lines(
4465 buf_T *buf,
4466 linenr_T start,
4467 linenr_T end,
4468 int retlist,
4469 typval_T *rettv)
4470{
4471 char_u *p;
4472
4473 rettv->v_type = VAR_STRING;
4474 rettv->vval.v_string = NULL;
4475 if (retlist && rettv_list_alloc(rettv) == FAIL)
4476 return;
4477
4478 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4479 return;
4480
4481 if (!retlist)
4482 {
4483 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4484 p = ml_get_buf(buf, start, FALSE);
4485 else
4486 p = (char_u *)"";
4487 rettv->vval.v_string = vim_strsave(p);
4488 }
4489 else
4490 {
4491 if (end < start)
4492 return;
4493
4494 if (start < 1)
4495 start = 1;
4496 if (end > buf->b_ml.ml_line_count)
4497 end = buf->b_ml.ml_line_count;
4498 while (start <= end)
4499 if (list_append_string(rettv->vval.v_list,
4500 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4501 break;
4502 }
4503}
4504
4505/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004506 * "getbufline()" function
4507 */
4508 static void
4509f_getbufline(typval_T *argvars, typval_T *rettv)
4510{
4511 linenr_T lnum;
4512 linenr_T end;
4513 buf_T *buf;
4514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004515 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004517 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518 --emsg_off;
4519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004520 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521 if (argvars[2].v_type == VAR_UNKNOWN)
4522 end = lnum;
4523 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004524 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525
4526 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4527}
4528
4529/*
4530 * "getbufvar()" function
4531 */
4532 static void
4533f_getbufvar(typval_T *argvars, typval_T *rettv)
4534{
4535 buf_T *buf;
4536 buf_T *save_curbuf;
4537 char_u *varname;
4538 dictitem_T *v;
4539 int done = FALSE;
4540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004541 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4542 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004544 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545
4546 rettv->v_type = VAR_STRING;
4547 rettv->vval.v_string = NULL;
4548
4549 if (buf != NULL && varname != NULL)
4550 {
4551 /* set curbuf to be our buf, temporarily */
4552 save_curbuf = curbuf;
4553 curbuf = buf;
4554
Bram Moolenaar30567352016-08-27 21:25:44 +02004555 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004557 if (varname[1] == NUL)
4558 {
4559 /* get all buffer-local options in a dict */
4560 dict_T *opts = get_winbuf_options(TRUE);
4561
4562 if (opts != NULL)
4563 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004564 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004565 done = TRUE;
4566 }
4567 }
4568 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4569 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570 done = TRUE;
4571 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572 else
4573 {
4574 /* Look up the variable. */
4575 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4576 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4577 'b', varname, FALSE);
4578 if (v != NULL)
4579 {
4580 copy_tv(&v->di_tv, rettv);
4581 done = TRUE;
4582 }
4583 }
4584
4585 /* restore previous notion of curbuf */
4586 curbuf = save_curbuf;
4587 }
4588
4589 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4590 /* use the default value */
4591 copy_tv(&argvars[2], rettv);
4592
4593 --emsg_off;
4594}
4595
4596/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004597 * "getchangelist()" function
4598 */
4599 static void
4600f_getchangelist(typval_T *argvars, typval_T *rettv)
4601{
4602#ifdef FEAT_JUMPLIST
4603 buf_T *buf;
4604 int i;
4605 list_T *l;
4606 dict_T *d;
4607#endif
4608
4609 if (rettv_list_alloc(rettv) != OK)
4610 return;
4611
4612#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004613 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004614 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004615 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004616 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004617 if (buf == NULL)
4618 return;
4619
4620 l = list_alloc();
4621 if (l == NULL)
4622 return;
4623
4624 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4625 return;
4626 /*
4627 * The current window change list index tracks only the position in the
4628 * current buffer change list. For other buffers, use the change list
4629 * length as the current index.
4630 */
4631 list_append_number(rettv->vval.v_list,
4632 (varnumber_T)((buf == curwin->w_buffer)
4633 ? curwin->w_changelistidx : buf->b_changelistlen));
4634
4635 for (i = 0; i < buf->b_changelistlen; ++i)
4636 {
4637 if (buf->b_changelist[i].lnum == 0)
4638 continue;
4639 if ((d = dict_alloc()) == NULL)
4640 return;
4641 if (list_append_dict(l, d) == FAIL)
4642 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004643 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4644 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004645 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004646 }
4647#endif
4648}
4649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004650 * "getchar()" function
4651 */
4652 static void
4653f_getchar(typval_T *argvars, typval_T *rettv)
4654{
4655 varnumber_T n;
4656 int error = FALSE;
4657
Bram Moolenaar84d93902018-09-11 20:10:20 +02004658#ifdef MESSAGE_QUEUE
4659 // vpeekc() used to check for messages, but that caused problems, invoking
4660 // a callback where it was not expected. Some plugins use getchar(1) in a
4661 // loop to await a message, therefore make sure we check for messages here.
4662 parse_queued_messages();
4663#endif
4664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665 /* Position the cursor. Needed after a message that ends in a space. */
4666 windgoto(msg_row, msg_col);
4667
4668 ++no_mapping;
4669 ++allow_keys;
4670 for (;;)
4671 {
4672 if (argvars[0].v_type == VAR_UNKNOWN)
4673 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004674 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004675 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 /* getchar(1): only check if char avail */
4677 n = vpeekc_any();
4678 else if (error || vpeekc_any() == NUL)
4679 /* illegal argument or getchar(0) and no char avail: return zero */
4680 n = 0;
4681 else
4682 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004683 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684
4685 if (n == K_IGNORE)
4686 continue;
4687 break;
4688 }
4689 --no_mapping;
4690 --allow_keys;
4691
4692 set_vim_var_nr(VV_MOUSE_WIN, 0);
4693 set_vim_var_nr(VV_MOUSE_WINID, 0);
4694 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4695 set_vim_var_nr(VV_MOUSE_COL, 0);
4696
4697 rettv->vval.v_number = n;
4698 if (IS_SPECIAL(n) || mod_mask != 0)
4699 {
4700 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4701 int i = 0;
4702
4703 /* Turn a special key into three bytes, plus modifier. */
4704 if (mod_mask != 0)
4705 {
4706 temp[i++] = K_SPECIAL;
4707 temp[i++] = KS_MODIFIER;
4708 temp[i++] = mod_mask;
4709 }
4710 if (IS_SPECIAL(n))
4711 {
4712 temp[i++] = K_SPECIAL;
4713 temp[i++] = K_SECOND(n);
4714 temp[i++] = K_THIRD(n);
4715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716 else if (has_mbyte)
4717 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718 else
4719 temp[i++] = n;
4720 temp[i++] = NUL;
4721 rettv->v_type = VAR_STRING;
4722 rettv->vval.v_string = vim_strsave(temp);
4723
4724#ifdef FEAT_MOUSE
4725 if (is_mouse_key(n))
4726 {
4727 int row = mouse_row;
4728 int col = mouse_col;
4729 win_T *win;
4730 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004731 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004732 int winnr = 1;
4733
4734 if (row >= 0 && col >= 0)
4735 {
4736 /* Find the window at the mouse coordinates and compute the
4737 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004738 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004739 if (win == NULL)
4740 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004742# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004743 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004744 winnr = 0;
4745 else
4746# endif
4747 for (wp = firstwin; wp != win && wp != NULL;
4748 wp = wp->w_next)
4749 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4751 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4752 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4753 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4754 }
4755 }
4756#endif
4757 }
4758}
4759
4760/*
4761 * "getcharmod()" function
4762 */
4763 static void
4764f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4765{
4766 rettv->vval.v_number = mod_mask;
4767}
4768
4769/*
4770 * "getcharsearch()" function
4771 */
4772 static void
4773f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4774{
4775 if (rettv_dict_alloc(rettv) != FAIL)
4776 {
4777 dict_T *dict = rettv->vval.v_dict;
4778
Bram Moolenaare0be1672018-07-08 16:50:37 +02004779 dict_add_string(dict, "char", last_csearch());
4780 dict_add_number(dict, "forward", last_csearch_forward());
4781 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782 }
4783}
4784
4785/*
4786 * "getcmdline()" function
4787 */
4788 static void
4789f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4790{
4791 rettv->v_type = VAR_STRING;
4792 rettv->vval.v_string = get_cmdline_str();
4793}
4794
4795/*
4796 * "getcmdpos()" function
4797 */
4798 static void
4799f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4800{
4801 rettv->vval.v_number = get_cmdline_pos() + 1;
4802}
4803
4804/*
4805 * "getcmdtype()" function
4806 */
4807 static void
4808f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4809{
4810 rettv->v_type = VAR_STRING;
4811 rettv->vval.v_string = alloc(2);
4812 if (rettv->vval.v_string != NULL)
4813 {
4814 rettv->vval.v_string[0] = get_cmdline_type();
4815 rettv->vval.v_string[1] = NUL;
4816 }
4817}
4818
4819/*
4820 * "getcmdwintype()" function
4821 */
4822 static void
4823f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4824{
4825 rettv->v_type = VAR_STRING;
4826 rettv->vval.v_string = NULL;
4827#ifdef FEAT_CMDWIN
4828 rettv->vval.v_string = alloc(2);
4829 if (rettv->vval.v_string != NULL)
4830 {
4831 rettv->vval.v_string[0] = cmdwin_type;
4832 rettv->vval.v_string[1] = NUL;
4833 }
4834#endif
4835}
4836
4837#if defined(FEAT_CMDL_COMPL)
4838/*
4839 * "getcompletion()" function
4840 */
4841 static void
4842f_getcompletion(typval_T *argvars, typval_T *rettv)
4843{
4844 char_u *pat;
4845 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004846 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004847 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4848 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004851 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 if (p_wic)
4854 options |= WILD_ICASE;
4855
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004856 /* For filtered results, 'wildignore' is used */
4857 if (!filtered)
4858 options |= WILD_KEEP_ALL;
4859
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004860 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004861 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004863 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004864 if (xpc.xp_context == EXPAND_NOTHING)
4865 {
4866 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004868 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004869 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 return;
4871 }
4872
4873# if defined(FEAT_MENU)
4874 if (xpc.xp_context == EXPAND_MENUS)
4875 {
4876 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4877 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4878 }
4879# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004880#ifdef FEAT_CSCOPE
4881 if (xpc.xp_context == EXPAND_CSCOPE)
4882 {
4883 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4884 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4885 }
4886#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004887#ifdef FEAT_SIGNS
4888 if (xpc.xp_context == EXPAND_SIGN)
4889 {
4890 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4891 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4892 }
4893#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894
4895 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4896 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4897 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004898 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899
4900 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4901
4902 for (i = 0; i < xpc.xp_numfiles; i++)
4903 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4904 }
4905 vim_free(pat);
4906 ExpandCleanup(&xpc);
4907}
4908#endif
4909
4910/*
4911 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004912 *
4913 * Return the current working directory of a window in a tab page.
4914 * First optional argument 'winnr' is the window number or -1 and the second
4915 * optional argument 'tabnr' is the tab page number.
4916 *
4917 * If no arguments are supplied, then return the directory of the current
4918 * window.
4919 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4920 * the specified window.
4921 * If 'winnr' is 0 then return the directory of the current window.
4922 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4923 * directory of the specified tab page. Otherwise return the directory of the
4924 * specified window in the specified tab page.
4925 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926 */
4927 static void
4928f_getcwd(typval_T *argvars, typval_T *rettv)
4929{
4930 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004931 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004932 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004933 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004934
4935 rettv->v_type = VAR_STRING;
4936 rettv->vval.v_string = NULL;
4937
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004938 if (argvars[0].v_type == VAR_NUMBER
4939 && argvars[0].vval.v_number == -1
4940 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004941 global = TRUE;
4942 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004943 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004944
4945 if (wp != NULL && wp->w_localdir != NULL)
4946 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004947 else if (tp != NULL && tp->tp_localdir != NULL)
4948 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4949 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004951 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004952 rettv->vval.v_string = vim_strsave(globaldir);
4953 else
4954 {
4955 cwd = alloc(MAXPATHL);
4956 if (cwd != NULL)
4957 {
4958 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4959 rettv->vval.v_string = vim_strsave(cwd);
4960 vim_free(cwd);
4961 }
4962 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004964#ifdef BACKSLASH_IN_FILENAME
4965 if (rettv->vval.v_string != NULL)
4966 slash_adjust(rettv->vval.v_string);
4967#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004968}
4969
4970/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004971 * "getenv()" function
4972 */
4973 static void
4974f_getenv(typval_T *argvars, typval_T *rettv)
4975{
4976 int mustfree = FALSE;
4977 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4978
4979 if (p == NULL)
4980 {
4981 rettv->v_type = VAR_SPECIAL;
4982 rettv->vval.v_number = VVAL_NULL;
4983 return;
4984 }
4985 if (!mustfree)
4986 p = vim_strsave(p);
4987 rettv->vval.v_string = p;
4988 rettv->v_type = VAR_STRING;
4989}
4990
4991/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 * "getfontname()" function
4993 */
4994 static void
4995f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4996{
4997 rettv->v_type = VAR_STRING;
4998 rettv->vval.v_string = NULL;
4999#ifdef FEAT_GUI
5000 if (gui.in_use)
5001 {
5002 GuiFont font;
5003 char_u *name = NULL;
5004
5005 if (argvars[0].v_type == VAR_UNKNOWN)
5006 {
5007 /* Get the "Normal" font. Either the name saved by
5008 * hl_set_font_name() or from the font ID. */
5009 font = gui.norm_font;
5010 name = hl_get_font_name();
5011 }
5012 else
5013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5016 return;
5017 font = gui_mch_get_font(name, FALSE);
5018 if (font == NOFONT)
5019 return; /* Invalid font name, return empty string. */
5020 }
5021 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5022 if (argvars[0].v_type != VAR_UNKNOWN)
5023 gui_mch_free_font(font);
5024 }
5025#endif
5026}
5027
5028/*
5029 * "getfperm({fname})" function
5030 */
5031 static void
5032f_getfperm(typval_T *argvars, typval_T *rettv)
5033{
5034 char_u *fname;
5035 stat_T st;
5036 char_u *perm = NULL;
5037 char_u flags[] = "rwx";
5038 int i;
5039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005040 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041
5042 rettv->v_type = VAR_STRING;
5043 if (mch_stat((char *)fname, &st) >= 0)
5044 {
5045 perm = vim_strsave((char_u *)"---------");
5046 if (perm != NULL)
5047 {
5048 for (i = 0; i < 9; i++)
5049 {
5050 if (st.st_mode & (1 << (8 - i)))
5051 perm[i] = flags[i % 3];
5052 }
5053 }
5054 }
5055 rettv->vval.v_string = perm;
5056}
5057
5058/*
5059 * "getfsize({fname})" function
5060 */
5061 static void
5062f_getfsize(typval_T *argvars, typval_T *rettv)
5063{
5064 char_u *fname;
5065 stat_T st;
5066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005067 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005068
5069 rettv->v_type = VAR_NUMBER;
5070
5071 if (mch_stat((char *)fname, &st) >= 0)
5072 {
5073 if (mch_isdir(fname))
5074 rettv->vval.v_number = 0;
5075 else
5076 {
5077 rettv->vval.v_number = (varnumber_T)st.st_size;
5078
5079 /* non-perfect check for overflow */
5080 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5081 rettv->vval.v_number = -2;
5082 }
5083 }
5084 else
5085 rettv->vval.v_number = -1;
5086}
5087
5088/*
5089 * "getftime({fname})" function
5090 */
5091 static void
5092f_getftime(typval_T *argvars, typval_T *rettv)
5093{
5094 char_u *fname;
5095 stat_T st;
5096
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098
5099 if (mch_stat((char *)fname, &st) >= 0)
5100 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5101 else
5102 rettv->vval.v_number = -1;
5103}
5104
5105/*
5106 * "getftype({fname})" function
5107 */
5108 static void
5109f_getftype(typval_T *argvars, typval_T *rettv)
5110{
5111 char_u *fname;
5112 stat_T st;
5113 char_u *type = NULL;
5114 char *t;
5115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005116 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117
5118 rettv->v_type = VAR_STRING;
5119 if (mch_lstat((char *)fname, &st) >= 0)
5120 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 if (S_ISREG(st.st_mode))
5122 t = "file";
5123 else if (S_ISDIR(st.st_mode))
5124 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 else if (S_ISLNK(st.st_mode))
5126 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005127 else if (S_ISBLK(st.st_mode))
5128 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005129 else if (S_ISCHR(st.st_mode))
5130 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005131 else if (S_ISFIFO(st.st_mode))
5132 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005133 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005134 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 else
5136 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 type = vim_strsave((char_u *)t);
5138 }
5139 rettv->vval.v_string = type;
5140}
5141
5142/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005143 * "getjumplist()" function
5144 */
5145 static void
5146f_getjumplist(typval_T *argvars, typval_T *rettv)
5147{
5148#ifdef FEAT_JUMPLIST
5149 win_T *wp;
5150 int i;
5151 list_T *l;
5152 dict_T *d;
5153#endif
5154
5155 if (rettv_list_alloc(rettv) != OK)
5156 return;
5157
5158#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005159 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005160 if (wp == NULL)
5161 return;
5162
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005163 cleanup_jumplist(wp, TRUE);
5164
Bram Moolenaar4f505882018-02-10 21:06:32 +01005165 l = list_alloc();
5166 if (l == NULL)
5167 return;
5168
5169 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5170 return;
5171 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5172
5173 for (i = 0; i < wp->w_jumplistlen; ++i)
5174 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005175 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5176 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005177 if ((d = dict_alloc()) == NULL)
5178 return;
5179 if (list_append_dict(l, d) == FAIL)
5180 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005181 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5182 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005183 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005184 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005185 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005186 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005187 }
5188#endif
5189}
5190
5191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 * "getline(lnum, [end])" function
5193 */
5194 static void
5195f_getline(typval_T *argvars, typval_T *rettv)
5196{
5197 linenr_T lnum;
5198 linenr_T end;
5199 int retlist;
5200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005201 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 if (argvars[1].v_type == VAR_UNKNOWN)
5203 {
5204 end = 0;
5205 retlist = FALSE;
5206 }
5207 else
5208 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005209 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210 retlist = TRUE;
5211 }
5212
5213 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5214}
5215
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005216#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005217 static void
5218get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5219{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005220 if (what_arg->v_type == VAR_UNKNOWN)
5221 {
5222 if (rettv_list_alloc(rettv) == OK)
5223 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005224 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005225 }
5226 else
5227 {
5228 if (rettv_dict_alloc(rettv) == OK)
5229 if (is_qf || (wp != NULL))
5230 {
5231 if (what_arg->v_type == VAR_DICT)
5232 {
5233 dict_T *d = what_arg->vval.v_dict;
5234
5235 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005236 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237 }
5238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005239 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005240 }
5241 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005242}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005243#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005244
5245/*
5246 * "getloclist()" function
5247 */
5248 static void
5249f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5250{
5251#ifdef FEAT_QUICKFIX
5252 win_T *wp;
5253
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005254 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005255 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5256#endif
5257}
5258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 * "getpid()" function
5261 */
5262 static void
5263f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5264{
5265 rettv->vval.v_number = mch_get_pid();
5266}
5267
5268 static void
5269getpos_both(
5270 typval_T *argvars,
5271 typval_T *rettv,
5272 int getcurpos)
5273{
5274 pos_T *fp;
5275 list_T *l;
5276 int fnum = -1;
5277
5278 if (rettv_list_alloc(rettv) == OK)
5279 {
5280 l = rettv->vval.v_list;
5281 if (getcurpos)
5282 fp = &curwin->w_cursor;
5283 else
5284 fp = var2fpos(&argvars[0], TRUE, &fnum);
5285 if (fnum != -1)
5286 list_append_number(l, (varnumber_T)fnum);
5287 else
5288 list_append_number(l, (varnumber_T)0);
5289 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5290 : (varnumber_T)0);
5291 list_append_number(l, (fp != NULL)
5292 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5293 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005294 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 (varnumber_T)0);
5296 if (getcurpos)
5297 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005298 int save_set_curswant = curwin->w_set_curswant;
5299 colnr_T save_curswant = curwin->w_curswant;
5300 colnr_T save_virtcol = curwin->w_virtcol;
5301
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 update_curswant();
5303 list_append_number(l, curwin->w_curswant == MAXCOL ?
5304 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005305
5306 // Do not change "curswant", as it is unexpected that a get
5307 // function has a side effect.
5308 if (save_set_curswant)
5309 {
5310 curwin->w_set_curswant = save_set_curswant;
5311 curwin->w_curswant = save_curswant;
5312 curwin->w_virtcol = save_virtcol;
5313 curwin->w_valid &= ~VALID_VIRTCOL;
5314 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005315 }
5316 }
5317 else
5318 rettv->vval.v_number = FALSE;
5319}
5320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321/*
5322 * "getcurpos()" function
5323 */
5324 static void
5325f_getcurpos(typval_T *argvars, typval_T *rettv)
5326{
5327 getpos_both(argvars, rettv, TRUE);
5328}
5329
5330/*
5331 * "getpos(string)" function
5332 */
5333 static void
5334f_getpos(typval_T *argvars, typval_T *rettv)
5335{
5336 getpos_both(argvars, rettv, FALSE);
5337}
5338
5339/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005340 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 */
5342 static void
5343f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5344{
5345#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005346 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347#endif
5348}
5349
5350/*
5351 * "getreg()" function
5352 */
5353 static void
5354f_getreg(typval_T *argvars, typval_T *rettv)
5355{
5356 char_u *strregname;
5357 int regname;
5358 int arg2 = FALSE;
5359 int return_list = FALSE;
5360 int error = FALSE;
5361
5362 if (argvars[0].v_type != VAR_UNKNOWN)
5363 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005364 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 error = strregname == NULL;
5366 if (argvars[1].v_type != VAR_UNKNOWN)
5367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005368 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005370 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005371 }
5372 }
5373 else
5374 strregname = get_vim_var_str(VV_REG);
5375
5376 if (error)
5377 return;
5378
5379 regname = (strregname == NULL ? '"' : *strregname);
5380 if (regname == 0)
5381 regname = '"';
5382
5383 if (return_list)
5384 {
5385 rettv->v_type = VAR_LIST;
5386 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5387 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5388 if (rettv->vval.v_list == NULL)
5389 (void)rettv_list_alloc(rettv);
5390 else
5391 ++rettv->vval.v_list->lv_refcount;
5392 }
5393 else
5394 {
5395 rettv->v_type = VAR_STRING;
5396 rettv->vval.v_string = get_reg_contents(regname,
5397 arg2 ? GREG_EXPR_SRC : 0);
5398 }
5399}
5400
5401/*
5402 * "getregtype()" function
5403 */
5404 static void
5405f_getregtype(typval_T *argvars, typval_T *rettv)
5406{
5407 char_u *strregname;
5408 int regname;
5409 char_u buf[NUMBUFLEN + 2];
5410 long reglen = 0;
5411
5412 if (argvars[0].v_type != VAR_UNKNOWN)
5413 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005414 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005415 if (strregname == NULL) /* type error; errmsg already given */
5416 {
5417 rettv->v_type = VAR_STRING;
5418 rettv->vval.v_string = NULL;
5419 return;
5420 }
5421 }
5422 else
5423 /* Default to v:register */
5424 strregname = get_vim_var_str(VV_REG);
5425
5426 regname = (strregname == NULL ? '"' : *strregname);
5427 if (regname == 0)
5428 regname = '"';
5429
5430 buf[0] = NUL;
5431 buf[1] = NUL;
5432 switch (get_reg_type(regname, &reglen))
5433 {
5434 case MLINE: buf[0] = 'V'; break;
5435 case MCHAR: buf[0] = 'v'; break;
5436 case MBLOCK:
5437 buf[0] = Ctrl_V;
5438 sprintf((char *)buf + 1, "%ld", reglen + 1);
5439 break;
5440 }
5441 rettv->v_type = VAR_STRING;
5442 rettv->vval.v_string = vim_strsave(buf);
5443}
5444
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005445/*
5446 * Returns information (variables, options, etc.) about a tab page
5447 * as a dictionary.
5448 */
5449 static dict_T *
5450get_tabpage_info(tabpage_T *tp, int tp_idx)
5451{
5452 win_T *wp;
5453 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 list_T *l;
5455
5456 dict = dict_alloc();
5457 if (dict == NULL)
5458 return NULL;
5459
Bram Moolenaare0be1672018-07-08 16:50:37 +02005460 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005461
5462 l = list_alloc();
5463 if (l != NULL)
5464 {
5465 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005466 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005467 list_append_number(l, (varnumber_T)wp->w_id);
5468 dict_add_list(dict, "windows", l);
5469 }
5470
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005471 /* Make a reference to tabpage variables */
5472 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005473
5474 return dict;
5475}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005476
5477/*
5478 * "gettabinfo()" function
5479 */
5480 static void
5481f_gettabinfo(typval_T *argvars, typval_T *rettv)
5482{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005483 tabpage_T *tp, *tparg = NULL;
5484 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005485 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005486
5487 if (rettv_list_alloc(rettv) != OK)
5488 return;
5489
5490 if (argvars[0].v_type != VAR_UNKNOWN)
5491 {
5492 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005493 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005494 if (tparg == NULL)
5495 return;
5496 }
5497
5498 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005499 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005500 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005501 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005502 if (tparg != NULL && tp != tparg)
5503 continue;
5504 d = get_tabpage_info(tp, tpnr);
5505 if (d != NULL)
5506 list_append_dict(rettv->vval.v_list, d);
5507 if (tparg != NULL)
5508 return;
5509 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005510}
5511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512/*
5513 * "gettabvar()" function
5514 */
5515 static void
5516f_gettabvar(typval_T *argvars, typval_T *rettv)
5517{
5518 win_T *oldcurwin;
5519 tabpage_T *tp, *oldtabpage;
5520 dictitem_T *v;
5521 char_u *varname;
5522 int done = FALSE;
5523
5524 rettv->v_type = VAR_STRING;
5525 rettv->vval.v_string = NULL;
5526
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005527 varname = tv_get_string_chk(&argvars[1]);
5528 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 if (tp != NULL && varname != NULL)
5530 {
5531 /* Set tp to be our tabpage, temporarily. Also set the window to the
5532 * first window in the tabpage, otherwise the window is not valid. */
5533 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005534 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5535 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 {
5537 /* look up the variable */
5538 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5539 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5540 if (v != NULL)
5541 {
5542 copy_tv(&v->di_tv, rettv);
5543 done = TRUE;
5544 }
5545 }
5546
5547 /* restore previous notion of curwin */
5548 restore_win(oldcurwin, oldtabpage, TRUE);
5549 }
5550
5551 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5552 copy_tv(&argvars[2], rettv);
5553}
5554
5555/*
5556 * "gettabwinvar()" function
5557 */
5558 static void
5559f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5560{
5561 getwinvar(argvars, rettv, 1);
5562}
5563
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005564/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005565 * "gettagstack()" function
5566 */
5567 static void
5568f_gettagstack(typval_T *argvars, typval_T *rettv)
5569{
5570 win_T *wp = curwin; // default is current window
5571
5572 if (rettv_dict_alloc(rettv) != OK)
5573 return;
5574
5575 if (argvars[0].v_type != VAR_UNKNOWN)
5576 {
5577 wp = find_win_by_nr_or_id(&argvars[0]);
5578 if (wp == NULL)
5579 return;
5580 }
5581
5582 get_tagstack(wp, rettv->vval.v_dict);
5583}
5584
5585/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005586 * Returns information about a window as a dictionary.
5587 */
5588 static dict_T *
5589get_win_info(win_T *wp, short tpnr, short winnr)
5590{
5591 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005592
5593 dict = dict_alloc();
5594 if (dict == NULL)
5595 return NULL;
5596
Bram Moolenaare0be1672018-07-08 16:50:37 +02005597 dict_add_number(dict, "tabnr", tpnr);
5598 dict_add_number(dict, "winnr", winnr);
5599 dict_add_number(dict, "winid", wp->w_id);
5600 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005601 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005602 dict_add_number(dict, "topline", wp->w_topline);
5603 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005604#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005605 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005606#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005607 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005608 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005609 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005610
Bram Moolenaar69905d12017-08-13 18:14:47 +02005611#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005612 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005613#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005614#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005615 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5616 dict_add_number(dict, "loclist",
5617 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005618#endif
5619
Bram Moolenaar30567352016-08-27 21:25:44 +02005620 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005621 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005623 return dict;
5624}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005625
5626/*
5627 * "getwininfo()" function
5628 */
5629 static void
5630f_getwininfo(typval_T *argvars, typval_T *rettv)
5631{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005632 tabpage_T *tp;
5633 win_T *wp = NULL, *wparg = NULL;
5634 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005635 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636
5637 if (rettv_list_alloc(rettv) != OK)
5638 return;
5639
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640 if (argvars[0].v_type != VAR_UNKNOWN)
5641 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005642 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643 if (wparg == NULL)
5644 return;
5645 }
5646
5647 /* Collect information about either all the windows across all the tab
5648 * pages or one particular window.
5649 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005650 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005651 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005652 tabnr++;
5653 winnr = 0;
5654 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005655 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005656 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005657 if (wparg != NULL && wp != wparg)
5658 continue;
5659 d = get_win_info(wp, tabnr, winnr);
5660 if (d != NULL)
5661 list_append_dict(rettv->vval.v_list, d);
5662 if (wparg != NULL)
5663 /* found information about a specific window */
5664 return;
5665 }
5666 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005667}
5668
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005670 * "win_execute()" function
5671 */
5672 static void
5673f_win_execute(typval_T *argvars, typval_T *rettv)
5674{
5675 int id = (int)tv_get_number(argvars);
5676 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005677 win_T *save_curwin;
5678 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005679
5680 if (wp != NULL)
5681 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005682 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5683 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005684 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005685 check_cursor();
5686 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005687 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005688 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005689 }
5690}
5691
5692/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 * "win_findbuf()" function
5694 */
5695 static void
5696f_win_findbuf(typval_T *argvars, typval_T *rettv)
5697{
5698 if (rettv_list_alloc(rettv) != FAIL)
5699 win_findbuf(argvars, rettv->vval.v_list);
5700}
5701
5702/*
5703 * "win_getid()" function
5704 */
5705 static void
5706f_win_getid(typval_T *argvars, typval_T *rettv)
5707{
5708 rettv->vval.v_number = win_getid(argvars);
5709}
5710
5711/*
5712 * "win_gotoid()" function
5713 */
5714 static void
5715f_win_gotoid(typval_T *argvars, typval_T *rettv)
5716{
5717 rettv->vval.v_number = win_gotoid(argvars);
5718}
5719
5720/*
5721 * "win_id2tabwin()" function
5722 */
5723 static void
5724f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5725{
5726 if (rettv_list_alloc(rettv) != FAIL)
5727 win_id2tabwin(argvars, rettv->vval.v_list);
5728}
5729
5730/*
5731 * "win_id2win()" function
5732 */
5733 static void
5734f_win_id2win(typval_T *argvars, typval_T *rettv)
5735{
5736 rettv->vval.v_number = win_id2win(argvars);
5737}
5738
5739/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005740 * "win_screenpos()" function
5741 */
5742 static void
5743f_win_screenpos(typval_T *argvars, typval_T *rettv)
5744{
5745 win_T *wp;
5746
5747 if (rettv_list_alloc(rettv) == FAIL)
5748 return;
5749
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005750 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005751 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5752 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5753}
5754
5755/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005756 * "getwinpos({timeout})" function
5757 */
5758 static void
5759f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5760{
5761 int x = -1;
5762 int y = -1;
5763
5764 if (rettv_list_alloc(rettv) == FAIL)
5765 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005766#if defined(FEAT_GUI) \
5767 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5768 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005769 {
5770 varnumber_T timeout = 100;
5771
5772 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005773 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005774
5775 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005776 }
5777#endif
5778 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5779 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5780}
5781
5782
5783/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005784 * "getwinposx()" function
5785 */
5786 static void
5787f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5788{
5789 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005790#if defined(FEAT_GUI) \
5791 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5792 || defined(MSWIN)
5793
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005794 {
5795 int x, y;
5796
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005797 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005798 rettv->vval.v_number = x;
5799 }
5800#endif
5801}
5802
5803/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005804 * "getwinposy()" function
5805 */
5806 static void
5807f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5808{
5809 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005810#if defined(FEAT_GUI) \
5811 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5812 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813 {
5814 int x, y;
5815
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005816 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005817 rettv->vval.v_number = y;
5818 }
5819#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005820}
5821
5822/*
5823 * "getwinvar()" function
5824 */
5825 static void
5826f_getwinvar(typval_T *argvars, typval_T *rettv)
5827{
5828 getwinvar(argvars, rettv, 0);
5829}
5830
5831/*
5832 * "glob()" function
5833 */
5834 static void
5835f_glob(typval_T *argvars, typval_T *rettv)
5836{
5837 int options = WILD_SILENT|WILD_USE_NL;
5838 expand_T xpc;
5839 int error = FALSE;
5840
5841 /* When the optional second argument is non-zero, don't remove matches
5842 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5843 rettv->v_type = VAR_STRING;
5844 if (argvars[1].v_type != VAR_UNKNOWN)
5845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005846 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 options |= WILD_KEEP_ALL;
5848 if (argvars[2].v_type != VAR_UNKNOWN)
5849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005851 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005853 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 options |= WILD_ALLLINKS;
5855 }
5856 }
5857 if (!error)
5858 {
5859 ExpandInit(&xpc);
5860 xpc.xp_context = EXPAND_FILES;
5861 if (p_wic)
5862 options += WILD_ICASE;
5863 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005864 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 NULL, options, WILD_ALL);
5866 else if (rettv_list_alloc(rettv) != FAIL)
5867 {
5868 int i;
5869
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005870 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871 NULL, options, WILD_ALL_KEEP);
5872 for (i = 0; i < xpc.xp_numfiles; i++)
5873 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5874
5875 ExpandCleanup(&xpc);
5876 }
5877 }
5878 else
5879 rettv->vval.v_string = NULL;
5880}
5881
5882/*
5883 * "globpath()" function
5884 */
5885 static void
5886f_globpath(typval_T *argvars, typval_T *rettv)
5887{
5888 int flags = 0;
5889 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005890 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 int error = FALSE;
5892 garray_T ga;
5893 int i;
5894
5895 /* When the optional second argument is non-zero, don't remove matches
5896 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5897 rettv->v_type = VAR_STRING;
5898 if (argvars[2].v_type != VAR_UNKNOWN)
5899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005900 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901 flags |= WILD_KEEP_ALL;
5902 if (argvars[3].v_type != VAR_UNKNOWN)
5903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005904 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005905 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 flags |= WILD_ALLLINKS;
5909 }
5910 }
5911 if (file != NULL && !error)
5912 {
5913 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005914 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 if (rettv->v_type == VAR_STRING)
5916 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5917 else if (rettv_list_alloc(rettv) != FAIL)
5918 for (i = 0; i < ga.ga_len; ++i)
5919 list_append_string(rettv->vval.v_list,
5920 ((char_u **)(ga.ga_data))[i], -1);
5921 ga_clear_strings(&ga);
5922 }
5923 else
5924 rettv->vval.v_string = NULL;
5925}
5926
5927/*
5928 * "glob2regpat()" function
5929 */
5930 static void
5931f_glob2regpat(typval_T *argvars, typval_T *rettv)
5932{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005933 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934
5935 rettv->v_type = VAR_STRING;
5936 rettv->vval.v_string = (pat == NULL)
5937 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5938}
5939
5940/* for VIM_VERSION_ defines */
5941#include "version.h"
5942
5943/*
5944 * "has()" function
5945 */
5946 static void
5947f_has(typval_T *argvars, typval_T *rettv)
5948{
5949 int i;
5950 char_u *name;
5951 int n = FALSE;
5952 static char *(has_list[]) =
5953 {
5954#ifdef AMIGA
5955 "amiga",
5956# ifdef FEAT_ARP
5957 "arp",
5958# endif
5959#endif
5960#ifdef __BEOS__
5961 "beos",
5962#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005963#if defined(BSD) && !defined(MACOS_X)
5964 "bsd",
5965#endif
5966#ifdef hpux
5967 "hpux",
5968#endif
5969#ifdef __linux__
5970 "linux",
5971#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005972#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005973 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5974 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005975# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005976 "macunix", /* Mac OS X, with the darwin feature */
5977 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005978# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979#endif
5980#ifdef __QNX__
5981 "qnx",
5982#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005983#ifdef SUN_SYSTEM
5984 "sun",
5985#else
5986 "moon",
5987#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988#ifdef UNIX
5989 "unix",
5990#endif
5991#ifdef VMS
5992 "vms",
5993#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005994#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 "win32",
5996#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005997#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005998 "win32unix",
5999#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006000#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006001 "win64",
6002#endif
6003#ifdef EBCDIC
6004 "ebcdic",
6005#endif
6006#ifndef CASE_INSENSITIVE_FILENAME
6007 "fname_case",
6008#endif
6009#ifdef HAVE_ACL
6010 "acl",
6011#endif
6012#ifdef FEAT_ARABIC
6013 "arabic",
6014#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006016#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006017 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006018#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006019#ifdef FEAT_AUTOSERVERNAME
6020 "autoservername",
6021#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006022#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006023 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006024# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 "balloon_multiline",
6026# endif
6027#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006028#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006029 "balloon_eval_term",
6030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6032 "builtin_terms",
6033# ifdef ALL_BUILTIN_TCAPS
6034 "all_builtin_terms",
6035# endif
6036#endif
6037#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006038 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006039 || defined(FEAT_GUI_MOTIF))
6040 "browsefilter",
6041#endif
6042#ifdef FEAT_BYTEOFF
6043 "byte_offset",
6044#endif
6045#ifdef FEAT_JOB_CHANNEL
6046 "channel",
6047#endif
6048#ifdef FEAT_CINDENT
6049 "cindent",
6050#endif
6051#ifdef FEAT_CLIENTSERVER
6052 "clientserver",
6053#endif
6054#ifdef FEAT_CLIPBOARD
6055 "clipboard",
6056#endif
6057#ifdef FEAT_CMDL_COMPL
6058 "cmdline_compl",
6059#endif
6060#ifdef FEAT_CMDHIST
6061 "cmdline_hist",
6062#endif
6063#ifdef FEAT_COMMENTS
6064 "comments",
6065#endif
6066#ifdef FEAT_CONCEAL
6067 "conceal",
6068#endif
6069#ifdef FEAT_CRYPT
6070 "cryptv",
6071 "crypt-blowfish",
6072 "crypt-blowfish2",
6073#endif
6074#ifdef FEAT_CSCOPE
6075 "cscope",
6076#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078#ifdef CURSOR_SHAPE
6079 "cursorshape",
6080#endif
6081#ifdef DEBUG
6082 "debug",
6083#endif
6084#ifdef FEAT_CON_DIALOG
6085 "dialog_con",
6086#endif
6087#ifdef FEAT_GUI_DIALOG
6088 "dialog_gui",
6089#endif
6090#ifdef FEAT_DIFF
6091 "diff",
6092#endif
6093#ifdef FEAT_DIGRAPHS
6094 "digraphs",
6095#endif
6096#ifdef FEAT_DIRECTX
6097 "directx",
6098#endif
6099#ifdef FEAT_DND
6100 "dnd",
6101#endif
6102#ifdef FEAT_EMACS_TAGS
6103 "emacs_tags",
6104#endif
6105 "eval", /* always present, of course! */
6106 "ex_extra", /* graduated feature */
6107#ifdef FEAT_SEARCH_EXTRA
6108 "extra_search",
6109#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110#ifdef FEAT_SEARCHPATH
6111 "file_in_path",
6112#endif
6113#ifdef FEAT_FILTERPIPE
6114 "filterpipe",
6115#endif
6116#ifdef FEAT_FIND_ID
6117 "find_in_path",
6118#endif
6119#ifdef FEAT_FLOAT
6120 "float",
6121#endif
6122#ifdef FEAT_FOLDING
6123 "folding",
6124#endif
6125#ifdef FEAT_FOOTER
6126 "footer",
6127#endif
6128#if !defined(USE_SYSTEM) && defined(UNIX)
6129 "fork",
6130#endif
6131#ifdef FEAT_GETTEXT
6132 "gettext",
6133#endif
6134#ifdef FEAT_GUI
6135 "gui",
6136#endif
6137#ifdef FEAT_GUI_ATHENA
6138# ifdef FEAT_GUI_NEXTAW
6139 "gui_neXtaw",
6140# else
6141 "gui_athena",
6142# endif
6143#endif
6144#ifdef FEAT_GUI_GTK
6145 "gui_gtk",
6146# ifdef USE_GTK3
6147 "gui_gtk3",
6148# else
6149 "gui_gtk2",
6150# endif
6151#endif
6152#ifdef FEAT_GUI_GNOME
6153 "gui_gnome",
6154#endif
6155#ifdef FEAT_GUI_MAC
6156 "gui_mac",
6157#endif
6158#ifdef FEAT_GUI_MOTIF
6159 "gui_motif",
6160#endif
6161#ifdef FEAT_GUI_PHOTON
6162 "gui_photon",
6163#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006164#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165 "gui_win32",
6166#endif
6167#ifdef FEAT_HANGULIN
6168 "hangul_input",
6169#endif
6170#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6171 "iconv",
6172#endif
6173#ifdef FEAT_INS_EXPAND
6174 "insert_expand",
6175#endif
6176#ifdef FEAT_JOB_CHANNEL
6177 "job",
6178#endif
6179#ifdef FEAT_JUMPLIST
6180 "jumplist",
6181#endif
6182#ifdef FEAT_KEYMAP
6183 "keymap",
6184#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006185 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186#ifdef FEAT_LANGMAP
6187 "langmap",
6188#endif
6189#ifdef FEAT_LIBCALL
6190 "libcall",
6191#endif
6192#ifdef FEAT_LINEBREAK
6193 "linebreak",
6194#endif
6195#ifdef FEAT_LISP
6196 "lispindent",
6197#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006198 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006199#ifdef FEAT_LOCALMAP
6200 "localmap",
6201#endif
6202#ifdef FEAT_LUA
6203# ifndef DYNAMIC_LUA
6204 "lua",
6205# endif
6206#endif
6207#ifdef FEAT_MENU
6208 "menu",
6209#endif
6210#ifdef FEAT_SESSION
6211 "mksession",
6212#endif
6213#ifdef FEAT_MODIFY_FNAME
6214 "modify_fname",
6215#endif
6216#ifdef FEAT_MOUSE
6217 "mouse",
6218#endif
6219#ifdef FEAT_MOUSESHAPE
6220 "mouseshape",
6221#endif
6222#if defined(UNIX) || defined(VMS)
6223# ifdef FEAT_MOUSE_DEC
6224 "mouse_dec",
6225# endif
6226# ifdef FEAT_MOUSE_GPM
6227 "mouse_gpm",
6228# endif
6229# ifdef FEAT_MOUSE_JSB
6230 "mouse_jsbterm",
6231# endif
6232# ifdef FEAT_MOUSE_NET
6233 "mouse_netterm",
6234# endif
6235# ifdef FEAT_MOUSE_PTERM
6236 "mouse_pterm",
6237# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006238# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 "mouse_sgr",
6240# endif
6241# ifdef FEAT_SYSMOUSE
6242 "mouse_sysmouse",
6243# endif
6244# ifdef FEAT_MOUSE_URXVT
6245 "mouse_urxvt",
6246# endif
6247# ifdef FEAT_MOUSE_XTERM
6248 "mouse_xterm",
6249# endif
6250#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252#ifdef FEAT_MBYTE_IME
6253 "multi_byte_ime",
6254#endif
6255#ifdef FEAT_MULTI_LANG
6256 "multi_lang",
6257#endif
6258#ifdef FEAT_MZSCHEME
6259#ifndef DYNAMIC_MZSCHEME
6260 "mzscheme",
6261#endif
6262#endif
6263#ifdef FEAT_NUM64
6264 "num64",
6265#endif
6266#ifdef FEAT_OLE
6267 "ole",
6268#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006269#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006271#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#ifdef FEAT_PATH_EXTRA
6273 "path_extra",
6274#endif
6275#ifdef FEAT_PERL
6276#ifndef DYNAMIC_PERL
6277 "perl",
6278#endif
6279#endif
6280#ifdef FEAT_PERSISTENT_UNDO
6281 "persistent_undo",
6282#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006283#if defined(FEAT_PYTHON)
6284 "python_compiled",
6285# if defined(DYNAMIC_PYTHON)
6286 "python_dynamic",
6287# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006289 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006290# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006292#if defined(FEAT_PYTHON3)
6293 "python3_compiled",
6294# if defined(DYNAMIC_PYTHON3)
6295 "python3_dynamic",
6296# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006298 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006299# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#endif
6301#ifdef FEAT_POSTSCRIPT
6302 "postscript",
6303#endif
6304#ifdef FEAT_PRINTER
6305 "printer",
6306#endif
6307#ifdef FEAT_PROFILE
6308 "profile",
6309#endif
6310#ifdef FEAT_RELTIME
6311 "reltime",
6312#endif
6313#ifdef FEAT_QUICKFIX
6314 "quickfix",
6315#endif
6316#ifdef FEAT_RIGHTLEFT
6317 "rightleft",
6318#endif
6319#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6320 "ruby",
6321#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006322 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006323#ifdef FEAT_CMDL_INFO
6324 "showcmd",
6325 "cmdline_info",
6326#endif
6327#ifdef FEAT_SIGNS
6328 "signs",
6329#endif
6330#ifdef FEAT_SMARTINDENT
6331 "smartindent",
6332#endif
6333#ifdef STARTUPTIME
6334 "startuptime",
6335#endif
6336#ifdef FEAT_STL_OPT
6337 "statusline",
6338#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339#ifdef FEAT_NETBEANS_INTG
6340 "netbeans_intg",
6341#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006342#ifdef FEAT_SOUND
6343 "sound",
6344#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006345#ifdef FEAT_SPELL
6346 "spell",
6347#endif
6348#ifdef FEAT_SYN_HL
6349 "syntax",
6350#endif
6351#if defined(USE_SYSTEM) || !defined(UNIX)
6352 "system",
6353#endif
6354#ifdef FEAT_TAG_BINS
6355 "tag_binary",
6356#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006357#ifdef FEAT_TCL
6358# ifndef DYNAMIC_TCL
6359 "tcl",
6360# endif
6361#endif
6362#ifdef FEAT_TERMGUICOLORS
6363 "termguicolors",
6364#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006365#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006366 "terminal",
6367#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368#ifdef TERMINFO
6369 "terminfo",
6370#endif
6371#ifdef FEAT_TERMRESPONSE
6372 "termresponse",
6373#endif
6374#ifdef FEAT_TEXTOBJ
6375 "textobjects",
6376#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006377#ifdef FEAT_TEXT_PROP
6378 "textprop",
6379#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006380#ifdef HAVE_TGETENT
6381 "tgetent",
6382#endif
6383#ifdef FEAT_TIMERS
6384 "timers",
6385#endif
6386#ifdef FEAT_TITLE
6387 "title",
6388#endif
6389#ifdef FEAT_TOOLBAR
6390 "toolbar",
6391#endif
6392#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6393 "unnamedplus",
6394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 "user-commands", /* was accidentally included in 5.4 */
6396 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006397#ifdef FEAT_VARTABS
6398 "vartabs",
6399#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006400 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401#ifdef FEAT_VIMINFO
6402 "viminfo",
6403#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006404 "vimscript-1",
6405 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006406 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006411#ifdef FEAT_VTP
6412 "vtp",
6413#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414#ifdef FEAT_WILDIGN
6415 "wildignore",
6416#endif
6417#ifdef FEAT_WILDMENU
6418 "wildmenu",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#ifdef FEAT_WAK
6422 "winaltkeys",
6423#endif
6424#ifdef FEAT_WRITEBACKUP
6425 "writebackup",
6426#endif
6427#ifdef FEAT_XIM
6428 "xim",
6429#endif
6430#ifdef FEAT_XFONTSET
6431 "xfontset",
6432#endif
6433#ifdef FEAT_XPM_W32
6434 "xpm",
6435 "xpm_w32", /* for backward compatibility */
6436#else
6437# if defined(HAVE_XPM)
6438 "xpm",
6439# endif
6440#endif
6441#ifdef USE_XSMP
6442 "xsmp",
6443#endif
6444#ifdef USE_XSMP_INTERACT
6445 "xsmp_interact",
6446#endif
6447#ifdef FEAT_XCLIPBOARD
6448 "xterm_clipboard",
6449#endif
6450#ifdef FEAT_XTERM_SAVE
6451 "xterm_save",
6452#endif
6453#if defined(UNIX) && defined(FEAT_X11)
6454 "X11",
6455#endif
6456 NULL
6457 };
6458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006459 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460 for (i = 0; has_list[i] != NULL; ++i)
6461 if (STRICMP(name, has_list[i]) == 0)
6462 {
6463 n = TRUE;
6464 break;
6465 }
6466
6467 if (n == FALSE)
6468 {
6469 if (STRNICMP(name, "patch", 5) == 0)
6470 {
6471 if (name[5] == '-'
6472 && STRLEN(name) >= 11
6473 && vim_isdigit(name[6])
6474 && vim_isdigit(name[8])
6475 && vim_isdigit(name[10]))
6476 {
6477 int major = atoi((char *)name + 6);
6478 int minor = atoi((char *)name + 8);
6479
6480 /* Expect "patch-9.9.01234". */
6481 n = (major < VIM_VERSION_MAJOR
6482 || (major == VIM_VERSION_MAJOR
6483 && (minor < VIM_VERSION_MINOR
6484 || (minor == VIM_VERSION_MINOR
6485 && has_patch(atoi((char *)name + 10))))));
6486 }
6487 else
6488 n = has_patch(atoi((char *)name + 5));
6489 }
6490 else if (STRICMP(name, "vim_starting") == 0)
6491 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006492 else if (STRICMP(name, "ttyin") == 0)
6493 n = mch_input_isatty();
6494 else if (STRICMP(name, "ttyout") == 0)
6495 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 else if (STRICMP(name, "multi_byte_encoding") == 0)
6497 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006498#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499 else if (STRICMP(name, "balloon_multiline") == 0)
6500 n = multiline_balloon_available();
6501#endif
6502#ifdef DYNAMIC_TCL
6503 else if (STRICMP(name, "tcl") == 0)
6504 n = tcl_enabled(FALSE);
6505#endif
6506#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6507 else if (STRICMP(name, "iconv") == 0)
6508 n = iconv_enabled(FALSE);
6509#endif
6510#ifdef DYNAMIC_LUA
6511 else if (STRICMP(name, "lua") == 0)
6512 n = lua_enabled(FALSE);
6513#endif
6514#ifdef DYNAMIC_MZSCHEME
6515 else if (STRICMP(name, "mzscheme") == 0)
6516 n = mzscheme_enabled(FALSE);
6517#endif
6518#ifdef DYNAMIC_RUBY
6519 else if (STRICMP(name, "ruby") == 0)
6520 n = ruby_enabled(FALSE);
6521#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#ifdef DYNAMIC_PYTHON
6523 else if (STRICMP(name, "python") == 0)
6524 n = python_enabled(FALSE);
6525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526#ifdef DYNAMIC_PYTHON3
6527 else if (STRICMP(name, "python3") == 0)
6528 n = python3_enabled(FALSE);
6529#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006530#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6531 else if (STRICMP(name, "pythonx") == 0)
6532 {
6533# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6534 if (p_pyx == 0)
6535 n = python3_enabled(FALSE) || python_enabled(FALSE);
6536 else if (p_pyx == 3)
6537 n = python3_enabled(FALSE);
6538 else if (p_pyx == 2)
6539 n = python_enabled(FALSE);
6540# elif defined(DYNAMIC_PYTHON)
6541 n = python_enabled(FALSE);
6542# elif defined(DYNAMIC_PYTHON3)
6543 n = python3_enabled(FALSE);
6544# endif
6545 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546#endif
6547#ifdef DYNAMIC_PERL
6548 else if (STRICMP(name, "perl") == 0)
6549 n = perl_enabled(FALSE);
6550#endif
6551#ifdef FEAT_GUI
6552 else if (STRICMP(name, "gui_running") == 0)
6553 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554# ifdef FEAT_BROWSE
6555 else if (STRICMP(name, "browse") == 0)
6556 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6557# endif
6558#endif
6559#ifdef FEAT_SYN_HL
6560 else if (STRICMP(name, "syntax_items") == 0)
6561 n = syntax_present(curwin);
6562#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006563#ifdef FEAT_VTP
6564 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006565 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#endif
6567#ifdef FEAT_NETBEANS_INTG
6568 else if (STRICMP(name, "netbeans_enabled") == 0)
6569 n = netbeans_active();
6570#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006571#ifdef FEAT_MOUSE_GPM
6572 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6573 n = gpm_enabled();
6574#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006575#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006576 else if (STRICMP(name, "terminal") == 0)
6577 n = terminal_enabled();
6578#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006579#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006580 else if (STRICMP(name, "conpty") == 0)
6581 n = use_conpty();
6582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583 }
6584
6585 rettv->vval.v_number = n;
6586}
6587
6588/*
6589 * "has_key()" function
6590 */
6591 static void
6592f_has_key(typval_T *argvars, typval_T *rettv)
6593{
6594 if (argvars[0].v_type != VAR_DICT)
6595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006596 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597 return;
6598 }
6599 if (argvars[0].vval.v_dict == NULL)
6600 return;
6601
6602 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006603 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604}
6605
6606/*
6607 * "haslocaldir()" function
6608 */
6609 static void
6610f_haslocaldir(typval_T *argvars, typval_T *rettv)
6611{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006612 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 win_T *wp = NULL;
6614
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006615 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6616
6617 // Check for window-local and tab-local directories
6618 if (wp != NULL && wp->w_localdir != NULL)
6619 rettv->vval.v_number = 1;
6620 else if (tp != NULL && tp->tp_localdir != NULL)
6621 rettv->vval.v_number = 2;
6622 else
6623 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006624}
6625
6626/*
6627 * "hasmapto()" function
6628 */
6629 static void
6630f_hasmapto(typval_T *argvars, typval_T *rettv)
6631{
6632 char_u *name;
6633 char_u *mode;
6634 char_u buf[NUMBUFLEN];
6635 int abbr = FALSE;
6636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006637 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 if (argvars[1].v_type == VAR_UNKNOWN)
6639 mode = (char_u *)"nvo";
6640 else
6641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006642 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006644 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 }
6646
6647 if (map_to_exists(name, mode, abbr))
6648 rettv->vval.v_number = TRUE;
6649 else
6650 rettv->vval.v_number = FALSE;
6651}
6652
6653/*
6654 * "histadd()" function
6655 */
6656 static void
6657f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6658{
6659#ifdef FEAT_CMDHIST
6660 int histype;
6661 char_u *str;
6662 char_u buf[NUMBUFLEN];
6663#endif
6664
6665 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006666 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 return;
6668#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006669 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 histype = str != NULL ? get_histtype(str) : -1;
6671 if (histype >= 0)
6672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006673 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674 if (*str != NUL)
6675 {
6676 init_history();
6677 add_to_history(histype, str, FALSE, NUL);
6678 rettv->vval.v_number = TRUE;
6679 return;
6680 }
6681 }
6682#endif
6683}
6684
6685/*
6686 * "histdel()" function
6687 */
6688 static void
6689f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6690{
6691#ifdef FEAT_CMDHIST
6692 int n;
6693 char_u buf[NUMBUFLEN];
6694 char_u *str;
6695
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006696 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 if (str == NULL)
6698 n = 0;
6699 else if (argvars[1].v_type == VAR_UNKNOWN)
6700 /* only one argument: clear entire history */
6701 n = clr_history(get_histtype(str));
6702 else if (argvars[1].v_type == VAR_NUMBER)
6703 /* index given: remove that entry */
6704 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006705 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706 else
6707 /* string given: remove all matching entries */
6708 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006709 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006710 rettv->vval.v_number = n;
6711#endif
6712}
6713
6714/*
6715 * "histget()" function
6716 */
6717 static void
6718f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6719{
6720#ifdef FEAT_CMDHIST
6721 int type;
6722 int idx;
6723 char_u *str;
6724
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006725 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726 if (str == NULL)
6727 rettv->vval.v_string = NULL;
6728 else
6729 {
6730 type = get_histtype(str);
6731 if (argvars[1].v_type == VAR_UNKNOWN)
6732 idx = get_history_idx(type);
6733 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006734 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006735 /* -1 on type error */
6736 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6737 }
6738#else
6739 rettv->vval.v_string = NULL;
6740#endif
6741 rettv->v_type = VAR_STRING;
6742}
6743
6744/*
6745 * "histnr()" function
6746 */
6747 static void
6748f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6749{
6750 int i;
6751
6752#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006753 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754
6755 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6756 if (i >= HIST_CMD && i < HIST_COUNT)
6757 i = get_history_idx(i);
6758 else
6759#endif
6760 i = -1;
6761 rettv->vval.v_number = i;
6762}
6763
6764/*
6765 * "highlightID(name)" function
6766 */
6767 static void
6768f_hlID(typval_T *argvars, typval_T *rettv)
6769{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006770 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771}
6772
6773/*
6774 * "highlight_exists()" function
6775 */
6776 static void
6777f_hlexists(typval_T *argvars, typval_T *rettv)
6778{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006779 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780}
6781
6782/*
6783 * "hostname()" function
6784 */
6785 static void
6786f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6787{
6788 char_u hostname[256];
6789
6790 mch_get_host_name(hostname, 256);
6791 rettv->v_type = VAR_STRING;
6792 rettv->vval.v_string = vim_strsave(hostname);
6793}
6794
6795/*
6796 * iconv() function
6797 */
6798 static void
6799f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6800{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801 char_u buf1[NUMBUFLEN];
6802 char_u buf2[NUMBUFLEN];
6803 char_u *from, *to, *str;
6804 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805
6806 rettv->v_type = VAR_STRING;
6807 rettv->vval.v_string = NULL;
6808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006809 str = tv_get_string(&argvars[0]);
6810 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6811 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 vimconv.vc_type = CONV_NONE;
6813 convert_setup(&vimconv, from, to);
6814
6815 /* If the encodings are equal, no conversion needed. */
6816 if (vimconv.vc_type == CONV_NONE)
6817 rettv->vval.v_string = vim_strsave(str);
6818 else
6819 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6820
6821 convert_setup(&vimconv, NULL, NULL);
6822 vim_free(from);
6823 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824}
6825
6826/*
6827 * "indent()" function
6828 */
6829 static void
6830f_indent(typval_T *argvars, typval_T *rettv)
6831{
6832 linenr_T lnum;
6833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006834 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6836 rettv->vval.v_number = get_indent_lnum(lnum);
6837 else
6838 rettv->vval.v_number = -1;
6839}
6840
6841/*
6842 * "index()" function
6843 */
6844 static void
6845f_index(typval_T *argvars, typval_T *rettv)
6846{
6847 list_T *l;
6848 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006849 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850 long idx = 0;
6851 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006852 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853
6854 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006855 if (argvars[0].v_type == VAR_BLOB)
6856 {
6857 typval_T tv;
6858 int start = 0;
6859
6860 if (argvars[2].v_type != VAR_UNKNOWN)
6861 {
6862 start = tv_get_number_chk(&argvars[2], &error);
6863 if (error)
6864 return;
6865 }
6866 b = argvars[0].vval.v_blob;
6867 if (b == NULL)
6868 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006869 if (start < 0)
6870 {
6871 start = blob_len(b) + start;
6872 if (start < 0)
6873 start = 0;
6874 }
6875
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006876 for (idx = start; idx < blob_len(b); ++idx)
6877 {
6878 tv.v_type = VAR_NUMBER;
6879 tv.vval.v_number = blob_get(b, idx);
6880 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6881 {
6882 rettv->vval.v_number = idx;
6883 return;
6884 }
6885 }
6886 return;
6887 }
6888 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006890 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 return;
6892 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 l = argvars[0].vval.v_list;
6895 if (l != NULL)
6896 {
6897 item = l->lv_first;
6898 if (argvars[2].v_type != VAR_UNKNOWN)
6899 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 /* Start at specified item. Use the cached index that list_find()
6901 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006902 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 idx = l->lv_idx;
6904 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 if (error)
6907 item = NULL;
6908 }
6909
6910 for ( ; item != NULL; item = item->li_next, ++idx)
6911 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6912 {
6913 rettv->vval.v_number = idx;
6914 break;
6915 }
6916 }
6917}
6918
6919static int inputsecret_flag = 0;
6920
6921/*
6922 * "input()" function
6923 * Also handles inputsecret() when inputsecret is set.
6924 */
6925 static void
6926f_input(typval_T *argvars, typval_T *rettv)
6927{
6928 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6929}
6930
6931/*
6932 * "inputdialog()" function
6933 */
6934 static void
6935f_inputdialog(typval_T *argvars, typval_T *rettv)
6936{
6937#if defined(FEAT_GUI_TEXTDIALOG)
6938 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6939 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6940 {
6941 char_u *message;
6942 char_u buf[NUMBUFLEN];
6943 char_u *defstr = (char_u *)"";
6944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006945 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6949 else
6950 IObuff[0] = NUL;
6951 if (message != NULL && defstr != NULL
6952 && do_dialog(VIM_QUESTION, NULL, message,
6953 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6954 rettv->vval.v_string = vim_strsave(IObuff);
6955 else
6956 {
6957 if (message != NULL && defstr != NULL
6958 && argvars[1].v_type != VAR_UNKNOWN
6959 && argvars[2].v_type != VAR_UNKNOWN)
6960 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006961 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962 else
6963 rettv->vval.v_string = NULL;
6964 }
6965 rettv->v_type = VAR_STRING;
6966 }
6967 else
6968#endif
6969 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6970}
6971
6972/*
6973 * "inputlist()" function
6974 */
6975 static void
6976f_inputlist(typval_T *argvars, typval_T *rettv)
6977{
6978 listitem_T *li;
6979 int selected;
6980 int mouse_used;
6981
6982#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006983 /* While starting up, there is no place to enter text. When running tests
6984 * with --not-a-term we assume feedkeys() will be used. */
6985 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 return;
6987#endif
6988 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006990 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006991 return;
6992 }
6993
6994 msg_start();
6995 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6996 lines_left = Rows; /* avoid more prompt */
6997 msg_scroll = TRUE;
6998 msg_clr_eos();
6999
7000 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7001 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007002 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003 msg_putchar('\n');
7004 }
7005
7006 /* Ask for choice. */
7007 selected = prompt_for_number(&mouse_used);
7008 if (mouse_used)
7009 selected -= lines_left;
7010
7011 rettv->vval.v_number = selected;
7012}
7013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7015
7016/*
7017 * "inputrestore()" function
7018 */
7019 static void
7020f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7021{
7022 if (ga_userinput.ga_len > 0)
7023 {
7024 --ga_userinput.ga_len;
7025 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7026 + ga_userinput.ga_len);
7027 /* default return is zero == OK */
7028 }
7029 else if (p_verbose > 1)
7030 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007031 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 rettv->vval.v_number = 1; /* Failed */
7033 }
7034}
7035
7036/*
7037 * "inputsave()" function
7038 */
7039 static void
7040f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7041{
7042 /* Add an entry to the stack of typeahead storage. */
7043 if (ga_grow(&ga_userinput, 1) == OK)
7044 {
7045 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7046 + ga_userinput.ga_len);
7047 ++ga_userinput.ga_len;
7048 /* default return is zero == OK */
7049 }
7050 else
7051 rettv->vval.v_number = 1; /* Failed */
7052}
7053
7054/*
7055 * "inputsecret()" function
7056 */
7057 static void
7058f_inputsecret(typval_T *argvars, typval_T *rettv)
7059{
7060 ++cmdline_star;
7061 ++inputsecret_flag;
7062 f_input(argvars, rettv);
7063 --cmdline_star;
7064 --inputsecret_flag;
7065}
7066
7067/*
7068 * "insert()" function
7069 */
7070 static void
7071f_insert(typval_T *argvars, typval_T *rettv)
7072{
7073 long before = 0;
7074 listitem_T *item;
7075 list_T *l;
7076 int error = FALSE;
7077
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007078 if (argvars[0].v_type == VAR_BLOB)
7079 {
7080 int val, len;
7081 char_u *p;
7082
7083 len = blob_len(argvars[0].vval.v_blob);
7084 if (argvars[2].v_type != VAR_UNKNOWN)
7085 {
7086 before = (long)tv_get_number_chk(&argvars[2], &error);
7087 if (error)
7088 return; // type error; errmsg already given
7089 if (before < 0 || before > len)
7090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007091 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007092 return;
7093 }
7094 }
7095 val = tv_get_number_chk(&argvars[1], &error);
7096 if (error)
7097 return;
7098 if (val < 0 || val > 255)
7099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007100 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007101 return;
7102 }
7103
7104 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7105 return;
7106 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7107 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7108 *(p + before) = val;
7109 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7110
7111 copy_tv(&argvars[0], rettv);
7112 }
7113 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007114 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007115 else if ((l = argvars[0].vval.v_list) != NULL
7116 && !var_check_lock(l->lv_lock,
7117 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 {
7119 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007120 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121 if (error)
7122 return; /* type error; errmsg already given */
7123
7124 if (before == l->lv_len)
7125 item = NULL;
7126 else
7127 {
7128 item = list_find(l, before);
7129 if (item == NULL)
7130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007131 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 l = NULL;
7133 }
7134 }
7135 if (l != NULL)
7136 {
7137 list_insert_tv(l, &argvars[1], item);
7138 copy_tv(&argvars[0], rettv);
7139 }
7140 }
7141}
7142
7143/*
7144 * "invert(expr)" function
7145 */
7146 static void
7147f_invert(typval_T *argvars, typval_T *rettv)
7148{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007149 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150}
7151
7152/*
7153 * "isdirectory()" function
7154 */
7155 static void
7156f_isdirectory(typval_T *argvars, typval_T *rettv)
7157{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007158 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159}
7160
7161/*
7162 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7163 * or it refers to a List or Dictionary that is locked.
7164 */
7165 static int
7166tv_islocked(typval_T *tv)
7167{
7168 return (tv->v_lock & VAR_LOCKED)
7169 || (tv->v_type == VAR_LIST
7170 && tv->vval.v_list != NULL
7171 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7172 || (tv->v_type == VAR_DICT
7173 && tv->vval.v_dict != NULL
7174 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7175}
7176
7177/*
7178 * "islocked()" function
7179 */
7180 static void
7181f_islocked(typval_T *argvars, typval_T *rettv)
7182{
7183 lval_T lv;
7184 char_u *end;
7185 dictitem_T *di;
7186
7187 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007188 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007189 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 if (end != NULL && lv.ll_name != NULL)
7191 {
7192 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007193 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 else
7195 {
7196 if (lv.ll_tv == NULL)
7197 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007198 di = find_var(lv.ll_name, NULL, TRUE);
7199 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007201 /* Consider a variable locked when:
7202 * 1. the variable itself is locked
7203 * 2. the value of the variable is locked.
7204 * 3. the List or Dict value is locked.
7205 */
7206 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7207 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 }
7209 }
7210 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007211 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007212 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 else if (lv.ll_list != NULL)
7215 /* List item. */
7216 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7217 else
7218 /* Dictionary item. */
7219 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7220 }
7221 }
7222
7223 clear_lval(&lv);
7224}
7225
7226#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7227/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007228 * "isinf()" function
7229 */
7230 static void
7231f_isinf(typval_T *argvars, typval_T *rettv)
7232{
7233 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7234 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7235}
7236
7237/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 * "isnan()" function
7239 */
7240 static void
7241f_isnan(typval_T *argvars, typval_T *rettv)
7242{
7243 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7244 && isnan(argvars[0].vval.v_float);
7245}
7246#endif
7247
7248/*
7249 * "items(dict)" function
7250 */
7251 static void
7252f_items(typval_T *argvars, typval_T *rettv)
7253{
7254 dict_list(argvars, rettv, 2);
7255}
7256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007257/*
7258 * "join()" function
7259 */
7260 static void
7261f_join(typval_T *argvars, typval_T *rettv)
7262{
7263 garray_T ga;
7264 char_u *sep;
7265
7266 if (argvars[0].v_type != VAR_LIST)
7267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007268 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 return;
7270 }
7271 if (argvars[0].vval.v_list == NULL)
7272 return;
7273 if (argvars[1].v_type == VAR_UNKNOWN)
7274 sep = (char_u *)" ";
7275 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007276 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277
7278 rettv->v_type = VAR_STRING;
7279
7280 if (sep != NULL)
7281 {
7282 ga_init2(&ga, (int)sizeof(char), 80);
7283 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7284 ga_append(&ga, NUL);
7285 rettv->vval.v_string = (char_u *)ga.ga_data;
7286 }
7287 else
7288 rettv->vval.v_string = NULL;
7289}
7290
7291/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 * "keys()" function
7293 */
7294 static void
7295f_keys(typval_T *argvars, typval_T *rettv)
7296{
7297 dict_list(argvars, rettv, 0);
7298}
7299
7300/*
7301 * "last_buffer_nr()" function.
7302 */
7303 static void
7304f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7305{
7306 int n = 0;
7307 buf_T *buf;
7308
Bram Moolenaar29323592016-07-24 22:04:11 +02007309 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 if (n < buf->b_fnum)
7311 n = buf->b_fnum;
7312
7313 rettv->vval.v_number = n;
7314}
7315
7316/*
7317 * "len()" function
7318 */
7319 static void
7320f_len(typval_T *argvars, typval_T *rettv)
7321{
7322 switch (argvars[0].v_type)
7323 {
7324 case VAR_STRING:
7325 case VAR_NUMBER:
7326 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007329 case VAR_BLOB:
7330 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7331 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 case VAR_LIST:
7333 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7334 break;
7335 case VAR_DICT:
7336 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7337 break;
7338 case VAR_UNKNOWN:
7339 case VAR_SPECIAL:
7340 case VAR_FLOAT:
7341 case VAR_FUNC:
7342 case VAR_PARTIAL:
7343 case VAR_JOB:
7344 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007345 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 break;
7347 }
7348}
7349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007351libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352{
7353#ifdef FEAT_LIBCALL
7354 char_u *string_in;
7355 char_u **string_result;
7356 int nr_result;
7357#endif
7358
7359 rettv->v_type = type;
7360 if (type != VAR_NUMBER)
7361 rettv->vval.v_string = NULL;
7362
7363 if (check_restricted() || check_secure())
7364 return;
7365
7366#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007367 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7369 {
7370 string_in = NULL;
7371 if (argvars[2].v_type == VAR_STRING)
7372 string_in = argvars[2].vval.v_string;
7373 if (type == VAR_NUMBER)
7374 string_result = NULL;
7375 else
7376 string_result = &rettv->vval.v_string;
7377 if (mch_libcall(argvars[0].vval.v_string,
7378 argvars[1].vval.v_string,
7379 string_in,
7380 argvars[2].vval.v_number,
7381 string_result,
7382 &nr_result) == OK
7383 && type == VAR_NUMBER)
7384 rettv->vval.v_number = nr_result;
7385 }
7386#endif
7387}
7388
7389/*
7390 * "libcall()" function
7391 */
7392 static void
7393f_libcall(typval_T *argvars, typval_T *rettv)
7394{
7395 libcall_common(argvars, rettv, VAR_STRING);
7396}
7397
7398/*
7399 * "libcallnr()" function
7400 */
7401 static void
7402f_libcallnr(typval_T *argvars, typval_T *rettv)
7403{
7404 libcall_common(argvars, rettv, VAR_NUMBER);
7405}
7406
7407/*
7408 * "line(string)" function
7409 */
7410 static void
7411f_line(typval_T *argvars, typval_T *rettv)
7412{
7413 linenr_T lnum = 0;
7414 pos_T *fp;
7415 int fnum;
7416
7417 fp = var2fpos(&argvars[0], TRUE, &fnum);
7418 if (fp != NULL)
7419 lnum = fp->lnum;
7420 rettv->vval.v_number = lnum;
7421}
7422
7423/*
7424 * "line2byte(lnum)" function
7425 */
7426 static void
7427f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7428{
7429#ifndef FEAT_BYTEOFF
7430 rettv->vval.v_number = -1;
7431#else
7432 linenr_T lnum;
7433
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007434 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7436 rettv->vval.v_number = -1;
7437 else
7438 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7439 if (rettv->vval.v_number >= 0)
7440 ++rettv->vval.v_number;
7441#endif
7442}
7443
7444/*
7445 * "lispindent(lnum)" function
7446 */
7447 static void
7448f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7449{
7450#ifdef FEAT_LISP
7451 pos_T pos;
7452 linenr_T lnum;
7453
7454 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007455 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7457 {
7458 curwin->w_cursor.lnum = lnum;
7459 rettv->vval.v_number = get_lisp_indent();
7460 curwin->w_cursor = pos;
7461 }
7462 else
7463#endif
7464 rettv->vval.v_number = -1;
7465}
7466
7467/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007468 * "list2str()" function
7469 */
7470 static void
7471f_list2str(typval_T *argvars, typval_T *rettv)
7472{
7473 list_T *l;
7474 listitem_T *li;
7475 garray_T ga;
7476 int utf8 = FALSE;
7477
7478 rettv->v_type = VAR_STRING;
7479 rettv->vval.v_string = NULL;
7480 if (argvars[0].v_type != VAR_LIST)
7481 {
7482 emsg(_(e_invarg));
7483 return;
7484 }
7485
7486 l = argvars[0].vval.v_list;
7487 if (l == NULL)
7488 return; // empty list results in empty string
7489
7490 if (argvars[1].v_type != VAR_UNKNOWN)
7491 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7492
7493 ga_init2(&ga, 1, 80);
7494 if (has_mbyte || utf8)
7495 {
7496 char_u buf[MB_MAXBYTES + 1];
7497 int (*char2bytes)(int, char_u *);
7498
7499 if (utf8 || enc_utf8)
7500 char2bytes = utf_char2bytes;
7501 else
7502 char2bytes = mb_char2bytes;
7503
7504 for (li = l->lv_first; li != NULL; li = li->li_next)
7505 {
7506 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7507 ga_concat(&ga, buf);
7508 }
7509 ga_append(&ga, NUL);
7510 }
7511 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7512 {
7513 for (li = l->lv_first; li != NULL; li = li->li_next)
7514 ga_append(&ga, tv_get_number(&li->li_tv));
7515 ga_append(&ga, NUL);
7516 }
7517
7518 rettv->v_type = VAR_STRING;
7519 rettv->vval.v_string = ga.ga_data;
7520}
7521
7522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 * "localtime()" function
7524 */
7525 static void
7526f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7527{
7528 rettv->vval.v_number = (varnumber_T)time(NULL);
7529}
7530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 static void
7532get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7533{
7534 char_u *keys;
7535 char_u *which;
7536 char_u buf[NUMBUFLEN];
7537 char_u *keys_buf = NULL;
7538 char_u *rhs;
7539 int mode;
7540 int abbr = FALSE;
7541 int get_dict = FALSE;
7542 mapblock_T *mp;
7543 int buffer_local;
7544
7545 /* return empty string for failure */
7546 rettv->v_type = VAR_STRING;
7547 rettv->vval.v_string = NULL;
7548
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007549 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550 if (*keys == NUL)
7551 return;
7552
7553 if (argvars[1].v_type != VAR_UNKNOWN)
7554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 if (argvars[2].v_type != VAR_UNKNOWN)
7557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007558 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007560 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 }
7562 }
7563 else
7564 which = (char_u *)"";
7565 if (which == NULL)
7566 return;
7567
7568 mode = get_map_mode(&which, 0);
7569
7570 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7571 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7572 vim_free(keys_buf);
7573
7574 if (!get_dict)
7575 {
7576 /* Return a string. */
7577 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007578 {
7579 if (*rhs == NUL)
7580 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7581 else
7582 rettv->vval.v_string = str2special_save(rhs, FALSE);
7583 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584
7585 }
7586 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7587 {
7588 /* Return a dictionary. */
7589 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7590 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7591 dict_T *dict = rettv->vval.v_dict;
7592
Bram Moolenaare0be1672018-07-08 16:50:37 +02007593 dict_add_string(dict, "lhs", lhs);
7594 dict_add_string(dict, "rhs", mp->m_orig_str);
7595 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7596 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7597 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007598 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7599 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007600 dict_add_number(dict, "buffer", (long)buffer_local);
7601 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7602 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603
7604 vim_free(lhs);
7605 vim_free(mapmode);
7606 }
7607}
7608
7609#ifdef FEAT_FLOAT
7610/*
7611 * "log()" function
7612 */
7613 static void
7614f_log(typval_T *argvars, typval_T *rettv)
7615{
7616 float_T f = 0.0;
7617
7618 rettv->v_type = VAR_FLOAT;
7619 if (get_float_arg(argvars, &f) == OK)
7620 rettv->vval.v_float = log(f);
7621 else
7622 rettv->vval.v_float = 0.0;
7623}
7624
7625/*
7626 * "log10()" function
7627 */
7628 static void
7629f_log10(typval_T *argvars, typval_T *rettv)
7630{
7631 float_T f = 0.0;
7632
7633 rettv->v_type = VAR_FLOAT;
7634 if (get_float_arg(argvars, &f) == OK)
7635 rettv->vval.v_float = log10(f);
7636 else
7637 rettv->vval.v_float = 0.0;
7638}
7639#endif
7640
7641#ifdef FEAT_LUA
7642/*
7643 * "luaeval()" function
7644 */
7645 static void
7646f_luaeval(typval_T *argvars, typval_T *rettv)
7647{
7648 char_u *str;
7649 char_u buf[NUMBUFLEN];
7650
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007651 if (check_restricted() || check_secure())
7652 return;
7653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007654 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 do_luaeval(str, argvars + 1, rettv);
7656}
7657#endif
7658
7659/*
7660 * "map()" function
7661 */
7662 static void
7663f_map(typval_T *argvars, typval_T *rettv)
7664{
7665 filter_map(argvars, rettv, TRUE);
7666}
7667
7668/*
7669 * "maparg()" function
7670 */
7671 static void
7672f_maparg(typval_T *argvars, typval_T *rettv)
7673{
7674 get_maparg(argvars, rettv, TRUE);
7675}
7676
7677/*
7678 * "mapcheck()" function
7679 */
7680 static void
7681f_mapcheck(typval_T *argvars, typval_T *rettv)
7682{
7683 get_maparg(argvars, rettv, FALSE);
7684}
7685
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007686typedef enum
7687{
7688 MATCH_END, /* matchend() */
7689 MATCH_MATCH, /* match() */
7690 MATCH_STR, /* matchstr() */
7691 MATCH_LIST, /* matchlist() */
7692 MATCH_POS /* matchstrpos() */
7693} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694
7695 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007696find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697{
7698 char_u *str = NULL;
7699 long len = 0;
7700 char_u *expr = NULL;
7701 char_u *pat;
7702 regmatch_T regmatch;
7703 char_u patbuf[NUMBUFLEN];
7704 char_u strbuf[NUMBUFLEN];
7705 char_u *save_cpo;
7706 long start = 0;
7707 long nth = 1;
7708 colnr_T startcol = 0;
7709 int match = 0;
7710 list_T *l = NULL;
7711 listitem_T *li = NULL;
7712 long idx = 0;
7713 char_u *tofree = NULL;
7714
7715 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7716 save_cpo = p_cpo;
7717 p_cpo = (char_u *)"";
7718
7719 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007720 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007722 /* type MATCH_LIST: return empty list when there are no matches.
7723 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724 if (rettv_list_alloc(rettv) == FAIL)
7725 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007726 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727 && (list_append_string(rettv->vval.v_list,
7728 (char_u *)"", 0) == FAIL
7729 || list_append_number(rettv->vval.v_list,
7730 (varnumber_T)-1) == FAIL
7731 || list_append_number(rettv->vval.v_list,
7732 (varnumber_T)-1) == FAIL
7733 || list_append_number(rettv->vval.v_list,
7734 (varnumber_T)-1) == FAIL))
7735 {
7736 list_free(rettv->vval.v_list);
7737 rettv->vval.v_list = NULL;
7738 goto theend;
7739 }
7740 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007741 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 {
7743 rettv->v_type = VAR_STRING;
7744 rettv->vval.v_string = NULL;
7745 }
7746
7747 if (argvars[0].v_type == VAR_LIST)
7748 {
7749 if ((l = argvars[0].vval.v_list) == NULL)
7750 goto theend;
7751 li = l->lv_first;
7752 }
7753 else
7754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007755 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 len = (long)STRLEN(str);
7757 }
7758
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007759 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 if (pat == NULL)
7761 goto theend;
7762
7763 if (argvars[2].v_type != VAR_UNKNOWN)
7764 {
7765 int error = FALSE;
7766
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007767 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007768 if (error)
7769 goto theend;
7770 if (l != NULL)
7771 {
7772 li = list_find(l, start);
7773 if (li == NULL)
7774 goto theend;
7775 idx = l->lv_idx; /* use the cached index */
7776 }
7777 else
7778 {
7779 if (start < 0)
7780 start = 0;
7781 if (start > len)
7782 goto theend;
7783 /* When "count" argument is there ignore matches before "start",
7784 * otherwise skip part of the string. Differs when pattern is "^"
7785 * or "\<". */
7786 if (argvars[3].v_type != VAR_UNKNOWN)
7787 startcol = start;
7788 else
7789 {
7790 str += start;
7791 len -= start;
7792 }
7793 }
7794
7795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007796 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 if (error)
7798 goto theend;
7799 }
7800
7801 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7802 if (regmatch.regprog != NULL)
7803 {
7804 regmatch.rm_ic = p_ic;
7805
7806 for (;;)
7807 {
7808 if (l != NULL)
7809 {
7810 if (li == NULL)
7811 {
7812 match = FALSE;
7813 break;
7814 }
7815 vim_free(tofree);
7816 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7817 if (str == NULL)
7818 break;
7819 }
7820
7821 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7822
7823 if (match && --nth <= 0)
7824 break;
7825 if (l == NULL && !match)
7826 break;
7827
7828 /* Advance to just after the match. */
7829 if (l != NULL)
7830 {
7831 li = li->li_next;
7832 ++idx;
7833 }
7834 else
7835 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 startcol = (colnr_T)(regmatch.startp[0]
7837 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 if (startcol > (colnr_T)len
7839 || str + startcol <= regmatch.startp[0])
7840 {
7841 match = FALSE;
7842 break;
7843 }
7844 }
7845 }
7846
7847 if (match)
7848 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007849 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 {
7851 listitem_T *li1 = rettv->vval.v_list->lv_first;
7852 listitem_T *li2 = li1->li_next;
7853 listitem_T *li3 = li2->li_next;
7854 listitem_T *li4 = li3->li_next;
7855
7856 vim_free(li1->li_tv.vval.v_string);
7857 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7858 (int)(regmatch.endp[0] - regmatch.startp[0]));
7859 li3->li_tv.vval.v_number =
7860 (varnumber_T)(regmatch.startp[0] - expr);
7861 li4->li_tv.vval.v_number =
7862 (varnumber_T)(regmatch.endp[0] - expr);
7863 if (l != NULL)
7864 li2->li_tv.vval.v_number = (varnumber_T)idx;
7865 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007866 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 {
7868 int i;
7869
7870 /* return list with matched string and submatches */
7871 for (i = 0; i < NSUBEXP; ++i)
7872 {
7873 if (regmatch.endp[i] == NULL)
7874 {
7875 if (list_append_string(rettv->vval.v_list,
7876 (char_u *)"", 0) == FAIL)
7877 break;
7878 }
7879 else if (list_append_string(rettv->vval.v_list,
7880 regmatch.startp[i],
7881 (int)(regmatch.endp[i] - regmatch.startp[i]))
7882 == FAIL)
7883 break;
7884 }
7885 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007886 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 {
7888 /* return matched string */
7889 if (l != NULL)
7890 copy_tv(&li->li_tv, rettv);
7891 else
7892 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7893 (int)(regmatch.endp[0] - regmatch.startp[0]));
7894 }
7895 else if (l != NULL)
7896 rettv->vval.v_number = idx;
7897 else
7898 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007899 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 rettv->vval.v_number =
7901 (varnumber_T)(regmatch.startp[0] - str);
7902 else
7903 rettv->vval.v_number =
7904 (varnumber_T)(regmatch.endp[0] - str);
7905 rettv->vval.v_number += (varnumber_T)(str - expr);
7906 }
7907 }
7908 vim_regfree(regmatch.regprog);
7909 }
7910
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007911theend:
7912 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 /* matchstrpos() without a list: drop the second item. */
7914 listitem_remove(rettv->vval.v_list,
7915 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 vim_free(tofree);
7917 p_cpo = save_cpo;
7918}
7919
7920/*
7921 * "match()" function
7922 */
7923 static void
7924f_match(typval_T *argvars, typval_T *rettv)
7925{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007926 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927}
7928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929/*
7930 * "matchend()" function
7931 */
7932 static void
7933f_matchend(typval_T *argvars, typval_T *rettv)
7934{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007935 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936}
7937
7938/*
7939 * "matchlist()" function
7940 */
7941 static void
7942f_matchlist(typval_T *argvars, typval_T *rettv)
7943{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007944 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945}
7946
7947/*
7948 * "matchstr()" function
7949 */
7950 static void
7951f_matchstr(typval_T *argvars, typval_T *rettv)
7952{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007953 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954}
7955
7956/*
7957 * "matchstrpos()" function
7958 */
7959 static void
7960f_matchstrpos(typval_T *argvars, typval_T *rettv)
7961{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007962 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963}
7964
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 static void
7966max_min(typval_T *argvars, typval_T *rettv, int domax)
7967{
7968 varnumber_T n = 0;
7969 varnumber_T i;
7970 int error = FALSE;
7971
7972 if (argvars[0].v_type == VAR_LIST)
7973 {
7974 list_T *l;
7975 listitem_T *li;
7976
7977 l = argvars[0].vval.v_list;
7978 if (l != NULL)
7979 {
7980 li = l->lv_first;
7981 if (li != NULL)
7982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007983 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 for (;;)
7985 {
7986 li = li->li_next;
7987 if (li == NULL)
7988 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007989 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 if (domax ? i > n : i < n)
7991 n = i;
7992 }
7993 }
7994 }
7995 }
7996 else if (argvars[0].v_type == VAR_DICT)
7997 {
7998 dict_T *d;
7999 int first = TRUE;
8000 hashitem_T *hi;
8001 int todo;
8002
8003 d = argvars[0].vval.v_dict;
8004 if (d != NULL)
8005 {
8006 todo = (int)d->dv_hashtab.ht_used;
8007 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8008 {
8009 if (!HASHITEM_EMPTY(hi))
8010 {
8011 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008012 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 if (first)
8014 {
8015 n = i;
8016 first = FALSE;
8017 }
8018 else if (domax ? i > n : i < n)
8019 n = i;
8020 }
8021 }
8022 }
8023 }
8024 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008025 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 rettv->vval.v_number = error ? 0 : n;
8027}
8028
8029/*
8030 * "max()" function
8031 */
8032 static void
8033f_max(typval_T *argvars, typval_T *rettv)
8034{
8035 max_min(argvars, rettv, TRUE);
8036}
8037
8038/*
8039 * "min()" function
8040 */
8041 static void
8042f_min(typval_T *argvars, typval_T *rettv)
8043{
8044 max_min(argvars, rettv, FALSE);
8045}
8046
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047/*
8048 * Create the directory in which "dir" is located, and higher levels when
8049 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008050 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 */
8052 static int
8053mkdir_recurse(char_u *dir, int prot)
8054{
8055 char_u *p;
8056 char_u *updir;
8057 int r = FAIL;
8058
8059 /* Get end of directory name in "dir".
8060 * We're done when it's "/" or "c:/". */
8061 p = gettail_sep(dir);
8062 if (p <= get_past_head(dir))
8063 return OK;
8064
8065 /* If the directory exists we're done. Otherwise: create it.*/
8066 updir = vim_strnsave(dir, (int)(p - dir));
8067 if (updir == NULL)
8068 return FAIL;
8069 if (mch_isdir(updir))
8070 r = OK;
8071 else if (mkdir_recurse(updir, prot) == OK)
8072 r = vim_mkdir_emsg(updir, prot);
8073 vim_free(updir);
8074 return r;
8075}
8076
8077#ifdef vim_mkdir
8078/*
8079 * "mkdir()" function
8080 */
8081 static void
8082f_mkdir(typval_T *argvars, typval_T *rettv)
8083{
8084 char_u *dir;
8085 char_u buf[NUMBUFLEN];
8086 int prot = 0755;
8087
8088 rettv->vval.v_number = FAIL;
8089 if (check_restricted() || check_secure())
8090 return;
8091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008092 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008094 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008096 if (*gettail(dir) == NUL)
8097 /* remove trailing slashes */
8098 *gettail_sep(dir) = NUL;
8099
8100 if (argvars[1].v_type != VAR_UNKNOWN)
8101 {
8102 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008104 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008105 if (prot == -1)
8106 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008109 {
8110 if (mch_isdir(dir))
8111 {
8112 /* With the "p" flag it's OK if the dir already exists. */
8113 rettv->vval.v_number = OK;
8114 return;
8115 }
8116 mkdir_recurse(dir, prot);
8117 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008119 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120}
8121#endif
8122
8123/*
8124 * "mode()" function
8125 */
8126 static void
8127f_mode(typval_T *argvars, typval_T *rettv)
8128{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008129 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130
Bram Moolenaar612cc382018-07-29 15:34:26 +02008131 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132
8133 if (time_for_testing == 93784)
8134 {
8135 /* Testing the two-character code. */
8136 buf[0] = 'x';
8137 buf[1] = '!';
8138 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008139#ifdef FEAT_TERMINAL
8140 else if (term_use_loop())
8141 buf[0] = 't';
8142#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143 else if (VIsual_active)
8144 {
8145 if (VIsual_select)
8146 buf[0] = VIsual_mode + 's' - 'v';
8147 else
8148 buf[0] = VIsual_mode;
8149 }
8150 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8151 || State == CONFIRM)
8152 {
8153 buf[0] = 'r';
8154 if (State == ASKMORE)
8155 buf[1] = 'm';
8156 else if (State == CONFIRM)
8157 buf[1] = '?';
8158 }
8159 else if (State == EXTERNCMD)
8160 buf[0] = '!';
8161 else if (State & INSERT)
8162 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 if (State & VREPLACE_FLAG)
8164 {
8165 buf[0] = 'R';
8166 buf[1] = 'v';
8167 }
8168 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008169 {
8170 if (State & REPLACE_FLAG)
8171 buf[0] = 'R';
8172 else
8173 buf[0] = 'i';
8174#ifdef FEAT_INS_EXPAND
8175 if (ins_compl_active())
8176 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008177 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008178 buf[1] = 'x';
8179#endif
8180 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008182 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 {
8184 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008185 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008187 else if (exmode_active == EXMODE_NORMAL)
8188 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 }
8190 else
8191 {
8192 buf[0] = 'n';
8193 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008194 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008196 // to be able to detect force-linewise/blockwise/characterwise operations
8197 buf[2] = motion_force;
8198 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008199 else if (restart_edit == 'I' || restart_edit == 'R'
8200 || restart_edit == 'V')
8201 {
8202 buf[1] = 'i';
8203 buf[2] = restart_edit;
8204 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 }
8206
8207 /* Clear out the minor mode when the argument is not a non-zero number or
8208 * non-empty string. */
8209 if (!non_zero_arg(&argvars[0]))
8210 buf[1] = NUL;
8211
8212 rettv->vval.v_string = vim_strsave(buf);
8213 rettv->v_type = VAR_STRING;
8214}
8215
8216#if defined(FEAT_MZSCHEME) || defined(PROTO)
8217/*
8218 * "mzeval()" function
8219 */
8220 static void
8221f_mzeval(typval_T *argvars, typval_T *rettv)
8222{
8223 char_u *str;
8224 char_u buf[NUMBUFLEN];
8225
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008226 if (check_restricted() || check_secure())
8227 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008228 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229 do_mzeval(str, rettv);
8230}
8231
8232 void
8233mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8234{
8235 typval_T argvars[3];
8236
8237 argvars[0].v_type = VAR_STRING;
8238 argvars[0].vval.v_string = name;
8239 copy_tv(args, &argvars[1]);
8240 argvars[2].v_type = VAR_UNKNOWN;
8241 f_call(argvars, rettv);
8242 clear_tv(&argvars[1]);
8243}
8244#endif
8245
8246/*
8247 * "nextnonblank()" function
8248 */
8249 static void
8250f_nextnonblank(typval_T *argvars, typval_T *rettv)
8251{
8252 linenr_T lnum;
8253
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008254 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255 {
8256 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8257 {
8258 lnum = 0;
8259 break;
8260 }
8261 if (*skipwhite(ml_get(lnum)) != NUL)
8262 break;
8263 }
8264 rettv->vval.v_number = lnum;
8265}
8266
8267/*
8268 * "nr2char()" function
8269 */
8270 static void
8271f_nr2char(typval_T *argvars, typval_T *rettv)
8272{
8273 char_u buf[NUMBUFLEN];
8274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 if (has_mbyte)
8276 {
8277 int utf8 = 0;
8278
8279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008280 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008282 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008284 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 }
8286 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 buf[1] = NUL;
8290 }
8291 rettv->v_type = VAR_STRING;
8292 rettv->vval.v_string = vim_strsave(buf);
8293}
8294
8295/*
8296 * "or(expr, expr)" function
8297 */
8298 static void
8299f_or(typval_T *argvars, typval_T *rettv)
8300{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008301 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8302 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008303}
8304
8305/*
8306 * "pathshorten()" function
8307 */
8308 static void
8309f_pathshorten(typval_T *argvars, typval_T *rettv)
8310{
8311 char_u *p;
8312
8313 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 if (p == NULL)
8316 rettv->vval.v_string = NULL;
8317 else
8318 {
8319 p = vim_strsave(p);
8320 rettv->vval.v_string = p;
8321 if (p != NULL)
8322 shorten_dir(p);
8323 }
8324}
8325
8326#ifdef FEAT_PERL
8327/*
8328 * "perleval()" function
8329 */
8330 static void
8331f_perleval(typval_T *argvars, typval_T *rettv)
8332{
8333 char_u *str;
8334 char_u buf[NUMBUFLEN];
8335
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008336 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 do_perleval(str, rettv);
8338}
8339#endif
8340
8341#ifdef FEAT_FLOAT
8342/*
8343 * "pow()" function
8344 */
8345 static void
8346f_pow(typval_T *argvars, typval_T *rettv)
8347{
8348 float_T fx = 0.0, fy = 0.0;
8349
8350 rettv->v_type = VAR_FLOAT;
8351 if (get_float_arg(argvars, &fx) == OK
8352 && get_float_arg(&argvars[1], &fy) == OK)
8353 rettv->vval.v_float = pow(fx, fy);
8354 else
8355 rettv->vval.v_float = 0.0;
8356}
8357#endif
8358
8359/*
8360 * "prevnonblank()" function
8361 */
8362 static void
8363f_prevnonblank(typval_T *argvars, typval_T *rettv)
8364{
8365 linenr_T lnum;
8366
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008367 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8369 lnum = 0;
8370 else
8371 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8372 --lnum;
8373 rettv->vval.v_number = lnum;
8374}
8375
8376/* This dummy va_list is here because:
8377 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8378 * - locally in the function results in a "used before set" warning
8379 * - using va_start() to initialize it gives "function with fixed args" error */
8380static va_list ap;
8381
8382/*
8383 * "printf()" function
8384 */
8385 static void
8386f_printf(typval_T *argvars, typval_T *rettv)
8387{
8388 char_u buf[NUMBUFLEN];
8389 int len;
8390 char_u *s;
8391 int saved_did_emsg = did_emsg;
8392 char *fmt;
8393
8394 rettv->v_type = VAR_STRING;
8395 rettv->vval.v_string = NULL;
8396
8397 /* Get the required length, allocate the buffer and do it for real. */
8398 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008400 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 if (!did_emsg)
8402 {
8403 s = alloc(len + 1);
8404 if (s != NULL)
8405 {
8406 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008407 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8408 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 }
8410 }
8411 did_emsg |= saved_did_emsg;
8412}
8413
8414/*
8415 * "pumvisible()" function
8416 */
8417 static void
8418f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8419{
8420#ifdef FEAT_INS_EXPAND
8421 if (pum_visible())
8422 rettv->vval.v_number = 1;
8423#endif
8424}
8425
8426#ifdef FEAT_PYTHON3
8427/*
8428 * "py3eval()" function
8429 */
8430 static void
8431f_py3eval(typval_T *argvars, typval_T *rettv)
8432{
8433 char_u *str;
8434 char_u buf[NUMBUFLEN];
8435
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008436 if (check_restricted() || check_secure())
8437 return;
8438
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008439 if (p_pyx == 0)
8440 p_pyx = 3;
8441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008442 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 do_py3eval(str, rettv);
8444}
8445#endif
8446
8447#ifdef FEAT_PYTHON
8448/*
8449 * "pyeval()" function
8450 */
8451 static void
8452f_pyeval(typval_T *argvars, typval_T *rettv)
8453{
8454 char_u *str;
8455 char_u buf[NUMBUFLEN];
8456
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008457 if (check_restricted() || check_secure())
8458 return;
8459
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008460 if (p_pyx == 0)
8461 p_pyx = 2;
8462
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008463 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 do_pyeval(str, rettv);
8465}
8466#endif
8467
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008468#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8469/*
8470 * "pyxeval()" function
8471 */
8472 static void
8473f_pyxeval(typval_T *argvars, typval_T *rettv)
8474{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008475 if (check_restricted() || check_secure())
8476 return;
8477
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008478# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8479 init_pyxversion();
8480 if (p_pyx == 2)
8481 f_pyeval(argvars, rettv);
8482 else
8483 f_py3eval(argvars, rettv);
8484# elif defined(FEAT_PYTHON)
8485 f_pyeval(argvars, rettv);
8486# elif defined(FEAT_PYTHON3)
8487 f_py3eval(argvars, rettv);
8488# endif
8489}
8490#endif
8491
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492/*
8493 * "range()" function
8494 */
8495 static void
8496f_range(typval_T *argvars, typval_T *rettv)
8497{
8498 varnumber_T start;
8499 varnumber_T end;
8500 varnumber_T stride = 1;
8501 varnumber_T i;
8502 int error = FALSE;
8503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008504 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 if (argvars[1].v_type == VAR_UNKNOWN)
8506 {
8507 end = start - 1;
8508 start = 0;
8509 }
8510 else
8511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008512 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008514 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 }
8516
8517 if (error)
8518 return; /* type error; errmsg already given */
8519 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008520 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008522 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 else
8524 {
8525 if (rettv_list_alloc(rettv) == OK)
8526 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8527 if (list_append_number(rettv->vval.v_list,
8528 (varnumber_T)i) == FAIL)
8529 break;
8530 }
8531}
8532
8533/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008534 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008535 */
8536 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008537readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008538{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008539 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008540 typval_T save_val;
8541 typval_T rettv;
8542 typval_T argv[2];
8543 int retval = 0;
8544 int error = FALSE;
8545
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008546 if (expr->v_type == VAR_UNKNOWN)
8547 return 1;
8548
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008549 prepare_vimvar(VV_VAL, &save_val);
8550 set_vim_var_string(VV_VAL, name, -1);
8551 argv[0].v_type = VAR_STRING;
8552 argv[0].vval.v_string = name;
8553
8554 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8555 goto theend;
8556
8557 retval = tv_get_number_chk(&rettv, &error);
8558 if (error)
8559 retval = -1;
8560 clear_tv(&rettv);
8561
8562theend:
8563 set_vim_var_string(VV_VAL, NULL, 0);
8564 restore_vimvar(VV_VAL, &save_val);
8565 return retval;
8566}
8567
8568/*
8569 * "readdir()" function
8570 */
8571 static void
8572f_readdir(typval_T *argvars, typval_T *rettv)
8573{
8574 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008575 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008576 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008577 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008578 garray_T ga;
8579 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008580
8581 if (rettv_list_alloc(rettv) == FAIL)
8582 return;
8583 path = tv_get_string(&argvars[0]);
8584 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008585
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008586 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8587 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008588 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008589 for (i = 0; i < ga.ga_len; i++)
8590 {
8591 p = ((char_u **)ga.ga_data)[i];
8592 list_append_string(rettv->vval.v_list, p, -1);
8593 }
8594 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008595 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008596}
8597
8598/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 * "readfile()" function
8600 */
8601 static void
8602f_readfile(typval_T *argvars, typval_T *rettv)
8603{
8604 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008605 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 int failed = FALSE;
8607 char_u *fname;
8608 FILE *fd;
8609 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8610 int io_size = sizeof(buf);
8611 int readlen; /* size of last fread() */
8612 char_u *prev = NULL; /* previously read bytes, if any */
8613 long prevlen = 0; /* length of data in prev */
8614 long prevsize = 0; /* size of prev buffer */
8615 long maxline = MAXLNUM;
8616 long cnt = 0;
8617 char_u *p; /* position in buf */
8618 char_u *start; /* start of current line */
8619
8620 if (argvars[1].v_type != VAR_UNKNOWN)
8621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008622 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008624 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8625 blob = TRUE;
8626
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008628 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 }
8630
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008631 if (blob)
8632 {
8633 if (rettv_blob_alloc(rettv) == FAIL)
8634 return;
8635 }
8636 else
8637 {
8638 if (rettv_list_alloc(rettv) == FAIL)
8639 return;
8640 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641
8642 /* Always open the file in binary mode, library functions have a mind of
8643 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008644 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008647 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 return;
8649 }
8650
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008651 if (blob)
8652 {
8653 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008655 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008656 blob_free(rettv->vval.v_blob);
8657 }
8658 fclose(fd);
8659 return;
8660 }
8661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 while (cnt < maxline || maxline < 0)
8663 {
8664 readlen = (int)fread(buf, 1, io_size, fd);
8665
8666 /* This for loop processes what was read, but is also entered at end
8667 * of file so that either:
8668 * - an incomplete line gets written
8669 * - a "binary" file gets an empty line at the end if it ends in a
8670 * newline. */
8671 for (p = buf, start = buf;
8672 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8673 ++p)
8674 {
8675 if (*p == '\n' || readlen <= 0)
8676 {
8677 listitem_T *li;
8678 char_u *s = NULL;
8679 long_u len = p - start;
8680
8681 /* Finished a line. Remove CRs before NL. */
8682 if (readlen > 0 && !binary)
8683 {
8684 while (len > 0 && start[len - 1] == '\r')
8685 --len;
8686 /* removal may cross back to the "prev" string */
8687 if (len == 0)
8688 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8689 --prevlen;
8690 }
8691 if (prevlen == 0)
8692 s = vim_strnsave(start, (int)len);
8693 else
8694 {
8695 /* Change "prev" buffer to be the right size. This way
8696 * the bytes are only copied once, and very long lines are
8697 * allocated only once. */
8698 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8699 {
8700 mch_memmove(s + prevlen, start, len);
8701 s[prevlen + len] = NUL;
8702 prev = NULL; /* the list will own the string */
8703 prevlen = prevsize = 0;
8704 }
8705 }
8706 if (s == NULL)
8707 {
8708 do_outofmem_msg((long_u) prevlen + len + 1);
8709 failed = TRUE;
8710 break;
8711 }
8712
8713 if ((li = listitem_alloc()) == NULL)
8714 {
8715 vim_free(s);
8716 failed = TRUE;
8717 break;
8718 }
8719 li->li_tv.v_type = VAR_STRING;
8720 li->li_tv.v_lock = 0;
8721 li->li_tv.vval.v_string = s;
8722 list_append(rettv->vval.v_list, li);
8723
8724 start = p + 1; /* step over newline */
8725 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8726 break;
8727 }
8728 else if (*p == NUL)
8729 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8731 * when finding the BF and check the previous two bytes. */
8732 else if (*p == 0xbf && enc_utf8 && !binary)
8733 {
8734 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8735 * + 1, these may be in the "prev" string. */
8736 char_u back1 = p >= buf + 1 ? p[-1]
8737 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8738 char_u back2 = p >= buf + 2 ? p[-2]
8739 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8740 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8741
8742 if (back2 == 0xef && back1 == 0xbb)
8743 {
8744 char_u *dest = p - 2;
8745
8746 /* Usually a BOM is at the beginning of a file, and so at
8747 * the beginning of a line; then we can just step over it.
8748 */
8749 if (start == dest)
8750 start = p + 1;
8751 else
8752 {
8753 /* have to shuffle buf to close gap */
8754 int adjust_prevlen = 0;
8755
8756 if (dest < buf)
8757 {
8758 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8759 dest = buf;
8760 }
8761 if (readlen > p - buf + 1)
8762 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8763 readlen -= 3 - adjust_prevlen;
8764 prevlen -= adjust_prevlen;
8765 p = dest - 1;
8766 }
8767 }
8768 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769 } /* for */
8770
8771 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8772 break;
8773 if (start < p)
8774 {
8775 /* There's part of a line in buf, store it in "prev". */
8776 if (p - start + prevlen >= prevsize)
8777 {
8778 /* need bigger "prev" buffer */
8779 char_u *newprev;
8780
8781 /* A common use case is ordinary text files and "prev" gets a
8782 * fragment of a line, so the first allocation is made
8783 * small, to avoid repeatedly 'allocing' large and
8784 * 'reallocing' small. */
8785 if (prevsize == 0)
8786 prevsize = (long)(p - start);
8787 else
8788 {
8789 long grow50pc = (prevsize * 3) / 2;
8790 long growmin = (long)((p - start) * 2 + prevlen);
8791 prevsize = grow50pc > growmin ? grow50pc : growmin;
8792 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008793 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 if (newprev == NULL)
8795 {
8796 do_outofmem_msg((long_u)prevsize);
8797 failed = TRUE;
8798 break;
8799 }
8800 prev = newprev;
8801 }
8802 /* Add the line part to end of "prev". */
8803 mch_memmove(prev + prevlen, start, p - start);
8804 prevlen += (long)(p - start);
8805 }
8806 } /* while */
8807
8808 /*
8809 * For a negative line count use only the lines at the end of the file,
8810 * free the rest.
8811 */
8812 if (!failed && maxline < 0)
8813 while (cnt > -maxline)
8814 {
8815 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8816 --cnt;
8817 }
8818
8819 if (failed)
8820 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008821 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008823 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 }
8825
8826 vim_free(prev);
8827 fclose(fd);
8828}
8829
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008830 static void
8831return_register(int regname, typval_T *rettv)
8832{
8833 char_u buf[2] = {0, 0};
8834
8835 buf[0] = (char_u)regname;
8836 rettv->v_type = VAR_STRING;
8837 rettv->vval.v_string = vim_strsave(buf);
8838}
8839
8840/*
8841 * "reg_executing()" function
8842 */
8843 static void
8844f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8845{
8846 return_register(reg_executing, rettv);
8847}
8848
8849/*
8850 * "reg_recording()" function
8851 */
8852 static void
8853f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8854{
8855 return_register(reg_recording, rettv);
8856}
8857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859/*
8860 * Convert a List to proftime_T.
8861 * Return FAIL when there is something wrong.
8862 */
8863 static int
8864list2proftime(typval_T *arg, proftime_T *tm)
8865{
8866 long n1, n2;
8867 int error = FALSE;
8868
8869 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8870 || arg->vval.v_list->lv_len != 2)
8871 return FAIL;
8872 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8873 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008874# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 tm->HighPart = n1;
8876 tm->LowPart = n2;
8877# else
8878 tm->tv_sec = n1;
8879 tm->tv_usec = n2;
8880# endif
8881 return error ? FAIL : OK;
8882}
8883#endif /* FEAT_RELTIME */
8884
8885/*
8886 * "reltime()" function
8887 */
8888 static void
8889f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8890{
8891#ifdef FEAT_RELTIME
8892 proftime_T res;
8893 proftime_T start;
8894
8895 if (argvars[0].v_type == VAR_UNKNOWN)
8896 {
8897 /* No arguments: get current time. */
8898 profile_start(&res);
8899 }
8900 else if (argvars[1].v_type == VAR_UNKNOWN)
8901 {
8902 if (list2proftime(&argvars[0], &res) == FAIL)
8903 return;
8904 profile_end(&res);
8905 }
8906 else
8907 {
8908 /* Two arguments: compute the difference. */
8909 if (list2proftime(&argvars[0], &start) == FAIL
8910 || list2proftime(&argvars[1], &res) == FAIL)
8911 return;
8912 profile_sub(&res, &start);
8913 }
8914
8915 if (rettv_list_alloc(rettv) == OK)
8916 {
8917 long n1, n2;
8918
Bram Moolenaar4f974752019-02-17 17:44:42 +01008919# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 n1 = res.HighPart;
8921 n2 = res.LowPart;
8922# else
8923 n1 = res.tv_sec;
8924 n2 = res.tv_usec;
8925# endif
8926 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8927 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8928 }
8929#endif
8930}
8931
8932#ifdef FEAT_FLOAT
8933/*
8934 * "reltimefloat()" function
8935 */
8936 static void
8937f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8938{
8939# ifdef FEAT_RELTIME
8940 proftime_T tm;
8941# endif
8942
8943 rettv->v_type = VAR_FLOAT;
8944 rettv->vval.v_float = 0;
8945# ifdef FEAT_RELTIME
8946 if (list2proftime(&argvars[0], &tm) == OK)
8947 rettv->vval.v_float = profile_float(&tm);
8948# endif
8949}
8950#endif
8951
8952/*
8953 * "reltimestr()" function
8954 */
8955 static void
8956f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8957{
8958#ifdef FEAT_RELTIME
8959 proftime_T tm;
8960#endif
8961
8962 rettv->v_type = VAR_STRING;
8963 rettv->vval.v_string = NULL;
8964#ifdef FEAT_RELTIME
8965 if (list2proftime(&argvars[0], &tm) == OK)
8966 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8967#endif
8968}
8969
8970#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 static void
8972make_connection(void)
8973{
8974 if (X_DISPLAY == NULL
8975# ifdef FEAT_GUI
8976 && !gui.in_use
8977# endif
8978 )
8979 {
8980 x_force_connect = TRUE;
8981 setup_term_clip();
8982 x_force_connect = FALSE;
8983 }
8984}
8985
8986 static int
8987check_connection(void)
8988{
8989 make_connection();
8990 if (X_DISPLAY == NULL)
8991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008992 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 return FAIL;
8994 }
8995 return OK;
8996}
8997#endif
8998
8999#ifdef FEAT_CLIENTSERVER
9000 static void
9001remote_common(typval_T *argvars, typval_T *rettv, int expr)
9002{
9003 char_u *server_name;
9004 char_u *keys;
9005 char_u *r = NULL;
9006 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009007 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009008# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 HWND w;
9010# else
9011 Window w;
9012# endif
9013
9014 if (check_restricted() || check_secure())
9015 return;
9016
9017# ifdef FEAT_X11
9018 if (check_connection() == FAIL)
9019 return;
9020# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009021 if (argvars[2].v_type != VAR_UNKNOWN
9022 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009023 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009025 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (server_name == NULL)
9027 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009028 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009029# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009030 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009032 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9033 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034# endif
9035 {
9036 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009038 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009039 vim_free(r);
9040 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009042 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 return;
9044 }
9045
9046 rettv->vval.v_string = r;
9047
9048 if (argvars[2].v_type != VAR_UNKNOWN)
9049 {
9050 dictitem_T v;
9051 char_u str[30];
9052 char_u *idvar;
9053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009054 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009055 if (idvar != NULL && *idvar != NUL)
9056 {
9057 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9058 v.di_tv.v_type = VAR_STRING;
9059 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009061 vim_free(v.di_tv.vval.v_string);
9062 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 }
9064}
9065#endif
9066
9067/*
9068 * "remote_expr()" function
9069 */
9070 static void
9071f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9072{
9073 rettv->v_type = VAR_STRING;
9074 rettv->vval.v_string = NULL;
9075#ifdef FEAT_CLIENTSERVER
9076 remote_common(argvars, rettv, TRUE);
9077#endif
9078}
9079
9080/*
9081 * "remote_foreground()" function
9082 */
9083 static void
9084f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9085{
9086#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009087# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 /* On Win32 it's done in this application. */
9089 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009090 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
9092 if (server_name != NULL)
9093 serverForeground(server_name);
9094 }
9095# else
9096 /* Send a foreground() expression to the server. */
9097 argvars[1].v_type = VAR_STRING;
9098 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9099 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009100 rettv->v_type = VAR_STRING;
9101 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 remote_common(argvars, rettv, TRUE);
9103 vim_free(argvars[1].vval.v_string);
9104# endif
9105#endif
9106}
9107
9108 static void
9109f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9110{
9111#ifdef FEAT_CLIENTSERVER
9112 dictitem_T v;
9113 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009114# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 long_u n = 0;
9116# endif
9117 char_u *serverid;
9118
9119 if (check_restricted() || check_secure())
9120 {
9121 rettv->vval.v_number = -1;
9122 return;
9123 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009124 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125 if (serverid == NULL)
9126 {
9127 rettv->vval.v_number = -1;
9128 return; /* type error; errmsg already given */
9129 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009130# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9132 if (n == 0)
9133 rettv->vval.v_number = -1;
9134 else
9135 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009136 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 rettv->vval.v_number = (s != NULL);
9138 }
9139# else
9140 if (check_connection() == FAIL)
9141 return;
9142
9143 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9144 serverStrToWin(serverid), &s);
9145# endif
9146
9147 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9148 {
9149 char_u *retvar;
9150
9151 v.di_tv.v_type = VAR_STRING;
9152 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009153 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 if (retvar != NULL)
9155 set_var(retvar, &v.di_tv, FALSE);
9156 vim_free(v.di_tv.vval.v_string);
9157 }
9158#else
9159 rettv->vval.v_number = -1;
9160#endif
9161}
9162
9163 static void
9164f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9165{
9166 char_u *r = NULL;
9167
9168#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009169 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170
9171 if (serverid != NULL && !check_restricted() && !check_secure())
9172 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009173 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009174# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009175 /* The server's HWND is encoded in the 'id' parameter */
9176 long_u n = 0;
9177# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009178
9179 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009180 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009181
Bram Moolenaar4f974752019-02-17 17:44:42 +01009182# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9184 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009185 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186 if (r == NULL)
9187# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009188 if (check_connection() == FAIL
9189 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9190 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009192 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193 }
9194#endif
9195 rettv->v_type = VAR_STRING;
9196 rettv->vval.v_string = r;
9197}
9198
9199/*
9200 * "remote_send()" function
9201 */
9202 static void
9203f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9204{
9205 rettv->v_type = VAR_STRING;
9206 rettv->vval.v_string = NULL;
9207#ifdef FEAT_CLIENTSERVER
9208 remote_common(argvars, rettv, FALSE);
9209#endif
9210}
9211
9212/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009213 * "remote_startserver()" function
9214 */
9215 static void
9216f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9217{
9218#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009219 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009220
9221 if (server == NULL)
9222 return; /* type error; errmsg already given */
9223 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009224 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009225 else
9226 {
9227# ifdef FEAT_X11
9228 if (check_connection() == OK)
9229 serverRegisterName(X_DISPLAY, server);
9230# else
9231 serverSetName(server);
9232# endif
9233 }
9234#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009235 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009236#endif
9237}
9238
9239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 * "remove()" function
9241 */
9242 static void
9243f_remove(typval_T *argvars, typval_T *rettv)
9244{
9245 list_T *l;
9246 listitem_T *item, *item2;
9247 listitem_T *li;
9248 long idx;
9249 long end;
9250 char_u *key;
9251 dict_T *d;
9252 dictitem_T *di;
9253 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009254 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009255
9256 if (argvars[0].v_type == VAR_DICT)
9257 {
9258 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009259 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009261 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009263 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 if (key != NULL)
9265 {
9266 di = dict_find(d, key, -1);
9267 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009268 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9270 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9271 {
9272 *rettv = di->di_tv;
9273 init_tv(&di->di_tv);
9274 dictitem_remove(d, di);
9275 }
9276 }
9277 }
9278 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009279 else if (argvars[0].v_type == VAR_BLOB)
9280 {
9281 idx = (long)tv_get_number_chk(&argvars[1], &error);
9282 if (!error)
9283 {
9284 blob_T *b = argvars[0].vval.v_blob;
9285 int len = blob_len(b);
9286 char_u *p;
9287
9288 if (idx < 0)
9289 // count from the end
9290 idx = len + idx;
9291 if (idx < 0 || idx >= len)
9292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009293 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009294 return;
9295 }
9296 if (argvars[2].v_type == VAR_UNKNOWN)
9297 {
9298 // Remove one item, return its value.
9299 p = (char_u *)b->bv_ga.ga_data;
9300 rettv->vval.v_number = (varnumber_T) *(p + idx);
9301 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9302 --b->bv_ga.ga_len;
9303 }
9304 else
9305 {
9306 blob_T *blob;
9307
9308 // Remove range of items, return list with values.
9309 end = (long)tv_get_number_chk(&argvars[2], &error);
9310 if (error)
9311 return;
9312 if (end < 0)
9313 // count from the end
9314 end = len + end;
9315 if (end >= len || idx > end)
9316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009317 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009318 return;
9319 }
9320 blob = blob_alloc();
9321 if (blob == NULL)
9322 return;
9323 blob->bv_ga.ga_len = end - idx + 1;
9324 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9325 {
9326 vim_free(blob);
9327 return;
9328 }
9329 p = (char_u *)b->bv_ga.ga_data;
9330 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9331 (size_t)(end - idx + 1));
9332 ++blob->bv_refcount;
9333 rettv->v_type = VAR_BLOB;
9334 rettv->vval.v_blob = blob;
9335
9336 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9337 b->bv_ga.ga_len -= end - idx + 1;
9338 }
9339 }
9340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009342 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009344 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009346 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009348 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009350 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 else
9352 {
9353 if (argvars[2].v_type == VAR_UNKNOWN)
9354 {
9355 /* Remove one item, return its value. */
9356 vimlist_remove(l, item, item);
9357 *rettv = item->li_tv;
9358 vim_free(item);
9359 }
9360 else
9361 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009362 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009365 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009367 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 else
9369 {
9370 int cnt = 0;
9371
9372 for (li = item; li != NULL; li = li->li_next)
9373 {
9374 ++cnt;
9375 if (li == item2)
9376 break;
9377 }
9378 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009379 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380 else
9381 {
9382 vimlist_remove(l, item, item2);
9383 if (rettv_list_alloc(rettv) == OK)
9384 {
9385 l = rettv->vval.v_list;
9386 l->lv_first = item;
9387 l->lv_last = item2;
9388 item->li_prev = NULL;
9389 item2->li_next = NULL;
9390 l->lv_len = cnt;
9391 }
9392 }
9393 }
9394 }
9395 }
9396 }
9397}
9398
9399/*
9400 * "rename({from}, {to})" function
9401 */
9402 static void
9403f_rename(typval_T *argvars, typval_T *rettv)
9404{
9405 char_u buf[NUMBUFLEN];
9406
9407 if (check_restricted() || check_secure())
9408 rettv->vval.v_number = -1;
9409 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9411 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412}
9413
9414/*
9415 * "repeat()" function
9416 */
9417 static void
9418f_repeat(typval_T *argvars, typval_T *rettv)
9419{
9420 char_u *p;
9421 int n;
9422 int slen;
9423 int len;
9424 char_u *r;
9425 int i;
9426
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009427 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428 if (argvars[0].v_type == VAR_LIST)
9429 {
9430 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9431 while (n-- > 0)
9432 if (list_extend(rettv->vval.v_list,
9433 argvars[0].vval.v_list, NULL) == FAIL)
9434 break;
9435 }
9436 else
9437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009438 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439 rettv->v_type = VAR_STRING;
9440 rettv->vval.v_string = NULL;
9441
9442 slen = (int)STRLEN(p);
9443 len = slen * n;
9444 if (len <= 0)
9445 return;
9446
9447 r = alloc(len + 1);
9448 if (r != NULL)
9449 {
9450 for (i = 0; i < n; i++)
9451 mch_memmove(r + i * slen, p, (size_t)slen);
9452 r[len] = NUL;
9453 }
9454
9455 rettv->vval.v_string = r;
9456 }
9457}
9458
9459/*
9460 * "resolve()" function
9461 */
9462 static void
9463f_resolve(typval_T *argvars, typval_T *rettv)
9464{
9465 char_u *p;
9466#ifdef HAVE_READLINK
9467 char_u *buf = NULL;
9468#endif
9469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009470 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471#ifdef FEAT_SHORTCUT
9472 {
9473 char_u *v = NULL;
9474
Bram Moolenaardce1e892019-02-10 23:18:53 +01009475 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 if (v != NULL)
9477 rettv->vval.v_string = v;
9478 else
9479 rettv->vval.v_string = vim_strsave(p);
9480 }
9481#else
9482# ifdef HAVE_READLINK
9483 {
9484 char_u *cpy;
9485 int len;
9486 char_u *remain = NULL;
9487 char_u *q;
9488 int is_relative_to_current = FALSE;
9489 int has_trailing_pathsep = FALSE;
9490 int limit = 100;
9491
9492 p = vim_strsave(p);
9493
9494 if (p[0] == '.' && (vim_ispathsep(p[1])
9495 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9496 is_relative_to_current = TRUE;
9497
9498 len = STRLEN(p);
9499 if (len > 0 && after_pathsep(p, p + len))
9500 {
9501 has_trailing_pathsep = TRUE;
9502 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9503 }
9504
9505 q = getnextcomp(p);
9506 if (*q != NUL)
9507 {
9508 /* Separate the first path component in "p", and keep the
9509 * remainder (beginning with the path separator). */
9510 remain = vim_strsave(q - 1);
9511 q[-1] = NUL;
9512 }
9513
9514 buf = alloc(MAXPATHL + 1);
9515 if (buf == NULL)
9516 goto fail;
9517
9518 for (;;)
9519 {
9520 for (;;)
9521 {
9522 len = readlink((char *)p, (char *)buf, MAXPATHL);
9523 if (len <= 0)
9524 break;
9525 buf[len] = NUL;
9526
9527 if (limit-- == 0)
9528 {
9529 vim_free(p);
9530 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009531 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 rettv->vval.v_string = NULL;
9533 goto fail;
9534 }
9535
9536 /* Ensure that the result will have a trailing path separator
9537 * if the argument has one. */
9538 if (remain == NULL && has_trailing_pathsep)
9539 add_pathsep(buf);
9540
9541 /* Separate the first path component in the link value and
9542 * concatenate the remainders. */
9543 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9544 if (*q != NUL)
9545 {
9546 if (remain == NULL)
9547 remain = vim_strsave(q - 1);
9548 else
9549 {
9550 cpy = concat_str(q - 1, remain);
9551 if (cpy != NULL)
9552 {
9553 vim_free(remain);
9554 remain = cpy;
9555 }
9556 }
9557 q[-1] = NUL;
9558 }
9559
9560 q = gettail(p);
9561 if (q > p && *q == NUL)
9562 {
9563 /* Ignore trailing path separator. */
9564 q[-1] = NUL;
9565 q = gettail(p);
9566 }
9567 if (q > p && !mch_isFullName(buf))
9568 {
9569 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009570 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 if (cpy != NULL)
9572 {
9573 STRCPY(cpy, p);
9574 STRCPY(gettail(cpy), buf);
9575 vim_free(p);
9576 p = cpy;
9577 }
9578 }
9579 else
9580 {
9581 vim_free(p);
9582 p = vim_strsave(buf);
9583 }
9584 }
9585
9586 if (remain == NULL)
9587 break;
9588
9589 /* Append the first path component of "remain" to "p". */
9590 q = getnextcomp(remain + 1);
9591 len = q - remain - (*q != NUL);
9592 cpy = vim_strnsave(p, STRLEN(p) + len);
9593 if (cpy != NULL)
9594 {
9595 STRNCAT(cpy, remain, len);
9596 vim_free(p);
9597 p = cpy;
9598 }
9599 /* Shorten "remain". */
9600 if (*q != NUL)
9601 STRMOVE(remain, q - 1);
9602 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009603 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 }
9605
9606 /* If the result is a relative path name, make it explicitly relative to
9607 * the current directory if and only if the argument had this form. */
9608 if (!vim_ispathsep(*p))
9609 {
9610 if (is_relative_to_current
9611 && *p != NUL
9612 && !(p[0] == '.'
9613 && (p[1] == NUL
9614 || vim_ispathsep(p[1])
9615 || (p[1] == '.'
9616 && (p[2] == NUL
9617 || vim_ispathsep(p[2]))))))
9618 {
9619 /* Prepend "./". */
9620 cpy = concat_str((char_u *)"./", p);
9621 if (cpy != NULL)
9622 {
9623 vim_free(p);
9624 p = cpy;
9625 }
9626 }
9627 else if (!is_relative_to_current)
9628 {
9629 /* Strip leading "./". */
9630 q = p;
9631 while (q[0] == '.' && vim_ispathsep(q[1]))
9632 q += 2;
9633 if (q > p)
9634 STRMOVE(p, p + 2);
9635 }
9636 }
9637
9638 /* Ensure that the result will have no trailing path separator
9639 * if the argument had none. But keep "/" or "//". */
9640 if (!has_trailing_pathsep)
9641 {
9642 q = p + STRLEN(p);
9643 if (after_pathsep(p, q))
9644 *gettail_sep(p) = NUL;
9645 }
9646
9647 rettv->vval.v_string = p;
9648 }
9649# else
9650 rettv->vval.v_string = vim_strsave(p);
9651# endif
9652#endif
9653
9654 simplify_filename(rettv->vval.v_string);
9655
9656#ifdef HAVE_READLINK
9657fail:
9658 vim_free(buf);
9659#endif
9660 rettv->v_type = VAR_STRING;
9661}
9662
9663/*
9664 * "reverse({list})" function
9665 */
9666 static void
9667f_reverse(typval_T *argvars, typval_T *rettv)
9668{
9669 list_T *l;
9670 listitem_T *li, *ni;
9671
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009672 if (argvars[0].v_type == VAR_BLOB)
9673 {
9674 blob_T *b = argvars[0].vval.v_blob;
9675 int i, len = blob_len(b);
9676
9677 for (i = 0; i < len / 2; i++)
9678 {
9679 int tmp = blob_get(b, i);
9680
9681 blob_set(b, i, blob_get(b, len - i - 1));
9682 blob_set(b, len - i - 1, tmp);
9683 }
9684 rettv_blob_set(rettv, b);
9685 return;
9686 }
9687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009689 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009691 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 (char_u *)N_("reverse() argument"), TRUE))
9693 {
9694 li = l->lv_last;
9695 l->lv_first = l->lv_last = NULL;
9696 l->lv_len = 0;
9697 while (li != NULL)
9698 {
9699 ni = li->li_prev;
9700 list_append(l, li);
9701 li = ni;
9702 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009703 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009704 l->lv_idx = l->lv_len - l->lv_idx - 1;
9705 }
9706}
9707
9708#define SP_NOMOVE 0x01 /* don't move cursor */
9709#define SP_REPEAT 0x02 /* repeat to find outer pair */
9710#define SP_RETCOUNT 0x04 /* return matchcount */
9711#define SP_SETPCMARK 0x08 /* set previous context mark */
9712#define SP_START 0x10 /* accept match at start position */
9713#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9714#define SP_END 0x40 /* leave cursor at end of match */
9715#define SP_COLUMN 0x80 /* start at cursor column */
9716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717/*
9718 * Get flags for a search function.
9719 * Possibly sets "p_ws".
9720 * Returns BACKWARD, FORWARD or zero (for an error).
9721 */
9722 static int
9723get_search_arg(typval_T *varp, int *flagsp)
9724{
9725 int dir = FORWARD;
9726 char_u *flags;
9727 char_u nbuf[NUMBUFLEN];
9728 int mask;
9729
9730 if (varp->v_type != VAR_UNKNOWN)
9731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009732 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009733 if (flags == NULL)
9734 return 0; /* type error; errmsg already given */
9735 while (*flags != NUL)
9736 {
9737 switch (*flags)
9738 {
9739 case 'b': dir = BACKWARD; break;
9740 case 'w': p_ws = TRUE; break;
9741 case 'W': p_ws = FALSE; break;
9742 default: mask = 0;
9743 if (flagsp != NULL)
9744 switch (*flags)
9745 {
9746 case 'c': mask = SP_START; break;
9747 case 'e': mask = SP_END; break;
9748 case 'm': mask = SP_RETCOUNT; break;
9749 case 'n': mask = SP_NOMOVE; break;
9750 case 'p': mask = SP_SUBPAT; break;
9751 case 'r': mask = SP_REPEAT; break;
9752 case 's': mask = SP_SETPCMARK; break;
9753 case 'z': mask = SP_COLUMN; break;
9754 }
9755 if (mask == 0)
9756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009757 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758 dir = 0;
9759 }
9760 else
9761 *flagsp |= mask;
9762 }
9763 if (dir == 0)
9764 break;
9765 ++flags;
9766 }
9767 }
9768 return dir;
9769}
9770
9771/*
9772 * Shared by search() and searchpos() functions.
9773 */
9774 static int
9775search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9776{
9777 int flags;
9778 char_u *pat;
9779 pos_T pos;
9780 pos_T save_cursor;
9781 int save_p_ws = p_ws;
9782 int dir;
9783 int retval = 0; /* default: FAIL */
9784 long lnum_stop = 0;
9785 proftime_T tm;
9786#ifdef FEAT_RELTIME
9787 long time_limit = 0;
9788#endif
9789 int options = SEARCH_KEEP;
9790 int subpatnum;
9791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009792 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9794 if (dir == 0)
9795 goto theend;
9796 flags = *flagsp;
9797 if (flags & SP_START)
9798 options |= SEARCH_START;
9799 if (flags & SP_END)
9800 options |= SEARCH_END;
9801 if (flags & SP_COLUMN)
9802 options |= SEARCH_COL;
9803
9804 /* Optional arguments: line number to stop searching and timeout. */
9805 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009807 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 if (lnum_stop < 0)
9809 goto theend;
9810#ifdef FEAT_RELTIME
9811 if (argvars[3].v_type != VAR_UNKNOWN)
9812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 if (time_limit < 0)
9815 goto theend;
9816 }
9817#endif
9818 }
9819
9820#ifdef FEAT_RELTIME
9821 /* Set the time limit, if there is one. */
9822 profile_setlimit(time_limit, &tm);
9823#endif
9824
9825 /*
9826 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9827 * Check to make sure only those flags are set.
9828 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9829 * flags cannot be set. Check for that condition also.
9830 */
9831 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9832 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009834 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 goto theend;
9836 }
9837
9838 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009839 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009840 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841 if (subpatnum != FAIL)
9842 {
9843 if (flags & SP_SUBPAT)
9844 retval = subpatnum;
9845 else
9846 retval = pos.lnum;
9847 if (flags & SP_SETPCMARK)
9848 setpcmark();
9849 curwin->w_cursor = pos;
9850 if (match_pos != NULL)
9851 {
9852 /* Store the match cursor position */
9853 match_pos->lnum = pos.lnum;
9854 match_pos->col = pos.col + 1;
9855 }
9856 /* "/$" will put the cursor after the end of the line, may need to
9857 * correct that here */
9858 check_cursor();
9859 }
9860
9861 /* If 'n' flag is used: restore cursor position. */
9862 if (flags & SP_NOMOVE)
9863 curwin->w_cursor = save_cursor;
9864 else
9865 curwin->w_set_curswant = TRUE;
9866theend:
9867 p_ws = save_p_ws;
9868
9869 return retval;
9870}
9871
9872#ifdef FEAT_FLOAT
9873
9874/*
9875 * round() is not in C90, use ceil() or floor() instead.
9876 */
9877 float_T
9878vim_round(float_T f)
9879{
9880 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9881}
9882
9883/*
9884 * "round({float})" function
9885 */
9886 static void
9887f_round(typval_T *argvars, typval_T *rettv)
9888{
9889 float_T f = 0.0;
9890
9891 rettv->v_type = VAR_FLOAT;
9892 if (get_float_arg(argvars, &f) == OK)
9893 rettv->vval.v_float = vim_round(f);
9894 else
9895 rettv->vval.v_float = 0.0;
9896}
9897#endif
9898
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009899#ifdef FEAT_RUBY
9900/*
9901 * "rubyeval()" function
9902 */
9903 static void
9904f_rubyeval(typval_T *argvars, typval_T *rettv)
9905{
9906 char_u *str;
9907 char_u buf[NUMBUFLEN];
9908
9909 str = tv_get_string_buf(&argvars[0], buf);
9910 do_rubyeval(str, rettv);
9911}
9912#endif
9913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914/*
9915 * "screenattr()" function
9916 */
9917 static void
9918f_screenattr(typval_T *argvars, typval_T *rettv)
9919{
9920 int row;
9921 int col;
9922 int c;
9923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009924 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9925 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 if (row < 0 || row >= screen_Rows
9927 || col < 0 || col >= screen_Columns)
9928 c = -1;
9929 else
9930 c = ScreenAttrs[LineOffset[row] + col];
9931 rettv->vval.v_number = c;
9932}
9933
9934/*
9935 * "screenchar()" function
9936 */
9937 static void
9938f_screenchar(typval_T *argvars, typval_T *rettv)
9939{
9940 int row;
9941 int col;
9942 int off;
9943 int c;
9944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009945 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9946 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009947 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 c = -1;
9949 else
9950 {
9951 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952 if (enc_utf8 && ScreenLinesUC[off] != 0)
9953 c = ScreenLinesUC[off];
9954 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 c = ScreenLines[off];
9956 }
9957 rettv->vval.v_number = c;
9958}
9959
9960/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009961 * "screenchars()" function
9962 */
9963 static void
9964f_screenchars(typval_T *argvars, typval_T *rettv)
9965{
9966 int row;
9967 int col;
9968 int off;
9969 int c;
9970 int i;
9971
9972 if (rettv_list_alloc(rettv) == FAIL)
9973 return;
9974 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9975 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9976 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9977 return;
9978
9979 off = LineOffset[row] + col;
9980 if (enc_utf8 && ScreenLinesUC[off] != 0)
9981 c = ScreenLinesUC[off];
9982 else
9983 c = ScreenLines[off];
9984 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9985
9986 if (enc_utf8)
9987
9988 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9989 list_append_number(rettv->vval.v_list,
9990 (varnumber_T)ScreenLinesC[i][off]);
9991}
9992
9993/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 * "screencol()" function
9995 *
9996 * First column is 1 to be consistent with virtcol().
9997 */
9998 static void
9999f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10000{
10001 rettv->vval.v_number = screen_screencol() + 1;
10002}
10003
10004/*
10005 * "screenrow()" function
10006 */
10007 static void
10008f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10009{
10010 rettv->vval.v_number = screen_screenrow() + 1;
10011}
10012
10013/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010014 * "screenstring()" function
10015 */
10016 static void
10017f_screenstring(typval_T *argvars, typval_T *rettv)
10018{
10019 int row;
10020 int col;
10021 int off;
10022 int c;
10023 int i;
10024 char_u buf[MB_MAXBYTES + 1];
10025 int buflen = 0;
10026
10027 rettv->vval.v_string = NULL;
10028 rettv->v_type = VAR_STRING;
10029
10030 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10031 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10032 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10033 return;
10034
10035 off = LineOffset[row] + col;
10036 if (enc_utf8 && ScreenLinesUC[off] != 0)
10037 c = ScreenLinesUC[off];
10038 else
10039 c = ScreenLines[off];
10040 buflen += mb_char2bytes(c, buf);
10041
10042 if (enc_utf8)
10043 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10044 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10045
10046 buf[buflen] = NUL;
10047 rettv->vval.v_string = vim_strsave(buf);
10048}
10049
10050/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 * "search()" function
10052 */
10053 static void
10054f_search(typval_T *argvars, typval_T *rettv)
10055{
10056 int flags = 0;
10057
10058 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10059}
10060
10061/*
10062 * "searchdecl()" function
10063 */
10064 static void
10065f_searchdecl(typval_T *argvars, typval_T *rettv)
10066{
10067 int locally = 1;
10068 int thisblock = 0;
10069 int error = FALSE;
10070 char_u *name;
10071
10072 rettv->vval.v_number = 1; /* default: FAIL */
10073
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010074 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010075 if (argvars[1].v_type != VAR_UNKNOWN)
10076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010077 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010078 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010079 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080 }
10081 if (!error && name != NULL)
10082 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10083 locally, thisblock, SEARCH_KEEP) == FAIL;
10084}
10085
10086/*
10087 * Used by searchpair() and searchpairpos()
10088 */
10089 static int
10090searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10091{
10092 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010093 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 int save_p_ws = p_ws;
10095 int dir;
10096 int flags = 0;
10097 char_u nbuf1[NUMBUFLEN];
10098 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010099 int retval = 0; /* default: FAIL */
10100 long lnum_stop = 0;
10101 long time_limit = 0;
10102
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010103 /* Get the three pattern arguments: start, middle, end. Will result in an
10104 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010105 spat = tv_get_string_chk(&argvars[0]);
10106 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10107 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010108 if (spat == NULL || mpat == NULL || epat == NULL)
10109 goto theend; /* type error */
10110
10111 /* Handle the optional fourth argument: flags */
10112 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10113 if (dir == 0)
10114 goto theend;
10115
10116 /* Don't accept SP_END or SP_SUBPAT.
10117 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10118 */
10119 if ((flags & (SP_END | SP_SUBPAT)) != 0
10120 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010122 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 goto theend;
10124 }
10125
10126 /* Using 'r' implies 'W', otherwise it doesn't work. */
10127 if (flags & SP_REPEAT)
10128 p_ws = FALSE;
10129
10130 /* Optional fifth argument: skip expression */
10131 if (argvars[3].v_type == VAR_UNKNOWN
10132 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010133 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010134 else
10135 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010136 skip = &argvars[4];
10137 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10138 && skip->v_type != VAR_STRING)
10139 {
10140 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010141 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010142 goto theend;
10143 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 if (argvars[5].v_type != VAR_UNKNOWN)
10145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010146 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010149 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010151 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152#ifdef FEAT_RELTIME
10153 if (argvars[6].v_type != VAR_UNKNOWN)
10154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010155 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010156 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010158 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 }
10162#endif
10163 }
10164 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165
10166 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10167 match_pos, lnum_stop, time_limit);
10168
10169theend:
10170 p_ws = save_p_ws;
10171
10172 return retval;
10173}
10174
10175/*
10176 * "searchpair()" function
10177 */
10178 static void
10179f_searchpair(typval_T *argvars, typval_T *rettv)
10180{
10181 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10182}
10183
10184/*
10185 * "searchpairpos()" function
10186 */
10187 static void
10188f_searchpairpos(typval_T *argvars, typval_T *rettv)
10189{
10190 pos_T match_pos;
10191 int lnum = 0;
10192 int col = 0;
10193
10194 if (rettv_list_alloc(rettv) == FAIL)
10195 return;
10196
10197 if (searchpair_cmn(argvars, &match_pos) > 0)
10198 {
10199 lnum = match_pos.lnum;
10200 col = match_pos.col;
10201 }
10202
10203 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10204 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10205}
10206
10207/*
10208 * Search for a start/middle/end thing.
10209 * Used by searchpair(), see its documentation for the details.
10210 * Returns 0 or -1 for no match,
10211 */
10212 long
10213do_searchpair(
10214 char_u *spat, /* start pattern */
10215 char_u *mpat, /* middle pattern */
10216 char_u *epat, /* end pattern */
10217 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010218 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010219 int flags, /* SP_SETPCMARK and other SP_ values */
10220 pos_T *match_pos,
10221 linenr_T lnum_stop, /* stop at this line if not zero */
10222 long time_limit UNUSED) /* stop after this many msec */
10223{
10224 char_u *save_cpo;
10225 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10226 long retval = 0;
10227 pos_T pos;
10228 pos_T firstpos;
10229 pos_T foundpos;
10230 pos_T save_cursor;
10231 pos_T save_pos;
10232 int n;
10233 int r;
10234 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010235 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236 int err;
10237 int options = SEARCH_KEEP;
10238 proftime_T tm;
10239
10240 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10241 save_cpo = p_cpo;
10242 p_cpo = empty_option;
10243
10244#ifdef FEAT_RELTIME
10245 /* Set the time limit, if there is one. */
10246 profile_setlimit(time_limit, &tm);
10247#endif
10248
10249 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10250 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010251 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10252 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 if (pat2 == NULL || pat3 == NULL)
10254 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010255 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 if (*mpat == NUL)
10257 STRCPY(pat3, pat2);
10258 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010259 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 spat, epat, mpat);
10261 if (flags & SP_START)
10262 options |= SEARCH_START;
10263
Bram Moolenaar48570482017-10-30 21:48:41 +010010264 if (skip != NULL)
10265 {
10266 /* Empty string means to not use the skip expression. */
10267 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10268 use_skip = skip->vval.v_string != NULL
10269 && *skip->vval.v_string != NUL;
10270 }
10271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 save_cursor = curwin->w_cursor;
10273 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010274 CLEAR_POS(&firstpos);
10275 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 pat = pat3;
10277 for (;;)
10278 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010279 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010280 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010281 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 /* didn't find it or found the first match again: FAIL */
10283 break;
10284
10285 if (firstpos.lnum == 0)
10286 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010287 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 {
10289 /* Found the same position again. Can happen with a pattern that
10290 * has "\zs" at the end and searching backwards. Advance one
10291 * character and try again. */
10292 if (dir == BACKWARD)
10293 decl(&pos);
10294 else
10295 incl(&pos);
10296 }
10297 foundpos = pos;
10298
10299 /* clear the start flag to avoid getting stuck here */
10300 options &= ~SEARCH_START;
10301
10302 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010303 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 {
10305 save_pos = curwin->w_cursor;
10306 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010307 err = FALSE;
10308 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309 curwin->w_cursor = save_pos;
10310 if (err)
10311 {
10312 /* Evaluating {skip} caused an error, break here. */
10313 curwin->w_cursor = save_cursor;
10314 retval = -1;
10315 break;
10316 }
10317 if (r)
10318 continue;
10319 }
10320
10321 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10322 {
10323 /* Found end when searching backwards or start when searching
10324 * forward: nested pair. */
10325 ++nest;
10326 pat = pat2; /* nested, don't search for middle */
10327 }
10328 else
10329 {
10330 /* Found end when searching forward or start when searching
10331 * backward: end of (nested) pair; or found middle in outer pair. */
10332 if (--nest == 1)
10333 pat = pat3; /* outer level, search for middle */
10334 }
10335
10336 if (nest == 0)
10337 {
10338 /* Found the match: return matchcount or line number. */
10339 if (flags & SP_RETCOUNT)
10340 ++retval;
10341 else
10342 retval = pos.lnum;
10343 if (flags & SP_SETPCMARK)
10344 setpcmark();
10345 curwin->w_cursor = pos;
10346 if (!(flags & SP_REPEAT))
10347 break;
10348 nest = 1; /* search for next unmatched */
10349 }
10350 }
10351
10352 if (match_pos != NULL)
10353 {
10354 /* Store the match cursor position */
10355 match_pos->lnum = curwin->w_cursor.lnum;
10356 match_pos->col = curwin->w_cursor.col + 1;
10357 }
10358
10359 /* If 'n' flag is used or search failed: restore cursor position. */
10360 if ((flags & SP_NOMOVE) || retval == 0)
10361 curwin->w_cursor = save_cursor;
10362
10363theend:
10364 vim_free(pat2);
10365 vim_free(pat3);
10366 if (p_cpo == empty_option)
10367 p_cpo = save_cpo;
10368 else
10369 /* Darn, evaluating the {skip} expression changed the value. */
10370 free_string_option(save_cpo);
10371
10372 return retval;
10373}
10374
10375/*
10376 * "searchpos()" function
10377 */
10378 static void
10379f_searchpos(typval_T *argvars, typval_T *rettv)
10380{
10381 pos_T match_pos;
10382 int lnum = 0;
10383 int col = 0;
10384 int n;
10385 int flags = 0;
10386
10387 if (rettv_list_alloc(rettv) == FAIL)
10388 return;
10389
10390 n = search_cmn(argvars, &match_pos, &flags);
10391 if (n > 0)
10392 {
10393 lnum = match_pos.lnum;
10394 col = match_pos.col;
10395 }
10396
10397 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10398 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10399 if (flags & SP_SUBPAT)
10400 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10401}
10402
10403 static void
10404f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10405{
10406#ifdef FEAT_CLIENTSERVER
10407 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010408 char_u *server = tv_get_string_chk(&argvars[0]);
10409 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410
10411 rettv->vval.v_number = -1;
10412 if (server == NULL || reply == NULL)
10413 return;
10414 if (check_restricted() || check_secure())
10415 return;
10416# ifdef FEAT_X11
10417 if (check_connection() == FAIL)
10418 return;
10419# endif
10420
10421 if (serverSendReply(server, reply) < 0)
10422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010423 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424 return;
10425 }
10426 rettv->vval.v_number = 0;
10427#else
10428 rettv->vval.v_number = -1;
10429#endif
10430}
10431
10432 static void
10433f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10434{
10435 char_u *r = NULL;
10436
10437#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010438# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010439 r = serverGetVimNames();
10440# else
10441 make_connection();
10442 if (X_DISPLAY != NULL)
10443 r = serverGetVimNames(X_DISPLAY);
10444# endif
10445#endif
10446 rettv->v_type = VAR_STRING;
10447 rettv->vval.v_string = r;
10448}
10449
10450/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010451 * "setbufline()" function
10452 */
10453 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010454f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010455{
10456 linenr_T lnum;
10457 buf_T *buf;
10458
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010459 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010460 if (buf == NULL)
10461 rettv->vval.v_number = 1; /* FAIL */
10462 else
10463 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010464 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010465 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010466 }
10467}
10468
10469/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 * "setbufvar()" function
10471 */
10472 static void
10473f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10474{
10475 buf_T *buf;
10476 char_u *varname, *bufvarname;
10477 typval_T *varp;
10478 char_u nbuf[NUMBUFLEN];
10479
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010480 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010481 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010482 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10483 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010484 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485 varp = &argvars[2];
10486
10487 if (buf != NULL && varname != NULL && varp != NULL)
10488 {
10489 if (*varname == '&')
10490 {
10491 long numval;
10492 char_u *strval;
10493 int error = FALSE;
10494 aco_save_T aco;
10495
10496 /* set curbuf to be our buf, temporarily */
10497 aucmd_prepbuf(&aco, buf);
10498
10499 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010500 numval = (long)tv_get_number_chk(varp, &error);
10501 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010502 if (!error && strval != NULL)
10503 set_option_value(varname, numval, strval, OPT_LOCAL);
10504
10505 /* reset notion of buffer */
10506 aucmd_restbuf(&aco);
10507 }
10508 else
10509 {
10510 buf_T *save_curbuf = curbuf;
10511
Bram Moolenaar964b3742019-05-24 18:54:09 +020010512 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (bufvarname != NULL)
10514 {
10515 curbuf = buf;
10516 STRCPY(bufvarname, "b:");
10517 STRCPY(bufvarname + 2, varname);
10518 set_var(bufvarname, varp, TRUE);
10519 vim_free(bufvarname);
10520 curbuf = save_curbuf;
10521 }
10522 }
10523 }
10524}
10525
10526 static void
10527f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10528{
10529 dict_T *d;
10530 dictitem_T *di;
10531 char_u *csearch;
10532
10533 if (argvars[0].v_type != VAR_DICT)
10534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010535 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536 return;
10537 }
10538
10539 if ((d = argvars[0].vval.v_dict) != NULL)
10540 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010541 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542 if (csearch != NULL)
10543 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010544 if (enc_utf8)
10545 {
10546 int pcc[MAX_MCO];
10547 int c = utfc_ptr2char(csearch, pcc);
10548
10549 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10550 }
10551 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 set_last_csearch(PTR2CHAR(csearch),
10553 csearch, MB_PTR2LEN(csearch));
10554 }
10555
10556 di = dict_find(d, (char_u *)"forward", -1);
10557 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010558 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 ? FORWARD : BACKWARD);
10560
10561 di = dict_find(d, (char_u *)"until", -1);
10562 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010563 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010564 }
10565}
10566
10567/*
10568 * "setcmdpos()" function
10569 */
10570 static void
10571f_setcmdpos(typval_T *argvars, typval_T *rettv)
10572{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010573 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574
10575 if (pos >= 0)
10576 rettv->vval.v_number = set_cmdline_pos(pos);
10577}
10578
10579/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010580 * "setenv()" function
10581 */
10582 static void
10583f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10584{
10585 char_u namebuf[NUMBUFLEN];
10586 char_u valbuf[NUMBUFLEN];
10587 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10588
10589 if (argvars[1].v_type == VAR_SPECIAL
10590 && argvars[1].vval.v_number == VVAL_NULL)
10591 vim_unsetenv(name);
10592 else
10593 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10594}
10595
10596/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010597 * "setfperm({fname}, {mode})" function
10598 */
10599 static void
10600f_setfperm(typval_T *argvars, typval_T *rettv)
10601{
10602 char_u *fname;
10603 char_u modebuf[NUMBUFLEN];
10604 char_u *mode_str;
10605 int i;
10606 int mask;
10607 int mode = 0;
10608
10609 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010610 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 if (fname == NULL)
10612 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010613 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 if (mode_str == NULL)
10615 return;
10616 if (STRLEN(mode_str) != 9)
10617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010618 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 return;
10620 }
10621
10622 mask = 1;
10623 for (i = 8; i >= 0; --i)
10624 {
10625 if (mode_str[i] != '-')
10626 mode |= mask;
10627 mask = mask << 1;
10628 }
10629 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10630}
10631
10632/*
10633 * "setline()" function
10634 */
10635 static void
10636f_setline(typval_T *argvars, typval_T *rettv)
10637{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010638 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639
Bram Moolenaarca851592018-06-06 21:04:07 +020010640 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010641}
10642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010643/*
10644 * Used by "setqflist()" and "setloclist()" functions
10645 */
10646 static void
10647set_qf_ll_list(
10648 win_T *wp UNUSED,
10649 typval_T *list_arg UNUSED,
10650 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010651 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 typval_T *rettv)
10653{
10654#ifdef FEAT_QUICKFIX
10655 static char *e_invact = N_("E927: Invalid action: '%s'");
10656 char_u *act;
10657 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010658 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010659#endif
10660
10661 rettv->vval.v_number = -1;
10662
10663#ifdef FEAT_QUICKFIX
10664 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010665 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010666 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010667 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668 else
10669 {
10670 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010671 dict_T *d = NULL;
10672 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673
10674 if (action_arg->v_type == VAR_STRING)
10675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010676 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 if (act == NULL)
10678 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010679 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10680 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010681 action = *act;
10682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010683 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684 }
10685 else if (action_arg->v_type == VAR_UNKNOWN)
10686 action = ' ';
10687 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010688 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689
Bram Moolenaard823fa92016-08-12 16:29:27 +020010690 if (action_arg->v_type != VAR_UNKNOWN
10691 && what_arg->v_type != VAR_UNKNOWN)
10692 {
10693 if (what_arg->v_type == VAR_DICT)
10694 d = what_arg->vval.v_dict;
10695 else
10696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010697 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010698 valid_dict = FALSE;
10699 }
10700 }
10701
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010702 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010703 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010704 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10705 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010707 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 }
10709#endif
10710}
10711
10712/*
10713 * "setloclist()" function
10714 */
10715 static void
10716f_setloclist(typval_T *argvars, typval_T *rettv)
10717{
10718 win_T *win;
10719
10720 rettv->vval.v_number = -1;
10721
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010722 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010723 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010724 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725}
10726
10727/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010728 * "setpos()" function
10729 */
10730 static void
10731f_setpos(typval_T *argvars, typval_T *rettv)
10732{
10733 pos_T pos;
10734 int fnum;
10735 char_u *name;
10736 colnr_T curswant = -1;
10737
10738 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010739 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740 if (name != NULL)
10741 {
10742 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10743 {
10744 if (--pos.col < 0)
10745 pos.col = 0;
10746 if (name[0] == '.' && name[1] == NUL)
10747 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010748 /* set cursor; "fnum" is ignored */
10749 curwin->w_cursor = pos;
10750 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010752 curwin->w_curswant = curswant - 1;
10753 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010755 check_cursor();
10756 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 }
10758 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10759 {
10760 /* set mark */
10761 if (setmark_pos(name[1], &pos, fnum) == OK)
10762 rettv->vval.v_number = 0;
10763 }
10764 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010765 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 }
10767 }
10768}
10769
10770/*
10771 * "setqflist()" function
10772 */
10773 static void
10774f_setqflist(typval_T *argvars, typval_T *rettv)
10775{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010776 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777}
10778
10779/*
10780 * "setreg()" function
10781 */
10782 static void
10783f_setreg(typval_T *argvars, typval_T *rettv)
10784{
10785 int regname;
10786 char_u *strregname;
10787 char_u *stropt;
10788 char_u *strval;
10789 int append;
10790 char_u yank_type;
10791 long block_len;
10792
10793 block_len = -1;
10794 yank_type = MAUTO;
10795 append = FALSE;
10796
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010797 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 rettv->vval.v_number = 1; /* FAIL is default */
10799
10800 if (strregname == NULL)
10801 return; /* type error; errmsg already given */
10802 regname = *strregname;
10803 if (regname == 0 || regname == '@')
10804 regname = '"';
10805
10806 if (argvars[2].v_type != VAR_UNKNOWN)
10807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010808 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809 if (stropt == NULL)
10810 return; /* type error */
10811 for (; *stropt != NUL; ++stropt)
10812 switch (*stropt)
10813 {
10814 case 'a': case 'A': /* append */
10815 append = TRUE;
10816 break;
10817 case 'v': case 'c': /* character-wise selection */
10818 yank_type = MCHAR;
10819 break;
10820 case 'V': case 'l': /* line-wise selection */
10821 yank_type = MLINE;
10822 break;
10823 case 'b': case Ctrl_V: /* block-wise selection */
10824 yank_type = MBLOCK;
10825 if (VIM_ISDIGIT(stropt[1]))
10826 {
10827 ++stropt;
10828 block_len = getdigits(&stropt) - 1;
10829 --stropt;
10830 }
10831 break;
10832 }
10833 }
10834
10835 if (argvars[1].v_type == VAR_LIST)
10836 {
10837 char_u **lstval;
10838 char_u **allocval;
10839 char_u buf[NUMBUFLEN];
10840 char_u **curval;
10841 char_u **curallocval;
10842 list_T *ll = argvars[1].vval.v_list;
10843 listitem_T *li;
10844 int len;
10845
10846 /* If the list is NULL handle like an empty list. */
10847 len = ll == NULL ? 0 : ll->lv_len;
10848
10849 /* First half: use for pointers to result lines; second half: use for
10850 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010851 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010852 if (lstval == NULL)
10853 return;
10854 curval = lstval;
10855 allocval = lstval + len + 2;
10856 curallocval = allocval;
10857
10858 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10859 li = li->li_next)
10860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010861 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010862 if (strval == NULL)
10863 goto free_lstval;
10864 if (strval == buf)
10865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010866 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010867 * overwrite the string. */
10868 strval = vim_strsave(buf);
10869 if (strval == NULL)
10870 goto free_lstval;
10871 *curallocval++ = strval;
10872 }
10873 *curval++ = strval;
10874 }
10875 *curval++ = NULL;
10876
10877 write_reg_contents_lst(regname, lstval, -1,
10878 append, yank_type, block_len);
10879free_lstval:
10880 while (curallocval > allocval)
10881 vim_free(*--curallocval);
10882 vim_free(lstval);
10883 }
10884 else
10885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010886 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010887 if (strval == NULL)
10888 return;
10889 write_reg_contents_ex(regname, strval, -1,
10890 append, yank_type, block_len);
10891 }
10892 rettv->vval.v_number = 0;
10893}
10894
10895/*
10896 * "settabvar()" function
10897 */
10898 static void
10899f_settabvar(typval_T *argvars, typval_T *rettv)
10900{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 tabpage_T *save_curtab;
10902 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 char_u *varname, *tabvarname;
10904 typval_T *varp;
10905
10906 rettv->vval.v_number = 0;
10907
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010908 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010909 return;
10910
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010911 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10912 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010913 varp = &argvars[2];
10914
Bram Moolenaar4033c552017-09-16 20:54:51 +020010915 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010917 save_curtab = curtab;
10918 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919
Bram Moolenaar964b3742019-05-24 18:54:09 +020010920 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921 if (tabvarname != NULL)
10922 {
10923 STRCPY(tabvarname, "t:");
10924 STRCPY(tabvarname + 2, varname);
10925 set_var(tabvarname, varp, TRUE);
10926 vim_free(tabvarname);
10927 }
10928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 /* Restore current tabpage */
10930 if (valid_tabpage(save_curtab))
10931 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 }
10933}
10934
10935/*
10936 * "settabwinvar()" function
10937 */
10938 static void
10939f_settabwinvar(typval_T *argvars, typval_T *rettv)
10940{
10941 setwinvar(argvars, rettv, 1);
10942}
10943
10944/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010945 * "settagstack()" function
10946 */
10947 static void
10948f_settagstack(typval_T *argvars, typval_T *rettv)
10949{
10950 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10951 win_T *wp;
10952 dict_T *d;
10953 int action = 'r';
10954
10955 rettv->vval.v_number = -1;
10956
10957 // first argument: window number or id
10958 wp = find_win_by_nr_or_id(&argvars[0]);
10959 if (wp == NULL)
10960 return;
10961
10962 // second argument: dict with items to set in the tag stack
10963 if (argvars[1].v_type != VAR_DICT)
10964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010965 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010966 return;
10967 }
10968 d = argvars[1].vval.v_dict;
10969 if (d == NULL)
10970 return;
10971
10972 // third argument: action - 'a' for append and 'r' for replace.
10973 // default is to replace the stack.
10974 if (argvars[2].v_type == VAR_UNKNOWN)
10975 action = 'r';
10976 else if (argvars[2].v_type == VAR_STRING)
10977 {
10978 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010980 if (actstr == NULL)
10981 return;
10982 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10983 action = *actstr;
10984 else
10985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010986 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010987 return;
10988 }
10989 }
10990 else
10991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010992 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010993 return;
10994 }
10995
10996 if (set_tagstack(wp, d, action) == OK)
10997 rettv->vval.v_number = 0;
10998}
10999
11000/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 * "setwinvar()" function
11002 */
11003 static void
11004f_setwinvar(typval_T *argvars, typval_T *rettv)
11005{
11006 setwinvar(argvars, rettv, 0);
11007}
11008
11009#ifdef FEAT_CRYPT
11010/*
11011 * "sha256({string})" function
11012 */
11013 static void
11014f_sha256(typval_T *argvars, typval_T *rettv)
11015{
11016 char_u *p;
11017
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011018 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 rettv->vval.v_string = vim_strsave(
11020 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11021 rettv->v_type = VAR_STRING;
11022}
11023#endif /* FEAT_CRYPT */
11024
11025/*
11026 * "shellescape({string})" function
11027 */
11028 static void
11029f_shellescape(typval_T *argvars, typval_T *rettv)
11030{
Bram Moolenaar20615522017-06-05 18:46:26 +020011031 int do_special = non_zero_arg(&argvars[1]);
11032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011034 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 rettv->v_type = VAR_STRING;
11036}
11037
11038/*
11039 * shiftwidth() function
11040 */
11041 static void
11042f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11043{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011044 rettv->vval.v_number = 0;
11045
11046 if (argvars[0].v_type != VAR_UNKNOWN)
11047 {
11048 long col;
11049
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011050 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011051 if (col < 0)
11052 return; // type error; errmsg already given
11053#ifdef FEAT_VARTABS
11054 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11055 return;
11056#endif
11057 }
11058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 rettv->vval.v_number = get_sw_value(curbuf);
11060}
11061
11062/*
11063 * "simplify()" function
11064 */
11065 static void
11066f_simplify(typval_T *argvars, typval_T *rettv)
11067{
11068 char_u *p;
11069
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011070 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011071 rettv->vval.v_string = vim_strsave(p);
11072 simplify_filename(rettv->vval.v_string); /* simplify in place */
11073 rettv->v_type = VAR_STRING;
11074}
11075
11076#ifdef FEAT_FLOAT
11077/*
11078 * "sin()" function
11079 */
11080 static void
11081f_sin(typval_T *argvars, typval_T *rettv)
11082{
11083 float_T f = 0.0;
11084
11085 rettv->v_type = VAR_FLOAT;
11086 if (get_float_arg(argvars, &f) == OK)
11087 rettv->vval.v_float = sin(f);
11088 else
11089 rettv->vval.v_float = 0.0;
11090}
11091
11092/*
11093 * "sinh()" function
11094 */
11095 static void
11096f_sinh(typval_T *argvars, typval_T *rettv)
11097{
11098 float_T f = 0.0;
11099
11100 rettv->v_type = VAR_FLOAT;
11101 if (get_float_arg(argvars, &f) == OK)
11102 rettv->vval.v_float = sinh(f);
11103 else
11104 rettv->vval.v_float = 0.0;
11105}
11106#endif
11107
Bram Moolenaareae1b912019-05-09 15:12:55 +020011108static int item_compare(const void *s1, const void *s2);
11109static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110
11111/* struct used in the array that's given to qsort() */
11112typedef struct
11113{
11114 listitem_T *item;
11115 int idx;
11116} sortItem_T;
11117
11118/* struct storing information about current sort */
11119typedef struct
11120{
11121 int item_compare_ic;
11122 int item_compare_numeric;
11123 int item_compare_numbers;
11124#ifdef FEAT_FLOAT
11125 int item_compare_float;
11126#endif
11127 char_u *item_compare_func;
11128 partial_T *item_compare_partial;
11129 dict_T *item_compare_selfdict;
11130 int item_compare_func_err;
11131 int item_compare_keep_zero;
11132} sortinfo_T;
11133static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134#define ITEM_COMPARE_FAIL 999
11135
11136/*
11137 * Compare functions for f_sort() and f_uniq() below.
11138 */
11139 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140item_compare(const void *s1, const void *s2)
11141{
11142 sortItem_T *si1, *si2;
11143 typval_T *tv1, *tv2;
11144 char_u *p1, *p2;
11145 char_u *tofree1 = NULL, *tofree2 = NULL;
11146 int res;
11147 char_u numbuf1[NUMBUFLEN];
11148 char_u numbuf2[NUMBUFLEN];
11149
11150 si1 = (sortItem_T *)s1;
11151 si2 = (sortItem_T *)s2;
11152 tv1 = &si1->item->li_tv;
11153 tv2 = &si2->item->li_tv;
11154
11155 if (sortinfo->item_compare_numbers)
11156 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011157 varnumber_T v1 = tv_get_number(tv1);
11158 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011159
11160 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11161 }
11162
11163#ifdef FEAT_FLOAT
11164 if (sortinfo->item_compare_float)
11165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011166 float_T v1 = tv_get_float(tv1);
11167 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011168
11169 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11170 }
11171#endif
11172
11173 /* tv2string() puts quotes around a string and allocates memory. Don't do
11174 * that for string variables. Use a single quote when comparing with a
11175 * non-string to do what the docs promise. */
11176 if (tv1->v_type == VAR_STRING)
11177 {
11178 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11179 p1 = (char_u *)"'";
11180 else
11181 p1 = tv1->vval.v_string;
11182 }
11183 else
11184 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11185 if (tv2->v_type == VAR_STRING)
11186 {
11187 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11188 p2 = (char_u *)"'";
11189 else
11190 p2 = tv2->vval.v_string;
11191 }
11192 else
11193 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11194 if (p1 == NULL)
11195 p1 = (char_u *)"";
11196 if (p2 == NULL)
11197 p2 = (char_u *)"";
11198 if (!sortinfo->item_compare_numeric)
11199 {
11200 if (sortinfo->item_compare_ic)
11201 res = STRICMP(p1, p2);
11202 else
11203 res = STRCMP(p1, p2);
11204 }
11205 else
11206 {
11207 double n1, n2;
11208 n1 = strtod((char *)p1, (char **)&p1);
11209 n2 = strtod((char *)p2, (char **)&p2);
11210 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11211 }
11212
11213 /* When the result would be zero, compare the item indexes. Makes the
11214 * sort stable. */
11215 if (res == 0 && !sortinfo->item_compare_keep_zero)
11216 res = si1->idx > si2->idx ? 1 : -1;
11217
11218 vim_free(tofree1);
11219 vim_free(tofree2);
11220 return res;
11221}
11222
11223 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224item_compare2(const void *s1, const void *s2)
11225{
11226 sortItem_T *si1, *si2;
11227 int res;
11228 typval_T rettv;
11229 typval_T argv[3];
11230 int dummy;
11231 char_u *func_name;
11232 partial_T *partial = sortinfo->item_compare_partial;
11233
11234 /* shortcut after failure in previous call; compare all items equal */
11235 if (sortinfo->item_compare_func_err)
11236 return 0;
11237
11238 si1 = (sortItem_T *)s1;
11239 si2 = (sortItem_T *)s2;
11240
11241 if (partial == NULL)
11242 func_name = sortinfo->item_compare_func;
11243 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011244 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245
11246 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11247 * in the copy without changing the original list items. */
11248 copy_tv(&si1->item->li_tv, &argv[0]);
11249 copy_tv(&si2->item->li_tv, &argv[1]);
11250
11251 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011252 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011253 partial, sortinfo->item_compare_selfdict);
11254 clear_tv(&argv[0]);
11255 clear_tv(&argv[1]);
11256
11257 if (res == FAIL)
11258 res = ITEM_COMPARE_FAIL;
11259 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011260 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011261 if (sortinfo->item_compare_func_err)
11262 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11263 clear_tv(&rettv);
11264
11265 /* When the result would be zero, compare the pointers themselves. Makes
11266 * the sort stable. */
11267 if (res == 0 && !sortinfo->item_compare_keep_zero)
11268 res = si1->idx > si2->idx ? 1 : -1;
11269
11270 return res;
11271}
11272
11273/*
11274 * "sort({list})" function
11275 */
11276 static void
11277do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11278{
11279 list_T *l;
11280 listitem_T *li;
11281 sortItem_T *ptrs;
11282 sortinfo_T *old_sortinfo;
11283 sortinfo_T info;
11284 long len;
11285 long i;
11286
11287 /* Pointer to current info struct used in compare function. Save and
11288 * restore the current one for nested calls. */
11289 old_sortinfo = sortinfo;
11290 sortinfo = &info;
11291
11292 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011293 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011294 else
11295 {
11296 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011297 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011298 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11299 TRUE))
11300 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011301 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302
11303 len = list_len(l);
11304 if (len <= 1)
11305 goto theend; /* short list sorts pretty quickly */
11306
11307 info.item_compare_ic = FALSE;
11308 info.item_compare_numeric = FALSE;
11309 info.item_compare_numbers = FALSE;
11310#ifdef FEAT_FLOAT
11311 info.item_compare_float = FALSE;
11312#endif
11313 info.item_compare_func = NULL;
11314 info.item_compare_partial = NULL;
11315 info.item_compare_selfdict = NULL;
11316 if (argvars[1].v_type != VAR_UNKNOWN)
11317 {
11318 /* optional second argument: {func} */
11319 if (argvars[1].v_type == VAR_FUNC)
11320 info.item_compare_func = argvars[1].vval.v_string;
11321 else if (argvars[1].v_type == VAR_PARTIAL)
11322 info.item_compare_partial = argvars[1].vval.v_partial;
11323 else
11324 {
11325 int error = FALSE;
11326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011327 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011328 if (error)
11329 goto theend; /* type error; errmsg already given */
11330 if (i == 1)
11331 info.item_compare_ic = TRUE;
11332 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011333 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 else if (i != 0)
11335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011336 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 goto theend;
11338 }
11339 if (info.item_compare_func != NULL)
11340 {
11341 if (*info.item_compare_func == NUL)
11342 {
11343 /* empty string means default sort */
11344 info.item_compare_func = NULL;
11345 }
11346 else if (STRCMP(info.item_compare_func, "n") == 0)
11347 {
11348 info.item_compare_func = NULL;
11349 info.item_compare_numeric = TRUE;
11350 }
11351 else if (STRCMP(info.item_compare_func, "N") == 0)
11352 {
11353 info.item_compare_func = NULL;
11354 info.item_compare_numbers = TRUE;
11355 }
11356#ifdef FEAT_FLOAT
11357 else if (STRCMP(info.item_compare_func, "f") == 0)
11358 {
11359 info.item_compare_func = NULL;
11360 info.item_compare_float = TRUE;
11361 }
11362#endif
11363 else if (STRCMP(info.item_compare_func, "i") == 0)
11364 {
11365 info.item_compare_func = NULL;
11366 info.item_compare_ic = TRUE;
11367 }
11368 }
11369 }
11370
11371 if (argvars[2].v_type != VAR_UNKNOWN)
11372 {
11373 /* optional third argument: {dict} */
11374 if (argvars[2].v_type != VAR_DICT)
11375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011376 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 goto theend;
11378 }
11379 info.item_compare_selfdict = argvars[2].vval.v_dict;
11380 }
11381 }
11382
11383 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011384 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 if (ptrs == NULL)
11386 goto theend;
11387
11388 i = 0;
11389 if (sort)
11390 {
11391 /* sort(): ptrs will be the list to sort */
11392 for (li = l->lv_first; li != NULL; li = li->li_next)
11393 {
11394 ptrs[i].item = li;
11395 ptrs[i].idx = i;
11396 ++i;
11397 }
11398
11399 info.item_compare_func_err = FALSE;
11400 info.item_compare_keep_zero = FALSE;
11401 /* test the compare function */
11402 if ((info.item_compare_func != NULL
11403 || info.item_compare_partial != NULL)
11404 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11405 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011406 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011407 else
11408 {
11409 /* Sort the array with item pointers. */
11410 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11411 info.item_compare_func == NULL
11412 && info.item_compare_partial == NULL
11413 ? item_compare : item_compare2);
11414
11415 if (!info.item_compare_func_err)
11416 {
11417 /* Clear the List and append the items in sorted order. */
11418 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11419 l->lv_len = 0;
11420 for (i = 0; i < len; ++i)
11421 list_append(l, ptrs[i].item);
11422 }
11423 }
11424 }
11425 else
11426 {
11427 int (*item_compare_func_ptr)(const void *, const void *);
11428
11429 /* f_uniq(): ptrs will be a stack of items to remove */
11430 info.item_compare_func_err = FALSE;
11431 info.item_compare_keep_zero = TRUE;
11432 item_compare_func_ptr = info.item_compare_func != NULL
11433 || info.item_compare_partial != NULL
11434 ? item_compare2 : item_compare;
11435
11436 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11437 li = li->li_next)
11438 {
11439 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11440 == 0)
11441 ptrs[i++].item = li;
11442 if (info.item_compare_func_err)
11443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011444 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011445 break;
11446 }
11447 }
11448
11449 if (!info.item_compare_func_err)
11450 {
11451 while (--i >= 0)
11452 {
11453 li = ptrs[i].item->li_next;
11454 ptrs[i].item->li_next = li->li_next;
11455 if (li->li_next != NULL)
11456 li->li_next->li_prev = ptrs[i].item;
11457 else
11458 l->lv_last = ptrs[i].item;
11459 list_fix_watch(l, li);
11460 listitem_free(li);
11461 l->lv_len--;
11462 }
11463 }
11464 }
11465
11466 vim_free(ptrs);
11467 }
11468theend:
11469 sortinfo = old_sortinfo;
11470}
11471
11472/*
11473 * "sort({list})" function
11474 */
11475 static void
11476f_sort(typval_T *argvars, typval_T *rettv)
11477{
11478 do_sort_uniq(argvars, rettv, TRUE);
11479}
11480
11481/*
11482 * "uniq({list})" function
11483 */
11484 static void
11485f_uniq(typval_T *argvars, typval_T *rettv)
11486{
11487 do_sort_uniq(argvars, rettv, FALSE);
11488}
11489
11490/*
11491 * "soundfold({word})" function
11492 */
11493 static void
11494f_soundfold(typval_T *argvars, typval_T *rettv)
11495{
11496 char_u *s;
11497
11498 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500#ifdef FEAT_SPELL
11501 rettv->vval.v_string = eval_soundfold(s);
11502#else
11503 rettv->vval.v_string = vim_strsave(s);
11504#endif
11505}
11506
11507/*
11508 * "spellbadword()" function
11509 */
11510 static void
11511f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11512{
11513 char_u *word = (char_u *)"";
11514 hlf_T attr = HLF_COUNT;
11515 int len = 0;
11516
11517 if (rettv_list_alloc(rettv) == FAIL)
11518 return;
11519
11520#ifdef FEAT_SPELL
11521 if (argvars[0].v_type == VAR_UNKNOWN)
11522 {
11523 /* Find the start and length of the badly spelled word. */
11524 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11525 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011526 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011527 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011528 curwin->w_set_curswant = TRUE;
11529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011530 }
11531 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011533 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534 int capcol = -1;
11535
11536 if (str != NULL)
11537 {
11538 /* Check the argument for spelling. */
11539 while (*str != NUL)
11540 {
11541 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11542 if (attr != HLF_COUNT)
11543 {
11544 word = str;
11545 break;
11546 }
11547 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011548 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549 }
11550 }
11551 }
11552#endif
11553
11554 list_append_string(rettv->vval.v_list, word, len);
11555 list_append_string(rettv->vval.v_list, (char_u *)(
11556 attr == HLF_SPB ? "bad" :
11557 attr == HLF_SPR ? "rare" :
11558 attr == HLF_SPL ? "local" :
11559 attr == HLF_SPC ? "caps" :
11560 ""), -1);
11561}
11562
11563/*
11564 * "spellsuggest()" function
11565 */
11566 static void
11567f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11568{
11569#ifdef FEAT_SPELL
11570 char_u *str;
11571 int typeerr = FALSE;
11572 int maxcount;
11573 garray_T ga;
11574 int i;
11575 listitem_T *li;
11576 int need_capital = FALSE;
11577#endif
11578
11579 if (rettv_list_alloc(rettv) == FAIL)
11580 return;
11581
11582#ifdef FEAT_SPELL
11583 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011585 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 if (argvars[1].v_type != VAR_UNKNOWN)
11587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011588 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589 if (maxcount <= 0)
11590 return;
11591 if (argvars[2].v_type != VAR_UNKNOWN)
11592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011593 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011594 if (typeerr)
11595 return;
11596 }
11597 }
11598 else
11599 maxcount = 25;
11600
11601 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11602
11603 for (i = 0; i < ga.ga_len; ++i)
11604 {
11605 str = ((char_u **)ga.ga_data)[i];
11606
11607 li = listitem_alloc();
11608 if (li == NULL)
11609 vim_free(str);
11610 else
11611 {
11612 li->li_tv.v_type = VAR_STRING;
11613 li->li_tv.v_lock = 0;
11614 li->li_tv.vval.v_string = str;
11615 list_append(rettv->vval.v_list, li);
11616 }
11617 }
11618 ga_clear(&ga);
11619 }
11620#endif
11621}
11622
11623 static void
11624f_split(typval_T *argvars, typval_T *rettv)
11625{
11626 char_u *str;
11627 char_u *end;
11628 char_u *pat = NULL;
11629 regmatch_T regmatch;
11630 char_u patbuf[NUMBUFLEN];
11631 char_u *save_cpo;
11632 int match;
11633 colnr_T col = 0;
11634 int keepempty = FALSE;
11635 int typeerr = FALSE;
11636
11637 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11638 save_cpo = p_cpo;
11639 p_cpo = (char_u *)"";
11640
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011641 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011642 if (argvars[1].v_type != VAR_UNKNOWN)
11643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011644 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011645 if (pat == NULL)
11646 typeerr = TRUE;
11647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011648 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 }
11650 if (pat == NULL || *pat == NUL)
11651 pat = (char_u *)"[\\x01- ]\\+";
11652
11653 if (rettv_list_alloc(rettv) == FAIL)
11654 return;
11655 if (typeerr)
11656 return;
11657
11658 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11659 if (regmatch.regprog != NULL)
11660 {
11661 regmatch.rm_ic = FALSE;
11662 while (*str != NUL || keepempty)
11663 {
11664 if (*str == NUL)
11665 match = FALSE; /* empty item at the end */
11666 else
11667 match = vim_regexec_nl(&regmatch, str, col);
11668 if (match)
11669 end = regmatch.startp[0];
11670 else
11671 end = str + STRLEN(str);
11672 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11673 && *str != NUL && match && end < regmatch.endp[0]))
11674 {
11675 if (list_append_string(rettv->vval.v_list, str,
11676 (int)(end - str)) == FAIL)
11677 break;
11678 }
11679 if (!match)
11680 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011681 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682 if (regmatch.endp[0] > str)
11683 col = 0;
11684 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011685 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011686 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011687 str = regmatch.endp[0];
11688 }
11689
11690 vim_regfree(regmatch.regprog);
11691 }
11692
11693 p_cpo = save_cpo;
11694}
11695
11696#ifdef FEAT_FLOAT
11697/*
11698 * "sqrt()" function
11699 */
11700 static void
11701f_sqrt(typval_T *argvars, typval_T *rettv)
11702{
11703 float_T f = 0.0;
11704
11705 rettv->v_type = VAR_FLOAT;
11706 if (get_float_arg(argvars, &f) == OK)
11707 rettv->vval.v_float = sqrt(f);
11708 else
11709 rettv->vval.v_float = 0.0;
11710}
11711
11712/*
11713 * "str2float()" function
11714 */
11715 static void
11716f_str2float(typval_T *argvars, typval_T *rettv)
11717{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011718 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011719 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720
Bram Moolenaar08243d22017-01-10 16:12:29 +010011721 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011722 p = skipwhite(p + 1);
11723 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011724 if (isneg)
11725 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726 rettv->v_type = VAR_FLOAT;
11727}
11728#endif
11729
11730/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011731 * "str2list()" function
11732 */
11733 static void
11734f_str2list(typval_T *argvars, typval_T *rettv)
11735{
11736 char_u *p;
11737 int utf8 = FALSE;
11738
11739 if (rettv_list_alloc(rettv) == FAIL)
11740 return;
11741
11742 if (argvars[1].v_type != VAR_UNKNOWN)
11743 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11744
11745 p = tv_get_string(&argvars[0]);
11746
11747 if (has_mbyte || utf8)
11748 {
11749 int (*ptr2len)(char_u *);
11750 int (*ptr2char)(char_u *);
11751
11752 if (utf8 || enc_utf8)
11753 {
11754 ptr2len = utf_ptr2len;
11755 ptr2char = utf_ptr2char;
11756 }
11757 else
11758 {
11759 ptr2len = mb_ptr2len;
11760 ptr2char = mb_ptr2char;
11761 }
11762
11763 for ( ; *p != NUL; p += (*ptr2len)(p))
11764 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11765 }
11766 else
11767 for ( ; *p != NUL; ++p)
11768 list_append_number(rettv->vval.v_list, *p);
11769}
11770
11771/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 * "str2nr()" function
11773 */
11774 static void
11775f_str2nr(typval_T *argvars, typval_T *rettv)
11776{
11777 int base = 10;
11778 char_u *p;
11779 varnumber_T n;
11780 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011781 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011782
11783 if (argvars[1].v_type != VAR_UNKNOWN)
11784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011785 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011786 if (base != 2 && base != 8 && base != 10 && base != 16)
11787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011788 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011789 return;
11790 }
11791 }
11792
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011793 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011794 isneg = (*p == '-');
11795 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 p = skipwhite(p + 1);
11797 switch (base)
11798 {
11799 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11800 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11801 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11802 default: what = 0;
11803 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011804 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11805 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011806 if (isneg)
11807 rettv->vval.v_number = -n;
11808 else
11809 rettv->vval.v_number = n;
11810
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011811}
11812
11813#ifdef HAVE_STRFTIME
11814/*
11815 * "strftime({format}[, {time}])" function
11816 */
11817 static void
11818f_strftime(typval_T *argvars, typval_T *rettv)
11819{
11820 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011821 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011822 struct tm *curtime;
11823 time_t seconds;
11824 char_u *p;
11825
11826 rettv->v_type = VAR_STRING;
11827
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011828 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829 if (argvars[1].v_type == VAR_UNKNOWN)
11830 seconds = time(NULL);
11831 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011832 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011833 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 /* MSVC returns NULL for an invalid value of seconds. */
11835 if (curtime == NULL)
11836 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11837 else
11838 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 vimconv_T conv;
11840 char_u *enc;
11841
11842 conv.vc_type = CONV_NONE;
11843 enc = enc_locale();
11844 convert_setup(&conv, p_enc, enc);
11845 if (conv.vc_type != CONV_NONE)
11846 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011847 if (p != NULL)
11848 (void)strftime((char *)result_buf, sizeof(result_buf),
11849 (char *)p, curtime);
11850 else
11851 result_buf[0] = NUL;
11852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 if (conv.vc_type != CONV_NONE)
11854 vim_free(p);
11855 convert_setup(&conv, enc, p_enc);
11856 if (conv.vc_type != CONV_NONE)
11857 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11858 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 rettv->vval.v_string = vim_strsave(result_buf);
11860
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861 /* Release conversion descriptors */
11862 convert_setup(&conv, NULL, NULL);
11863 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011864 }
11865}
11866#endif
11867
11868/*
11869 * "strgetchar()" function
11870 */
11871 static void
11872f_strgetchar(typval_T *argvars, typval_T *rettv)
11873{
11874 char_u *str;
11875 int len;
11876 int error = FALSE;
11877 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011878 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879
11880 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011881 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882 if (str == NULL)
11883 return;
11884 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011885 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011886 if (error)
11887 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888
Bram Moolenaar13505972019-01-24 15:04:48 +010011889 while (charidx >= 0 && byteidx < len)
11890 {
11891 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011892 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011893 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11894 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011895 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011896 --charidx;
11897 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011898 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011899}
11900
11901/*
11902 * "stridx()" function
11903 */
11904 static void
11905f_stridx(typval_T *argvars, typval_T *rettv)
11906{
11907 char_u buf[NUMBUFLEN];
11908 char_u *needle;
11909 char_u *haystack;
11910 char_u *save_haystack;
11911 char_u *pos;
11912 int start_idx;
11913
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011914 needle = tv_get_string_chk(&argvars[1]);
11915 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916 rettv->vval.v_number = -1;
11917 if (needle == NULL || haystack == NULL)
11918 return; /* type error; errmsg already given */
11919
11920 if (argvars[2].v_type != VAR_UNKNOWN)
11921 {
11922 int error = FALSE;
11923
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011924 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925 if (error || start_idx >= (int)STRLEN(haystack))
11926 return;
11927 if (start_idx >= 0)
11928 haystack += start_idx;
11929 }
11930
11931 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11932 if (pos != NULL)
11933 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11934}
11935
11936/*
11937 * "string()" function
11938 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011939 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011940f_string(typval_T *argvars, typval_T *rettv)
11941{
11942 char_u *tofree;
11943 char_u numbuf[NUMBUFLEN];
11944
11945 rettv->v_type = VAR_STRING;
11946 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11947 get_copyID());
11948 /* Make a copy if we have a value but it's not in allocated memory. */
11949 if (rettv->vval.v_string != NULL && tofree == NULL)
11950 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11951}
11952
11953/*
11954 * "strlen()" function
11955 */
11956 static void
11957f_strlen(typval_T *argvars, typval_T *rettv)
11958{
11959 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011960 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961}
11962
11963/*
11964 * "strchars()" function
11965 */
11966 static void
11967f_strchars(typval_T *argvars, typval_T *rettv)
11968{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011969 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011970 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 varnumber_T len = 0;
11972 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973
11974 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011975 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011976 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011977 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011978 else
11979 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011980 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11981 while (*s != NUL)
11982 {
11983 func_mb_ptr2char_adv(&s);
11984 ++len;
11985 }
11986 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011987 }
11988}
11989
11990/*
11991 * "strdisplaywidth()" function
11992 */
11993 static void
11994f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11995{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011996 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011997 int col = 0;
11998
11999 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012000 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012001
12002 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12003}
12004
12005/*
12006 * "strwidth()" function
12007 */
12008 static void
12009f_strwidth(typval_T *argvars, typval_T *rettv)
12010{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012011 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012012
Bram Moolenaar13505972019-01-24 15:04:48 +010012013 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012014}
12015
12016/*
12017 * "strcharpart()" function
12018 */
12019 static void
12020f_strcharpart(typval_T *argvars, typval_T *rettv)
12021{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012022 char_u *p;
12023 int nchar;
12024 int nbyte = 0;
12025 int charlen;
12026 int len = 0;
12027 int slen;
12028 int error = FALSE;
12029
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012030 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012031 slen = (int)STRLEN(p);
12032
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012033 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012034 if (!error)
12035 {
12036 if (nchar > 0)
12037 while (nchar > 0 && nbyte < slen)
12038 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012039 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 --nchar;
12041 }
12042 else
12043 nbyte = nchar;
12044 if (argvars[2].v_type != VAR_UNKNOWN)
12045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012046 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047 while (charlen > 0 && nbyte + len < slen)
12048 {
12049 int off = nbyte + len;
12050
12051 if (off < 0)
12052 len += 1;
12053 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012054 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012055 --charlen;
12056 }
12057 }
12058 else
12059 len = slen - nbyte; /* default: all bytes that are available. */
12060 }
12061
12062 /*
12063 * Only return the overlap between the specified part and the actual
12064 * string.
12065 */
12066 if (nbyte < 0)
12067 {
12068 len += nbyte;
12069 nbyte = 0;
12070 }
12071 else if (nbyte > slen)
12072 nbyte = slen;
12073 if (len < 0)
12074 len = 0;
12075 else if (nbyte + len > slen)
12076 len = slen - nbyte;
12077
12078 rettv->v_type = VAR_STRING;
12079 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012080}
12081
12082/*
12083 * "strpart()" function
12084 */
12085 static void
12086f_strpart(typval_T *argvars, typval_T *rettv)
12087{
12088 char_u *p;
12089 int n;
12090 int len;
12091 int slen;
12092 int error = FALSE;
12093
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012094 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095 slen = (int)STRLEN(p);
12096
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012097 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012098 if (error)
12099 len = 0;
12100 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012101 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012102 else
12103 len = slen - n; /* default len: all bytes that are available. */
12104
12105 /*
12106 * Only return the overlap between the specified part and the actual
12107 * string.
12108 */
12109 if (n < 0)
12110 {
12111 len += n;
12112 n = 0;
12113 }
12114 else if (n > slen)
12115 n = slen;
12116 if (len < 0)
12117 len = 0;
12118 else if (n + len > slen)
12119 len = slen - n;
12120
12121 rettv->v_type = VAR_STRING;
12122 rettv->vval.v_string = vim_strnsave(p + n, len);
12123}
12124
12125/*
12126 * "strridx()" function
12127 */
12128 static void
12129f_strridx(typval_T *argvars, typval_T *rettv)
12130{
12131 char_u buf[NUMBUFLEN];
12132 char_u *needle;
12133 char_u *haystack;
12134 char_u *rest;
12135 char_u *lastmatch = NULL;
12136 int haystack_len, end_idx;
12137
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012138 needle = tv_get_string_chk(&argvars[1]);
12139 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012140
12141 rettv->vval.v_number = -1;
12142 if (needle == NULL || haystack == NULL)
12143 return; /* type error; errmsg already given */
12144
12145 haystack_len = (int)STRLEN(haystack);
12146 if (argvars[2].v_type != VAR_UNKNOWN)
12147 {
12148 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012149 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012150 if (end_idx < 0)
12151 return; /* can never find a match */
12152 }
12153 else
12154 end_idx = haystack_len;
12155
12156 if (*needle == NUL)
12157 {
12158 /* Empty string matches past the end. */
12159 lastmatch = haystack + end_idx;
12160 }
12161 else
12162 {
12163 for (rest = haystack; *rest != '\0'; ++rest)
12164 {
12165 rest = (char_u *)strstr((char *)rest, (char *)needle);
12166 if (rest == NULL || rest > haystack + end_idx)
12167 break;
12168 lastmatch = rest;
12169 }
12170 }
12171
12172 if (lastmatch == NULL)
12173 rettv->vval.v_number = -1;
12174 else
12175 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12176}
12177
12178/*
12179 * "strtrans()" function
12180 */
12181 static void
12182f_strtrans(typval_T *argvars, typval_T *rettv)
12183{
12184 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012185 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012186}
12187
12188/*
12189 * "submatch()" function
12190 */
12191 static void
12192f_submatch(typval_T *argvars, typval_T *rettv)
12193{
12194 int error = FALSE;
12195 int no;
12196 int retList = 0;
12197
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012198 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012199 if (error)
12200 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012201 if (no < 0 || no >= NSUBEXP)
12202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012203 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012204 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012205 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012206 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012207 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012208 if (error)
12209 return;
12210
12211 if (retList == 0)
12212 {
12213 rettv->v_type = VAR_STRING;
12214 rettv->vval.v_string = reg_submatch(no);
12215 }
12216 else
12217 {
12218 rettv->v_type = VAR_LIST;
12219 rettv->vval.v_list = reg_submatch_list(no);
12220 }
12221}
12222
12223/*
12224 * "substitute()" function
12225 */
12226 static void
12227f_substitute(typval_T *argvars, typval_T *rettv)
12228{
12229 char_u patbuf[NUMBUFLEN];
12230 char_u subbuf[NUMBUFLEN];
12231 char_u flagsbuf[NUMBUFLEN];
12232
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012233 char_u *str = tv_get_string_chk(&argvars[0]);
12234 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012235 char_u *sub = NULL;
12236 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012237 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012238
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012239 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12240 expr = &argvars[2];
12241 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012242 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012244 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012245 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12246 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012247 rettv->vval.v_string = NULL;
12248 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012249 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250}
12251
12252/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012253 * "swapinfo(swap_filename)" function
12254 */
12255 static void
12256f_swapinfo(typval_T *argvars, typval_T *rettv)
12257{
12258 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012259 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012260}
12261
12262/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012263 * "swapname(expr)" function
12264 */
12265 static void
12266f_swapname(typval_T *argvars, typval_T *rettv)
12267{
12268 buf_T *buf;
12269
12270 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012271 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012272 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12273 || buf->b_ml.ml_mfp->mf_fname == NULL)
12274 rettv->vval.v_string = NULL;
12275 else
12276 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12277}
12278
12279/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280 * "synID(lnum, col, trans)" function
12281 */
12282 static void
12283f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12284{
12285 int id = 0;
12286#ifdef FEAT_SYN_HL
12287 linenr_T lnum;
12288 colnr_T col;
12289 int trans;
12290 int transerr = FALSE;
12291
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012292 lnum = tv_get_lnum(argvars); /* -1 on type error */
12293 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12294 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012295
12296 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12297 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12298 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12299#endif
12300
12301 rettv->vval.v_number = id;
12302}
12303
12304/*
12305 * "synIDattr(id, what [, mode])" function
12306 */
12307 static void
12308f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12309{
12310 char_u *p = NULL;
12311#ifdef FEAT_SYN_HL
12312 int id;
12313 char_u *what;
12314 char_u *mode;
12315 char_u modebuf[NUMBUFLEN];
12316 int modec;
12317
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012318 id = (int)tv_get_number(&argvars[0]);
12319 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012320 if (argvars[2].v_type != VAR_UNKNOWN)
12321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012322 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012323 modec = TOLOWER_ASC(mode[0]);
12324 if (modec != 't' && modec != 'c' && modec != 'g')
12325 modec = 0; /* replace invalid with current */
12326 }
12327 else
12328 {
12329#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12330 if (USE_24BIT)
12331 modec = 'g';
12332 else
12333#endif
12334 if (t_colors > 1)
12335 modec = 'c';
12336 else
12337 modec = 't';
12338 }
12339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012340 switch (TOLOWER_ASC(what[0]))
12341 {
12342 case 'b':
12343 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12344 p = highlight_color(id, what, modec);
12345 else /* bold */
12346 p = highlight_has_attr(id, HL_BOLD, modec);
12347 break;
12348
12349 case 'f': /* fg[#] or font */
12350 p = highlight_color(id, what, modec);
12351 break;
12352
12353 case 'i':
12354 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12355 p = highlight_has_attr(id, HL_INVERSE, modec);
12356 else /* italic */
12357 p = highlight_has_attr(id, HL_ITALIC, modec);
12358 break;
12359
12360 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012361 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362 break;
12363
12364 case 'r': /* reverse */
12365 p = highlight_has_attr(id, HL_INVERSE, modec);
12366 break;
12367
12368 case 's':
12369 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12370 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012371 /* strikeout */
12372 else if (TOLOWER_ASC(what[1]) == 't' &&
12373 TOLOWER_ASC(what[2]) == 'r')
12374 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012375 else /* standout */
12376 p = highlight_has_attr(id, HL_STANDOUT, modec);
12377 break;
12378
12379 case 'u':
12380 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12381 /* underline */
12382 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12383 else
12384 /* undercurl */
12385 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12386 break;
12387 }
12388
12389 if (p != NULL)
12390 p = vim_strsave(p);
12391#endif
12392 rettv->v_type = VAR_STRING;
12393 rettv->vval.v_string = p;
12394}
12395
12396/*
12397 * "synIDtrans(id)" function
12398 */
12399 static void
12400f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12401{
12402 int id;
12403
12404#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012405 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012406
12407 if (id > 0)
12408 id = syn_get_final_id(id);
12409 else
12410#endif
12411 id = 0;
12412
12413 rettv->vval.v_number = id;
12414}
12415
12416/*
12417 * "synconcealed(lnum, col)" function
12418 */
12419 static void
12420f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12421{
12422#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12423 linenr_T lnum;
12424 colnr_T col;
12425 int syntax_flags = 0;
12426 int cchar;
12427 int matchid = 0;
12428 char_u str[NUMBUFLEN];
12429#endif
12430
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012431 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432
12433#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012434 lnum = tv_get_lnum(argvars); /* -1 on type error */
12435 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436
12437 vim_memset(str, NUL, sizeof(str));
12438
12439 if (rettv_list_alloc(rettv) != FAIL)
12440 {
12441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12442 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12443 && curwin->w_p_cole > 0)
12444 {
12445 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12446 syntax_flags = get_syntax_info(&matchid);
12447
12448 /* get the conceal character */
12449 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12450 {
12451 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012452 if (cchar == NUL && curwin->w_p_cole == 1)
12453 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 if (cchar != NUL)
12455 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456 if (has_mbyte)
12457 (*mb_char2bytes)(cchar, str);
12458 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 str[0] = cchar;
12460 }
12461 }
12462 }
12463
12464 list_append_number(rettv->vval.v_list,
12465 (syntax_flags & HL_CONCEAL) != 0);
12466 /* -1 to auto-determine strlen */
12467 list_append_string(rettv->vval.v_list, str, -1);
12468 list_append_number(rettv->vval.v_list, matchid);
12469 }
12470#endif
12471}
12472
12473/*
12474 * "synstack(lnum, col)" function
12475 */
12476 static void
12477f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12478{
12479#ifdef FEAT_SYN_HL
12480 linenr_T lnum;
12481 colnr_T col;
12482 int i;
12483 int id;
12484#endif
12485
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012486 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487
12488#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012489 lnum = tv_get_lnum(argvars); /* -1 on type error */
12490 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012491
12492 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12493 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12494 && rettv_list_alloc(rettv) != FAIL)
12495 {
12496 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12497 for (i = 0; ; ++i)
12498 {
12499 id = syn_get_stack_item(i);
12500 if (id < 0)
12501 break;
12502 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12503 break;
12504 }
12505 }
12506#endif
12507}
12508
12509 static void
12510get_cmd_output_as_rettv(
12511 typval_T *argvars,
12512 typval_T *rettv,
12513 int retlist)
12514{
12515 char_u *res = NULL;
12516 char_u *p;
12517 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012518 int err = FALSE;
12519 FILE *fd;
12520 list_T *list = NULL;
12521 int flags = SHELL_SILENT;
12522
12523 rettv->v_type = VAR_STRING;
12524 rettv->vval.v_string = NULL;
12525 if (check_restricted() || check_secure())
12526 goto errret;
12527
12528 if (argvars[1].v_type != VAR_UNKNOWN)
12529 {
12530 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012531 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012532 * command.
12533 */
12534 if ((infile = vim_tempname('i', TRUE)) == NULL)
12535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012536 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537 goto errret;
12538 }
12539
12540 fd = mch_fopen((char *)infile, WRITEBIN);
12541 if (fd == NULL)
12542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012543 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544 goto errret;
12545 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012546 if (argvars[1].v_type == VAR_NUMBER)
12547 {
12548 linenr_T lnum;
12549 buf_T *buf;
12550
12551 buf = buflist_findnr(argvars[1].vval.v_number);
12552 if (buf == NULL)
12553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012554 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012555 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012556 goto errret;
12557 }
12558
12559 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12560 {
12561 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12562 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12563 {
12564 err = TRUE;
12565 break;
12566 }
12567 if (putc(NL, fd) == EOF)
12568 {
12569 err = TRUE;
12570 break;
12571 }
12572 }
12573 }
12574 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575 {
12576 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12577 err = TRUE;
12578 }
12579 else
12580 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012581 size_t len;
12582 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012584 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 if (p == NULL)
12586 {
12587 fclose(fd);
12588 goto errret; /* type error; errmsg already given */
12589 }
12590 len = STRLEN(p);
12591 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12592 err = TRUE;
12593 }
12594 if (fclose(fd) != 0)
12595 err = TRUE;
12596 if (err)
12597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012598 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012599 goto errret;
12600 }
12601 }
12602
12603 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12604 * echoes typeahead, that messes up the display. */
12605 if (!msg_silent)
12606 flags += SHELL_COOKED;
12607
12608 if (retlist)
12609 {
12610 int len;
12611 listitem_T *li;
12612 char_u *s = NULL;
12613 char_u *start;
12614 char_u *end;
12615 int i;
12616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012617 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 if (res == NULL)
12619 goto errret;
12620
12621 list = list_alloc();
12622 if (list == NULL)
12623 goto errret;
12624
12625 for (i = 0; i < len; ++i)
12626 {
12627 start = res + i;
12628 while (i < len && res[i] != NL)
12629 ++i;
12630 end = res + i;
12631
Bram Moolenaar964b3742019-05-24 18:54:09 +020012632 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012633 if (s == NULL)
12634 goto errret;
12635
12636 for (p = s; start < end; ++p, ++start)
12637 *p = *start == NUL ? NL : *start;
12638 *p = NUL;
12639
12640 li = listitem_alloc();
12641 if (li == NULL)
12642 {
12643 vim_free(s);
12644 goto errret;
12645 }
12646 li->li_tv.v_type = VAR_STRING;
12647 li->li_tv.v_lock = 0;
12648 li->li_tv.vval.v_string = s;
12649 list_append(list, li);
12650 }
12651
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012652 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653 list = NULL;
12654 }
12655 else
12656 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012657 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010012658#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659 /* translate <CR><NL> into <NL> */
12660 if (res != NULL)
12661 {
12662 char_u *s, *d;
12663
12664 d = res;
12665 for (s = res; *s; ++s)
12666 {
12667 if (s[0] == CAR && s[1] == NL)
12668 ++s;
12669 *d++ = *s;
12670 }
12671 *d = NUL;
12672 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012673#endif
12674 rettv->vval.v_string = res;
12675 res = NULL;
12676 }
12677
12678errret:
12679 if (infile != NULL)
12680 {
12681 mch_remove(infile);
12682 vim_free(infile);
12683 }
12684 if (res != NULL)
12685 vim_free(res);
12686 if (list != NULL)
12687 list_free(list);
12688}
12689
12690/*
12691 * "system()" function
12692 */
12693 static void
12694f_system(typval_T *argvars, typval_T *rettv)
12695{
12696 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12697}
12698
12699/*
12700 * "systemlist()" function
12701 */
12702 static void
12703f_systemlist(typval_T *argvars, typval_T *rettv)
12704{
12705 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12706}
12707
12708/*
12709 * "tabpagebuflist()" function
12710 */
12711 static void
12712f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12713{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012714 tabpage_T *tp;
12715 win_T *wp = NULL;
12716
12717 if (argvars[0].v_type == VAR_UNKNOWN)
12718 wp = firstwin;
12719 else
12720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012721 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012722 if (tp != NULL)
12723 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12724 }
12725 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12726 {
12727 for (; wp != NULL; wp = wp->w_next)
12728 if (list_append_number(rettv->vval.v_list,
12729 wp->w_buffer->b_fnum) == FAIL)
12730 break;
12731 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732}
12733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734/*
12735 * "tabpagenr()" function
12736 */
12737 static void
12738f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12739{
12740 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 char_u *arg;
12742
12743 if (argvars[0].v_type != VAR_UNKNOWN)
12744 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012745 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746 nr = 0;
12747 if (arg != NULL)
12748 {
12749 if (STRCMP(arg, "$") == 0)
12750 nr = tabpage_index(NULL) - 1;
12751 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012752 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 }
12754 }
12755 else
12756 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012757 rettv->vval.v_number = nr;
12758}
12759
12760
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012761/*
12762 * Common code for tabpagewinnr() and winnr().
12763 */
12764 static int
12765get_winnr(tabpage_T *tp, typval_T *argvar)
12766{
12767 win_T *twin;
12768 int nr = 1;
12769 win_T *wp;
12770 char_u *arg;
12771
12772 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12773 if (argvar->v_type != VAR_UNKNOWN)
12774 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012775 int invalid_arg = FALSE;
12776
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012777 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012778 if (arg == NULL)
12779 nr = 0; /* type error; errmsg already given */
12780 else if (STRCMP(arg, "$") == 0)
12781 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12782 else if (STRCMP(arg, "#") == 0)
12783 {
12784 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12785 if (twin == NULL)
12786 nr = 0;
12787 }
12788 else
12789 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012790 long count;
12791 char_u *endp;
12792
12793 // Extract the window count (if specified). e.g. winnr('3j')
12794 count = strtol((char *)arg, (char **)&endp, 10);
12795 if (count <= 0)
12796 count = 1; // if count is not specified, default to 1
12797 if (endp != NULL && *endp != '\0')
12798 {
12799 if (STRCMP(endp, "j") == 0)
12800 twin = win_vert_neighbor(tp, twin, FALSE, count);
12801 else if (STRCMP(endp, "k") == 0)
12802 twin = win_vert_neighbor(tp, twin, TRUE, count);
12803 else if (STRCMP(endp, "h") == 0)
12804 twin = win_horz_neighbor(tp, twin, TRUE, count);
12805 else if (STRCMP(endp, "l") == 0)
12806 twin = win_horz_neighbor(tp, twin, FALSE, count);
12807 else
12808 invalid_arg = TRUE;
12809 }
12810 else
12811 invalid_arg = TRUE;
12812 }
12813
12814 if (invalid_arg)
12815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012816 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012817 nr = 0;
12818 }
12819 }
12820
12821 if (nr > 0)
12822 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12823 wp != twin; wp = wp->w_next)
12824 {
12825 if (wp == NULL)
12826 {
12827 /* didn't find it in this tabpage */
12828 nr = 0;
12829 break;
12830 }
12831 ++nr;
12832 }
12833 return nr;
12834}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835
12836/*
12837 * "tabpagewinnr()" function
12838 */
12839 static void
12840f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12841{
12842 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012843 tabpage_T *tp;
12844
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012845 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012846 if (tp == NULL)
12847 nr = 0;
12848 else
12849 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 rettv->vval.v_number = nr;
12851}
12852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853/*
12854 * "tagfiles()" function
12855 */
12856 static void
12857f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12858{
12859 char_u *fname;
12860 tagname_T tn;
12861 int first;
12862
12863 if (rettv_list_alloc(rettv) == FAIL)
12864 return;
12865 fname = alloc(MAXPATHL);
12866 if (fname == NULL)
12867 return;
12868
12869 for (first = TRUE; ; first = FALSE)
12870 if (get_tagfname(&tn, first, fname) == FAIL
12871 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12872 break;
12873 tagname_free(&tn);
12874 vim_free(fname);
12875}
12876
12877/*
12878 * "taglist()" function
12879 */
12880 static void
12881f_taglist(typval_T *argvars, typval_T *rettv)
12882{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012883 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 char_u *tag_pattern;
12885
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012886 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887
12888 rettv->vval.v_number = FALSE;
12889 if (*tag_pattern == NUL)
12890 return;
12891
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012892 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012893 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012895 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896}
12897
12898/*
12899 * "tempname()" function
12900 */
12901 static void
12902f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12903{
12904 static int x = 'A';
12905
12906 rettv->v_type = VAR_STRING;
12907 rettv->vval.v_string = vim_tempname(x, FALSE);
12908
12909 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12910 * names. Skip 'I' and 'O', they are used for shell redirection. */
12911 do
12912 {
12913 if (x == 'Z')
12914 x = '0';
12915 else if (x == '9')
12916 x = 'A';
12917 else
12918 {
12919#ifdef EBCDIC
12920 if (x == 'I')
12921 x = 'J';
12922 else if (x == 'R')
12923 x = 'S';
12924 else
12925#endif
12926 ++x;
12927 }
12928 } while (x == 'I' || x == 'O');
12929}
12930
12931#ifdef FEAT_FLOAT
12932/*
12933 * "tan()" function
12934 */
12935 static void
12936f_tan(typval_T *argvars, typval_T *rettv)
12937{
12938 float_T f = 0.0;
12939
12940 rettv->v_type = VAR_FLOAT;
12941 if (get_float_arg(argvars, &f) == OK)
12942 rettv->vval.v_float = tan(f);
12943 else
12944 rettv->vval.v_float = 0.0;
12945}
12946
12947/*
12948 * "tanh()" function
12949 */
12950 static void
12951f_tanh(typval_T *argvars, typval_T *rettv)
12952{
12953 float_T f = 0.0;
12954
12955 rettv->v_type = VAR_FLOAT;
12956 if (get_float_arg(argvars, &f) == OK)
12957 rettv->vval.v_float = tanh(f);
12958 else
12959 rettv->vval.v_float = 0.0;
12960}
12961#endif
12962
12963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964 * Get a callback from "arg". It can be a Funcref or a function name.
12965 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012966 * "cb_name" is not allocated.
12967 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012968 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012969 callback_T
12970get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012972 callback_T res;
12973
12974 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12976 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012977 res.cb_partial = arg->vval.v_partial;
12978 ++res.cb_partial->pt_refcount;
12979 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012981 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012983 res.cb_partial = NULL;
12984 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12985 {
12986 // Note that we don't make a copy of the string.
12987 res.cb_name = arg->vval.v_string;
12988 func_ref(res.cb_name);
12989 }
12990 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12991 {
12992 res.cb_name = (char_u *)"";
12993 }
12994 else
12995 {
12996 emsg(_("E921: Invalid callback argument"));
12997 res.cb_name = NULL;
12998 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013000 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013001}
13002
13003/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013004 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005 */
13006 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013007put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013009 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013011 tv->v_type = VAR_PARTIAL;
13012 tv->vval.v_partial = cb->cb_partial;
13013 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013015 else
13016 {
13017 tv->v_type = VAR_FUNC;
13018 tv->vval.v_string = vim_strsave(cb->cb_name);
13019 func_ref(cb->cb_name);
13020 }
13021}
13022
13023/*
13024 * Make a copy of "src" into "dest", allocating the function name if needed,
13025 * without incrementing the refcount.
13026 */
13027 void
13028set_callback(callback_T *dest, callback_T *src)
13029{
13030 if (src->cb_partial == NULL)
13031 {
13032 // just a function name, make a copy
13033 dest->cb_name = vim_strsave(src->cb_name);
13034 dest->cb_free_name = TRUE;
13035 }
13036 else
13037 {
13038 // cb_name is a pointer into cb_partial
13039 dest->cb_name = src->cb_name;
13040 dest->cb_free_name = FALSE;
13041 }
13042 dest->cb_partial = src->cb_partial;
13043}
13044
13045/*
13046 * Unref/free "callback" returned by get_callback() or set_callback().
13047 */
13048 void
13049free_callback(callback_T *callback)
13050{
13051 if (callback->cb_partial != NULL)
13052 {
13053 partial_unref(callback->cb_partial);
13054 callback->cb_partial = NULL;
13055 }
13056 else if (callback->cb_name != NULL)
13057 func_unref(callback->cb_name);
13058 if (callback->cb_free_name)
13059 {
13060 vim_free(callback->cb_name);
13061 callback->cb_free_name = FALSE;
13062 }
13063 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065
13066#ifdef FEAT_TIMERS
13067/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013068 * "timer_info([timer])" function
13069 */
13070 static void
13071f_timer_info(typval_T *argvars, typval_T *rettv)
13072{
13073 timer_T *timer = NULL;
13074
13075 if (rettv_list_alloc(rettv) != OK)
13076 return;
13077 if (argvars[0].v_type != VAR_UNKNOWN)
13078 {
13079 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013080 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013081 else
13082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013083 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013084 if (timer != NULL)
13085 add_timer_info(rettv, timer);
13086 }
13087 }
13088 else
13089 add_timer_info_all(rettv);
13090}
13091
13092/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013093 * "timer_pause(timer, paused)" function
13094 */
13095 static void
13096f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13097{
13098 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013099 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013100
13101 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013102 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013103 else
13104 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013105 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013106 if (timer != NULL)
13107 timer->tr_paused = paused;
13108 }
13109}
13110
13111/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112 * "timer_start(time, callback [, options])" function
13113 */
13114 static void
13115f_timer_start(typval_T *argvars, typval_T *rettv)
13116{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013117 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013118 timer_T *timer;
13119 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013120 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013121 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122
Bram Moolenaar75537a92016-09-05 22:45:28 +020013123 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124 if (check_secure())
13125 return;
13126 if (argvars[2].v_type != VAR_UNKNOWN)
13127 {
13128 if (argvars[2].v_type != VAR_DICT
13129 || (dict = argvars[2].vval.v_dict) == NULL)
13130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013131 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132 return;
13133 }
13134 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013135 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 }
13137
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013138 callback = get_callback(&argvars[1]);
13139 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013140 return;
13141
13142 timer = create_timer(msec, repeat);
13143 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013144 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 else
13146 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013147 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013148 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013149 }
13150}
13151
13152/*
13153 * "timer_stop(timer)" function
13154 */
13155 static void
13156f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13157{
13158 timer_T *timer;
13159
13160 if (argvars[0].v_type != VAR_NUMBER)
13161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013162 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 return;
13164 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013165 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 if (timer != NULL)
13167 stop_timer(timer);
13168}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013169
13170/*
13171 * "timer_stopall()" function
13172 */
13173 static void
13174f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13175{
13176 stop_all_timers();
13177}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013178#endif
13179
13180/*
13181 * "tolower(string)" function
13182 */
13183 static void
13184f_tolower(typval_T *argvars, typval_T *rettv)
13185{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013187 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188}
13189
13190/*
13191 * "toupper(string)" function
13192 */
13193 static void
13194f_toupper(typval_T *argvars, typval_T *rettv)
13195{
13196 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013197 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013198}
13199
13200/*
13201 * "tr(string, fromstr, tostr)" function
13202 */
13203 static void
13204f_tr(typval_T *argvars, typval_T *rettv)
13205{
13206 char_u *in_str;
13207 char_u *fromstr;
13208 char_u *tostr;
13209 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 int inlen;
13211 int fromlen;
13212 int tolen;
13213 int idx;
13214 char_u *cpstr;
13215 int cplen;
13216 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 char_u buf[NUMBUFLEN];
13218 char_u buf2[NUMBUFLEN];
13219 garray_T ga;
13220
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013221 in_str = tv_get_string(&argvars[0]);
13222 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13223 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013224
13225 /* Default return value: empty string. */
13226 rettv->v_type = VAR_STRING;
13227 rettv->vval.v_string = NULL;
13228 if (fromstr == NULL || tostr == NULL)
13229 return; /* type error; errmsg already given */
13230 ga_init2(&ga, (int)sizeof(char), 80);
13231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 /* not multi-byte: fromstr and tostr must be the same length */
13234 if (STRLEN(fromstr) != STRLEN(tostr))
13235 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013236error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013237 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238 ga_clear(&ga);
13239 return;
13240 }
13241
13242 /* fromstr and tostr have to contain the same number of chars */
13243 while (*in_str != NUL)
13244 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245 if (has_mbyte)
13246 {
13247 inlen = (*mb_ptr2len)(in_str);
13248 cpstr = in_str;
13249 cplen = inlen;
13250 idx = 0;
13251 for (p = fromstr; *p != NUL; p += fromlen)
13252 {
13253 fromlen = (*mb_ptr2len)(p);
13254 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13255 {
13256 for (p = tostr; *p != NUL; p += tolen)
13257 {
13258 tolen = (*mb_ptr2len)(p);
13259 if (idx-- == 0)
13260 {
13261 cplen = tolen;
13262 cpstr = p;
13263 break;
13264 }
13265 }
13266 if (*p == NUL) /* tostr is shorter than fromstr */
13267 goto error;
13268 break;
13269 }
13270 ++idx;
13271 }
13272
13273 if (first && cpstr == in_str)
13274 {
13275 /* Check that fromstr and tostr have the same number of
13276 * (multi-byte) characters. Done only once when a character
13277 * of in_str doesn't appear in fromstr. */
13278 first = FALSE;
13279 for (p = tostr; *p != NUL; p += tolen)
13280 {
13281 tolen = (*mb_ptr2len)(p);
13282 --idx;
13283 }
13284 if (idx != 0)
13285 goto error;
13286 }
13287
13288 (void)ga_grow(&ga, cplen);
13289 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13290 ga.ga_len += cplen;
13291
13292 in_str += inlen;
13293 }
13294 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 {
13296 /* When not using multi-byte chars we can do it faster. */
13297 p = vim_strchr(fromstr, *in_str);
13298 if (p != NULL)
13299 ga_append(&ga, tostr[p - fromstr]);
13300 else
13301 ga_append(&ga, *in_str);
13302 ++in_str;
13303 }
13304 }
13305
13306 /* add a terminating NUL */
13307 (void)ga_grow(&ga, 1);
13308 ga_append(&ga, NUL);
13309
13310 rettv->vval.v_string = ga.ga_data;
13311}
13312
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013313/*
13314 * "trim({expr})" function
13315 */
13316 static void
13317f_trim(typval_T *argvars, typval_T *rettv)
13318{
13319 char_u buf1[NUMBUFLEN];
13320 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013321 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013322 char_u *mask = NULL;
13323 char_u *tail;
13324 char_u *prev;
13325 char_u *p;
13326 int c1;
13327
13328 rettv->v_type = VAR_STRING;
13329 if (head == NULL)
13330 {
13331 rettv->vval.v_string = NULL;
13332 return;
13333 }
13334
13335 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013336 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013337
13338 while (*head != NUL)
13339 {
13340 c1 = PTR2CHAR(head);
13341 if (mask == NULL)
13342 {
13343 if (c1 > ' ' && c1 != 0xa0)
13344 break;
13345 }
13346 else
13347 {
13348 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13349 if (c1 == PTR2CHAR(p))
13350 break;
13351 if (*p == NUL)
13352 break;
13353 }
13354 MB_PTR_ADV(head);
13355 }
13356
13357 for (tail = head + STRLEN(head); tail > head; tail = prev)
13358 {
13359 prev = tail;
13360 MB_PTR_BACK(head, prev);
13361 c1 = PTR2CHAR(prev);
13362 if (mask == NULL)
13363 {
13364 if (c1 > ' ' && c1 != 0xa0)
13365 break;
13366 }
13367 else
13368 {
13369 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13370 if (c1 == PTR2CHAR(p))
13371 break;
13372 if (*p == NUL)
13373 break;
13374 }
13375 }
13376 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13377}
13378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013379#ifdef FEAT_FLOAT
13380/*
13381 * "trunc({float})" function
13382 */
13383 static void
13384f_trunc(typval_T *argvars, typval_T *rettv)
13385{
13386 float_T f = 0.0;
13387
13388 rettv->v_type = VAR_FLOAT;
13389 if (get_float_arg(argvars, &f) == OK)
13390 /* trunc() is not in C90, use floor() or ceil() instead. */
13391 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13392 else
13393 rettv->vval.v_float = 0.0;
13394}
13395#endif
13396
13397/*
13398 * "type(expr)" function
13399 */
13400 static void
13401f_type(typval_T *argvars, typval_T *rettv)
13402{
13403 int n = -1;
13404
13405 switch (argvars[0].v_type)
13406 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013407 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13408 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013410 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13411 case VAR_LIST: n = VAR_TYPE_LIST; break;
13412 case VAR_DICT: n = VAR_TYPE_DICT; break;
13413 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 case VAR_SPECIAL:
13415 if (argvars[0].vval.v_number == VVAL_FALSE
13416 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013417 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013419 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013421 case VAR_JOB: n = VAR_TYPE_JOB; break;
13422 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013423 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013425 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 n = -1;
13427 break;
13428 }
13429 rettv->vval.v_number = n;
13430}
13431
13432/*
13433 * "undofile(name)" function
13434 */
13435 static void
13436f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13437{
13438 rettv->v_type = VAR_STRING;
13439#ifdef FEAT_PERSISTENT_UNDO
13440 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442
13443 if (*fname == NUL)
13444 {
13445 /* If there is no file name there will be no undo file. */
13446 rettv->vval.v_string = NULL;
13447 }
13448 else
13449 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020013450 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451
13452 if (ffname != NULL)
13453 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13454 vim_free(ffname);
13455 }
13456 }
13457#else
13458 rettv->vval.v_string = NULL;
13459#endif
13460}
13461
13462/*
13463 * "undotree()" function
13464 */
13465 static void
13466f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13467{
13468 if (rettv_dict_alloc(rettv) == OK)
13469 {
13470 dict_T *dict = rettv->vval.v_dict;
13471 list_T *list;
13472
Bram Moolenaare0be1672018-07-08 16:50:37 +020013473 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13474 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13475 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13476 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13477 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13478 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013479
13480 list = list_alloc();
13481 if (list != NULL)
13482 {
13483 u_eval_tree(curbuf->b_u_oldhead, list);
13484 dict_add_list(dict, "entries", list);
13485 }
13486 }
13487}
13488
13489/*
13490 * "values(dict)" function
13491 */
13492 static void
13493f_values(typval_T *argvars, typval_T *rettv)
13494{
13495 dict_list(argvars, rettv, 1);
13496}
13497
13498/*
13499 * "virtcol(string)" function
13500 */
13501 static void
13502f_virtcol(typval_T *argvars, typval_T *rettv)
13503{
13504 colnr_T vcol = 0;
13505 pos_T *fp;
13506 int fnum = curbuf->b_fnum;
13507
13508 fp = var2fpos(&argvars[0], FALSE, &fnum);
13509 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13510 && fnum == curbuf->b_fnum)
13511 {
13512 getvvcol(curwin, fp, NULL, NULL, &vcol);
13513 ++vcol;
13514 }
13515
13516 rettv->vval.v_number = vcol;
13517}
13518
13519/*
13520 * "visualmode()" function
13521 */
13522 static void
13523f_visualmode(typval_T *argvars, typval_T *rettv)
13524{
13525 char_u str[2];
13526
13527 rettv->v_type = VAR_STRING;
13528 str[0] = curbuf->b_visual_mode_eval;
13529 str[1] = NUL;
13530 rettv->vval.v_string = vim_strsave(str);
13531
13532 /* A non-zero number or non-empty string argument: reset mode. */
13533 if (non_zero_arg(&argvars[0]))
13534 curbuf->b_visual_mode_eval = NUL;
13535}
13536
13537/*
13538 * "wildmenumode()" function
13539 */
13540 static void
13541f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13542{
13543#ifdef FEAT_WILDMENU
13544 if (wild_menu_showing)
13545 rettv->vval.v_number = 1;
13546#endif
13547}
13548
13549/*
13550 * "winbufnr(nr)" function
13551 */
13552 static void
13553f_winbufnr(typval_T *argvars, typval_T *rettv)
13554{
13555 win_T *wp;
13556
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013557 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558 if (wp == NULL)
13559 rettv->vval.v_number = -1;
13560 else
13561 rettv->vval.v_number = wp->w_buffer->b_fnum;
13562}
13563
13564/*
13565 * "wincol()" function
13566 */
13567 static void
13568f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13569{
13570 validate_cursor();
13571 rettv->vval.v_number = curwin->w_wcol + 1;
13572}
13573
13574/*
13575 * "winheight(nr)" function
13576 */
13577 static void
13578f_winheight(typval_T *argvars, typval_T *rettv)
13579{
13580 win_T *wp;
13581
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013582 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583 if (wp == NULL)
13584 rettv->vval.v_number = -1;
13585 else
13586 rettv->vval.v_number = wp->w_height;
13587}
13588
13589/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013590 * "winlayout()" function
13591 */
13592 static void
13593f_winlayout(typval_T *argvars, typval_T *rettv)
13594{
13595 tabpage_T *tp;
13596
13597 if (rettv_list_alloc(rettv) != OK)
13598 return;
13599
13600 if (argvars[0].v_type == VAR_UNKNOWN)
13601 tp = curtab;
13602 else
13603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013604 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013605 if (tp == NULL)
13606 return;
13607 }
13608
13609 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13610}
13611
13612/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613 * "winline()" function
13614 */
13615 static void
13616f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13617{
13618 validate_cursor();
13619 rettv->vval.v_number = curwin->w_wrow + 1;
13620}
13621
13622/*
13623 * "winnr()" function
13624 */
13625 static void
13626f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13627{
13628 int nr = 1;
13629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013630 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013631 rettv->vval.v_number = nr;
13632}
13633
13634/*
13635 * "winrestcmd()" function
13636 */
13637 static void
13638f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13639{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640 win_T *wp;
13641 int winnr = 1;
13642 garray_T ga;
13643 char_u buf[50];
13644
13645 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013646 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013647 {
13648 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13649 ga_concat(&ga, buf);
13650 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13651 ga_concat(&ga, buf);
13652 ++winnr;
13653 }
13654 ga_append(&ga, NUL);
13655
13656 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657 rettv->v_type = VAR_STRING;
13658}
13659
13660/*
13661 * "winrestview()" function
13662 */
13663 static void
13664f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13665{
13666 dict_T *dict;
13667
13668 if (argvars[0].v_type != VAR_DICT
13669 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013670 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 else
13672 {
13673 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013674 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013675 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013676 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013677 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013678 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13680 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010013681 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013682 curwin->w_set_curswant = FALSE;
13683 }
13684
13685 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013686 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013687#ifdef FEAT_DIFF
13688 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013689 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013690#endif
13691 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013692 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013694 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695
13696 check_cursor();
13697 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013698 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013699 changed_window_setting();
13700
13701 if (curwin->w_topline <= 0)
13702 curwin->w_topline = 1;
13703 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13704 curwin->w_topline = curbuf->b_ml.ml_line_count;
13705#ifdef FEAT_DIFF
13706 check_topfill(curwin, TRUE);
13707#endif
13708 }
13709}
13710
13711/*
13712 * "winsaveview()" function
13713 */
13714 static void
13715f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13716{
13717 dict_T *dict;
13718
13719 if (rettv_dict_alloc(rettv) == FAIL)
13720 return;
13721 dict = rettv->vval.v_dict;
13722
Bram Moolenaare0be1672018-07-08 16:50:37 +020013723 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13724 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013725 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013726 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013727 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013728
Bram Moolenaare0be1672018-07-08 16:50:37 +020013729 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013731 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013732#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013733 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13734 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013735}
13736
13737/*
13738 * "winwidth(nr)" function
13739 */
13740 static void
13741f_winwidth(typval_T *argvars, typval_T *rettv)
13742{
13743 win_T *wp;
13744
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013745 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013746 if (wp == NULL)
13747 rettv->vval.v_number = -1;
13748 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013749 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013750}
13751
13752/*
13753 * "wordcount()" function
13754 */
13755 static void
13756f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13757{
13758 if (rettv_dict_alloc(rettv) == FAIL)
13759 return;
13760 cursor_pos_info(rettv->vval.v_dict);
13761}
13762
13763/*
13764 * "writefile()" function
13765 */
13766 static void
13767f_writefile(typval_T *argvars, typval_T *rettv)
13768{
13769 int binary = FALSE;
13770 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013771#ifdef HAVE_FSYNC
13772 int do_fsync = p_fs;
13773#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013774 char_u *fname;
13775 FILE *fd;
13776 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013777 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013778 list_T *list = NULL;
13779 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013780
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013781 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013782 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013783 return;
13784
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013785 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013786 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013787 list = argvars[0].vval.v_list;
13788 if (list == NULL)
13789 return;
13790 for (li = list->lv_first; li != NULL; li = li->li_next)
13791 if (tv_get_string_chk(&li->li_tv) == NULL)
13792 return;
13793 }
13794 else if (argvars[0].v_type == VAR_BLOB)
13795 {
13796 blob = argvars[0].vval.v_blob;
13797 if (blob == NULL)
13798 return;
13799 }
13800 else
13801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013802 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013803 return;
13804 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805
13806 if (argvars[2].v_type != VAR_UNKNOWN)
13807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013808 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013809
13810 if (arg2 == NULL)
13811 return;
13812 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013813 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013814 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013815 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013816#ifdef HAVE_FSYNC
13817 if (vim_strchr(arg2, 's') != NULL)
13818 do_fsync = TRUE;
13819 else if (vim_strchr(arg2, 'S') != NULL)
13820 do_fsync = FALSE;
13821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013822 }
13823
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013824 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013825 if (fname == NULL)
13826 return;
13827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 /* Always open the file in binary mode, library functions have a mind of
13829 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013830 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13831 append ? APPENDBIN : WRITEBIN)) == NULL)
13832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013833 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013834 ret = -1;
13835 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013836 else if (blob)
13837 {
13838 if (write_blob(fd, blob) == FAIL)
13839 ret = -1;
13840#ifdef HAVE_FSYNC
13841 else if (do_fsync)
13842 // Ignore the error, the user wouldn't know what to do about it.
13843 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013844 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013845#endif
13846 fclose(fd);
13847 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013848 else
13849 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013850 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013851 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013852#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013853 else if (do_fsync)
13854 /* Ignore the error, the user wouldn't know what to do about it.
13855 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013856 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013857#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013858 fclose(fd);
13859 }
13860
13861 rettv->vval.v_number = ret;
13862}
13863
13864/*
13865 * "xor(expr, expr)" function
13866 */
13867 static void
13868f_xor(typval_T *argvars, typval_T *rettv)
13869{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013870 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13871 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872}
13873
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013874#endif /* FEAT_EVAL */