blob: c0b6788c292f2da5b01776e44dfdc2381ff8ae85 [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();
Bram Moolenaar29846662019-07-27 17:39:15 +02001404 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001405 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001406
1407 if (!is_curbuf)
1408 {
1409 curbuf = curbuf_save;
1410 curwin = curwin_save;
1411 }
1412}
1413
1414/*
1415 * "append(lnum, string/list)" function
1416 */
1417 static void
1418f_append(typval_T *argvars, typval_T *rettv)
1419{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001420 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001421
1422 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1423}
1424
1425/*
1426 * "appendbufline(buf, lnum, string/list)" function
1427 */
1428 static void
1429f_appendbufline(typval_T *argvars, typval_T *rettv)
1430{
1431 linenr_T lnum;
1432 buf_T *buf;
1433
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001434 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001435 if (buf == NULL)
1436 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001437 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001440 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1441 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001442}
1443
1444/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001445 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001446 */
1447 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001448f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001449{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001450 win_T *wp;
1451
1452 if (argvars[0].v_type == VAR_UNKNOWN)
1453 // use the current window
1454 rettv->vval.v_number = ARGCOUNT;
1455 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001456 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001457 // use the global argument list
1458 rettv->vval.v_number = GARGCOUNT;
1459 else
1460 {
1461 // use the argument list of the specified window
1462 wp = find_win_by_nr_or_id(&argvars[0]);
1463 if (wp != NULL)
1464 rettv->vval.v_number = WARGCOUNT(wp);
1465 else
1466 rettv->vval.v_number = -1;
1467 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468}
1469
1470/*
1471 * "argidx()" function
1472 */
1473 static void
1474f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1475{
1476 rettv->vval.v_number = curwin->w_arg_idx;
1477}
1478
1479/*
1480 * "arglistid()" function
1481 */
1482 static void
1483f_arglistid(typval_T *argvars, typval_T *rettv)
1484{
1485 win_T *wp;
1486
1487 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001488 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489 if (wp != NULL)
1490 rettv->vval.v_number = wp->w_alist->id;
1491}
1492
1493/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001494 * Get the argument list for a given window
1495 */
1496 static void
1497get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1498{
1499 int idx;
1500
1501 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1502 for (idx = 0; idx < argcount; ++idx)
1503 list_append_string(rettv->vval.v_list,
1504 alist_name(&arglist[idx]), -1);
1505}
1506
1507/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508 * "argv(nr)" function
1509 */
1510 static void
1511f_argv(typval_T *argvars, typval_T *rettv)
1512{
1513 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001514 aentry_T *arglist = NULL;
1515 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516
1517 if (argvars[0].v_type != VAR_UNKNOWN)
1518 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519 if (argvars[1].v_type == VAR_UNKNOWN)
1520 {
1521 arglist = ARGLIST;
1522 argcount = ARGCOUNT;
1523 }
1524 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001525 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001526 {
1527 arglist = GARGLIST;
1528 argcount = GARGCOUNT;
1529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001530 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001531 {
1532 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1533
1534 if (wp != NULL)
1535 {
1536 /* Use the argument list of the specified window */
1537 arglist = WARGLIST(wp);
1538 argcount = WARGCOUNT(wp);
1539 }
1540 }
1541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001543 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001545 if (arglist != NULL && idx >= 0 && idx < argcount)
1546 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1547 else if (idx == -1)
1548 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 else
1551 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552}
1553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554#ifdef FEAT_FLOAT
1555/*
1556 * "asin()" function
1557 */
1558 static void
1559f_asin(typval_T *argvars, typval_T *rettv)
1560{
1561 float_T f = 0.0;
1562
1563 rettv->v_type = VAR_FLOAT;
1564 if (get_float_arg(argvars, &f) == OK)
1565 rettv->vval.v_float = asin(f);
1566 else
1567 rettv->vval.v_float = 0.0;
1568}
1569
1570/*
1571 * "atan()" function
1572 */
1573 static void
1574f_atan(typval_T *argvars, typval_T *rettv)
1575{
1576 float_T f = 0.0;
1577
1578 rettv->v_type = VAR_FLOAT;
1579 if (get_float_arg(argvars, &f) == OK)
1580 rettv->vval.v_float = atan(f);
1581 else
1582 rettv->vval.v_float = 0.0;
1583}
1584
1585/*
1586 * "atan2()" function
1587 */
1588 static void
1589f_atan2(typval_T *argvars, typval_T *rettv)
1590{
1591 float_T fx = 0.0, fy = 0.0;
1592
1593 rettv->v_type = VAR_FLOAT;
1594 if (get_float_arg(argvars, &fx) == OK
1595 && get_float_arg(&argvars[1], &fy) == OK)
1596 rettv->vval.v_float = atan2(fx, fy);
1597 else
1598 rettv->vval.v_float = 0.0;
1599}
1600#endif
1601
1602/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001603 * "balloon_show()" function
1604 */
1605#ifdef FEAT_BEVAL
1606 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001607f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1608{
1609 rettv->v_type = VAR_STRING;
1610 if (balloonEval != NULL)
1611 {
1612 if (balloonEval->msg == NULL)
1613 rettv->vval.v_string = NULL;
1614 else
1615 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1616 }
1617}
1618
1619 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001620f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1621{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001622 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001623 {
1624 if (argvars[0].v_type == VAR_LIST
1625# ifdef FEAT_GUI
1626 && !gui.in_use
1627# endif
1628 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001629 {
1630 list_T *l = argvars[0].vval.v_list;
1631
1632 // empty list removes the balloon
1633 post_balloon(balloonEval, NULL,
1634 l == NULL || l->lv_len == 0 ? NULL : l);
1635 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001636 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001637 {
1638 char_u *mesg = tv_get_string_chk(&argvars[0]);
1639
1640 if (mesg != NULL)
1641 // empty string removes the balloon
1642 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1643 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001644 }
1645}
1646
Bram Moolenaar669a8282017-11-19 20:13:05 +01001647# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001648 static void
1649f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1650{
1651 if (rettv_list_alloc(rettv) == OK)
1652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001653 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001654
1655 if (msg != NULL)
1656 {
1657 pumitem_T *array;
1658 int size = split_message(msg, &array);
1659 int i;
1660
1661 /* Skip the first and last item, they are always empty. */
1662 for (i = 1; i < size - 1; ++i)
1663 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001664 while (size > 0)
1665 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001666 vim_free(array);
1667 }
1668 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001669}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001670# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001671#endif
1672
1673/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674 * "browse(save, title, initdir, default)" function
1675 */
1676 static void
1677f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1678{
1679#ifdef FEAT_BROWSE
1680 int save;
1681 char_u *title;
1682 char_u *initdir;
1683 char_u *defname;
1684 char_u buf[NUMBUFLEN];
1685 char_u buf2[NUMBUFLEN];
1686 int error = FALSE;
1687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001688 save = (int)tv_get_number_chk(&argvars[0], &error);
1689 title = tv_get_string_chk(&argvars[1]);
1690 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1691 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692
1693 if (error || title == NULL || initdir == NULL || defname == NULL)
1694 rettv->vval.v_string = NULL;
1695 else
1696 rettv->vval.v_string =
1697 do_browse(save ? BROWSE_SAVE : 0,
1698 title, defname, NULL, initdir, NULL, curbuf);
1699#else
1700 rettv->vval.v_string = NULL;
1701#endif
1702 rettv->v_type = VAR_STRING;
1703}
1704
1705/*
1706 * "browsedir(title, initdir)" function
1707 */
1708 static void
1709f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1710{
1711#ifdef FEAT_BROWSE
1712 char_u *title;
1713 char_u *initdir;
1714 char_u buf[NUMBUFLEN];
1715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001716 title = tv_get_string_chk(&argvars[0]);
1717 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718
1719 if (title == NULL || initdir == NULL)
1720 rettv->vval.v_string = NULL;
1721 else
1722 rettv->vval.v_string = do_browse(BROWSE_DIR,
1723 title, NULL, NULL, initdir, NULL, curbuf);
1724#else
1725 rettv->vval.v_string = NULL;
1726#endif
1727 rettv->v_type = VAR_STRING;
1728}
1729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730/*
1731 * Find a buffer by number or exact name.
1732 */
1733 static buf_T *
1734find_buffer(typval_T *avar)
1735{
1736 buf_T *buf = NULL;
1737
1738 if (avar->v_type == VAR_NUMBER)
1739 buf = buflist_findnr((int)avar->vval.v_number);
1740 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1741 {
1742 buf = buflist_findname_exp(avar->vval.v_string);
1743 if (buf == NULL)
1744 {
1745 /* No full path name match, try a match with a URL or a "nofile"
1746 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001747 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001748 if (buf->b_fname != NULL
1749 && (path_with_url(buf->b_fname)
1750#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001751 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001752#endif
1753 )
1754 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1755 break;
1756 }
1757 }
1758 return buf;
1759}
1760
1761/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001762 * "bufadd(expr)" function
1763 */
1764 static void
1765f_bufadd(typval_T *argvars, typval_T *rettv)
1766{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001767 char_u *name = tv_get_string(&argvars[0]);
1768
1769 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001770}
1771
1772/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001773 * "bufexists(expr)" function
1774 */
1775 static void
1776f_bufexists(typval_T *argvars, typval_T *rettv)
1777{
1778 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1779}
1780
1781/*
1782 * "buflisted(expr)" function
1783 */
1784 static void
1785f_buflisted(typval_T *argvars, typval_T *rettv)
1786{
1787 buf_T *buf;
1788
1789 buf = find_buffer(&argvars[0]);
1790 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1791}
1792
1793/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001794 * "bufload(expr)" function
1795 */
1796 static void
1797f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1798{
1799 buf_T *buf = get_buf_arg(&argvars[0]);
1800
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001801 if (buf != NULL)
1802 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001803}
1804
1805/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001806 * "bufloaded(expr)" function
1807 */
1808 static void
1809f_bufloaded(typval_T *argvars, typval_T *rettv)
1810{
1811 buf_T *buf;
1812
1813 buf = find_buffer(&argvars[0]);
1814 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1815}
1816
1817 buf_T *
1818buflist_find_by_name(char_u *name, int curtab_only)
1819{
1820 int save_magic;
1821 char_u *save_cpo;
1822 buf_T *buf;
1823
1824 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1825 save_magic = p_magic;
1826 p_magic = TRUE;
1827 save_cpo = p_cpo;
1828 p_cpo = (char_u *)"";
1829
1830 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1831 TRUE, FALSE, curtab_only));
1832
1833 p_magic = save_magic;
1834 p_cpo = save_cpo;
1835 return buf;
1836}
1837
1838/*
1839 * Get buffer by number or pattern.
1840 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001841 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001842tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001843{
1844 char_u *name = tv->vval.v_string;
1845 buf_T *buf;
1846
1847 if (tv->v_type == VAR_NUMBER)
1848 return buflist_findnr((int)tv->vval.v_number);
1849 if (tv->v_type != VAR_STRING)
1850 return NULL;
1851 if (name == NULL || *name == NUL)
1852 return curbuf;
1853 if (name[0] == '$' && name[1] == NUL)
1854 return lastbuf;
1855
1856 buf = buflist_find_by_name(name, curtab_only);
1857
1858 /* If not found, try expanding the name, like done for bufexists(). */
1859 if (buf == NULL)
1860 buf = find_buffer(tv);
1861
1862 return buf;
1863}
1864
1865/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001866 * Get the buffer from "arg" and give an error and return NULL if it is not
1867 * valid.
1868 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001869 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001870get_buf_arg(typval_T *arg)
1871{
1872 buf_T *buf;
1873
1874 ++emsg_off;
1875 buf = tv_get_buf(arg, FALSE);
1876 --emsg_off;
1877 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001878 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001879 return buf;
1880}
1881
1882/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 * "bufname(expr)" function
1884 */
1885 static void
1886f_bufname(typval_T *argvars, typval_T *rettv)
1887{
1888 buf_T *buf;
1889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001890 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001892 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001893 rettv->v_type = VAR_STRING;
1894 if (buf != NULL && buf->b_fname != NULL)
1895 rettv->vval.v_string = vim_strsave(buf->b_fname);
1896 else
1897 rettv->vval.v_string = NULL;
1898 --emsg_off;
1899}
1900
1901/*
1902 * "bufnr(expr)" function
1903 */
1904 static void
1905f_bufnr(typval_T *argvars, typval_T *rettv)
1906{
1907 buf_T *buf;
1908 int error = FALSE;
1909 char_u *name;
1910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001911 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001913 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001914 --emsg_off;
1915
1916 /* If the buffer isn't found and the second argument is not zero create a
1917 * new buffer. */
1918 if (buf == NULL
1919 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001920 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001922 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923 && !error)
1924 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1925
1926 if (buf != NULL)
1927 rettv->vval.v_number = buf->b_fnum;
1928 else
1929 rettv->vval.v_number = -1;
1930}
1931
1932 static void
1933buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1934{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 win_T *wp;
1936 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 buf_T *buf;
1938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001939 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001940 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001941 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001942 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943 {
1944 ++winnr;
1945 if (wp->w_buffer == buf)
1946 break;
1947 }
1948 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001949 --emsg_off;
1950}
1951
1952/*
1953 * "bufwinid(nr)" function
1954 */
1955 static void
1956f_bufwinid(typval_T *argvars, typval_T *rettv)
1957{
1958 buf_win_common(argvars, rettv, FALSE);
1959}
1960
1961/*
1962 * "bufwinnr(nr)" function
1963 */
1964 static void
1965f_bufwinnr(typval_T *argvars, typval_T *rettv)
1966{
1967 buf_win_common(argvars, rettv, TRUE);
1968}
1969
1970/*
1971 * "byte2line(byte)" function
1972 */
1973 static void
1974f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1975{
1976#ifndef FEAT_BYTEOFF
1977 rettv->vval.v_number = -1;
1978#else
1979 long boff = 0;
1980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001981 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001982 if (boff < 0)
1983 rettv->vval.v_number = -1;
1984 else
1985 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1986 (linenr_T)0, &boff);
1987#endif
1988}
1989
1990 static void
1991byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1992{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 char_u *str;
1995 varnumber_T idx;
1996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001997 str = tv_get_string_chk(&argvars[0]);
1998 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 rettv->vval.v_number = -1;
2000 if (str == NULL || idx < 0)
2001 return;
2002
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003 t = str;
2004 for ( ; idx > 0; idx--)
2005 {
2006 if (*t == NUL) /* EOL reached */
2007 return;
2008 if (enc_utf8 && comp)
2009 t += utf_ptr2len(t);
2010 else
2011 t += (*mb_ptr2len)(t);
2012 }
2013 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014}
2015
2016/*
2017 * "byteidx()" function
2018 */
2019 static void
2020f_byteidx(typval_T *argvars, typval_T *rettv)
2021{
2022 byteidx(argvars, rettv, FALSE);
2023}
2024
2025/*
2026 * "byteidxcomp()" function
2027 */
2028 static void
2029f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2030{
2031 byteidx(argvars, rettv, TRUE);
2032}
2033
2034/*
2035 * "call(func, arglist [, dict])" function
2036 */
2037 static void
2038f_call(typval_T *argvars, typval_T *rettv)
2039{
2040 char_u *func;
2041 partial_T *partial = NULL;
2042 dict_T *selfdict = NULL;
2043
2044 if (argvars[1].v_type != VAR_LIST)
2045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 return;
2048 }
2049 if (argvars[1].vval.v_list == NULL)
2050 return;
2051
2052 if (argvars[0].v_type == VAR_FUNC)
2053 func = argvars[0].vval.v_string;
2054 else if (argvars[0].v_type == VAR_PARTIAL)
2055 {
2056 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002057 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 }
2059 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002060 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 if (*func == NUL)
2062 return; /* type error or empty name */
2063
2064 if (argvars[2].v_type != VAR_UNKNOWN)
2065 {
2066 if (argvars[2].v_type != VAR_DICT)
2067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 return;
2070 }
2071 selfdict = argvars[2].vval.v_dict;
2072 }
2073
2074 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2075}
2076
2077#ifdef FEAT_FLOAT
2078/*
2079 * "ceil({float})" function
2080 */
2081 static void
2082f_ceil(typval_T *argvars, typval_T *rettv)
2083{
2084 float_T f = 0.0;
2085
2086 rettv->v_type = VAR_FLOAT;
2087 if (get_float_arg(argvars, &f) == OK)
2088 rettv->vval.v_float = ceil(f);
2089 else
2090 rettv->vval.v_float = 0.0;
2091}
2092#endif
2093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094/*
2095 * "changenr()" function
2096 */
2097 static void
2098f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2099{
2100 rettv->vval.v_number = curbuf->b_u_seq_cur;
2101}
2102
2103/*
2104 * "char2nr(string)" function
2105 */
2106 static void
2107f_char2nr(typval_T *argvars, typval_T *rettv)
2108{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 if (has_mbyte)
2110 {
2111 int utf8 = 0;
2112
2113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002114 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115
2116 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002117 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002118 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002119 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120 }
2121 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002122 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002123}
2124
2125/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002126 * "chdir(dir)" function
2127 */
2128 static void
2129f_chdir(typval_T *argvars, typval_T *rettv)
2130{
2131 char_u *cwd;
2132 cdscope_T scope = CDSCOPE_GLOBAL;
2133
2134 rettv->v_type = VAR_STRING;
2135 rettv->vval.v_string = NULL;
2136
2137 if (argvars[0].v_type != VAR_STRING)
2138 return;
2139
2140 // Return the current directory
2141 cwd = alloc(MAXPATHL);
2142 if (cwd != NULL)
2143 {
2144 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2145 {
2146#ifdef BACKSLASH_IN_FILENAME
2147 slash_adjust(cwd);
2148#endif
2149 rettv->vval.v_string = vim_strsave(cwd);
2150 }
2151 vim_free(cwd);
2152 }
2153
2154 if (curwin->w_localdir != NULL)
2155 scope = CDSCOPE_WINDOW;
2156 else if (curtab->tp_localdir != NULL)
2157 scope = CDSCOPE_TABPAGE;
2158
2159 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2160 // Directory change failed
2161 VIM_CLEAR(rettv->vval.v_string);
2162}
2163
2164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 * "cindent(lnum)" function
2166 */
2167 static void
2168f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2169{
2170#ifdef FEAT_CINDENT
2171 pos_T pos;
2172 linenr_T lnum;
2173
2174 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002175 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2177 {
2178 curwin->w_cursor.lnum = lnum;
2179 rettv->vval.v_number = get_c_indent();
2180 curwin->w_cursor = pos;
2181 }
2182 else
2183#endif
2184 rettv->vval.v_number = -1;
2185}
2186
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002187 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002188get_optional_window(typval_T *argvars, int idx)
2189{
2190 win_T *win = curwin;
2191
2192 if (argvars[idx].v_type != VAR_UNKNOWN)
2193 {
2194 win = find_win_by_nr_or_id(&argvars[idx]);
2195 if (win == NULL)
2196 {
2197 emsg(_(e_invalwindow));
2198 return NULL;
2199 }
2200 }
2201 return win;
2202}
2203
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002205 * "col(string)" function
2206 */
2207 static void
2208f_col(typval_T *argvars, typval_T *rettv)
2209{
2210 colnr_T col = 0;
2211 pos_T *fp;
2212 int fnum = curbuf->b_fnum;
2213
2214 fp = var2fpos(&argvars[0], FALSE, &fnum);
2215 if (fp != NULL && fnum == curbuf->b_fnum)
2216 {
2217 if (fp->col == MAXCOL)
2218 {
2219 /* '> can be MAXCOL, get the length of the line then */
2220 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2221 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2222 else
2223 col = MAXCOL;
2224 }
2225 else
2226 {
2227 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 /* col(".") when the cursor is on the NUL at the end of the line
2229 * because of "coladd" can be seen as an extra column. */
2230 if (virtual_active() && fp == &curwin->w_cursor)
2231 {
2232 char_u *p = ml_get_cursor();
2233
2234 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2235 curwin->w_virtcol - curwin->w_cursor.coladd))
2236 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237 int l;
2238
2239 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2240 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002241 }
2242 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 }
2244 }
2245 rettv->vval.v_number = col;
2246}
2247
2248#if defined(FEAT_INS_EXPAND)
2249/*
2250 * "complete()" function
2251 */
2252 static void
2253f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2254{
2255 int startcol;
2256
2257 if ((State & INSERT) == 0)
2258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002259 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002260 return;
2261 }
2262
2263 /* Check for undo allowed here, because if something was already inserted
2264 * the line was already saved for undo and this check isn't done. */
2265 if (!undo_allowed())
2266 return;
2267
2268 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002270 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 return;
2272 }
2273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002274 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275 if (startcol <= 0)
2276 return;
2277
2278 set_completion(startcol - 1, argvars[1].vval.v_list);
2279}
2280
2281/*
2282 * "complete_add()" function
2283 */
2284 static void
2285f_complete_add(typval_T *argvars, typval_T *rettv)
2286{
2287 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2288}
2289
2290/*
2291 * "complete_check()" function
2292 */
2293 static void
2294f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2295{
2296 int saved = RedrawingDisabled;
2297
2298 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002299 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002300 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301 RedrawingDisabled = saved;
2302}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002303
2304/*
2305 * "complete_info()" function
2306 */
2307 static void
2308f_complete_info(typval_T *argvars, typval_T *rettv)
2309{
2310 list_T *what_list = NULL;
2311
2312 if (rettv_dict_alloc(rettv) != OK)
2313 return;
2314
2315 if (argvars[0].v_type != VAR_UNKNOWN)
2316 {
2317 if (argvars[0].v_type != VAR_LIST)
2318 {
2319 emsg(_(e_listreq));
2320 return;
2321 }
2322 what_list = argvars[0].vval.v_list;
2323 }
2324 get_complete_info(what_list, rettv->vval.v_dict);
2325}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326#endif
2327
2328/*
2329 * "confirm(message, buttons[, default [, type]])" function
2330 */
2331 static void
2332f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2333{
2334#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2335 char_u *message;
2336 char_u *buttons = NULL;
2337 char_u buf[NUMBUFLEN];
2338 char_u buf2[NUMBUFLEN];
2339 int def = 1;
2340 int type = VIM_GENERIC;
2341 char_u *typestr;
2342 int error = FALSE;
2343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002344 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 if (message == NULL)
2346 error = TRUE;
2347 if (argvars[1].v_type != VAR_UNKNOWN)
2348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002349 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 if (buttons == NULL)
2351 error = TRUE;
2352 if (argvars[2].v_type != VAR_UNKNOWN)
2353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002354 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355 if (argvars[3].v_type != VAR_UNKNOWN)
2356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002357 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 if (typestr == NULL)
2359 error = TRUE;
2360 else
2361 {
2362 switch (TOUPPER_ASC(*typestr))
2363 {
2364 case 'E': type = VIM_ERROR; break;
2365 case 'Q': type = VIM_QUESTION; break;
2366 case 'I': type = VIM_INFO; break;
2367 case 'W': type = VIM_WARNING; break;
2368 case 'G': type = VIM_GENERIC; break;
2369 }
2370 }
2371 }
2372 }
2373 }
2374
2375 if (buttons == NULL || *buttons == NUL)
2376 buttons = (char_u *)_("&Ok");
2377
2378 if (!error)
2379 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2380 def, NULL, FALSE);
2381#endif
2382}
2383
2384/*
2385 * "copy()" function
2386 */
2387 static void
2388f_copy(typval_T *argvars, typval_T *rettv)
2389{
2390 item_copy(&argvars[0], rettv, FALSE, 0);
2391}
2392
2393#ifdef FEAT_FLOAT
2394/*
2395 * "cos()" function
2396 */
2397 static void
2398f_cos(typval_T *argvars, typval_T *rettv)
2399{
2400 float_T f = 0.0;
2401
2402 rettv->v_type = VAR_FLOAT;
2403 if (get_float_arg(argvars, &f) == OK)
2404 rettv->vval.v_float = cos(f);
2405 else
2406 rettv->vval.v_float = 0.0;
2407}
2408
2409/*
2410 * "cosh()" function
2411 */
2412 static void
2413f_cosh(typval_T *argvars, typval_T *rettv)
2414{
2415 float_T f = 0.0;
2416
2417 rettv->v_type = VAR_FLOAT;
2418 if (get_float_arg(argvars, &f) == OK)
2419 rettv->vval.v_float = cosh(f);
2420 else
2421 rettv->vval.v_float = 0.0;
2422}
2423#endif
2424
2425/*
2426 * "count()" function
2427 */
2428 static void
2429f_count(typval_T *argvars, typval_T *rettv)
2430{
2431 long n = 0;
2432 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002433 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002434
Bram Moolenaar9966b212017-07-28 16:46:57 +02002435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002436 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002437
2438 if (argvars[0].v_type == VAR_STRING)
2439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002440 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002441 char_u *p = argvars[0].vval.v_string;
2442 char_u *next;
2443
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002444 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002445 {
2446 if (ic)
2447 {
2448 size_t len = STRLEN(expr);
2449
2450 while (*p != NUL)
2451 {
2452 if (MB_STRNICMP(p, expr, len) == 0)
2453 {
2454 ++n;
2455 p += len;
2456 }
2457 else
2458 MB_PTR_ADV(p);
2459 }
2460 }
2461 else
2462 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2463 != NULL)
2464 {
2465 ++n;
2466 p = next + STRLEN(expr);
2467 }
2468 }
2469
2470 }
2471 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 {
2473 listitem_T *li;
2474 list_T *l;
2475 long idx;
2476
2477 if ((l = argvars[0].vval.v_list) != NULL)
2478 {
2479 li = l->lv_first;
2480 if (argvars[2].v_type != VAR_UNKNOWN)
2481 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002482 if (argvars[3].v_type != VAR_UNKNOWN)
2483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485 if (!error)
2486 {
2487 li = list_find(l, idx);
2488 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002489 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002490 }
2491 }
2492 if (error)
2493 li = NULL;
2494 }
2495
2496 for ( ; li != NULL; li = li->li_next)
2497 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2498 ++n;
2499 }
2500 }
2501 else if (argvars[0].v_type == VAR_DICT)
2502 {
2503 int todo;
2504 dict_T *d;
2505 hashitem_T *hi;
2506
2507 if ((d = argvars[0].vval.v_dict) != NULL)
2508 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509 if (argvars[2].v_type != VAR_UNKNOWN)
2510 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002512 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 }
2514
2515 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2516 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2517 {
2518 if (!HASHITEM_EMPTY(hi))
2519 {
2520 --todo;
2521 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2522 ++n;
2523 }
2524 }
2525 }
2526 }
2527 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 rettv->vval.v_number = n;
2530}
2531
2532/*
2533 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2534 *
2535 * Checks the existence of a cscope connection.
2536 */
2537 static void
2538f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2539{
2540#ifdef FEAT_CSCOPE
2541 int num = 0;
2542 char_u *dbpath = NULL;
2543 char_u *prepend = NULL;
2544 char_u buf[NUMBUFLEN];
2545
2546 if (argvars[0].v_type != VAR_UNKNOWN
2547 && argvars[1].v_type != VAR_UNKNOWN)
2548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002549 num = (int)tv_get_number(&argvars[0]);
2550 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002552 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002553 }
2554
2555 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2556#endif
2557}
2558
2559/*
2560 * "cursor(lnum, col)" function, or
2561 * "cursor(list)"
2562 *
2563 * Moves the cursor to the specified line and column.
2564 * Returns 0 when the position could be set, -1 otherwise.
2565 */
2566 static void
2567f_cursor(typval_T *argvars, typval_T *rettv)
2568{
2569 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002570 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 int set_curswant = TRUE;
2572
2573 rettv->vval.v_number = -1;
2574 if (argvars[1].v_type == VAR_UNKNOWN)
2575 {
2576 pos_T pos;
2577 colnr_T curswant = -1;
2578
2579 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 return;
2583 }
2584 line = pos.lnum;
2585 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002587 if (curswant >= 0)
2588 {
2589 curwin->w_curswant = curswant - 1;
2590 set_curswant = FALSE;
2591 }
2592 }
2593 else
2594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002595 line = tv_get_lnum(argvars);
2596 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002598 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002599 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002600 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002601 return; /* type error; errmsg already given */
2602 if (line > 0)
2603 curwin->w_cursor.lnum = line;
2604 if (col > 0)
2605 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607
2608 /* Make sure the cursor is in a valid position. */
2609 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 /* Correct cursor for multi-byte character. */
2611 if (has_mbyte)
2612 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002613
2614 curwin->w_set_curswant = set_curswant;
2615 rettv->vval.v_number = 0;
2616}
2617
Bram Moolenaar4f974752019-02-17 17:44:42 +01002618#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002619/*
2620 * "debugbreak()" function
2621 */
2622 static void
2623f_debugbreak(typval_T *argvars, typval_T *rettv)
2624{
2625 int pid;
2626
2627 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002629 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002631 else
2632 {
2633 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2634
2635 if (hProcess != NULL)
2636 {
2637 DebugBreakProcess(hProcess);
2638 CloseHandle(hProcess);
2639 rettv->vval.v_number = OK;
2640 }
2641 }
2642}
2643#endif
2644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002645/*
2646 * "deepcopy()" function
2647 */
2648 static void
2649f_deepcopy(typval_T *argvars, typval_T *rettv)
2650{
2651 int noref = 0;
2652 int copyID;
2653
2654 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002655 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658 else
2659 {
2660 copyID = get_copyID();
2661 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2662 }
2663}
2664
2665/*
2666 * "delete()" function
2667 */
2668 static void
2669f_delete(typval_T *argvars, typval_T *rettv)
2670{
2671 char_u nbuf[NUMBUFLEN];
2672 char_u *name;
2673 char_u *flags;
2674
2675 rettv->vval.v_number = -1;
2676 if (check_restricted() || check_secure())
2677 return;
2678
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002679 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 if (name == NULL || *name == NUL)
2681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002682 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 return;
2684 }
2685
2686 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 else
2689 flags = (char_u *)"";
2690
2691 if (*flags == NUL)
2692 /* delete a file */
2693 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2694 else if (STRCMP(flags, "d") == 0)
2695 /* delete an empty directory */
2696 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2697 else if (STRCMP(flags, "rf") == 0)
2698 /* delete a directory recursively */
2699 rettv->vval.v_number = delete_recursive(name);
2700 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002701 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702}
2703
2704/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002705 * "deletebufline()" function
2706 */
2707 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002708f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002709{
2710 buf_T *buf;
2711 linenr_T first, last;
2712 linenr_T lnum;
2713 long count;
2714 int is_curbuf;
2715 buf_T *curbuf_save = NULL;
2716 win_T *curwin_save = NULL;
2717 tabpage_T *tp;
2718 win_T *wp;
2719
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002720 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002721 if (buf == NULL)
2722 {
2723 rettv->vval.v_number = 1; /* FAIL */
2724 return;
2725 }
2726 is_curbuf = buf == curbuf;
2727
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002728 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002729 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002730 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002731 else
2732 last = first;
2733
2734 if (buf->b_ml.ml_mfp == NULL || first < 1
2735 || first > buf->b_ml.ml_line_count || last < first)
2736 {
2737 rettv->vval.v_number = 1; /* FAIL */
2738 return;
2739 }
2740
2741 if (!is_curbuf)
2742 {
2743 curbuf_save = curbuf;
2744 curwin_save = curwin;
2745 curbuf = buf;
2746 find_win_for_curbuf();
2747 }
2748 if (last > curbuf->b_ml.ml_line_count)
2749 last = curbuf->b_ml.ml_line_count;
2750 count = last - first + 1;
2751
2752 // When coming here from Insert mode, sync undo, so that this can be
2753 // undone separately from what was previously inserted.
2754 if (u_sync_once == 2)
2755 {
2756 u_sync_once = 1; // notify that u_sync() was called
2757 u_sync(TRUE);
2758 }
2759
2760 if (u_save(first - 1, last + 1) == FAIL)
2761 {
2762 rettv->vval.v_number = 1; /* FAIL */
2763 return;
2764 }
2765
2766 for (lnum = first; lnum <= last; ++lnum)
2767 ml_delete(first, TRUE);
2768
2769 FOR_ALL_TAB_WINDOWS(tp, wp)
2770 if (wp->w_buffer == buf)
2771 {
2772 if (wp->w_cursor.lnum > last)
2773 wp->w_cursor.lnum -= count;
2774 else if (wp->w_cursor.lnum> first)
2775 wp->w_cursor.lnum = first;
2776 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2777 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2778 }
2779 check_cursor_col();
2780 deleted_lines_mark(first, count);
2781
2782 if (!is_curbuf)
2783 {
2784 curbuf = curbuf_save;
2785 curwin = curwin_save;
2786 }
2787}
2788
2789/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 * "did_filetype()" function
2791 */
2792 static void
2793f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2794{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796}
2797
2798/*
2799 * "diff_filler()" function
2800 */
2801 static void
2802f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2803{
2804#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002805 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806#endif
2807}
2808
2809/*
2810 * "diff_hlID()" function
2811 */
2812 static void
2813f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2814{
2815#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002816 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002818 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 static int fnum = 0;
2820 static int change_start = 0;
2821 static int change_end = 0;
2822 static hlf_T hlID = (hlf_T)0;
2823 int filler_lines;
2824 int col;
2825
2826 if (lnum < 0) /* ignore type error in {lnum} arg */
2827 lnum = 0;
2828 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002829 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002830 || fnum != curbuf->b_fnum)
2831 {
2832 /* New line, buffer, change: need to get the values. */
2833 filler_lines = diff_check(curwin, lnum);
2834 if (filler_lines < 0)
2835 {
2836 if (filler_lines == -1)
2837 {
2838 change_start = MAXCOL;
2839 change_end = -1;
2840 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2841 hlID = HLF_ADD; /* added line */
2842 else
2843 hlID = HLF_CHD; /* changed line */
2844 }
2845 else
2846 hlID = HLF_ADD; /* added line */
2847 }
2848 else
2849 hlID = (hlf_T)0;
2850 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002851 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 fnum = curbuf->b_fnum;
2853 }
2854
2855 if (hlID == HLF_CHD || hlID == HLF_TXD)
2856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002857 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 if (col >= change_start && col <= change_end)
2859 hlID = HLF_TXD; /* changed text */
2860 else
2861 hlID = HLF_CHD; /* changed line */
2862 }
2863 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2864#endif
2865}
2866
2867/*
2868 * "empty({expr})" function
2869 */
2870 static void
2871f_empty(typval_T *argvars, typval_T *rettv)
2872{
2873 int n = FALSE;
2874
2875 switch (argvars[0].v_type)
2876 {
2877 case VAR_STRING:
2878 case VAR_FUNC:
2879 n = argvars[0].vval.v_string == NULL
2880 || *argvars[0].vval.v_string == NUL;
2881 break;
2882 case VAR_PARTIAL:
2883 n = FALSE;
2884 break;
2885 case VAR_NUMBER:
2886 n = argvars[0].vval.v_number == 0;
2887 break;
2888 case VAR_FLOAT:
2889#ifdef FEAT_FLOAT
2890 n = argvars[0].vval.v_float == 0.0;
2891 break;
2892#endif
2893 case VAR_LIST:
2894 n = argvars[0].vval.v_list == NULL
2895 || argvars[0].vval.v_list->lv_first == NULL;
2896 break;
2897 case VAR_DICT:
2898 n = argvars[0].vval.v_dict == NULL
2899 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2900 break;
2901 case VAR_SPECIAL:
2902 n = argvars[0].vval.v_number != VVAL_TRUE;
2903 break;
2904
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002905 case VAR_BLOB:
2906 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002907 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2908 break;
2909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910 case VAR_JOB:
2911#ifdef FEAT_JOB_CHANNEL
2912 n = argvars[0].vval.v_job == NULL
2913 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2914 break;
2915#endif
2916 case VAR_CHANNEL:
2917#ifdef FEAT_JOB_CHANNEL
2918 n = argvars[0].vval.v_channel == NULL
2919 || !channel_is_open(argvars[0].vval.v_channel);
2920 break;
2921#endif
2922 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002923 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 n = TRUE;
2925 break;
2926 }
2927
2928 rettv->vval.v_number = n;
2929}
2930
2931/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002932 * "environ()" function
2933 */
2934 static void
2935f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2936{
2937#if !defined(AMIGA)
2938 int i = 0;
2939 char_u *entry, *value;
2940# ifdef MSWIN
2941 extern wchar_t **_wenviron;
2942# else
2943 extern char **environ;
2944# endif
2945
2946 if (rettv_dict_alloc(rettv) != OK)
2947 return;
2948
2949# ifdef MSWIN
2950 if (*_wenviron == NULL)
2951 return;
2952# else
2953 if (*environ == NULL)
2954 return;
2955# endif
2956
2957 for (i = 0; ; ++i)
2958 {
2959# ifdef MSWIN
2960 short_u *p;
2961
2962 if ((p = (short_u *)_wenviron[i]) == NULL)
2963 return;
2964 entry = utf16_to_enc(p, NULL);
2965# else
2966 if ((entry = (char_u *)environ[i]) == NULL)
2967 return;
2968 entry = vim_strsave(entry);
2969# endif
2970 if (entry == NULL) // out of memory
2971 return;
2972 if ((value = vim_strchr(entry, '=')) == NULL)
2973 {
2974 vim_free(entry);
2975 continue;
2976 }
2977 *value++ = NUL;
2978 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2979 vim_free(entry);
2980 }
2981#endif
2982}
2983
2984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 * "escape({string}, {chars})" function
2986 */
2987 static void
2988f_escape(typval_T *argvars, typval_T *rettv)
2989{
2990 char_u buf[NUMBUFLEN];
2991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2993 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 rettv->v_type = VAR_STRING;
2995}
2996
2997/*
2998 * "eval()" function
2999 */
3000 static void
3001f_eval(typval_T *argvars, typval_T *rettv)
3002{
3003 char_u *s, *p;
3004
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003005 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 if (s != NULL)
3007 s = skipwhite(s);
3008
3009 p = s;
3010 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3011 {
3012 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 need_clr_eos = FALSE;
3015 rettv->v_type = VAR_NUMBER;
3016 rettv->vval.v_number = 0;
3017 }
3018 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020}
3021
3022/*
3023 * "eventhandler()" function
3024 */
3025 static void
3026f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3027{
3028 rettv->vval.v_number = vgetc_busy;
3029}
3030
3031/*
3032 * "executable()" function
3033 */
3034 static void
3035f_executable(typval_T *argvars, typval_T *rettv)
3036{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038
3039 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003040 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041}
3042
3043static garray_T redir_execute_ga;
3044
3045/*
3046 * Append "value[value_len]" to the execute() output.
3047 */
3048 void
3049execute_redir_str(char_u *value, int value_len)
3050{
3051 int len;
3052
3053 if (value_len == -1)
3054 len = (int)STRLEN(value); /* Append the entire string */
3055 else
3056 len = value_len; /* Append only "value_len" characters */
3057 if (ga_grow(&redir_execute_ga, len) == OK)
3058 {
3059 mch_memmove((char *)redir_execute_ga.ga_data
3060 + redir_execute_ga.ga_len, value, len);
3061 redir_execute_ga.ga_len += len;
3062 }
3063}
3064
3065/*
3066 * Get next line from a list.
3067 * Called by do_cmdline() to get the next line.
3068 * Returns allocated string, or NULL for end of function.
3069 */
3070
3071 static char_u *
3072get_list_line(
3073 int c UNUSED,
3074 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003075 int indent UNUSED,
3076 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077{
3078 listitem_T **p = (listitem_T **)cookie;
3079 listitem_T *item = *p;
3080 char_u buf[NUMBUFLEN];
3081 char_u *s;
3082
3083 if (item == NULL)
3084 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 *p = item->li_next;
3087 return s == NULL ? NULL : vim_strsave(s);
3088}
3089
3090/*
3091 * "execute()" function
3092 */
3093 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003094execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095{
3096 char_u *cmd = NULL;
3097 list_T *list = NULL;
3098 int save_msg_silent = msg_silent;
3099 int save_emsg_silent = emsg_silent;
3100 int save_emsg_noredir = emsg_noredir;
3101 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003102 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003104 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003105 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106
3107 rettv->vval.v_string = NULL;
3108 rettv->v_type = VAR_STRING;
3109
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003110 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003112 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 if (list == NULL || list->lv_first == NULL)
3114 /* empty list, no commands, empty output */
3115 return;
3116 ++list->lv_refcount;
3117 }
3118 else
3119 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003120 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 if (cmd == NULL)
3122 return;
3123 }
3124
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003125 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 {
3127 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003128 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129
3130 if (s == NULL)
3131 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003132 if (*s == NUL)
3133 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 if (STRNCMP(s, "silent", 6) == 0)
3135 ++msg_silent;
3136 if (STRCMP(s, "silent!") == 0)
3137 {
3138 emsg_silent = TRUE;
3139 emsg_noredir = TRUE;
3140 }
3141 }
3142 else
3143 ++msg_silent;
3144
3145 if (redir_execute)
3146 save_ga = redir_execute_ga;
3147 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3148 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003149 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003150 if (!echo_output)
3151 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152
3153 if (cmd != NULL)
3154 do_cmdline_cmd(cmd);
3155 else
3156 {
3157 listitem_T *item = list->lv_first;
3158
3159 do_cmdline(NULL, get_list_line, (void *)&item,
3160 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3161 --list->lv_refcount;
3162 }
3163
Bram Moolenaard297f352017-01-29 20:31:21 +01003164 /* Need to append a NUL to the result. */
3165 if (ga_grow(&redir_execute_ga, 1) == OK)
3166 {
3167 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3168 rettv->vval.v_string = redir_execute_ga.ga_data;
3169 }
3170 else
3171 {
3172 ga_clear(&redir_execute_ga);
3173 rettv->vval.v_string = NULL;
3174 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 msg_silent = save_msg_silent;
3176 emsg_silent = save_emsg_silent;
3177 emsg_noredir = save_emsg_noredir;
3178
3179 redir_execute = save_redir_execute;
3180 if (redir_execute)
3181 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003182 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003183
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003184 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003185 if (echo_output)
3186 // When not working silently: put it in column zero. A following
3187 // "echon" will overwrite the message, unavoidably.
3188 msg_col = 0;
3189 else
3190 // When working silently: Put it back where it was, since nothing
3191 // should have been written.
3192 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193}
3194
3195/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003196 * "execute()" function
3197 */
3198 static void
3199f_execute(typval_T *argvars, typval_T *rettv)
3200{
3201 execute_common(argvars, rettv, 0);
3202}
3203
3204/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205 * "exepath()" function
3206 */
3207 static void
3208f_exepath(typval_T *argvars, typval_T *rettv)
3209{
3210 char_u *p = NULL;
3211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003212 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 rettv->v_type = VAR_STRING;
3214 rettv->vval.v_string = p;
3215}
3216
3217/*
3218 * "exists()" function
3219 */
3220 static void
3221f_exists(typval_T *argvars, typval_T *rettv)
3222{
3223 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003226 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003227 if (*p == '$') /* environment variable */
3228 {
3229 /* first try "normal" environment variables (fast) */
3230 if (mch_getenv(p + 1) != NULL)
3231 n = TRUE;
3232 else
3233 {
3234 /* try expanding things like $VIM and ${HOME} */
3235 p = expand_env_save(p);
3236 if (p != NULL && *p != '$')
3237 n = TRUE;
3238 vim_free(p);
3239 }
3240 }
3241 else if (*p == '&' || *p == '+') /* option */
3242 {
3243 n = (get_option_tv(&p, NULL, TRUE) == OK);
3244 if (*skipwhite(p) != NUL)
3245 n = FALSE; /* trailing garbage */
3246 }
3247 else if (*p == '*') /* internal or user defined function */
3248 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003249 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 }
3251 else if (*p == ':')
3252 {
3253 n = cmd_exists(p + 1);
3254 }
3255 else if (*p == '#')
3256 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 if (p[1] == '#')
3258 n = autocmd_supported(p + 2);
3259 else
3260 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 }
3262 else /* internal variable */
3263 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003264 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 }
3266
3267 rettv->vval.v_number = n;
3268}
3269
3270#ifdef FEAT_FLOAT
3271/*
3272 * "exp()" function
3273 */
3274 static void
3275f_exp(typval_T *argvars, typval_T *rettv)
3276{
3277 float_T f = 0.0;
3278
3279 rettv->v_type = VAR_FLOAT;
3280 if (get_float_arg(argvars, &f) == OK)
3281 rettv->vval.v_float = exp(f);
3282 else
3283 rettv->vval.v_float = 0.0;
3284}
3285#endif
3286
3287/*
3288 * "expand()" function
3289 */
3290 static void
3291f_expand(typval_T *argvars, typval_T *rettv)
3292{
3293 char_u *s;
3294 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003295 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3297 expand_T xpc;
3298 int error = FALSE;
3299 char_u *result;
3300
3301 rettv->v_type = VAR_STRING;
3302 if (argvars[1].v_type != VAR_UNKNOWN
3303 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003304 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003306 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003308 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309 if (*s == '%' || *s == '#' || *s == '<')
3310 {
3311 ++emsg_off;
3312 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3313 --emsg_off;
3314 if (rettv->v_type == VAR_LIST)
3315 {
3316 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3317 list_append_string(rettv->vval.v_list, result, -1);
3318 else
3319 vim_free(result);
3320 }
3321 else
3322 rettv->vval.v_string = result;
3323 }
3324 else
3325 {
3326 /* When the optional second argument is non-zero, don't remove matches
3327 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3328 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003329 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 options |= WILD_KEEP_ALL;
3331 if (!error)
3332 {
3333 ExpandInit(&xpc);
3334 xpc.xp_context = EXPAND_FILES;
3335 if (p_wic)
3336 options += WILD_ICASE;
3337 if (rettv->v_type == VAR_STRING)
3338 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3339 options, WILD_ALL);
3340 else if (rettv_list_alloc(rettv) != FAIL)
3341 {
3342 int i;
3343
3344 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3345 for (i = 0; i < xpc.xp_numfiles; i++)
3346 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3347 ExpandCleanup(&xpc);
3348 }
3349 }
3350 else
3351 rettv->vval.v_string = NULL;
3352 }
3353}
3354
3355/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003356 * "expandcmd()" function
3357 * Expand all the special characters in a command string.
3358 */
3359 static void
3360f_expandcmd(typval_T *argvars, typval_T *rettv)
3361{
3362 exarg_T eap;
3363 char_u *cmdstr;
3364 char *errormsg = NULL;
3365
3366 rettv->v_type = VAR_STRING;
3367 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3368
3369 memset(&eap, 0, sizeof(eap));
3370 eap.cmd = cmdstr;
3371 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003372 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003373 eap.usefilter = FALSE;
3374 eap.nextcmd = NULL;
3375 eap.cmdidx = CMD_USER;
3376
3377 expand_filename(&eap, &cmdstr, &errormsg);
3378 if (errormsg != NULL && *errormsg != NUL)
3379 emsg(errormsg);
3380
3381 rettv->vval.v_string = cmdstr;
3382}
3383
3384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003385 * "extend(list, list [, idx])" function
3386 * "extend(dict, dict [, action])" function
3387 */
3388 static void
3389f_extend(typval_T *argvars, typval_T *rettv)
3390{
3391 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3392
3393 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3394 {
3395 list_T *l1, *l2;
3396 listitem_T *item;
3397 long before;
3398 int error = FALSE;
3399
3400 l1 = argvars[0].vval.v_list;
3401 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003402 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 && l2 != NULL)
3404 {
3405 if (argvars[2].v_type != VAR_UNKNOWN)
3406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003407 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003408 if (error)
3409 return; /* type error; errmsg already given */
3410
3411 if (before == l1->lv_len)
3412 item = NULL;
3413 else
3414 {
3415 item = list_find(l1, before);
3416 if (item == NULL)
3417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003418 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003419 return;
3420 }
3421 }
3422 }
3423 else
3424 item = NULL;
3425 list_extend(l1, l2, item);
3426
3427 copy_tv(&argvars[0], rettv);
3428 }
3429 }
3430 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3431 {
3432 dict_T *d1, *d2;
3433 char_u *action;
3434 int i;
3435
3436 d1 = argvars[0].vval.v_dict;
3437 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003438 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 && d2 != NULL)
3440 {
3441 /* Check the third argument. */
3442 if (argvars[2].v_type != VAR_UNKNOWN)
3443 {
3444 static char *(av[]) = {"keep", "force", "error"};
3445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003446 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447 if (action == NULL)
3448 return; /* type error; errmsg already given */
3449 for (i = 0; i < 3; ++i)
3450 if (STRCMP(action, av[i]) == 0)
3451 break;
3452 if (i == 3)
3453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 return;
3456 }
3457 }
3458 else
3459 action = (char_u *)"force";
3460
3461 dict_extend(d1, d2, action);
3462
3463 copy_tv(&argvars[0], rettv);
3464 }
3465 }
3466 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003467 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468}
3469
3470/*
3471 * "feedkeys()" function
3472 */
3473 static void
3474f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3475{
3476 int remap = TRUE;
3477 int insert = FALSE;
3478 char_u *keys, *flags;
3479 char_u nbuf[NUMBUFLEN];
3480 int typed = FALSE;
3481 int execute = FALSE;
3482 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003483 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003484 char_u *keys_esc;
3485
3486 /* This is not allowed in the sandbox. If the commands would still be
3487 * executed in the sandbox it would be OK, but it probably happens later,
3488 * when "sandbox" is no longer set. */
3489 if (check_secure())
3490 return;
3491
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003492 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493
3494 if (argvars[1].v_type != VAR_UNKNOWN)
3495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003496 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003497 for ( ; *flags != NUL; ++flags)
3498 {
3499 switch (*flags)
3500 {
3501 case 'n': remap = FALSE; break;
3502 case 'm': remap = TRUE; break;
3503 case 't': typed = TRUE; break;
3504 case 'i': insert = TRUE; break;
3505 case 'x': execute = TRUE; break;
3506 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003507 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508 }
3509 }
3510 }
3511
3512 if (*keys != NUL || execute)
3513 {
3514 /* Need to escape K_SPECIAL and CSI before putting the string in the
3515 * typeahead buffer. */
3516 keys_esc = vim_strsave_escape_csi(keys);
3517 if (keys_esc != NULL)
3518 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003519 if (lowlevel)
3520 {
3521#ifdef USE_INPUT_BUF
3522 add_to_input_buf(keys, (int)STRLEN(keys));
3523#else
3524 emsg(_("E980: lowlevel input not supported"));
3525#endif
3526 }
3527 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003528 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003529 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003531 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003532#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003533 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003534#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003535 )
3536 typebuf_was_filled = TRUE;
3537 }
3538 vim_free(keys_esc);
3539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 if (execute)
3541 {
3542 int save_msg_scroll = msg_scroll;
3543
3544 /* Avoid a 1 second delay when the keys start Insert mode. */
3545 msg_scroll = FALSE;
3546
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003547 if (!dangerous)
3548 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003549 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003550 if (!dangerous)
3551 --ex_normal_busy;
3552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 msg_scroll |= save_msg_scroll;
3554 }
3555 }
3556 }
3557}
3558
3559/*
3560 * "filereadable()" function
3561 */
3562 static void
3563f_filereadable(typval_T *argvars, typval_T *rettv)
3564{
3565 int fd;
3566 char_u *p;
3567 int n;
3568
3569#ifndef O_NONBLOCK
3570# define O_NONBLOCK 0
3571#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003572 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3574 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3575 {
3576 n = TRUE;
3577 close(fd);
3578 }
3579 else
3580 n = FALSE;
3581
3582 rettv->vval.v_number = n;
3583}
3584
3585/*
3586 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3587 * rights to write into.
3588 */
3589 static void
3590f_filewritable(typval_T *argvars, typval_T *rettv)
3591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003592 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593}
3594
3595 static void
3596findfilendir(
3597 typval_T *argvars UNUSED,
3598 typval_T *rettv,
3599 int find_what UNUSED)
3600{
3601#ifdef FEAT_SEARCHPATH
3602 char_u *fname;
3603 char_u *fresult = NULL;
3604 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3605 char_u *p;
3606 char_u pathbuf[NUMBUFLEN];
3607 int count = 1;
3608 int first = TRUE;
3609 int error = FALSE;
3610#endif
3611
3612 rettv->vval.v_string = NULL;
3613 rettv->v_type = VAR_STRING;
3614
3615#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003616 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617
3618 if (argvars[1].v_type != VAR_UNKNOWN)
3619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003620 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621 if (p == NULL)
3622 error = TRUE;
3623 else
3624 {
3625 if (*p != NUL)
3626 path = p;
3627
3628 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003629 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630 }
3631 }
3632
3633 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3634 error = TRUE;
3635
3636 if (*fname != NUL && !error)
3637 {
3638 do
3639 {
3640 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3641 vim_free(fresult);
3642 fresult = find_file_in_path_option(first ? fname : NULL,
3643 first ? (int)STRLEN(fname) : 0,
3644 0, first, path,
3645 find_what,
3646 curbuf->b_ffname,
3647 find_what == FINDFILE_DIR
3648 ? (char_u *)"" : curbuf->b_p_sua);
3649 first = FALSE;
3650
3651 if (fresult != NULL && rettv->v_type == VAR_LIST)
3652 list_append_string(rettv->vval.v_list, fresult, -1);
3653
3654 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3655 }
3656
3657 if (rettv->v_type == VAR_STRING)
3658 rettv->vval.v_string = fresult;
3659#endif
3660}
3661
3662/*
3663 * "filter()" function
3664 */
3665 static void
3666f_filter(typval_T *argvars, typval_T *rettv)
3667{
3668 filter_map(argvars, rettv, FALSE);
3669}
3670
3671/*
3672 * "finddir({fname}[, {path}[, {count}]])" function
3673 */
3674 static void
3675f_finddir(typval_T *argvars, typval_T *rettv)
3676{
3677 findfilendir(argvars, rettv, FINDFILE_DIR);
3678}
3679
3680/*
3681 * "findfile({fname}[, {path}[, {count}]])" function
3682 */
3683 static void
3684f_findfile(typval_T *argvars, typval_T *rettv)
3685{
3686 findfilendir(argvars, rettv, FINDFILE_FILE);
3687}
3688
3689#ifdef FEAT_FLOAT
3690/*
3691 * "float2nr({float})" function
3692 */
3693 static void
3694f_float2nr(typval_T *argvars, typval_T *rettv)
3695{
3696 float_T f = 0.0;
3697
3698 if (get_float_arg(argvars, &f) == OK)
3699 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003700 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003701 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003702 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003703 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 else
3705 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707}
3708
3709/*
3710 * "floor({float})" function
3711 */
3712 static void
3713f_floor(typval_T *argvars, typval_T *rettv)
3714{
3715 float_T f = 0.0;
3716
3717 rettv->v_type = VAR_FLOAT;
3718 if (get_float_arg(argvars, &f) == OK)
3719 rettv->vval.v_float = floor(f);
3720 else
3721 rettv->vval.v_float = 0.0;
3722}
3723
3724/*
3725 * "fmod()" function
3726 */
3727 static void
3728f_fmod(typval_T *argvars, typval_T *rettv)
3729{
3730 float_T fx = 0.0, fy = 0.0;
3731
3732 rettv->v_type = VAR_FLOAT;
3733 if (get_float_arg(argvars, &fx) == OK
3734 && get_float_arg(&argvars[1], &fy) == OK)
3735 rettv->vval.v_float = fmod(fx, fy);
3736 else
3737 rettv->vval.v_float = 0.0;
3738}
3739#endif
3740
3741/*
3742 * "fnameescape({string})" function
3743 */
3744 static void
3745f_fnameescape(typval_T *argvars, typval_T *rettv)
3746{
3747 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 rettv->v_type = VAR_STRING;
3750}
3751
3752/*
3753 * "fnamemodify({fname}, {mods})" function
3754 */
3755 static void
3756f_fnamemodify(typval_T *argvars, typval_T *rettv)
3757{
3758 char_u *fname;
3759 char_u *mods;
3760 int usedlen = 0;
3761 int len;
3762 char_u *fbuf = NULL;
3763 char_u buf[NUMBUFLEN];
3764
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003765 fname = tv_get_string_chk(&argvars[0]);
3766 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 if (fname == NULL || mods == NULL)
3768 fname = NULL;
3769 else
3770 {
3771 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003772 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773 }
3774
3775 rettv->v_type = VAR_STRING;
3776 if (fname == NULL)
3777 rettv->vval.v_string = NULL;
3778 else
3779 rettv->vval.v_string = vim_strnsave(fname, len);
3780 vim_free(fbuf);
3781}
3782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003783/*
3784 * "foldclosed()" function
3785 */
3786 static void
3787foldclosed_both(
3788 typval_T *argvars UNUSED,
3789 typval_T *rettv,
3790 int end UNUSED)
3791{
3792#ifdef FEAT_FOLDING
3793 linenr_T lnum;
3794 linenr_T first, last;
3795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3798 {
3799 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3800 {
3801 if (end)
3802 rettv->vval.v_number = (varnumber_T)last;
3803 else
3804 rettv->vval.v_number = (varnumber_T)first;
3805 return;
3806 }
3807 }
3808#endif
3809 rettv->vval.v_number = -1;
3810}
3811
3812/*
3813 * "foldclosed()" function
3814 */
3815 static void
3816f_foldclosed(typval_T *argvars, typval_T *rettv)
3817{
3818 foldclosed_both(argvars, rettv, FALSE);
3819}
3820
3821/*
3822 * "foldclosedend()" function
3823 */
3824 static void
3825f_foldclosedend(typval_T *argvars, typval_T *rettv)
3826{
3827 foldclosed_both(argvars, rettv, TRUE);
3828}
3829
3830/*
3831 * "foldlevel()" function
3832 */
3833 static void
3834f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3835{
3836#ifdef FEAT_FOLDING
3837 linenr_T lnum;
3838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003839 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003840 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3841 rettv->vval.v_number = foldLevel(lnum);
3842#endif
3843}
3844
3845/*
3846 * "foldtext()" function
3847 */
3848 static void
3849f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3850{
3851#ifdef FEAT_FOLDING
3852 linenr_T foldstart;
3853 linenr_T foldend;
3854 char_u *dashes;
3855 linenr_T lnum;
3856 char_u *s;
3857 char_u *r;
3858 int len;
3859 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003860 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003861#endif
3862
3863 rettv->v_type = VAR_STRING;
3864 rettv->vval.v_string = NULL;
3865#ifdef FEAT_FOLDING
3866 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3867 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3868 dashes = get_vim_var_str(VV_FOLDDASHES);
3869 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3870 && dashes != NULL)
3871 {
3872 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003873 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 if (!linewhite(lnum))
3875 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876
3877 /* Find interesting text in this line. */
3878 s = skipwhite(ml_get(lnum));
3879 /* skip C comment-start */
3880 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3881 {
3882 s = skipwhite(s + 2);
3883 if (*skipwhite(s) == NUL
3884 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3885 {
3886 s = skipwhite(ml_get(lnum + 1));
3887 if (*s == '*')
3888 s = skipwhite(s + 1);
3889 }
3890 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003891 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003892 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003893 r = alloc(STRLEN(txt)
3894 + STRLEN(dashes) // for %s
3895 + 20 // for %3ld
3896 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 if (r != NULL)
3898 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003899 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 len = (int)STRLEN(r);
3901 STRCAT(r, s);
3902 /* remove 'foldmarker' and 'commentstring' */
3903 foldtext_cleanup(r + len);
3904 rettv->vval.v_string = r;
3905 }
3906 }
3907#endif
3908}
3909
3910/*
3911 * "foldtextresult(lnum)" function
3912 */
3913 static void
3914f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3915{
3916#ifdef FEAT_FOLDING
3917 linenr_T lnum;
3918 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003919 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 foldinfo_T foldinfo;
3921 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003922 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923#endif
3924
3925 rettv->v_type = VAR_STRING;
3926 rettv->vval.v_string = NULL;
3927#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003928 if (entered)
3929 return; /* reject recursive use */
3930 entered = TRUE;
3931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003932 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 /* treat illegal types and illegal string values for {lnum} the same */
3934 if (lnum < 0)
3935 lnum = 0;
3936 fold_count = foldedCount(curwin, lnum, &foldinfo);
3937 if (fold_count > 0)
3938 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003939 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3940 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941 if (text == buf)
3942 text = vim_strsave(text);
3943 rettv->vval.v_string = text;
3944 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003945
3946 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947#endif
3948}
3949
3950/*
3951 * "foreground()" function
3952 */
3953 static void
3954f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3955{
3956#ifdef FEAT_GUI
3957 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003958 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003960 return;
3961 }
3962#endif
3963#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965#endif
3966}
3967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003969common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003970{
3971 char_u *s;
3972 char_u *name;
3973 int use_string = FALSE;
3974 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003975 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976
3977 if (argvars[0].v_type == VAR_FUNC)
3978 {
3979 /* function(MyFunc, [arg], dict) */
3980 s = argvars[0].vval.v_string;
3981 }
3982 else if (argvars[0].v_type == VAR_PARTIAL
3983 && argvars[0].vval.v_partial != NULL)
3984 {
3985 /* function(dict.MyFunc, [arg]) */
3986 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003987 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003988 }
3989 else
3990 {
3991 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003992 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 use_string = TRUE;
3994 }
3995
Bram Moolenaar843b8842016-08-21 14:36:15 +02003996 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003997 {
3998 name = s;
3999 trans_name = trans_function_name(&name, FALSE,
4000 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4001 if (*name != NUL)
4002 s = NULL;
4003 }
4004
Bram Moolenaar843b8842016-08-21 14:36:15 +02004005 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4006 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004007 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009 else if (trans_name != NULL && (is_funcref
4010 ? find_func(trans_name) == NULL
4011 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004012 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 else
4014 {
4015 int dict_idx = 0;
4016 int arg_idx = 0;
4017 list_T *list = NULL;
4018
4019 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4020 {
4021 char sid_buf[25];
4022 int off = *s == 's' ? 2 : 5;
4023
4024 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4025 * also be called from another script. Using trans_function_name()
4026 * would also work, but some plugins depend on the name being
4027 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004028 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004029 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 if (name != NULL)
4031 {
4032 STRCPY(name, sid_buf);
4033 STRCAT(name, s + off);
4034 }
4035 }
4036 else
4037 name = vim_strsave(s);
4038
4039 if (argvars[1].v_type != VAR_UNKNOWN)
4040 {
4041 if (argvars[2].v_type != VAR_UNKNOWN)
4042 {
4043 /* function(name, [args], dict) */
4044 arg_idx = 1;
4045 dict_idx = 2;
4046 }
4047 else if (argvars[1].v_type == VAR_DICT)
4048 /* function(name, dict) */
4049 dict_idx = 1;
4050 else
4051 /* function(name, [args]) */
4052 arg_idx = 1;
4053 if (dict_idx > 0)
4054 {
4055 if (argvars[dict_idx].v_type != VAR_DICT)
4056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004057 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004059 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060 }
4061 if (argvars[dict_idx].vval.v_dict == NULL)
4062 dict_idx = 0;
4063 }
4064 if (arg_idx > 0)
4065 {
4066 if (argvars[arg_idx].v_type != VAR_LIST)
4067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004068 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004070 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004071 }
4072 list = argvars[arg_idx].vval.v_list;
4073 if (list == NULL || list->lv_len == 0)
4074 arg_idx = 0;
4075 }
4076 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004077 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004079 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080
4081 /* result is a VAR_PARTIAL */
4082 if (pt == NULL)
4083 vim_free(name);
4084 else
4085 {
4086 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4087 {
4088 listitem_T *li;
4089 int i = 0;
4090 int arg_len = 0;
4091 int lv_len = 0;
4092
4093 if (arg_pt != NULL)
4094 arg_len = arg_pt->pt_argc;
4095 if (list != NULL)
4096 lv_len = list->lv_len;
4097 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004098 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004099 if (pt->pt_argv == NULL)
4100 {
4101 vim_free(pt);
4102 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004103 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004104 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004105 for (i = 0; i < arg_len; i++)
4106 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4107 if (lv_len > 0)
4108 for (li = list->lv_first; li != NULL;
4109 li = li->li_next)
4110 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 }
4112
4113 /* For "function(dict.func, [], dict)" and "func" is a partial
4114 * use "dict". That is backwards compatible. */
4115 if (dict_idx > 0)
4116 {
4117 /* The dict is bound explicitly, pt_auto is FALSE. */
4118 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4119 ++pt->pt_dict->dv_refcount;
4120 }
4121 else if (arg_pt != NULL)
4122 {
4123 /* If the dict was bound automatically the result is also
4124 * bound automatically. */
4125 pt->pt_dict = arg_pt->pt_dict;
4126 pt->pt_auto = arg_pt->pt_auto;
4127 if (pt->pt_dict != NULL)
4128 ++pt->pt_dict->dv_refcount;
4129 }
4130
4131 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4133 {
4134 pt->pt_func = arg_pt->pt_func;
4135 func_ptr_ref(pt->pt_func);
4136 vim_free(name);
4137 }
4138 else if (is_funcref)
4139 {
4140 pt->pt_func = find_func(trans_name);
4141 func_ptr_ref(pt->pt_func);
4142 vim_free(name);
4143 }
4144 else
4145 {
4146 pt->pt_name = name;
4147 func_ref(name);
4148 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 }
4150 rettv->v_type = VAR_PARTIAL;
4151 rettv->vval.v_partial = pt;
4152 }
4153 else
4154 {
4155 /* result is a VAR_FUNC */
4156 rettv->v_type = VAR_FUNC;
4157 rettv->vval.v_string = name;
4158 func_ref(name);
4159 }
4160 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004161theend:
4162 vim_free(trans_name);
4163}
4164
4165/*
4166 * "funcref()" function
4167 */
4168 static void
4169f_funcref(typval_T *argvars, typval_T *rettv)
4170{
4171 common_function(argvars, rettv, TRUE);
4172}
4173
4174/*
4175 * "function()" function
4176 */
4177 static void
4178f_function(typval_T *argvars, typval_T *rettv)
4179{
4180 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004181}
4182
4183/*
4184 * "garbagecollect()" function
4185 */
4186 static void
4187f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4188{
4189 /* This is postponed until we are back at the toplevel, because we may be
4190 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4191 want_garbage_collect = TRUE;
4192
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004193 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 garbage_collect_at_exit = TRUE;
4195}
4196
4197/*
4198 * "get()" function
4199 */
4200 static void
4201f_get(typval_T *argvars, typval_T *rettv)
4202{
4203 listitem_T *li;
4204 list_T *l;
4205 dictitem_T *di;
4206 dict_T *d;
4207 typval_T *tv = NULL;
4208
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004209 if (argvars[0].v_type == VAR_BLOB)
4210 {
4211 int error = FALSE;
4212 int idx = tv_get_number_chk(&argvars[1], &error);
4213
4214 if (!error)
4215 {
4216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004217 if (idx < 0)
4218 idx = blob_len(argvars[0].vval.v_blob) + idx;
4219 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4220 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004221 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004222 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004223 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004224 tv = rettv;
4225 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004226 }
4227 }
4228 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004229 {
4230 if ((l = argvars[0].vval.v_list) != NULL)
4231 {
4232 int error = FALSE;
4233
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004234 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004235 if (!error && li != NULL)
4236 tv = &li->li_tv;
4237 }
4238 }
4239 else if (argvars[0].v_type == VAR_DICT)
4240 {
4241 if ((d = argvars[0].vval.v_dict) != NULL)
4242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004243 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244 if (di != NULL)
4245 tv = &di->di_tv;
4246 }
4247 }
4248 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4249 {
4250 partial_T *pt;
4251 partial_T fref_pt;
4252
4253 if (argvars[0].v_type == VAR_PARTIAL)
4254 pt = argvars[0].vval.v_partial;
4255 else
4256 {
4257 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4258 fref_pt.pt_name = argvars[0].vval.v_string;
4259 pt = &fref_pt;
4260 }
4261
4262 if (pt != NULL)
4263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004264 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004265 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004266
4267 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4268 {
4269 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004270 n = partial_name(pt);
4271 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 rettv->vval.v_string = NULL;
4273 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004274 {
4275 rettv->vval.v_string = vim_strsave(n);
4276 if (rettv->v_type == VAR_FUNC)
4277 func_ref(rettv->vval.v_string);
4278 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279 }
4280 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004281 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282 else if (STRCMP(what, "args") == 0)
4283 {
4284 rettv->v_type = VAR_LIST;
4285 if (rettv_list_alloc(rettv) == OK)
4286 {
4287 int i;
4288
4289 for (i = 0; i < pt->pt_argc; ++i)
4290 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4291 }
4292 }
4293 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004295 return;
4296 }
4297 }
4298 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004299 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300
4301 if (tv == NULL)
4302 {
4303 if (argvars[2].v_type != VAR_UNKNOWN)
4304 copy_tv(&argvars[2], rettv);
4305 }
4306 else
4307 copy_tv(tv, rettv);
4308}
4309
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004310/*
4311 * Returns buffer options, variables and other attributes in a dictionary.
4312 */
4313 static dict_T *
4314get_buffer_info(buf_T *buf)
4315{
4316 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004317 tabpage_T *tp;
4318 win_T *wp;
4319 list_T *windows;
4320
4321 dict = dict_alloc();
4322 if (dict == NULL)
4323 return NULL;
4324
Bram Moolenaare0be1672018-07-08 16:50:37 +02004325 dict_add_number(dict, "bufnr", buf->b_fnum);
4326 dict_add_string(dict, "name", buf->b_ffname);
4327 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4328 : buflist_findlnum(buf));
4329 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4330 dict_add_number(dict, "listed", buf->b_p_bl);
4331 dict_add_number(dict, "changed", bufIsChanged(buf));
4332 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4333 dict_add_number(dict, "hidden",
4334 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004335
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004336 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004337 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004338
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004339 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004340 windows = list_alloc();
4341 if (windows != NULL)
4342 {
4343 FOR_ALL_TAB_WINDOWS(tp, wp)
4344 if (wp->w_buffer == buf)
4345 list_append_number(windows, (varnumber_T)wp->w_id);
4346 dict_add_list(dict, "windows", windows);
4347 }
4348
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004349#ifdef FEAT_TEXT_PROP
4350 // List of popup windows displaying this buffer
4351 windows = list_alloc();
4352 if (windows != NULL)
4353 {
4354 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4355 if (wp->w_buffer == buf)
4356 list_append_number(windows, (varnumber_T)wp->w_id);
4357 FOR_ALL_TABPAGES(tp)
4358 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4359 if (wp->w_buffer == buf)
4360 list_append_number(windows, (varnumber_T)wp->w_id);
4361
4362 dict_add_list(dict, "popups", windows);
4363 }
4364#endif
4365
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004366#ifdef FEAT_SIGNS
4367 if (buf->b_signlist != NULL)
4368 {
4369 /* List of signs placed in this buffer */
4370 list_T *signs = list_alloc();
4371 if (signs != NULL)
4372 {
4373 get_buffer_signs(buf, signs);
4374 dict_add_list(dict, "signs", signs);
4375 }
4376 }
4377#endif
4378
4379 return dict;
4380}
4381
4382/*
4383 * "getbufinfo()" function
4384 */
4385 static void
4386f_getbufinfo(typval_T *argvars, typval_T *rettv)
4387{
4388 buf_T *buf = NULL;
4389 buf_T *argbuf = NULL;
4390 dict_T *d;
4391 int filtered = FALSE;
4392 int sel_buflisted = FALSE;
4393 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004394 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004395
4396 if (rettv_list_alloc(rettv) != OK)
4397 return;
4398
4399 /* List of all the buffers or selected buffers */
4400 if (argvars[0].v_type == VAR_DICT)
4401 {
4402 dict_T *sel_d = argvars[0].vval.v_dict;
4403
4404 if (sel_d != NULL)
4405 {
4406 dictitem_T *di;
4407
4408 filtered = TRUE;
4409
4410 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004411 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004412 sel_buflisted = TRUE;
4413
4414 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004415 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004416 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004417
4418 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004420 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004421 }
4422 }
4423 else if (argvars[0].v_type != VAR_UNKNOWN)
4424 {
4425 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004426 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004427 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004428 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004429 --emsg_off;
4430 if (argbuf == NULL)
4431 return;
4432 }
4433
4434 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004435 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004436 {
4437 if (argbuf != NULL && argbuf != buf)
4438 continue;
4439 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004440 || (sel_buflisted && !buf->b_p_bl)
4441 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004442 continue;
4443
4444 d = get_buffer_info(buf);
4445 if (d != NULL)
4446 list_append_dict(rettv->vval.v_list, d);
4447 if (argbuf != NULL)
4448 return;
4449 }
4450}
4451
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004452/*
4453 * Get line or list of lines from buffer "buf" into "rettv".
4454 * Return a range (from start to end) of lines in rettv from the specified
4455 * buffer.
4456 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4457 */
4458 static void
4459get_buffer_lines(
4460 buf_T *buf,
4461 linenr_T start,
4462 linenr_T end,
4463 int retlist,
4464 typval_T *rettv)
4465{
4466 char_u *p;
4467
4468 rettv->v_type = VAR_STRING;
4469 rettv->vval.v_string = NULL;
4470 if (retlist && rettv_list_alloc(rettv) == FAIL)
4471 return;
4472
4473 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4474 return;
4475
4476 if (!retlist)
4477 {
4478 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4479 p = ml_get_buf(buf, start, FALSE);
4480 else
4481 p = (char_u *)"";
4482 rettv->vval.v_string = vim_strsave(p);
4483 }
4484 else
4485 {
4486 if (end < start)
4487 return;
4488
4489 if (start < 1)
4490 start = 1;
4491 if (end > buf->b_ml.ml_line_count)
4492 end = buf->b_ml.ml_line_count;
4493 while (start <= end)
4494 if (list_append_string(rettv->vval.v_list,
4495 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4496 break;
4497 }
4498}
4499
4500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 * "getbufline()" function
4502 */
4503 static void
4504f_getbufline(typval_T *argvars, typval_T *rettv)
4505{
4506 linenr_T lnum;
4507 linenr_T end;
4508 buf_T *buf;
4509
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004510 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004512 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513 --emsg_off;
4514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004515 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 if (argvars[2].v_type == VAR_UNKNOWN)
4517 end = lnum;
4518 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004519 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520
4521 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4522}
4523
4524/*
4525 * "getbufvar()" function
4526 */
4527 static void
4528f_getbufvar(typval_T *argvars, typval_T *rettv)
4529{
4530 buf_T *buf;
4531 buf_T *save_curbuf;
4532 char_u *varname;
4533 dictitem_T *v;
4534 int done = FALSE;
4535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004536 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4537 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004539 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540
4541 rettv->v_type = VAR_STRING;
4542 rettv->vval.v_string = NULL;
4543
4544 if (buf != NULL && varname != NULL)
4545 {
4546 /* set curbuf to be our buf, temporarily */
4547 save_curbuf = curbuf;
4548 curbuf = buf;
4549
Bram Moolenaar30567352016-08-27 21:25:44 +02004550 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004552 if (varname[1] == NUL)
4553 {
4554 /* get all buffer-local options in a dict */
4555 dict_T *opts = get_winbuf_options(TRUE);
4556
4557 if (opts != NULL)
4558 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004559 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004560 done = TRUE;
4561 }
4562 }
4563 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4564 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565 done = TRUE;
4566 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004567 else
4568 {
4569 /* Look up the variable. */
4570 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4571 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4572 'b', varname, FALSE);
4573 if (v != NULL)
4574 {
4575 copy_tv(&v->di_tv, rettv);
4576 done = TRUE;
4577 }
4578 }
4579
4580 /* restore previous notion of curbuf */
4581 curbuf = save_curbuf;
4582 }
4583
4584 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4585 /* use the default value */
4586 copy_tv(&argvars[2], rettv);
4587
4588 --emsg_off;
4589}
4590
4591/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004592 * "getchangelist()" function
4593 */
4594 static void
4595f_getchangelist(typval_T *argvars, typval_T *rettv)
4596{
4597#ifdef FEAT_JUMPLIST
4598 buf_T *buf;
4599 int i;
4600 list_T *l;
4601 dict_T *d;
4602#endif
4603
4604 if (rettv_list_alloc(rettv) != OK)
4605 return;
4606
4607#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004608 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004609 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004610 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004611 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004612 if (buf == NULL)
4613 return;
4614
4615 l = list_alloc();
4616 if (l == NULL)
4617 return;
4618
4619 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4620 return;
4621 /*
4622 * The current window change list index tracks only the position in the
4623 * current buffer change list. For other buffers, use the change list
4624 * length as the current index.
4625 */
4626 list_append_number(rettv->vval.v_list,
4627 (varnumber_T)((buf == curwin->w_buffer)
4628 ? curwin->w_changelistidx : buf->b_changelistlen));
4629
4630 for (i = 0; i < buf->b_changelistlen; ++i)
4631 {
4632 if (buf->b_changelist[i].lnum == 0)
4633 continue;
4634 if ((d = dict_alloc()) == NULL)
4635 return;
4636 if (list_append_dict(l, d) == FAIL)
4637 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004638 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4639 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004640 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004641 }
4642#endif
4643}
4644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004645 * "getchar()" function
4646 */
4647 static void
4648f_getchar(typval_T *argvars, typval_T *rettv)
4649{
4650 varnumber_T n;
4651 int error = FALSE;
4652
Bram Moolenaar84d93902018-09-11 20:10:20 +02004653#ifdef MESSAGE_QUEUE
4654 // vpeekc() used to check for messages, but that caused problems, invoking
4655 // a callback where it was not expected. Some plugins use getchar(1) in a
4656 // loop to await a message, therefore make sure we check for messages here.
4657 parse_queued_messages();
4658#endif
4659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660 /* Position the cursor. Needed after a message that ends in a space. */
4661 windgoto(msg_row, msg_col);
4662
4663 ++no_mapping;
4664 ++allow_keys;
4665 for (;;)
4666 {
4667 if (argvars[0].v_type == VAR_UNKNOWN)
4668 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004669 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004670 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 /* getchar(1): only check if char avail */
4672 n = vpeekc_any();
4673 else if (error || vpeekc_any() == NUL)
4674 /* illegal argument or getchar(0) and no char avail: return zero */
4675 n = 0;
4676 else
4677 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004678 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 if (n == K_IGNORE)
4681 continue;
4682 break;
4683 }
4684 --no_mapping;
4685 --allow_keys;
4686
4687 set_vim_var_nr(VV_MOUSE_WIN, 0);
4688 set_vim_var_nr(VV_MOUSE_WINID, 0);
4689 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4690 set_vim_var_nr(VV_MOUSE_COL, 0);
4691
4692 rettv->vval.v_number = n;
4693 if (IS_SPECIAL(n) || mod_mask != 0)
4694 {
4695 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4696 int i = 0;
4697
4698 /* Turn a special key into three bytes, plus modifier. */
4699 if (mod_mask != 0)
4700 {
4701 temp[i++] = K_SPECIAL;
4702 temp[i++] = KS_MODIFIER;
4703 temp[i++] = mod_mask;
4704 }
4705 if (IS_SPECIAL(n))
4706 {
4707 temp[i++] = K_SPECIAL;
4708 temp[i++] = K_SECOND(n);
4709 temp[i++] = K_THIRD(n);
4710 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711 else if (has_mbyte)
4712 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 else
4714 temp[i++] = n;
4715 temp[i++] = NUL;
4716 rettv->v_type = VAR_STRING;
4717 rettv->vval.v_string = vim_strsave(temp);
4718
4719#ifdef FEAT_MOUSE
4720 if (is_mouse_key(n))
4721 {
4722 int row = mouse_row;
4723 int col = mouse_col;
4724 win_T *win;
4725 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 int winnr = 1;
4728
4729 if (row >= 0 && col >= 0)
4730 {
4731 /* Find the window at the mouse coordinates and compute the
4732 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004733 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004734 if (win == NULL)
4735 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004736 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004737# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004738 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004739 winnr = 0;
4740 else
4741# endif
4742 for (wp = firstwin; wp != win && wp != NULL;
4743 wp = wp->w_next)
4744 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4746 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4747 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4748 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4749 }
4750 }
4751#endif
4752 }
4753}
4754
4755/*
4756 * "getcharmod()" function
4757 */
4758 static void
4759f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4760{
4761 rettv->vval.v_number = mod_mask;
4762}
4763
4764/*
4765 * "getcharsearch()" function
4766 */
4767 static void
4768f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4769{
4770 if (rettv_dict_alloc(rettv) != FAIL)
4771 {
4772 dict_T *dict = rettv->vval.v_dict;
4773
Bram Moolenaare0be1672018-07-08 16:50:37 +02004774 dict_add_string(dict, "char", last_csearch());
4775 dict_add_number(dict, "forward", last_csearch_forward());
4776 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004777 }
4778}
4779
4780/*
4781 * "getcmdline()" function
4782 */
4783 static void
4784f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4785{
4786 rettv->v_type = VAR_STRING;
4787 rettv->vval.v_string = get_cmdline_str();
4788}
4789
4790/*
4791 * "getcmdpos()" function
4792 */
4793 static void
4794f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4795{
4796 rettv->vval.v_number = get_cmdline_pos() + 1;
4797}
4798
4799/*
4800 * "getcmdtype()" function
4801 */
4802 static void
4803f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4804{
4805 rettv->v_type = VAR_STRING;
4806 rettv->vval.v_string = alloc(2);
4807 if (rettv->vval.v_string != NULL)
4808 {
4809 rettv->vval.v_string[0] = get_cmdline_type();
4810 rettv->vval.v_string[1] = NUL;
4811 }
4812}
4813
4814/*
4815 * "getcmdwintype()" function
4816 */
4817 static void
4818f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4819{
4820 rettv->v_type = VAR_STRING;
4821 rettv->vval.v_string = NULL;
4822#ifdef FEAT_CMDWIN
4823 rettv->vval.v_string = alloc(2);
4824 if (rettv->vval.v_string != NULL)
4825 {
4826 rettv->vval.v_string[0] = cmdwin_type;
4827 rettv->vval.v_string[1] = NUL;
4828 }
4829#endif
4830}
4831
4832#if defined(FEAT_CMDL_COMPL)
4833/*
4834 * "getcompletion()" function
4835 */
4836 static void
4837f_getcompletion(typval_T *argvars, typval_T *rettv)
4838{
4839 char_u *pat;
4840 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004841 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004842 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4843 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004845 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848 if (p_wic)
4849 options |= WILD_ICASE;
4850
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004851 /* For filtered results, 'wildignore' is used */
4852 if (!filtered)
4853 options |= WILD_KEEP_ALL;
4854
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004855 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004856 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004857 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004858 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859 if (xpc.xp_context == EXPAND_NOTHING)
4860 {
4861 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004865 return;
4866 }
4867
4868# if defined(FEAT_MENU)
4869 if (xpc.xp_context == EXPAND_MENUS)
4870 {
4871 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4872 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4873 }
4874# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004875#ifdef FEAT_CSCOPE
4876 if (xpc.xp_context == EXPAND_CSCOPE)
4877 {
4878 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4879 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4880 }
4881#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004882#ifdef FEAT_SIGNS
4883 if (xpc.xp_context == EXPAND_SIGN)
4884 {
4885 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4886 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4887 }
4888#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889
4890 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4891 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4892 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004893 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894
4895 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4896
4897 for (i = 0; i < xpc.xp_numfiles; i++)
4898 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4899 }
4900 vim_free(pat);
4901 ExpandCleanup(&xpc);
4902}
4903#endif
4904
4905/*
4906 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004907 *
4908 * Return the current working directory of a window in a tab page.
4909 * First optional argument 'winnr' is the window number or -1 and the second
4910 * optional argument 'tabnr' is the tab page number.
4911 *
4912 * If no arguments are supplied, then return the directory of the current
4913 * window.
4914 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4915 * the specified window.
4916 * If 'winnr' is 0 then return the directory of the current window.
4917 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4918 * directory of the specified tab page. Otherwise return the directory of the
4919 * specified window in the specified tab page.
4920 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921 */
4922 static void
4923f_getcwd(typval_T *argvars, typval_T *rettv)
4924{
4925 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004926 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004928 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929
4930 rettv->v_type = VAR_STRING;
4931 rettv->vval.v_string = NULL;
4932
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004933 if (argvars[0].v_type == VAR_NUMBER
4934 && argvars[0].vval.v_number == -1
4935 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004936 global = TRUE;
4937 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004938 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004939
4940 if (wp != NULL && wp->w_localdir != NULL)
4941 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004942 else if (tp != NULL && tp->tp_localdir != NULL)
4943 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4944 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004946 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 rettv->vval.v_string = vim_strsave(globaldir);
4948 else
4949 {
4950 cwd = alloc(MAXPATHL);
4951 if (cwd != NULL)
4952 {
4953 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4954 rettv->vval.v_string = vim_strsave(cwd);
4955 vim_free(cwd);
4956 }
4957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004959#ifdef BACKSLASH_IN_FILENAME
4960 if (rettv->vval.v_string != NULL)
4961 slash_adjust(rettv->vval.v_string);
4962#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963}
4964
4965/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004966 * "getenv()" function
4967 */
4968 static void
4969f_getenv(typval_T *argvars, typval_T *rettv)
4970{
4971 int mustfree = FALSE;
4972 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4973
4974 if (p == NULL)
4975 {
4976 rettv->v_type = VAR_SPECIAL;
4977 rettv->vval.v_number = VVAL_NULL;
4978 return;
4979 }
4980 if (!mustfree)
4981 p = vim_strsave(p);
4982 rettv->vval.v_string = p;
4983 rettv->v_type = VAR_STRING;
4984}
4985
4986/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004987 * "getfontname()" function
4988 */
4989 static void
4990f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4991{
4992 rettv->v_type = VAR_STRING;
4993 rettv->vval.v_string = NULL;
4994#ifdef FEAT_GUI
4995 if (gui.in_use)
4996 {
4997 GuiFont font;
4998 char_u *name = NULL;
4999
5000 if (argvars[0].v_type == VAR_UNKNOWN)
5001 {
5002 /* Get the "Normal" font. Either the name saved by
5003 * hl_set_font_name() or from the font ID. */
5004 font = gui.norm_font;
5005 name = hl_get_font_name();
5006 }
5007 else
5008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5011 return;
5012 font = gui_mch_get_font(name, FALSE);
5013 if (font == NOFONT)
5014 return; /* Invalid font name, return empty string. */
5015 }
5016 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5017 if (argvars[0].v_type != VAR_UNKNOWN)
5018 gui_mch_free_font(font);
5019 }
5020#endif
5021}
5022
5023/*
5024 * "getfperm({fname})" function
5025 */
5026 static void
5027f_getfperm(typval_T *argvars, typval_T *rettv)
5028{
5029 char_u *fname;
5030 stat_T st;
5031 char_u *perm = NULL;
5032 char_u flags[] = "rwx";
5033 int i;
5034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005035 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036
5037 rettv->v_type = VAR_STRING;
5038 if (mch_stat((char *)fname, &st) >= 0)
5039 {
5040 perm = vim_strsave((char_u *)"---------");
5041 if (perm != NULL)
5042 {
5043 for (i = 0; i < 9; i++)
5044 {
5045 if (st.st_mode & (1 << (8 - i)))
5046 perm[i] = flags[i % 3];
5047 }
5048 }
5049 }
5050 rettv->vval.v_string = perm;
5051}
5052
5053/*
5054 * "getfsize({fname})" function
5055 */
5056 static void
5057f_getfsize(typval_T *argvars, typval_T *rettv)
5058{
5059 char_u *fname;
5060 stat_T st;
5061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005062 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005063
5064 rettv->v_type = VAR_NUMBER;
5065
5066 if (mch_stat((char *)fname, &st) >= 0)
5067 {
5068 if (mch_isdir(fname))
5069 rettv->vval.v_number = 0;
5070 else
5071 {
5072 rettv->vval.v_number = (varnumber_T)st.st_size;
5073
5074 /* non-perfect check for overflow */
5075 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5076 rettv->vval.v_number = -2;
5077 }
5078 }
5079 else
5080 rettv->vval.v_number = -1;
5081}
5082
5083/*
5084 * "getftime({fname})" function
5085 */
5086 static void
5087f_getftime(typval_T *argvars, typval_T *rettv)
5088{
5089 char_u *fname;
5090 stat_T st;
5091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005092 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093
5094 if (mch_stat((char *)fname, &st) >= 0)
5095 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5096 else
5097 rettv->vval.v_number = -1;
5098}
5099
5100/*
5101 * "getftype({fname})" function
5102 */
5103 static void
5104f_getftype(typval_T *argvars, typval_T *rettv)
5105{
5106 char_u *fname;
5107 stat_T st;
5108 char_u *type = NULL;
5109 char *t;
5110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112
5113 rettv->v_type = VAR_STRING;
5114 if (mch_lstat((char *)fname, &st) >= 0)
5115 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 if (S_ISREG(st.st_mode))
5117 t = "file";
5118 else if (S_ISDIR(st.st_mode))
5119 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 else if (S_ISLNK(st.st_mode))
5121 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 else if (S_ISBLK(st.st_mode))
5123 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 else if (S_ISCHR(st.st_mode))
5125 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 else if (S_ISFIFO(st.st_mode))
5127 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005129 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130 else
5131 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132 type = vim_strsave((char_u *)t);
5133 }
5134 rettv->vval.v_string = type;
5135}
5136
5137/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005138 * "getjumplist()" function
5139 */
5140 static void
5141f_getjumplist(typval_T *argvars, typval_T *rettv)
5142{
5143#ifdef FEAT_JUMPLIST
5144 win_T *wp;
5145 int i;
5146 list_T *l;
5147 dict_T *d;
5148#endif
5149
5150 if (rettv_list_alloc(rettv) != OK)
5151 return;
5152
5153#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005154 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005155 if (wp == NULL)
5156 return;
5157
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005158 cleanup_jumplist(wp, TRUE);
5159
Bram Moolenaar4f505882018-02-10 21:06:32 +01005160 l = list_alloc();
5161 if (l == NULL)
5162 return;
5163
5164 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5165 return;
5166 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5167
5168 for (i = 0; i < wp->w_jumplistlen; ++i)
5169 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005170 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5171 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005172 if ((d = dict_alloc()) == NULL)
5173 return;
5174 if (list_append_dict(l, d) == FAIL)
5175 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005176 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5177 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005178 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005179 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005180 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005181 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005182 }
5183#endif
5184}
5185
5186/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005187 * "getline(lnum, [end])" function
5188 */
5189 static void
5190f_getline(typval_T *argvars, typval_T *rettv)
5191{
5192 linenr_T lnum;
5193 linenr_T end;
5194 int retlist;
5195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005196 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005197 if (argvars[1].v_type == VAR_UNKNOWN)
5198 {
5199 end = 0;
5200 retlist = FALSE;
5201 }
5202 else
5203 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005204 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005205 retlist = TRUE;
5206 }
5207
5208 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5209}
5210
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005211#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005212 static void
5213get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5214{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005215 if (what_arg->v_type == VAR_UNKNOWN)
5216 {
5217 if (rettv_list_alloc(rettv) == OK)
5218 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005219 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005220 }
5221 else
5222 {
5223 if (rettv_dict_alloc(rettv) == OK)
5224 if (is_qf || (wp != NULL))
5225 {
5226 if (what_arg->v_type == VAR_DICT)
5227 {
5228 dict_T *d = what_arg->vval.v_dict;
5229
5230 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005231 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005232 }
5233 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005234 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235 }
5236 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005238#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005239
5240/*
5241 * "getloclist()" function
5242 */
5243 static void
5244f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5245{
5246#ifdef FEAT_QUICKFIX
5247 win_T *wp;
5248
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005249 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005250 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5251#endif
5252}
5253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 * "getpid()" function
5256 */
5257 static void
5258f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5259{
5260 rettv->vval.v_number = mch_get_pid();
5261}
5262
5263 static void
5264getpos_both(
5265 typval_T *argvars,
5266 typval_T *rettv,
5267 int getcurpos)
5268{
5269 pos_T *fp;
5270 list_T *l;
5271 int fnum = -1;
5272
5273 if (rettv_list_alloc(rettv) == OK)
5274 {
5275 l = rettv->vval.v_list;
5276 if (getcurpos)
5277 fp = &curwin->w_cursor;
5278 else
5279 fp = var2fpos(&argvars[0], TRUE, &fnum);
5280 if (fnum != -1)
5281 list_append_number(l, (varnumber_T)fnum);
5282 else
5283 list_append_number(l, (varnumber_T)0);
5284 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5285 : (varnumber_T)0);
5286 list_append_number(l, (fp != NULL)
5287 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5288 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005289 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 (varnumber_T)0);
5291 if (getcurpos)
5292 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005293 int save_set_curswant = curwin->w_set_curswant;
5294 colnr_T save_curswant = curwin->w_curswant;
5295 colnr_T save_virtcol = curwin->w_virtcol;
5296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005297 update_curswant();
5298 list_append_number(l, curwin->w_curswant == MAXCOL ?
5299 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005300
5301 // Do not change "curswant", as it is unexpected that a get
5302 // function has a side effect.
5303 if (save_set_curswant)
5304 {
5305 curwin->w_set_curswant = save_set_curswant;
5306 curwin->w_curswant = save_curswant;
5307 curwin->w_virtcol = save_virtcol;
5308 curwin->w_valid &= ~VALID_VIRTCOL;
5309 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 }
5311 }
5312 else
5313 rettv->vval.v_number = FALSE;
5314}
5315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316/*
5317 * "getcurpos()" function
5318 */
5319 static void
5320f_getcurpos(typval_T *argvars, typval_T *rettv)
5321{
5322 getpos_both(argvars, rettv, TRUE);
5323}
5324
5325/*
5326 * "getpos(string)" function
5327 */
5328 static void
5329f_getpos(typval_T *argvars, typval_T *rettv)
5330{
5331 getpos_both(argvars, rettv, FALSE);
5332}
5333
5334/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005335 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336 */
5337 static void
5338f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5339{
5340#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005341 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342#endif
5343}
5344
5345/*
5346 * "getreg()" function
5347 */
5348 static void
5349f_getreg(typval_T *argvars, typval_T *rettv)
5350{
5351 char_u *strregname;
5352 int regname;
5353 int arg2 = FALSE;
5354 int return_list = FALSE;
5355 int error = FALSE;
5356
5357 if (argvars[0].v_type != VAR_UNKNOWN)
5358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005359 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 error = strregname == NULL;
5361 if (argvars[1].v_type != VAR_UNKNOWN)
5362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005363 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005365 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005366 }
5367 }
5368 else
5369 strregname = get_vim_var_str(VV_REG);
5370
5371 if (error)
5372 return;
5373
5374 regname = (strregname == NULL ? '"' : *strregname);
5375 if (regname == 0)
5376 regname = '"';
5377
5378 if (return_list)
5379 {
5380 rettv->v_type = VAR_LIST;
5381 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5382 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5383 if (rettv->vval.v_list == NULL)
5384 (void)rettv_list_alloc(rettv);
5385 else
5386 ++rettv->vval.v_list->lv_refcount;
5387 }
5388 else
5389 {
5390 rettv->v_type = VAR_STRING;
5391 rettv->vval.v_string = get_reg_contents(regname,
5392 arg2 ? GREG_EXPR_SRC : 0);
5393 }
5394}
5395
5396/*
5397 * "getregtype()" function
5398 */
5399 static void
5400f_getregtype(typval_T *argvars, typval_T *rettv)
5401{
5402 char_u *strregname;
5403 int regname;
5404 char_u buf[NUMBUFLEN + 2];
5405 long reglen = 0;
5406
5407 if (argvars[0].v_type != VAR_UNKNOWN)
5408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005409 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005410 if (strregname == NULL) /* type error; errmsg already given */
5411 {
5412 rettv->v_type = VAR_STRING;
5413 rettv->vval.v_string = NULL;
5414 return;
5415 }
5416 }
5417 else
5418 /* Default to v:register */
5419 strregname = get_vim_var_str(VV_REG);
5420
5421 regname = (strregname == NULL ? '"' : *strregname);
5422 if (regname == 0)
5423 regname = '"';
5424
5425 buf[0] = NUL;
5426 buf[1] = NUL;
5427 switch (get_reg_type(regname, &reglen))
5428 {
5429 case MLINE: buf[0] = 'V'; break;
5430 case MCHAR: buf[0] = 'v'; break;
5431 case MBLOCK:
5432 buf[0] = Ctrl_V;
5433 sprintf((char *)buf + 1, "%ld", reglen + 1);
5434 break;
5435 }
5436 rettv->v_type = VAR_STRING;
5437 rettv->vval.v_string = vim_strsave(buf);
5438}
5439
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005440/*
5441 * Returns information (variables, options, etc.) about a tab page
5442 * as a dictionary.
5443 */
5444 static dict_T *
5445get_tabpage_info(tabpage_T *tp, int tp_idx)
5446{
5447 win_T *wp;
5448 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005449 list_T *l;
5450
5451 dict = dict_alloc();
5452 if (dict == NULL)
5453 return NULL;
5454
Bram Moolenaare0be1672018-07-08 16:50:37 +02005455 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005456
5457 l = list_alloc();
5458 if (l != NULL)
5459 {
5460 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005461 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005462 list_append_number(l, (varnumber_T)wp->w_id);
5463 dict_add_list(dict, "windows", l);
5464 }
5465
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005466 /* Make a reference to tabpage variables */
5467 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005468
5469 return dict;
5470}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005471
5472/*
5473 * "gettabinfo()" function
5474 */
5475 static void
5476f_gettabinfo(typval_T *argvars, typval_T *rettv)
5477{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005478 tabpage_T *tp, *tparg = NULL;
5479 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005480 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005481
5482 if (rettv_list_alloc(rettv) != OK)
5483 return;
5484
5485 if (argvars[0].v_type != VAR_UNKNOWN)
5486 {
5487 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005488 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005489 if (tparg == NULL)
5490 return;
5491 }
5492
5493 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005494 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005495 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005496 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005497 if (tparg != NULL && tp != tparg)
5498 continue;
5499 d = get_tabpage_info(tp, tpnr);
5500 if (d != NULL)
5501 list_append_dict(rettv->vval.v_list, d);
5502 if (tparg != NULL)
5503 return;
5504 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005505}
5506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507/*
5508 * "gettabvar()" function
5509 */
5510 static void
5511f_gettabvar(typval_T *argvars, typval_T *rettv)
5512{
5513 win_T *oldcurwin;
5514 tabpage_T *tp, *oldtabpage;
5515 dictitem_T *v;
5516 char_u *varname;
5517 int done = FALSE;
5518
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = NULL;
5521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005522 varname = tv_get_string_chk(&argvars[1]);
5523 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524 if (tp != NULL && varname != NULL)
5525 {
5526 /* Set tp to be our tabpage, temporarily. Also set the window to the
5527 * first window in the tabpage, otherwise the window is not valid. */
5528 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005529 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5530 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531 {
5532 /* look up the variable */
5533 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5534 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5535 if (v != NULL)
5536 {
5537 copy_tv(&v->di_tv, rettv);
5538 done = TRUE;
5539 }
5540 }
5541
5542 /* restore previous notion of curwin */
5543 restore_win(oldcurwin, oldtabpage, TRUE);
5544 }
5545
5546 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5547 copy_tv(&argvars[2], rettv);
5548}
5549
5550/*
5551 * "gettabwinvar()" function
5552 */
5553 static void
5554f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5555{
5556 getwinvar(argvars, rettv, 1);
5557}
5558
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005559/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005560 * "gettagstack()" function
5561 */
5562 static void
5563f_gettagstack(typval_T *argvars, typval_T *rettv)
5564{
5565 win_T *wp = curwin; // default is current window
5566
5567 if (rettv_dict_alloc(rettv) != OK)
5568 return;
5569
5570 if (argvars[0].v_type != VAR_UNKNOWN)
5571 {
5572 wp = find_win_by_nr_or_id(&argvars[0]);
5573 if (wp == NULL)
5574 return;
5575 }
5576
5577 get_tagstack(wp, rettv->vval.v_dict);
5578}
5579
5580/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005581 * Returns information about a window as a dictionary.
5582 */
5583 static dict_T *
5584get_win_info(win_T *wp, short tpnr, short winnr)
5585{
5586 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005587
5588 dict = dict_alloc();
5589 if (dict == NULL)
5590 return NULL;
5591
Bram Moolenaare0be1672018-07-08 16:50:37 +02005592 dict_add_number(dict, "tabnr", tpnr);
5593 dict_add_number(dict, "winnr", winnr);
5594 dict_add_number(dict, "winid", wp->w_id);
5595 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005596 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005597 dict_add_number(dict, "topline", wp->w_topline);
5598 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005599#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005600 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005601#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005602 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005603 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005604 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005605
Bram Moolenaar69905d12017-08-13 18:14:47 +02005606#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005607 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005608#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005609#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005610 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5611 dict_add_number(dict, "loclist",
5612 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005613#endif
5614
Bram Moolenaar30567352016-08-27 21:25:44 +02005615 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005616 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005617
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005618 return dict;
5619}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005620
5621/*
5622 * "getwininfo()" function
5623 */
5624 static void
5625f_getwininfo(typval_T *argvars, typval_T *rettv)
5626{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005627 tabpage_T *tp;
5628 win_T *wp = NULL, *wparg = NULL;
5629 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005630 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005631
5632 if (rettv_list_alloc(rettv) != OK)
5633 return;
5634
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005635 if (argvars[0].v_type != VAR_UNKNOWN)
5636 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005637 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005638 if (wparg == NULL)
5639 return;
5640 }
5641
5642 /* Collect information about either all the windows across all the tab
5643 * pages or one particular window.
5644 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005645 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005646 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005647 tabnr++;
5648 winnr = 0;
5649 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005650 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005651 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005652 if (wparg != NULL && wp != wparg)
5653 continue;
5654 d = get_win_info(wp, tabnr, winnr);
5655 if (d != NULL)
5656 list_append_dict(rettv->vval.v_list, d);
5657 if (wparg != NULL)
5658 /* found information about a specific window */
5659 return;
5660 }
5661 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005662}
5663
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005665 * "win_execute()" function
5666 */
5667 static void
5668f_win_execute(typval_T *argvars, typval_T *rettv)
5669{
5670 int id = (int)tv_get_number(argvars);
5671 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005672 win_T *save_curwin;
5673 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005674
5675 if (wp != NULL)
5676 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005677 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5678 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005679 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005680 check_cursor();
5681 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005682 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005683 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005684 }
5685}
5686
5687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 * "win_findbuf()" function
5689 */
5690 static void
5691f_win_findbuf(typval_T *argvars, typval_T *rettv)
5692{
5693 if (rettv_list_alloc(rettv) != FAIL)
5694 win_findbuf(argvars, rettv->vval.v_list);
5695}
5696
5697/*
5698 * "win_getid()" function
5699 */
5700 static void
5701f_win_getid(typval_T *argvars, typval_T *rettv)
5702{
5703 rettv->vval.v_number = win_getid(argvars);
5704}
5705
5706/*
5707 * "win_gotoid()" function
5708 */
5709 static void
5710f_win_gotoid(typval_T *argvars, typval_T *rettv)
5711{
5712 rettv->vval.v_number = win_gotoid(argvars);
5713}
5714
5715/*
5716 * "win_id2tabwin()" function
5717 */
5718 static void
5719f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5720{
5721 if (rettv_list_alloc(rettv) != FAIL)
5722 win_id2tabwin(argvars, rettv->vval.v_list);
5723}
5724
5725/*
5726 * "win_id2win()" function
5727 */
5728 static void
5729f_win_id2win(typval_T *argvars, typval_T *rettv)
5730{
5731 rettv->vval.v_number = win_id2win(argvars);
5732}
5733
5734/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005735 * "win_screenpos()" function
5736 */
5737 static void
5738f_win_screenpos(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *wp;
5741
5742 if (rettv_list_alloc(rettv) == FAIL)
5743 return;
5744
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005745 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005746 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5747 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5748}
5749
5750/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005751 * "getwinpos({timeout})" function
5752 */
5753 static void
5754f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 int x = -1;
5757 int y = -1;
5758
5759 if (rettv_list_alloc(rettv) == FAIL)
5760 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005761#if defined(FEAT_GUI) \
5762 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5763 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005764 {
5765 varnumber_T timeout = 100;
5766
5767 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005768 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005769
5770 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005771 }
5772#endif
5773 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5774 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5775}
5776
5777
5778/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005779 * "getwinposx()" function
5780 */
5781 static void
5782f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5783{
5784 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005785#if defined(FEAT_GUI) \
5786 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5787 || defined(MSWIN)
5788
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005789 {
5790 int x, y;
5791
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005792 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005793 rettv->vval.v_number = x;
5794 }
5795#endif
5796}
5797
5798/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799 * "getwinposy()" function
5800 */
5801 static void
5802f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5803{
5804 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005805#if defined(FEAT_GUI) \
5806 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5807 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 {
5809 int x, y;
5810
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005811 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005812 rettv->vval.v_number = y;
5813 }
5814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005815}
5816
5817/*
5818 * "getwinvar()" function
5819 */
5820 static void
5821f_getwinvar(typval_T *argvars, typval_T *rettv)
5822{
5823 getwinvar(argvars, rettv, 0);
5824}
5825
5826/*
5827 * "glob()" function
5828 */
5829 static void
5830f_glob(typval_T *argvars, typval_T *rettv)
5831{
5832 int options = WILD_SILENT|WILD_USE_NL;
5833 expand_T xpc;
5834 int error = FALSE;
5835
5836 /* When the optional second argument is non-zero, don't remove matches
5837 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5838 rettv->v_type = VAR_STRING;
5839 if (argvars[1].v_type != VAR_UNKNOWN)
5840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005841 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 options |= WILD_KEEP_ALL;
5843 if (argvars[2].v_type != VAR_UNKNOWN)
5844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005845 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005846 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005848 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 options |= WILD_ALLLINKS;
5850 }
5851 }
5852 if (!error)
5853 {
5854 ExpandInit(&xpc);
5855 xpc.xp_context = EXPAND_FILES;
5856 if (p_wic)
5857 options += WILD_ICASE;
5858 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 NULL, options, WILD_ALL);
5861 else if (rettv_list_alloc(rettv) != FAIL)
5862 {
5863 int i;
5864
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 NULL, options, WILD_ALL_KEEP);
5867 for (i = 0; i < xpc.xp_numfiles; i++)
5868 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5869
5870 ExpandCleanup(&xpc);
5871 }
5872 }
5873 else
5874 rettv->vval.v_string = NULL;
5875}
5876
5877/*
5878 * "globpath()" function
5879 */
5880 static void
5881f_globpath(typval_T *argvars, typval_T *rettv)
5882{
5883 int flags = 0;
5884 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 int error = FALSE;
5887 garray_T ga;
5888 int i;
5889
5890 /* When the optional second argument is non-zero, don't remove matches
5891 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5892 rettv->v_type = VAR_STRING;
5893 if (argvars[2].v_type != VAR_UNKNOWN)
5894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005895 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 flags |= WILD_KEEP_ALL;
5897 if (argvars[3].v_type != VAR_UNKNOWN)
5898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005899 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005900 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005902 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903 flags |= WILD_ALLLINKS;
5904 }
5905 }
5906 if (file != NULL && !error)
5907 {
5908 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005909 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005910 if (rettv->v_type == VAR_STRING)
5911 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5912 else if (rettv_list_alloc(rettv) != FAIL)
5913 for (i = 0; i < ga.ga_len; ++i)
5914 list_append_string(rettv->vval.v_list,
5915 ((char_u **)(ga.ga_data))[i], -1);
5916 ga_clear_strings(&ga);
5917 }
5918 else
5919 rettv->vval.v_string = NULL;
5920}
5921
5922/*
5923 * "glob2regpat()" function
5924 */
5925 static void
5926f_glob2regpat(typval_T *argvars, typval_T *rettv)
5927{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005928 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005929
5930 rettv->v_type = VAR_STRING;
5931 rettv->vval.v_string = (pat == NULL)
5932 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5933}
5934
5935/* for VIM_VERSION_ defines */
5936#include "version.h"
5937
5938/*
5939 * "has()" function
5940 */
5941 static void
5942f_has(typval_T *argvars, typval_T *rettv)
5943{
5944 int i;
5945 char_u *name;
5946 int n = FALSE;
5947 static char *(has_list[]) =
5948 {
5949#ifdef AMIGA
5950 "amiga",
5951# ifdef FEAT_ARP
5952 "arp",
5953# endif
5954#endif
5955#ifdef __BEOS__
5956 "beos",
5957#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005958#if defined(BSD) && !defined(MACOS_X)
5959 "bsd",
5960#endif
5961#ifdef hpux
5962 "hpux",
5963#endif
5964#ifdef __linux__
5965 "linux",
5966#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005967#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005968 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5969 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005970# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005971 "macunix", /* Mac OS X, with the darwin feature */
5972 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005973# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974#endif
5975#ifdef __QNX__
5976 "qnx",
5977#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005978#ifdef SUN_SYSTEM
5979 "sun",
5980#else
5981 "moon",
5982#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983#ifdef UNIX
5984 "unix",
5985#endif
5986#ifdef VMS
5987 "vms",
5988#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005989#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990 "win32",
5991#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005992#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993 "win32unix",
5994#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005995#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996 "win64",
5997#endif
5998#ifdef EBCDIC
5999 "ebcdic",
6000#endif
6001#ifndef CASE_INSENSITIVE_FILENAME
6002 "fname_case",
6003#endif
6004#ifdef HAVE_ACL
6005 "acl",
6006#endif
6007#ifdef FEAT_ARABIC
6008 "arabic",
6009#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006011#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006012 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006013#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006014#ifdef FEAT_AUTOSERVERNAME
6015 "autoservername",
6016#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006017#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006019# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006020 "balloon_multiline",
6021# endif
6022#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006023#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006024 "balloon_eval_term",
6025#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6027 "builtin_terms",
6028# ifdef ALL_BUILTIN_TCAPS
6029 "all_builtin_terms",
6030# endif
6031#endif
6032#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006033 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034 || defined(FEAT_GUI_MOTIF))
6035 "browsefilter",
6036#endif
6037#ifdef FEAT_BYTEOFF
6038 "byte_offset",
6039#endif
6040#ifdef FEAT_JOB_CHANNEL
6041 "channel",
6042#endif
6043#ifdef FEAT_CINDENT
6044 "cindent",
6045#endif
6046#ifdef FEAT_CLIENTSERVER
6047 "clientserver",
6048#endif
6049#ifdef FEAT_CLIPBOARD
6050 "clipboard",
6051#endif
6052#ifdef FEAT_CMDL_COMPL
6053 "cmdline_compl",
6054#endif
6055#ifdef FEAT_CMDHIST
6056 "cmdline_hist",
6057#endif
6058#ifdef FEAT_COMMENTS
6059 "comments",
6060#endif
6061#ifdef FEAT_CONCEAL
6062 "conceal",
6063#endif
6064#ifdef FEAT_CRYPT
6065 "cryptv",
6066 "crypt-blowfish",
6067 "crypt-blowfish2",
6068#endif
6069#ifdef FEAT_CSCOPE
6070 "cscope",
6071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073#ifdef CURSOR_SHAPE
6074 "cursorshape",
6075#endif
6076#ifdef DEBUG
6077 "debug",
6078#endif
6079#ifdef FEAT_CON_DIALOG
6080 "dialog_con",
6081#endif
6082#ifdef FEAT_GUI_DIALOG
6083 "dialog_gui",
6084#endif
6085#ifdef FEAT_DIFF
6086 "diff",
6087#endif
6088#ifdef FEAT_DIGRAPHS
6089 "digraphs",
6090#endif
6091#ifdef FEAT_DIRECTX
6092 "directx",
6093#endif
6094#ifdef FEAT_DND
6095 "dnd",
6096#endif
6097#ifdef FEAT_EMACS_TAGS
6098 "emacs_tags",
6099#endif
6100 "eval", /* always present, of course! */
6101 "ex_extra", /* graduated feature */
6102#ifdef FEAT_SEARCH_EXTRA
6103 "extra_search",
6104#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105#ifdef FEAT_SEARCHPATH
6106 "file_in_path",
6107#endif
6108#ifdef FEAT_FILTERPIPE
6109 "filterpipe",
6110#endif
6111#ifdef FEAT_FIND_ID
6112 "find_in_path",
6113#endif
6114#ifdef FEAT_FLOAT
6115 "float",
6116#endif
6117#ifdef FEAT_FOLDING
6118 "folding",
6119#endif
6120#ifdef FEAT_FOOTER
6121 "footer",
6122#endif
6123#if !defined(USE_SYSTEM) && defined(UNIX)
6124 "fork",
6125#endif
6126#ifdef FEAT_GETTEXT
6127 "gettext",
6128#endif
6129#ifdef FEAT_GUI
6130 "gui",
6131#endif
6132#ifdef FEAT_GUI_ATHENA
6133# ifdef FEAT_GUI_NEXTAW
6134 "gui_neXtaw",
6135# else
6136 "gui_athena",
6137# endif
6138#endif
6139#ifdef FEAT_GUI_GTK
6140 "gui_gtk",
6141# ifdef USE_GTK3
6142 "gui_gtk3",
6143# else
6144 "gui_gtk2",
6145# endif
6146#endif
6147#ifdef FEAT_GUI_GNOME
6148 "gui_gnome",
6149#endif
6150#ifdef FEAT_GUI_MAC
6151 "gui_mac",
6152#endif
6153#ifdef FEAT_GUI_MOTIF
6154 "gui_motif",
6155#endif
6156#ifdef FEAT_GUI_PHOTON
6157 "gui_photon",
6158#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006159#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 "gui_win32",
6161#endif
6162#ifdef FEAT_HANGULIN
6163 "hangul_input",
6164#endif
6165#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6166 "iconv",
6167#endif
6168#ifdef FEAT_INS_EXPAND
6169 "insert_expand",
6170#endif
6171#ifdef FEAT_JOB_CHANNEL
6172 "job",
6173#endif
6174#ifdef FEAT_JUMPLIST
6175 "jumplist",
6176#endif
6177#ifdef FEAT_KEYMAP
6178 "keymap",
6179#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006180 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181#ifdef FEAT_LANGMAP
6182 "langmap",
6183#endif
6184#ifdef FEAT_LIBCALL
6185 "libcall",
6186#endif
6187#ifdef FEAT_LINEBREAK
6188 "linebreak",
6189#endif
6190#ifdef FEAT_LISP
6191 "lispindent",
6192#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194#ifdef FEAT_LOCALMAP
6195 "localmap",
6196#endif
6197#ifdef FEAT_LUA
6198# ifndef DYNAMIC_LUA
6199 "lua",
6200# endif
6201#endif
6202#ifdef FEAT_MENU
6203 "menu",
6204#endif
6205#ifdef FEAT_SESSION
6206 "mksession",
6207#endif
6208#ifdef FEAT_MODIFY_FNAME
6209 "modify_fname",
6210#endif
6211#ifdef FEAT_MOUSE
6212 "mouse",
6213#endif
6214#ifdef FEAT_MOUSESHAPE
6215 "mouseshape",
6216#endif
6217#if defined(UNIX) || defined(VMS)
6218# ifdef FEAT_MOUSE_DEC
6219 "mouse_dec",
6220# endif
6221# ifdef FEAT_MOUSE_GPM
6222 "mouse_gpm",
6223# endif
6224# ifdef FEAT_MOUSE_JSB
6225 "mouse_jsbterm",
6226# endif
6227# ifdef FEAT_MOUSE_NET
6228 "mouse_netterm",
6229# endif
6230# ifdef FEAT_MOUSE_PTERM
6231 "mouse_pterm",
6232# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006233# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 "mouse_sgr",
6235# endif
6236# ifdef FEAT_SYSMOUSE
6237 "mouse_sysmouse",
6238# endif
6239# ifdef FEAT_MOUSE_URXVT
6240 "mouse_urxvt",
6241# endif
6242# ifdef FEAT_MOUSE_XTERM
6243 "mouse_xterm",
6244# endif
6245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247#ifdef FEAT_MBYTE_IME
6248 "multi_byte_ime",
6249#endif
6250#ifdef FEAT_MULTI_LANG
6251 "multi_lang",
6252#endif
6253#ifdef FEAT_MZSCHEME
6254#ifndef DYNAMIC_MZSCHEME
6255 "mzscheme",
6256#endif
6257#endif
6258#ifdef FEAT_NUM64
6259 "num64",
6260#endif
6261#ifdef FEAT_OLE
6262 "ole",
6263#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006264#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006266#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267#ifdef FEAT_PATH_EXTRA
6268 "path_extra",
6269#endif
6270#ifdef FEAT_PERL
6271#ifndef DYNAMIC_PERL
6272 "perl",
6273#endif
6274#endif
6275#ifdef FEAT_PERSISTENT_UNDO
6276 "persistent_undo",
6277#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006278#if defined(FEAT_PYTHON)
6279 "python_compiled",
6280# if defined(DYNAMIC_PYTHON)
6281 "python_dynamic",
6282# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006284 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006285# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006287#if defined(FEAT_PYTHON3)
6288 "python3_compiled",
6289# if defined(DYNAMIC_PYTHON3)
6290 "python3_dynamic",
6291# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006293 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006294# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295#endif
6296#ifdef FEAT_POSTSCRIPT
6297 "postscript",
6298#endif
6299#ifdef FEAT_PRINTER
6300 "printer",
6301#endif
6302#ifdef FEAT_PROFILE
6303 "profile",
6304#endif
6305#ifdef FEAT_RELTIME
6306 "reltime",
6307#endif
6308#ifdef FEAT_QUICKFIX
6309 "quickfix",
6310#endif
6311#ifdef FEAT_RIGHTLEFT
6312 "rightleft",
6313#endif
6314#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6315 "ruby",
6316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318#ifdef FEAT_CMDL_INFO
6319 "showcmd",
6320 "cmdline_info",
6321#endif
6322#ifdef FEAT_SIGNS
6323 "signs",
6324#endif
6325#ifdef FEAT_SMARTINDENT
6326 "smartindent",
6327#endif
6328#ifdef STARTUPTIME
6329 "startuptime",
6330#endif
6331#ifdef FEAT_STL_OPT
6332 "statusline",
6333#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334#ifdef FEAT_NETBEANS_INTG
6335 "netbeans_intg",
6336#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006337#ifdef FEAT_SOUND
6338 "sound",
6339#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340#ifdef FEAT_SPELL
6341 "spell",
6342#endif
6343#ifdef FEAT_SYN_HL
6344 "syntax",
6345#endif
6346#if defined(USE_SYSTEM) || !defined(UNIX)
6347 "system",
6348#endif
6349#ifdef FEAT_TAG_BINS
6350 "tag_binary",
6351#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352#ifdef FEAT_TCL
6353# ifndef DYNAMIC_TCL
6354 "tcl",
6355# endif
6356#endif
6357#ifdef FEAT_TERMGUICOLORS
6358 "termguicolors",
6359#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006360#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006361 "terminal",
6362#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363#ifdef TERMINFO
6364 "terminfo",
6365#endif
6366#ifdef FEAT_TERMRESPONSE
6367 "termresponse",
6368#endif
6369#ifdef FEAT_TEXTOBJ
6370 "textobjects",
6371#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006372#ifdef FEAT_TEXT_PROP
6373 "textprop",
6374#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006375#ifdef HAVE_TGETENT
6376 "tgetent",
6377#endif
6378#ifdef FEAT_TIMERS
6379 "timers",
6380#endif
6381#ifdef FEAT_TITLE
6382 "title",
6383#endif
6384#ifdef FEAT_TOOLBAR
6385 "toolbar",
6386#endif
6387#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6388 "unnamedplus",
6389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006390 "user-commands", /* was accidentally included in 5.4 */
6391 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006392#ifdef FEAT_VARTABS
6393 "vartabs",
6394#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006395 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396#ifdef FEAT_VIMINFO
6397 "viminfo",
6398#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006399 "vimscript-1",
6400 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006401 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006406#ifdef FEAT_VTP
6407 "vtp",
6408#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409#ifdef FEAT_WILDIGN
6410 "wildignore",
6411#endif
6412#ifdef FEAT_WILDMENU
6413 "wildmenu",
6414#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006416#ifdef FEAT_WAK
6417 "winaltkeys",
6418#endif
6419#ifdef FEAT_WRITEBACKUP
6420 "writebackup",
6421#endif
6422#ifdef FEAT_XIM
6423 "xim",
6424#endif
6425#ifdef FEAT_XFONTSET
6426 "xfontset",
6427#endif
6428#ifdef FEAT_XPM_W32
6429 "xpm",
6430 "xpm_w32", /* for backward compatibility */
6431#else
6432# if defined(HAVE_XPM)
6433 "xpm",
6434# endif
6435#endif
6436#ifdef USE_XSMP
6437 "xsmp",
6438#endif
6439#ifdef USE_XSMP_INTERACT
6440 "xsmp_interact",
6441#endif
6442#ifdef FEAT_XCLIPBOARD
6443 "xterm_clipboard",
6444#endif
6445#ifdef FEAT_XTERM_SAVE
6446 "xterm_save",
6447#endif
6448#if defined(UNIX) && defined(FEAT_X11)
6449 "X11",
6450#endif
6451 NULL
6452 };
6453
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006454 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455 for (i = 0; has_list[i] != NULL; ++i)
6456 if (STRICMP(name, has_list[i]) == 0)
6457 {
6458 n = TRUE;
6459 break;
6460 }
6461
6462 if (n == FALSE)
6463 {
6464 if (STRNICMP(name, "patch", 5) == 0)
6465 {
6466 if (name[5] == '-'
6467 && STRLEN(name) >= 11
6468 && vim_isdigit(name[6])
6469 && vim_isdigit(name[8])
6470 && vim_isdigit(name[10]))
6471 {
6472 int major = atoi((char *)name + 6);
6473 int minor = atoi((char *)name + 8);
6474
6475 /* Expect "patch-9.9.01234". */
6476 n = (major < VIM_VERSION_MAJOR
6477 || (major == VIM_VERSION_MAJOR
6478 && (minor < VIM_VERSION_MINOR
6479 || (minor == VIM_VERSION_MINOR
6480 && has_patch(atoi((char *)name + 10))))));
6481 }
6482 else
6483 n = has_patch(atoi((char *)name + 5));
6484 }
6485 else if (STRICMP(name, "vim_starting") == 0)
6486 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006487 else if (STRICMP(name, "ttyin") == 0)
6488 n = mch_input_isatty();
6489 else if (STRICMP(name, "ttyout") == 0)
6490 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 else if (STRICMP(name, "multi_byte_encoding") == 0)
6492 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006493#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 else if (STRICMP(name, "balloon_multiline") == 0)
6495 n = multiline_balloon_available();
6496#endif
6497#ifdef DYNAMIC_TCL
6498 else if (STRICMP(name, "tcl") == 0)
6499 n = tcl_enabled(FALSE);
6500#endif
6501#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6502 else if (STRICMP(name, "iconv") == 0)
6503 n = iconv_enabled(FALSE);
6504#endif
6505#ifdef DYNAMIC_LUA
6506 else if (STRICMP(name, "lua") == 0)
6507 n = lua_enabled(FALSE);
6508#endif
6509#ifdef DYNAMIC_MZSCHEME
6510 else if (STRICMP(name, "mzscheme") == 0)
6511 n = mzscheme_enabled(FALSE);
6512#endif
6513#ifdef DYNAMIC_RUBY
6514 else if (STRICMP(name, "ruby") == 0)
6515 n = ruby_enabled(FALSE);
6516#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef DYNAMIC_PYTHON
6518 else if (STRICMP(name, "python") == 0)
6519 n = python_enabled(FALSE);
6520#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef DYNAMIC_PYTHON3
6522 else if (STRICMP(name, "python3") == 0)
6523 n = python3_enabled(FALSE);
6524#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006525#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6526 else if (STRICMP(name, "pythonx") == 0)
6527 {
6528# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6529 if (p_pyx == 0)
6530 n = python3_enabled(FALSE) || python_enabled(FALSE);
6531 else if (p_pyx == 3)
6532 n = python3_enabled(FALSE);
6533 else if (p_pyx == 2)
6534 n = python_enabled(FALSE);
6535# elif defined(DYNAMIC_PYTHON)
6536 n = python_enabled(FALSE);
6537# elif defined(DYNAMIC_PYTHON3)
6538 n = python3_enabled(FALSE);
6539# endif
6540 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541#endif
6542#ifdef DYNAMIC_PERL
6543 else if (STRICMP(name, "perl") == 0)
6544 n = perl_enabled(FALSE);
6545#endif
6546#ifdef FEAT_GUI
6547 else if (STRICMP(name, "gui_running") == 0)
6548 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549# ifdef FEAT_BROWSE
6550 else if (STRICMP(name, "browse") == 0)
6551 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6552# endif
6553#endif
6554#ifdef FEAT_SYN_HL
6555 else if (STRICMP(name, "syntax_items") == 0)
6556 n = syntax_present(curwin);
6557#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006558#ifdef FEAT_VTP
6559 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006560 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561#endif
6562#ifdef FEAT_NETBEANS_INTG
6563 else if (STRICMP(name, "netbeans_enabled") == 0)
6564 n = netbeans_active();
6565#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006566#ifdef FEAT_MOUSE_GPM
6567 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6568 n = gpm_enabled();
6569#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006570#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006571 else if (STRICMP(name, "terminal") == 0)
6572 n = terminal_enabled();
6573#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006574#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006575 else if (STRICMP(name, "conpty") == 0)
6576 n = use_conpty();
6577#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578 }
6579
6580 rettv->vval.v_number = n;
6581}
6582
6583/*
6584 * "has_key()" function
6585 */
6586 static void
6587f_has_key(typval_T *argvars, typval_T *rettv)
6588{
6589 if (argvars[0].v_type != VAR_DICT)
6590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006591 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 return;
6593 }
6594 if (argvars[0].vval.v_dict == NULL)
6595 return;
6596
6597 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006598 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599}
6600
6601/*
6602 * "haslocaldir()" function
6603 */
6604 static void
6605f_haslocaldir(typval_T *argvars, typval_T *rettv)
6606{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006607 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 win_T *wp = NULL;
6609
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006610 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6611
6612 // Check for window-local and tab-local directories
6613 if (wp != NULL && wp->w_localdir != NULL)
6614 rettv->vval.v_number = 1;
6615 else if (tp != NULL && tp->tp_localdir != NULL)
6616 rettv->vval.v_number = 2;
6617 else
6618 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619}
6620
6621/*
6622 * "hasmapto()" function
6623 */
6624 static void
6625f_hasmapto(typval_T *argvars, typval_T *rettv)
6626{
6627 char_u *name;
6628 char_u *mode;
6629 char_u buf[NUMBUFLEN];
6630 int abbr = FALSE;
6631
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006632 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 if (argvars[1].v_type == VAR_UNKNOWN)
6634 mode = (char_u *)"nvo";
6635 else
6636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006637 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006639 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 }
6641
6642 if (map_to_exists(name, mode, abbr))
6643 rettv->vval.v_number = TRUE;
6644 else
6645 rettv->vval.v_number = FALSE;
6646}
6647
6648/*
6649 * "histadd()" function
6650 */
6651 static void
6652f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6653{
6654#ifdef FEAT_CMDHIST
6655 int histype;
6656 char_u *str;
6657 char_u buf[NUMBUFLEN];
6658#endif
6659
6660 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006661 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 return;
6663#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006664 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 histype = str != NULL ? get_histtype(str) : -1;
6666 if (histype >= 0)
6667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006668 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 if (*str != NUL)
6670 {
6671 init_history();
6672 add_to_history(histype, str, FALSE, NUL);
6673 rettv->vval.v_number = TRUE;
6674 return;
6675 }
6676 }
6677#endif
6678}
6679
6680/*
6681 * "histdel()" function
6682 */
6683 static void
6684f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6685{
6686#ifdef FEAT_CMDHIST
6687 int n;
6688 char_u buf[NUMBUFLEN];
6689 char_u *str;
6690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006691 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 if (str == NULL)
6693 n = 0;
6694 else if (argvars[1].v_type == VAR_UNKNOWN)
6695 /* only one argument: clear entire history */
6696 n = clr_history(get_histtype(str));
6697 else if (argvars[1].v_type == VAR_NUMBER)
6698 /* index given: remove that entry */
6699 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006700 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 else
6702 /* string given: remove all matching entries */
6703 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006704 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705 rettv->vval.v_number = n;
6706#endif
6707}
6708
6709/*
6710 * "histget()" function
6711 */
6712 static void
6713f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6714{
6715#ifdef FEAT_CMDHIST
6716 int type;
6717 int idx;
6718 char_u *str;
6719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006720 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721 if (str == NULL)
6722 rettv->vval.v_string = NULL;
6723 else
6724 {
6725 type = get_histtype(str);
6726 if (argvars[1].v_type == VAR_UNKNOWN)
6727 idx = get_history_idx(type);
6728 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006729 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 /* -1 on type error */
6731 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6732 }
6733#else
6734 rettv->vval.v_string = NULL;
6735#endif
6736 rettv->v_type = VAR_STRING;
6737}
6738
6739/*
6740 * "histnr()" function
6741 */
6742 static void
6743f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6744{
6745 int i;
6746
6747#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006748 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749
6750 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6751 if (i >= HIST_CMD && i < HIST_COUNT)
6752 i = get_history_idx(i);
6753 else
6754#endif
6755 i = -1;
6756 rettv->vval.v_number = i;
6757}
6758
6759/*
6760 * "highlightID(name)" function
6761 */
6762 static void
6763f_hlID(typval_T *argvars, typval_T *rettv)
6764{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006765 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766}
6767
6768/*
6769 * "highlight_exists()" function
6770 */
6771 static void
6772f_hlexists(typval_T *argvars, typval_T *rettv)
6773{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006774 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775}
6776
6777/*
6778 * "hostname()" function
6779 */
6780 static void
6781f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6782{
6783 char_u hostname[256];
6784
6785 mch_get_host_name(hostname, 256);
6786 rettv->v_type = VAR_STRING;
6787 rettv->vval.v_string = vim_strsave(hostname);
6788}
6789
6790/*
6791 * iconv() function
6792 */
6793 static void
6794f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6795{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 char_u buf1[NUMBUFLEN];
6797 char_u buf2[NUMBUFLEN];
6798 char_u *from, *to, *str;
6799 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006800
6801 rettv->v_type = VAR_STRING;
6802 rettv->vval.v_string = NULL;
6803
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006804 str = tv_get_string(&argvars[0]);
6805 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6806 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 vimconv.vc_type = CONV_NONE;
6808 convert_setup(&vimconv, from, to);
6809
6810 /* If the encodings are equal, no conversion needed. */
6811 if (vimconv.vc_type == CONV_NONE)
6812 rettv->vval.v_string = vim_strsave(str);
6813 else
6814 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6815
6816 convert_setup(&vimconv, NULL, NULL);
6817 vim_free(from);
6818 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819}
6820
6821/*
6822 * "indent()" function
6823 */
6824 static void
6825f_indent(typval_T *argvars, typval_T *rettv)
6826{
6827 linenr_T lnum;
6828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006829 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6831 rettv->vval.v_number = get_indent_lnum(lnum);
6832 else
6833 rettv->vval.v_number = -1;
6834}
6835
6836/*
6837 * "index()" function
6838 */
6839 static void
6840f_index(typval_T *argvars, typval_T *rettv)
6841{
6842 list_T *l;
6843 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006844 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845 long idx = 0;
6846 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006847 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848
6849 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006850 if (argvars[0].v_type == VAR_BLOB)
6851 {
6852 typval_T tv;
6853 int start = 0;
6854
6855 if (argvars[2].v_type != VAR_UNKNOWN)
6856 {
6857 start = tv_get_number_chk(&argvars[2], &error);
6858 if (error)
6859 return;
6860 }
6861 b = argvars[0].vval.v_blob;
6862 if (b == NULL)
6863 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006864 if (start < 0)
6865 {
6866 start = blob_len(b) + start;
6867 if (start < 0)
6868 start = 0;
6869 }
6870
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006871 for (idx = start; idx < blob_len(b); ++idx)
6872 {
6873 tv.v_type = VAR_NUMBER;
6874 tv.vval.v_number = blob_get(b, idx);
6875 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6876 {
6877 rettv->vval.v_number = idx;
6878 return;
6879 }
6880 }
6881 return;
6882 }
6883 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006885 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 return;
6887 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 l = argvars[0].vval.v_list;
6890 if (l != NULL)
6891 {
6892 item = l->lv_first;
6893 if (argvars[2].v_type != VAR_UNKNOWN)
6894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 /* Start at specified item. Use the cached index that list_find()
6896 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006897 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 idx = l->lv_idx;
6899 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (error)
6902 item = NULL;
6903 }
6904
6905 for ( ; item != NULL; item = item->li_next, ++idx)
6906 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6907 {
6908 rettv->vval.v_number = idx;
6909 break;
6910 }
6911 }
6912}
6913
6914static int inputsecret_flag = 0;
6915
6916/*
6917 * "input()" function
6918 * Also handles inputsecret() when inputsecret is set.
6919 */
6920 static void
6921f_input(typval_T *argvars, typval_T *rettv)
6922{
6923 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6924}
6925
6926/*
6927 * "inputdialog()" function
6928 */
6929 static void
6930f_inputdialog(typval_T *argvars, typval_T *rettv)
6931{
6932#if defined(FEAT_GUI_TEXTDIALOG)
6933 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6934 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6935 {
6936 char_u *message;
6937 char_u buf[NUMBUFLEN];
6938 char_u *defstr = (char_u *)"";
6939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006940 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006941 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6944 else
6945 IObuff[0] = NUL;
6946 if (message != NULL && defstr != NULL
6947 && do_dialog(VIM_QUESTION, NULL, message,
6948 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6949 rettv->vval.v_string = vim_strsave(IObuff);
6950 else
6951 {
6952 if (message != NULL && defstr != NULL
6953 && argvars[1].v_type != VAR_UNKNOWN
6954 && argvars[2].v_type != VAR_UNKNOWN)
6955 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 else
6958 rettv->vval.v_string = NULL;
6959 }
6960 rettv->v_type = VAR_STRING;
6961 }
6962 else
6963#endif
6964 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6965}
6966
6967/*
6968 * "inputlist()" function
6969 */
6970 static void
6971f_inputlist(typval_T *argvars, typval_T *rettv)
6972{
6973 listitem_T *li;
6974 int selected;
6975 int mouse_used;
6976
6977#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006978 /* While starting up, there is no place to enter text. When running tests
6979 * with --not-a-term we assume feedkeys() will be used. */
6980 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981 return;
6982#endif
6983 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006985 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 return;
6987 }
6988
6989 msg_start();
6990 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6991 lines_left = Rows; /* avoid more prompt */
6992 msg_scroll = TRUE;
6993 msg_clr_eos();
6994
6995 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6996 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006997 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998 msg_putchar('\n');
6999 }
7000
7001 /* Ask for choice. */
7002 selected = prompt_for_number(&mouse_used);
7003 if (mouse_used)
7004 selected -= lines_left;
7005
7006 rettv->vval.v_number = selected;
7007}
7008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7010
7011/*
7012 * "inputrestore()" function
7013 */
7014 static void
7015f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7016{
7017 if (ga_userinput.ga_len > 0)
7018 {
7019 --ga_userinput.ga_len;
7020 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7021 + ga_userinput.ga_len);
7022 /* default return is zero == OK */
7023 }
7024 else if (p_verbose > 1)
7025 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007026 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 rettv->vval.v_number = 1; /* Failed */
7028 }
7029}
7030
7031/*
7032 * "inputsave()" function
7033 */
7034 static void
7035f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7036{
7037 /* Add an entry to the stack of typeahead storage. */
7038 if (ga_grow(&ga_userinput, 1) == OK)
7039 {
7040 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7041 + ga_userinput.ga_len);
7042 ++ga_userinput.ga_len;
7043 /* default return is zero == OK */
7044 }
7045 else
7046 rettv->vval.v_number = 1; /* Failed */
7047}
7048
7049/*
7050 * "inputsecret()" function
7051 */
7052 static void
7053f_inputsecret(typval_T *argvars, typval_T *rettv)
7054{
7055 ++cmdline_star;
7056 ++inputsecret_flag;
7057 f_input(argvars, rettv);
7058 --cmdline_star;
7059 --inputsecret_flag;
7060}
7061
7062/*
7063 * "insert()" function
7064 */
7065 static void
7066f_insert(typval_T *argvars, typval_T *rettv)
7067{
7068 long before = 0;
7069 listitem_T *item;
7070 list_T *l;
7071 int error = FALSE;
7072
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007073 if (argvars[0].v_type == VAR_BLOB)
7074 {
7075 int val, len;
7076 char_u *p;
7077
7078 len = blob_len(argvars[0].vval.v_blob);
7079 if (argvars[2].v_type != VAR_UNKNOWN)
7080 {
7081 before = (long)tv_get_number_chk(&argvars[2], &error);
7082 if (error)
7083 return; // type error; errmsg already given
7084 if (before < 0 || before > len)
7085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007087 return;
7088 }
7089 }
7090 val = tv_get_number_chk(&argvars[1], &error);
7091 if (error)
7092 return;
7093 if (val < 0 || val > 255)
7094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007095 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007096 return;
7097 }
7098
7099 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7100 return;
7101 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7102 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7103 *(p + before) = val;
7104 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7105
7106 copy_tv(&argvars[0], rettv);
7107 }
7108 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007109 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007110 else if ((l = argvars[0].vval.v_list) != NULL
7111 && !var_check_lock(l->lv_lock,
7112 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 {
7114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007115 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 if (error)
7117 return; /* type error; errmsg already given */
7118
7119 if (before == l->lv_len)
7120 item = NULL;
7121 else
7122 {
7123 item = list_find(l, before);
7124 if (item == NULL)
7125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007126 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007127 l = NULL;
7128 }
7129 }
7130 if (l != NULL)
7131 {
7132 list_insert_tv(l, &argvars[1], item);
7133 copy_tv(&argvars[0], rettv);
7134 }
7135 }
7136}
7137
7138/*
7139 * "invert(expr)" function
7140 */
7141 static void
7142f_invert(typval_T *argvars, typval_T *rettv)
7143{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007144 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145}
7146
7147/*
7148 * "isdirectory()" function
7149 */
7150 static void
7151f_isdirectory(typval_T *argvars, typval_T *rettv)
7152{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007153 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154}
7155
7156/*
7157 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7158 * or it refers to a List or Dictionary that is locked.
7159 */
7160 static int
7161tv_islocked(typval_T *tv)
7162{
7163 return (tv->v_lock & VAR_LOCKED)
7164 || (tv->v_type == VAR_LIST
7165 && tv->vval.v_list != NULL
7166 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7167 || (tv->v_type == VAR_DICT
7168 && tv->vval.v_dict != NULL
7169 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7170}
7171
7172/*
7173 * "islocked()" function
7174 */
7175 static void
7176f_islocked(typval_T *argvars, typval_T *rettv)
7177{
7178 lval_T lv;
7179 char_u *end;
7180 dictitem_T *di;
7181
7182 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007184 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 if (end != NULL && lv.ll_name != NULL)
7186 {
7187 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007188 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 else
7190 {
7191 if (lv.ll_tv == NULL)
7192 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007193 di = find_var(lv.ll_name, NULL, TRUE);
7194 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007195 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007196 /* Consider a variable locked when:
7197 * 1. the variable itself is locked
7198 * 2. the value of the variable is locked.
7199 * 3. the List or Dict value is locked.
7200 */
7201 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7202 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203 }
7204 }
7205 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007206 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007208 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 else if (lv.ll_list != NULL)
7210 /* List item. */
7211 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7212 else
7213 /* Dictionary item. */
7214 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7215 }
7216 }
7217
7218 clear_lval(&lv);
7219}
7220
7221#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7222/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007223 * "isinf()" function
7224 */
7225 static void
7226f_isinf(typval_T *argvars, typval_T *rettv)
7227{
7228 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7229 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7230}
7231
7232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 * "isnan()" function
7234 */
7235 static void
7236f_isnan(typval_T *argvars, typval_T *rettv)
7237{
7238 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7239 && isnan(argvars[0].vval.v_float);
7240}
7241#endif
7242
7243/*
7244 * "items(dict)" function
7245 */
7246 static void
7247f_items(typval_T *argvars, typval_T *rettv)
7248{
7249 dict_list(argvars, rettv, 2);
7250}
7251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252/*
7253 * "join()" function
7254 */
7255 static void
7256f_join(typval_T *argvars, typval_T *rettv)
7257{
7258 garray_T ga;
7259 char_u *sep;
7260
7261 if (argvars[0].v_type != VAR_LIST)
7262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264 return;
7265 }
7266 if (argvars[0].vval.v_list == NULL)
7267 return;
7268 if (argvars[1].v_type == VAR_UNKNOWN)
7269 sep = (char_u *)" ";
7270 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007271 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272
7273 rettv->v_type = VAR_STRING;
7274
7275 if (sep != NULL)
7276 {
7277 ga_init2(&ga, (int)sizeof(char), 80);
7278 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7279 ga_append(&ga, NUL);
7280 rettv->vval.v_string = (char_u *)ga.ga_data;
7281 }
7282 else
7283 rettv->vval.v_string = NULL;
7284}
7285
7286/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 * "keys()" function
7288 */
7289 static void
7290f_keys(typval_T *argvars, typval_T *rettv)
7291{
7292 dict_list(argvars, rettv, 0);
7293}
7294
7295/*
7296 * "last_buffer_nr()" function.
7297 */
7298 static void
7299f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7300{
7301 int n = 0;
7302 buf_T *buf;
7303
Bram Moolenaar29323592016-07-24 22:04:11 +02007304 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305 if (n < buf->b_fnum)
7306 n = buf->b_fnum;
7307
7308 rettv->vval.v_number = n;
7309}
7310
7311/*
7312 * "len()" function
7313 */
7314 static void
7315f_len(typval_T *argvars, typval_T *rettv)
7316{
7317 switch (argvars[0].v_type)
7318 {
7319 case VAR_STRING:
7320 case VAR_NUMBER:
7321 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007322 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007324 case VAR_BLOB:
7325 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7326 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 case VAR_LIST:
7328 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7329 break;
7330 case VAR_DICT:
7331 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7332 break;
7333 case VAR_UNKNOWN:
7334 case VAR_SPECIAL:
7335 case VAR_FLOAT:
7336 case VAR_FUNC:
7337 case VAR_PARTIAL:
7338 case VAR_JOB:
7339 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007340 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 break;
7342 }
7343}
7344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007346libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347{
7348#ifdef FEAT_LIBCALL
7349 char_u *string_in;
7350 char_u **string_result;
7351 int nr_result;
7352#endif
7353
7354 rettv->v_type = type;
7355 if (type != VAR_NUMBER)
7356 rettv->vval.v_string = NULL;
7357
7358 if (check_restricted() || check_secure())
7359 return;
7360
7361#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007362 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7364 {
7365 string_in = NULL;
7366 if (argvars[2].v_type == VAR_STRING)
7367 string_in = argvars[2].vval.v_string;
7368 if (type == VAR_NUMBER)
7369 string_result = NULL;
7370 else
7371 string_result = &rettv->vval.v_string;
7372 if (mch_libcall(argvars[0].vval.v_string,
7373 argvars[1].vval.v_string,
7374 string_in,
7375 argvars[2].vval.v_number,
7376 string_result,
7377 &nr_result) == OK
7378 && type == VAR_NUMBER)
7379 rettv->vval.v_number = nr_result;
7380 }
7381#endif
7382}
7383
7384/*
7385 * "libcall()" function
7386 */
7387 static void
7388f_libcall(typval_T *argvars, typval_T *rettv)
7389{
7390 libcall_common(argvars, rettv, VAR_STRING);
7391}
7392
7393/*
7394 * "libcallnr()" function
7395 */
7396 static void
7397f_libcallnr(typval_T *argvars, typval_T *rettv)
7398{
7399 libcall_common(argvars, rettv, VAR_NUMBER);
7400}
7401
7402/*
7403 * "line(string)" function
7404 */
7405 static void
7406f_line(typval_T *argvars, typval_T *rettv)
7407{
7408 linenr_T lnum = 0;
7409 pos_T *fp;
7410 int fnum;
7411
7412 fp = var2fpos(&argvars[0], TRUE, &fnum);
7413 if (fp != NULL)
7414 lnum = fp->lnum;
7415 rettv->vval.v_number = lnum;
7416}
7417
7418/*
7419 * "line2byte(lnum)" function
7420 */
7421 static void
7422f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7423{
7424#ifndef FEAT_BYTEOFF
7425 rettv->vval.v_number = -1;
7426#else
7427 linenr_T lnum;
7428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007429 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7431 rettv->vval.v_number = -1;
7432 else
7433 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7434 if (rettv->vval.v_number >= 0)
7435 ++rettv->vval.v_number;
7436#endif
7437}
7438
7439/*
7440 * "lispindent(lnum)" function
7441 */
7442 static void
7443f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7444{
7445#ifdef FEAT_LISP
7446 pos_T pos;
7447 linenr_T lnum;
7448
7449 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007450 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7452 {
7453 curwin->w_cursor.lnum = lnum;
7454 rettv->vval.v_number = get_lisp_indent();
7455 curwin->w_cursor = pos;
7456 }
7457 else
7458#endif
7459 rettv->vval.v_number = -1;
7460}
7461
7462/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007463 * "list2str()" function
7464 */
7465 static void
7466f_list2str(typval_T *argvars, typval_T *rettv)
7467{
7468 list_T *l;
7469 listitem_T *li;
7470 garray_T ga;
7471 int utf8 = FALSE;
7472
7473 rettv->v_type = VAR_STRING;
7474 rettv->vval.v_string = NULL;
7475 if (argvars[0].v_type != VAR_LIST)
7476 {
7477 emsg(_(e_invarg));
7478 return;
7479 }
7480
7481 l = argvars[0].vval.v_list;
7482 if (l == NULL)
7483 return; // empty list results in empty string
7484
7485 if (argvars[1].v_type != VAR_UNKNOWN)
7486 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7487
7488 ga_init2(&ga, 1, 80);
7489 if (has_mbyte || utf8)
7490 {
7491 char_u buf[MB_MAXBYTES + 1];
7492 int (*char2bytes)(int, char_u *);
7493
7494 if (utf8 || enc_utf8)
7495 char2bytes = utf_char2bytes;
7496 else
7497 char2bytes = mb_char2bytes;
7498
7499 for (li = l->lv_first; li != NULL; li = li->li_next)
7500 {
7501 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7502 ga_concat(&ga, buf);
7503 }
7504 ga_append(&ga, NUL);
7505 }
7506 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7507 {
7508 for (li = l->lv_first; li != NULL; li = li->li_next)
7509 ga_append(&ga, tv_get_number(&li->li_tv));
7510 ga_append(&ga, NUL);
7511 }
7512
7513 rettv->v_type = VAR_STRING;
7514 rettv->vval.v_string = ga.ga_data;
7515}
7516
7517/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518 * "localtime()" function
7519 */
7520 static void
7521f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7522{
7523 rettv->vval.v_number = (varnumber_T)time(NULL);
7524}
7525
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 static void
7527get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7528{
7529 char_u *keys;
7530 char_u *which;
7531 char_u buf[NUMBUFLEN];
7532 char_u *keys_buf = NULL;
7533 char_u *rhs;
7534 int mode;
7535 int abbr = FALSE;
7536 int get_dict = FALSE;
7537 mapblock_T *mp;
7538 int buffer_local;
7539
7540 /* return empty string for failure */
7541 rettv->v_type = VAR_STRING;
7542 rettv->vval.v_string = NULL;
7543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007544 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 if (*keys == NUL)
7546 return;
7547
7548 if (argvars[1].v_type != VAR_UNKNOWN)
7549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007550 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551 if (argvars[2].v_type != VAR_UNKNOWN)
7552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 }
7557 }
7558 else
7559 which = (char_u *)"";
7560 if (which == NULL)
7561 return;
7562
7563 mode = get_map_mode(&which, 0);
7564
7565 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7566 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7567 vim_free(keys_buf);
7568
7569 if (!get_dict)
7570 {
7571 /* Return a string. */
7572 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007573 {
7574 if (*rhs == NUL)
7575 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7576 else
7577 rettv->vval.v_string = str2special_save(rhs, FALSE);
7578 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579
7580 }
7581 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7582 {
7583 /* Return a dictionary. */
7584 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7585 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7586 dict_T *dict = rettv->vval.v_dict;
7587
Bram Moolenaare0be1672018-07-08 16:50:37 +02007588 dict_add_string(dict, "lhs", lhs);
7589 dict_add_string(dict, "rhs", mp->m_orig_str);
7590 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7591 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7592 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007593 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7594 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007595 dict_add_number(dict, "buffer", (long)buffer_local);
7596 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7597 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598
7599 vim_free(lhs);
7600 vim_free(mapmode);
7601 }
7602}
7603
7604#ifdef FEAT_FLOAT
7605/*
7606 * "log()" function
7607 */
7608 static void
7609f_log(typval_T *argvars, typval_T *rettv)
7610{
7611 float_T f = 0.0;
7612
7613 rettv->v_type = VAR_FLOAT;
7614 if (get_float_arg(argvars, &f) == OK)
7615 rettv->vval.v_float = log(f);
7616 else
7617 rettv->vval.v_float = 0.0;
7618}
7619
7620/*
7621 * "log10()" function
7622 */
7623 static void
7624f_log10(typval_T *argvars, typval_T *rettv)
7625{
7626 float_T f = 0.0;
7627
7628 rettv->v_type = VAR_FLOAT;
7629 if (get_float_arg(argvars, &f) == OK)
7630 rettv->vval.v_float = log10(f);
7631 else
7632 rettv->vval.v_float = 0.0;
7633}
7634#endif
7635
7636#ifdef FEAT_LUA
7637/*
7638 * "luaeval()" function
7639 */
7640 static void
7641f_luaeval(typval_T *argvars, typval_T *rettv)
7642{
7643 char_u *str;
7644 char_u buf[NUMBUFLEN];
7645
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007646 if (check_restricted() || check_secure())
7647 return;
7648
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007649 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 do_luaeval(str, argvars + 1, rettv);
7651}
7652#endif
7653
7654/*
7655 * "map()" function
7656 */
7657 static void
7658f_map(typval_T *argvars, typval_T *rettv)
7659{
7660 filter_map(argvars, rettv, TRUE);
7661}
7662
7663/*
7664 * "maparg()" function
7665 */
7666 static void
7667f_maparg(typval_T *argvars, typval_T *rettv)
7668{
7669 get_maparg(argvars, rettv, TRUE);
7670}
7671
7672/*
7673 * "mapcheck()" function
7674 */
7675 static void
7676f_mapcheck(typval_T *argvars, typval_T *rettv)
7677{
7678 get_maparg(argvars, rettv, FALSE);
7679}
7680
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007681typedef enum
7682{
7683 MATCH_END, /* matchend() */
7684 MATCH_MATCH, /* match() */
7685 MATCH_STR, /* matchstr() */
7686 MATCH_LIST, /* matchlist() */
7687 MATCH_POS /* matchstrpos() */
7688} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689
7690 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007691find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692{
7693 char_u *str = NULL;
7694 long len = 0;
7695 char_u *expr = NULL;
7696 char_u *pat;
7697 regmatch_T regmatch;
7698 char_u patbuf[NUMBUFLEN];
7699 char_u strbuf[NUMBUFLEN];
7700 char_u *save_cpo;
7701 long start = 0;
7702 long nth = 1;
7703 colnr_T startcol = 0;
7704 int match = 0;
7705 list_T *l = NULL;
7706 listitem_T *li = NULL;
7707 long idx = 0;
7708 char_u *tofree = NULL;
7709
7710 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7711 save_cpo = p_cpo;
7712 p_cpo = (char_u *)"";
7713
7714 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007715 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007717 /* type MATCH_LIST: return empty list when there are no matches.
7718 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 if (rettv_list_alloc(rettv) == FAIL)
7720 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007721 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007722 && (list_append_string(rettv->vval.v_list,
7723 (char_u *)"", 0) == FAIL
7724 || list_append_number(rettv->vval.v_list,
7725 (varnumber_T)-1) == FAIL
7726 || list_append_number(rettv->vval.v_list,
7727 (varnumber_T)-1) == FAIL
7728 || list_append_number(rettv->vval.v_list,
7729 (varnumber_T)-1) == FAIL))
7730 {
7731 list_free(rettv->vval.v_list);
7732 rettv->vval.v_list = NULL;
7733 goto theend;
7734 }
7735 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007736 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 {
7738 rettv->v_type = VAR_STRING;
7739 rettv->vval.v_string = NULL;
7740 }
7741
7742 if (argvars[0].v_type == VAR_LIST)
7743 {
7744 if ((l = argvars[0].vval.v_list) == NULL)
7745 goto theend;
7746 li = l->lv_first;
7747 }
7748 else
7749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007750 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 len = (long)STRLEN(str);
7752 }
7753
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007754 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755 if (pat == NULL)
7756 goto theend;
7757
7758 if (argvars[2].v_type != VAR_UNKNOWN)
7759 {
7760 int error = FALSE;
7761
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007762 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 if (error)
7764 goto theend;
7765 if (l != NULL)
7766 {
7767 li = list_find(l, start);
7768 if (li == NULL)
7769 goto theend;
7770 idx = l->lv_idx; /* use the cached index */
7771 }
7772 else
7773 {
7774 if (start < 0)
7775 start = 0;
7776 if (start > len)
7777 goto theend;
7778 /* When "count" argument is there ignore matches before "start",
7779 * otherwise skip part of the string. Differs when pattern is "^"
7780 * or "\<". */
7781 if (argvars[3].v_type != VAR_UNKNOWN)
7782 startcol = start;
7783 else
7784 {
7785 str += start;
7786 len -= start;
7787 }
7788 }
7789
7790 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007791 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792 if (error)
7793 goto theend;
7794 }
7795
7796 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7797 if (regmatch.regprog != NULL)
7798 {
7799 regmatch.rm_ic = p_ic;
7800
7801 for (;;)
7802 {
7803 if (l != NULL)
7804 {
7805 if (li == NULL)
7806 {
7807 match = FALSE;
7808 break;
7809 }
7810 vim_free(tofree);
7811 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7812 if (str == NULL)
7813 break;
7814 }
7815
7816 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7817
7818 if (match && --nth <= 0)
7819 break;
7820 if (l == NULL && !match)
7821 break;
7822
7823 /* Advance to just after the match. */
7824 if (l != NULL)
7825 {
7826 li = li->li_next;
7827 ++idx;
7828 }
7829 else
7830 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 startcol = (colnr_T)(regmatch.startp[0]
7832 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 if (startcol > (colnr_T)len
7834 || str + startcol <= regmatch.startp[0])
7835 {
7836 match = FALSE;
7837 break;
7838 }
7839 }
7840 }
7841
7842 if (match)
7843 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007844 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845 {
7846 listitem_T *li1 = rettv->vval.v_list->lv_first;
7847 listitem_T *li2 = li1->li_next;
7848 listitem_T *li3 = li2->li_next;
7849 listitem_T *li4 = li3->li_next;
7850
7851 vim_free(li1->li_tv.vval.v_string);
7852 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7853 (int)(regmatch.endp[0] - regmatch.startp[0]));
7854 li3->li_tv.vval.v_number =
7855 (varnumber_T)(regmatch.startp[0] - expr);
7856 li4->li_tv.vval.v_number =
7857 (varnumber_T)(regmatch.endp[0] - expr);
7858 if (l != NULL)
7859 li2->li_tv.vval.v_number = (varnumber_T)idx;
7860 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007861 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 {
7863 int i;
7864
7865 /* return list with matched string and submatches */
7866 for (i = 0; i < NSUBEXP; ++i)
7867 {
7868 if (regmatch.endp[i] == NULL)
7869 {
7870 if (list_append_string(rettv->vval.v_list,
7871 (char_u *)"", 0) == FAIL)
7872 break;
7873 }
7874 else if (list_append_string(rettv->vval.v_list,
7875 regmatch.startp[i],
7876 (int)(regmatch.endp[i] - regmatch.startp[i]))
7877 == FAIL)
7878 break;
7879 }
7880 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007881 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 {
7883 /* return matched string */
7884 if (l != NULL)
7885 copy_tv(&li->li_tv, rettv);
7886 else
7887 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7888 (int)(regmatch.endp[0] - regmatch.startp[0]));
7889 }
7890 else if (l != NULL)
7891 rettv->vval.v_number = idx;
7892 else
7893 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007894 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 rettv->vval.v_number =
7896 (varnumber_T)(regmatch.startp[0] - str);
7897 else
7898 rettv->vval.v_number =
7899 (varnumber_T)(regmatch.endp[0] - str);
7900 rettv->vval.v_number += (varnumber_T)(str - expr);
7901 }
7902 }
7903 vim_regfree(regmatch.regprog);
7904 }
7905
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007906theend:
7907 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 /* matchstrpos() without a list: drop the second item. */
7909 listitem_remove(rettv->vval.v_list,
7910 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 vim_free(tofree);
7912 p_cpo = save_cpo;
7913}
7914
7915/*
7916 * "match()" function
7917 */
7918 static void
7919f_match(typval_T *argvars, typval_T *rettv)
7920{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007921 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922}
7923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007924/*
7925 * "matchend()" function
7926 */
7927 static void
7928f_matchend(typval_T *argvars, typval_T *rettv)
7929{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007930 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931}
7932
7933/*
7934 * "matchlist()" function
7935 */
7936 static void
7937f_matchlist(typval_T *argvars, typval_T *rettv)
7938{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007939 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940}
7941
7942/*
7943 * "matchstr()" function
7944 */
7945 static void
7946f_matchstr(typval_T *argvars, typval_T *rettv)
7947{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007948 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949}
7950
7951/*
7952 * "matchstrpos()" function
7953 */
7954 static void
7955f_matchstrpos(typval_T *argvars, typval_T *rettv)
7956{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007957 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958}
7959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 static void
7961max_min(typval_T *argvars, typval_T *rettv, int domax)
7962{
7963 varnumber_T n = 0;
7964 varnumber_T i;
7965 int error = FALSE;
7966
7967 if (argvars[0].v_type == VAR_LIST)
7968 {
7969 list_T *l;
7970 listitem_T *li;
7971
7972 l = argvars[0].vval.v_list;
7973 if (l != NULL)
7974 {
7975 li = l->lv_first;
7976 if (li != NULL)
7977 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007978 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979 for (;;)
7980 {
7981 li = li->li_next;
7982 if (li == NULL)
7983 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007984 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 if (domax ? i > n : i < n)
7986 n = i;
7987 }
7988 }
7989 }
7990 }
7991 else if (argvars[0].v_type == VAR_DICT)
7992 {
7993 dict_T *d;
7994 int first = TRUE;
7995 hashitem_T *hi;
7996 int todo;
7997
7998 d = argvars[0].vval.v_dict;
7999 if (d != NULL)
8000 {
8001 todo = (int)d->dv_hashtab.ht_used;
8002 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8003 {
8004 if (!HASHITEM_EMPTY(hi))
8005 {
8006 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008007 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (first)
8009 {
8010 n = i;
8011 first = FALSE;
8012 }
8013 else if (domax ? i > n : i < n)
8014 n = i;
8015 }
8016 }
8017 }
8018 }
8019 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008020 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 rettv->vval.v_number = error ? 0 : n;
8022}
8023
8024/*
8025 * "max()" function
8026 */
8027 static void
8028f_max(typval_T *argvars, typval_T *rettv)
8029{
8030 max_min(argvars, rettv, TRUE);
8031}
8032
8033/*
8034 * "min()" function
8035 */
8036 static void
8037f_min(typval_T *argvars, typval_T *rettv)
8038{
8039 max_min(argvars, rettv, FALSE);
8040}
8041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042/*
8043 * Create the directory in which "dir" is located, and higher levels when
8044 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008045 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 */
8047 static int
8048mkdir_recurse(char_u *dir, int prot)
8049{
8050 char_u *p;
8051 char_u *updir;
8052 int r = FAIL;
8053
8054 /* Get end of directory name in "dir".
8055 * We're done when it's "/" or "c:/". */
8056 p = gettail_sep(dir);
8057 if (p <= get_past_head(dir))
8058 return OK;
8059
8060 /* If the directory exists we're done. Otherwise: create it.*/
8061 updir = vim_strnsave(dir, (int)(p - dir));
8062 if (updir == NULL)
8063 return FAIL;
8064 if (mch_isdir(updir))
8065 r = OK;
8066 else if (mkdir_recurse(updir, prot) == OK)
8067 r = vim_mkdir_emsg(updir, prot);
8068 vim_free(updir);
8069 return r;
8070}
8071
8072#ifdef vim_mkdir
8073/*
8074 * "mkdir()" function
8075 */
8076 static void
8077f_mkdir(typval_T *argvars, typval_T *rettv)
8078{
8079 char_u *dir;
8080 char_u buf[NUMBUFLEN];
8081 int prot = 0755;
8082
8083 rettv->vval.v_number = FAIL;
8084 if (check_restricted() || check_secure())
8085 return;
8086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008087 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008089 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008091 if (*gettail(dir) == NUL)
8092 /* remove trailing slashes */
8093 *gettail_sep(dir) = NUL;
8094
8095 if (argvars[1].v_type != VAR_UNKNOWN)
8096 {
8097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008099 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008100 if (prot == -1)
8101 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008103 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008104 {
8105 if (mch_isdir(dir))
8106 {
8107 /* With the "p" flag it's OK if the dir already exists. */
8108 rettv->vval.v_number = OK;
8109 return;
8110 }
8111 mkdir_recurse(dir, prot);
8112 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008114 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115}
8116#endif
8117
8118/*
8119 * "mode()" function
8120 */
8121 static void
8122f_mode(typval_T *argvars, typval_T *rettv)
8123{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008124 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125
Bram Moolenaar612cc382018-07-29 15:34:26 +02008126 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127
8128 if (time_for_testing == 93784)
8129 {
8130 /* Testing the two-character code. */
8131 buf[0] = 'x';
8132 buf[1] = '!';
8133 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008134#ifdef FEAT_TERMINAL
8135 else if (term_use_loop())
8136 buf[0] = 't';
8137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 else if (VIsual_active)
8139 {
8140 if (VIsual_select)
8141 buf[0] = VIsual_mode + 's' - 'v';
8142 else
8143 buf[0] = VIsual_mode;
8144 }
8145 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8146 || State == CONFIRM)
8147 {
8148 buf[0] = 'r';
8149 if (State == ASKMORE)
8150 buf[1] = 'm';
8151 else if (State == CONFIRM)
8152 buf[1] = '?';
8153 }
8154 else if (State == EXTERNCMD)
8155 buf[0] = '!';
8156 else if (State & INSERT)
8157 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 if (State & VREPLACE_FLAG)
8159 {
8160 buf[0] = 'R';
8161 buf[1] = 'v';
8162 }
8163 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008164 {
8165 if (State & REPLACE_FLAG)
8166 buf[0] = 'R';
8167 else
8168 buf[0] = 'i';
8169#ifdef FEAT_INS_EXPAND
8170 if (ins_compl_active())
8171 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008172 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008173 buf[1] = 'x';
8174#endif
8175 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008177 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 {
8179 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008180 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008182 else if (exmode_active == EXMODE_NORMAL)
8183 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184 }
8185 else
8186 {
8187 buf[0] = 'n';
8188 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008189 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008191 // to be able to detect force-linewise/blockwise/characterwise operations
8192 buf[2] = motion_force;
8193 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008194 else if (restart_edit == 'I' || restart_edit == 'R'
8195 || restart_edit == 'V')
8196 {
8197 buf[1] = 'i';
8198 buf[2] = restart_edit;
8199 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 }
8201
8202 /* Clear out the minor mode when the argument is not a non-zero number or
8203 * non-empty string. */
8204 if (!non_zero_arg(&argvars[0]))
8205 buf[1] = NUL;
8206
8207 rettv->vval.v_string = vim_strsave(buf);
8208 rettv->v_type = VAR_STRING;
8209}
8210
8211#if defined(FEAT_MZSCHEME) || defined(PROTO)
8212/*
8213 * "mzeval()" function
8214 */
8215 static void
8216f_mzeval(typval_T *argvars, typval_T *rettv)
8217{
8218 char_u *str;
8219 char_u buf[NUMBUFLEN];
8220
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008221 if (check_restricted() || check_secure())
8222 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008223 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008224 do_mzeval(str, rettv);
8225}
8226
8227 void
8228mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8229{
8230 typval_T argvars[3];
8231
8232 argvars[0].v_type = VAR_STRING;
8233 argvars[0].vval.v_string = name;
8234 copy_tv(args, &argvars[1]);
8235 argvars[2].v_type = VAR_UNKNOWN;
8236 f_call(argvars, rettv);
8237 clear_tv(&argvars[1]);
8238}
8239#endif
8240
8241/*
8242 * "nextnonblank()" function
8243 */
8244 static void
8245f_nextnonblank(typval_T *argvars, typval_T *rettv)
8246{
8247 linenr_T lnum;
8248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 {
8251 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8252 {
8253 lnum = 0;
8254 break;
8255 }
8256 if (*skipwhite(ml_get(lnum)) != NUL)
8257 break;
8258 }
8259 rettv->vval.v_number = lnum;
8260}
8261
8262/*
8263 * "nr2char()" function
8264 */
8265 static void
8266f_nr2char(typval_T *argvars, typval_T *rettv)
8267{
8268 char_u buf[NUMBUFLEN];
8269
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 if (has_mbyte)
8271 {
8272 int utf8 = 0;
8273
8274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008275 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008277 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008279 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 }
8281 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008283 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 buf[1] = NUL;
8285 }
8286 rettv->v_type = VAR_STRING;
8287 rettv->vval.v_string = vim_strsave(buf);
8288}
8289
8290/*
8291 * "or(expr, expr)" function
8292 */
8293 static void
8294f_or(typval_T *argvars, typval_T *rettv)
8295{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008296 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8297 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298}
8299
8300/*
8301 * "pathshorten()" function
8302 */
8303 static void
8304f_pathshorten(typval_T *argvars, typval_T *rettv)
8305{
8306 char_u *p;
8307
8308 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008309 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 if (p == NULL)
8311 rettv->vval.v_string = NULL;
8312 else
8313 {
8314 p = vim_strsave(p);
8315 rettv->vval.v_string = p;
8316 if (p != NULL)
8317 shorten_dir(p);
8318 }
8319}
8320
8321#ifdef FEAT_PERL
8322/*
8323 * "perleval()" function
8324 */
8325 static void
8326f_perleval(typval_T *argvars, typval_T *rettv)
8327{
8328 char_u *str;
8329 char_u buf[NUMBUFLEN];
8330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008331 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 do_perleval(str, rettv);
8333}
8334#endif
8335
8336#ifdef FEAT_FLOAT
8337/*
8338 * "pow()" function
8339 */
8340 static void
8341f_pow(typval_T *argvars, typval_T *rettv)
8342{
8343 float_T fx = 0.0, fy = 0.0;
8344
8345 rettv->v_type = VAR_FLOAT;
8346 if (get_float_arg(argvars, &fx) == OK
8347 && get_float_arg(&argvars[1], &fy) == OK)
8348 rettv->vval.v_float = pow(fx, fy);
8349 else
8350 rettv->vval.v_float = 0.0;
8351}
8352#endif
8353
8354/*
8355 * "prevnonblank()" function
8356 */
8357 static void
8358f_prevnonblank(typval_T *argvars, typval_T *rettv)
8359{
8360 linenr_T lnum;
8361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008362 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8364 lnum = 0;
8365 else
8366 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8367 --lnum;
8368 rettv->vval.v_number = lnum;
8369}
8370
8371/* This dummy va_list is here because:
8372 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8373 * - locally in the function results in a "used before set" warning
8374 * - using va_start() to initialize it gives "function with fixed args" error */
8375static va_list ap;
8376
8377/*
8378 * "printf()" function
8379 */
8380 static void
8381f_printf(typval_T *argvars, typval_T *rettv)
8382{
8383 char_u buf[NUMBUFLEN];
8384 int len;
8385 char_u *s;
8386 int saved_did_emsg = did_emsg;
8387 char *fmt;
8388
8389 rettv->v_type = VAR_STRING;
8390 rettv->vval.v_string = NULL;
8391
8392 /* Get the required length, allocate the buffer and do it for real. */
8393 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008394 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008395 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 if (!did_emsg)
8397 {
8398 s = alloc(len + 1);
8399 if (s != NULL)
8400 {
8401 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008402 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8403 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 }
8405 }
8406 did_emsg |= saved_did_emsg;
8407}
8408
8409/*
8410 * "pumvisible()" function
8411 */
8412 static void
8413f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8414{
8415#ifdef FEAT_INS_EXPAND
8416 if (pum_visible())
8417 rettv->vval.v_number = 1;
8418#endif
8419}
8420
8421#ifdef FEAT_PYTHON3
8422/*
8423 * "py3eval()" function
8424 */
8425 static void
8426f_py3eval(typval_T *argvars, typval_T *rettv)
8427{
8428 char_u *str;
8429 char_u buf[NUMBUFLEN];
8430
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008431 if (check_restricted() || check_secure())
8432 return;
8433
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008434 if (p_pyx == 0)
8435 p_pyx = 3;
8436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 do_py3eval(str, rettv);
8439}
8440#endif
8441
8442#ifdef FEAT_PYTHON
8443/*
8444 * "pyeval()" function
8445 */
8446 static void
8447f_pyeval(typval_T *argvars, typval_T *rettv)
8448{
8449 char_u *str;
8450 char_u buf[NUMBUFLEN];
8451
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008452 if (check_restricted() || check_secure())
8453 return;
8454
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008455 if (p_pyx == 0)
8456 p_pyx = 2;
8457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008458 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 do_pyeval(str, rettv);
8460}
8461#endif
8462
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008463#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8464/*
8465 * "pyxeval()" function
8466 */
8467 static void
8468f_pyxeval(typval_T *argvars, typval_T *rettv)
8469{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008470 if (check_restricted() || check_secure())
8471 return;
8472
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008473# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8474 init_pyxversion();
8475 if (p_pyx == 2)
8476 f_pyeval(argvars, rettv);
8477 else
8478 f_py3eval(argvars, rettv);
8479# elif defined(FEAT_PYTHON)
8480 f_pyeval(argvars, rettv);
8481# elif defined(FEAT_PYTHON3)
8482 f_py3eval(argvars, rettv);
8483# endif
8484}
8485#endif
8486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487/*
8488 * "range()" function
8489 */
8490 static void
8491f_range(typval_T *argvars, typval_T *rettv)
8492{
8493 varnumber_T start;
8494 varnumber_T end;
8495 varnumber_T stride = 1;
8496 varnumber_T i;
8497 int error = FALSE;
8498
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008499 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 if (argvars[1].v_type == VAR_UNKNOWN)
8501 {
8502 end = start - 1;
8503 start = 0;
8504 }
8505 else
8506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008507 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008509 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 }
8511
8512 if (error)
8513 return; /* type error; errmsg already given */
8514 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008515 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008517 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 else
8519 {
8520 if (rettv_list_alloc(rettv) == OK)
8521 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8522 if (list_append_number(rettv->vval.v_list,
8523 (varnumber_T)i) == FAIL)
8524 break;
8525 }
8526}
8527
8528/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008529 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008530 */
8531 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008532readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008533{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008534 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008535 typval_T save_val;
8536 typval_T rettv;
8537 typval_T argv[2];
8538 int retval = 0;
8539 int error = FALSE;
8540
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008541 if (expr->v_type == VAR_UNKNOWN)
8542 return 1;
8543
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008544 prepare_vimvar(VV_VAL, &save_val);
8545 set_vim_var_string(VV_VAL, name, -1);
8546 argv[0].v_type = VAR_STRING;
8547 argv[0].vval.v_string = name;
8548
8549 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8550 goto theend;
8551
8552 retval = tv_get_number_chk(&rettv, &error);
8553 if (error)
8554 retval = -1;
8555 clear_tv(&rettv);
8556
8557theend:
8558 set_vim_var_string(VV_VAL, NULL, 0);
8559 restore_vimvar(VV_VAL, &save_val);
8560 return retval;
8561}
8562
8563/*
8564 * "readdir()" function
8565 */
8566 static void
8567f_readdir(typval_T *argvars, typval_T *rettv)
8568{
8569 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008570 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008571 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008572 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008573 garray_T ga;
8574 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008575
8576 if (rettv_list_alloc(rettv) == FAIL)
8577 return;
8578 path = tv_get_string(&argvars[0]);
8579 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008580
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008581 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8582 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008583 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008584 for (i = 0; i < ga.ga_len; i++)
8585 {
8586 p = ((char_u **)ga.ga_data)[i];
8587 list_append_string(rettv->vval.v_list, p, -1);
8588 }
8589 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008590 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008591}
8592
8593/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 * "readfile()" function
8595 */
8596 static void
8597f_readfile(typval_T *argvars, typval_T *rettv)
8598{
8599 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008600 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 int failed = FALSE;
8602 char_u *fname;
8603 FILE *fd;
8604 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8605 int io_size = sizeof(buf);
8606 int readlen; /* size of last fread() */
8607 char_u *prev = NULL; /* previously read bytes, if any */
8608 long prevlen = 0; /* length of data in prev */
8609 long prevsize = 0; /* size of prev buffer */
8610 long maxline = MAXLNUM;
8611 long cnt = 0;
8612 char_u *p; /* position in buf */
8613 char_u *start; /* start of current line */
8614
8615 if (argvars[1].v_type != VAR_UNKNOWN)
8616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008619 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8620 blob = TRUE;
8621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 }
8625
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008626 if (blob)
8627 {
8628 if (rettv_blob_alloc(rettv) == FAIL)
8629 return;
8630 }
8631 else
8632 {
8633 if (rettv_list_alloc(rettv) == FAIL)
8634 return;
8635 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636
8637 /* Always open the file in binary mode, library functions have a mind of
8638 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008639 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008642 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 return;
8644 }
8645
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008646 if (blob)
8647 {
8648 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008650 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008651 blob_free(rettv->vval.v_blob);
8652 }
8653 fclose(fd);
8654 return;
8655 }
8656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 while (cnt < maxline || maxline < 0)
8658 {
8659 readlen = (int)fread(buf, 1, io_size, fd);
8660
8661 /* This for loop processes what was read, but is also entered at end
8662 * of file so that either:
8663 * - an incomplete line gets written
8664 * - a "binary" file gets an empty line at the end if it ends in a
8665 * newline. */
8666 for (p = buf, start = buf;
8667 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8668 ++p)
8669 {
8670 if (*p == '\n' || readlen <= 0)
8671 {
8672 listitem_T *li;
8673 char_u *s = NULL;
8674 long_u len = p - start;
8675
8676 /* Finished a line. Remove CRs before NL. */
8677 if (readlen > 0 && !binary)
8678 {
8679 while (len > 0 && start[len - 1] == '\r')
8680 --len;
8681 /* removal may cross back to the "prev" string */
8682 if (len == 0)
8683 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8684 --prevlen;
8685 }
8686 if (prevlen == 0)
8687 s = vim_strnsave(start, (int)len);
8688 else
8689 {
8690 /* Change "prev" buffer to be the right size. This way
8691 * the bytes are only copied once, and very long lines are
8692 * allocated only once. */
8693 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8694 {
8695 mch_memmove(s + prevlen, start, len);
8696 s[prevlen + len] = NUL;
8697 prev = NULL; /* the list will own the string */
8698 prevlen = prevsize = 0;
8699 }
8700 }
8701 if (s == NULL)
8702 {
8703 do_outofmem_msg((long_u) prevlen + len + 1);
8704 failed = TRUE;
8705 break;
8706 }
8707
8708 if ((li = listitem_alloc()) == NULL)
8709 {
8710 vim_free(s);
8711 failed = TRUE;
8712 break;
8713 }
8714 li->li_tv.v_type = VAR_STRING;
8715 li->li_tv.v_lock = 0;
8716 li->li_tv.vval.v_string = s;
8717 list_append(rettv->vval.v_list, li);
8718
8719 start = p + 1; /* step over newline */
8720 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8721 break;
8722 }
8723 else if (*p == NUL)
8724 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8726 * when finding the BF and check the previous two bytes. */
8727 else if (*p == 0xbf && enc_utf8 && !binary)
8728 {
8729 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8730 * + 1, these may be in the "prev" string. */
8731 char_u back1 = p >= buf + 1 ? p[-1]
8732 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8733 char_u back2 = p >= buf + 2 ? p[-2]
8734 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8735 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8736
8737 if (back2 == 0xef && back1 == 0xbb)
8738 {
8739 char_u *dest = p - 2;
8740
8741 /* Usually a BOM is at the beginning of a file, and so at
8742 * the beginning of a line; then we can just step over it.
8743 */
8744 if (start == dest)
8745 start = p + 1;
8746 else
8747 {
8748 /* have to shuffle buf to close gap */
8749 int adjust_prevlen = 0;
8750
8751 if (dest < buf)
8752 {
8753 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8754 dest = buf;
8755 }
8756 if (readlen > p - buf + 1)
8757 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8758 readlen -= 3 - adjust_prevlen;
8759 prevlen -= adjust_prevlen;
8760 p = dest - 1;
8761 }
8762 }
8763 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 } /* for */
8765
8766 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8767 break;
8768 if (start < p)
8769 {
8770 /* There's part of a line in buf, store it in "prev". */
8771 if (p - start + prevlen >= prevsize)
8772 {
8773 /* need bigger "prev" buffer */
8774 char_u *newprev;
8775
8776 /* A common use case is ordinary text files and "prev" gets a
8777 * fragment of a line, so the first allocation is made
8778 * small, to avoid repeatedly 'allocing' large and
8779 * 'reallocing' small. */
8780 if (prevsize == 0)
8781 prevsize = (long)(p - start);
8782 else
8783 {
8784 long grow50pc = (prevsize * 3) / 2;
8785 long growmin = (long)((p - start) * 2 + prevlen);
8786 prevsize = grow50pc > growmin ? grow50pc : growmin;
8787 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008788 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 if (newprev == NULL)
8790 {
8791 do_outofmem_msg((long_u)prevsize);
8792 failed = TRUE;
8793 break;
8794 }
8795 prev = newprev;
8796 }
8797 /* Add the line part to end of "prev". */
8798 mch_memmove(prev + prevlen, start, p - start);
8799 prevlen += (long)(p - start);
8800 }
8801 } /* while */
8802
8803 /*
8804 * For a negative line count use only the lines at the end of the file,
8805 * free the rest.
8806 */
8807 if (!failed && maxline < 0)
8808 while (cnt > -maxline)
8809 {
8810 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8811 --cnt;
8812 }
8813
8814 if (failed)
8815 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008816 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008818 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 }
8820
8821 vim_free(prev);
8822 fclose(fd);
8823}
8824
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008825 static void
8826return_register(int regname, typval_T *rettv)
8827{
8828 char_u buf[2] = {0, 0};
8829
8830 buf[0] = (char_u)regname;
8831 rettv->v_type = VAR_STRING;
8832 rettv->vval.v_string = vim_strsave(buf);
8833}
8834
8835/*
8836 * "reg_executing()" function
8837 */
8838 static void
8839f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8840{
8841 return_register(reg_executing, rettv);
8842}
8843
8844/*
8845 * "reg_recording()" function
8846 */
8847 static void
8848f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8849{
8850 return_register(reg_recording, rettv);
8851}
8852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854/*
8855 * Convert a List to proftime_T.
8856 * Return FAIL when there is something wrong.
8857 */
8858 static int
8859list2proftime(typval_T *arg, proftime_T *tm)
8860{
8861 long n1, n2;
8862 int error = FALSE;
8863
8864 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8865 || arg->vval.v_list->lv_len != 2)
8866 return FAIL;
8867 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8868 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008869# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 tm->HighPart = n1;
8871 tm->LowPart = n2;
8872# else
8873 tm->tv_sec = n1;
8874 tm->tv_usec = n2;
8875# endif
8876 return error ? FAIL : OK;
8877}
8878#endif /* FEAT_RELTIME */
8879
8880/*
8881 * "reltime()" function
8882 */
8883 static void
8884f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8885{
8886#ifdef FEAT_RELTIME
8887 proftime_T res;
8888 proftime_T start;
8889
8890 if (argvars[0].v_type == VAR_UNKNOWN)
8891 {
8892 /* No arguments: get current time. */
8893 profile_start(&res);
8894 }
8895 else if (argvars[1].v_type == VAR_UNKNOWN)
8896 {
8897 if (list2proftime(&argvars[0], &res) == FAIL)
8898 return;
8899 profile_end(&res);
8900 }
8901 else
8902 {
8903 /* Two arguments: compute the difference. */
8904 if (list2proftime(&argvars[0], &start) == FAIL
8905 || list2proftime(&argvars[1], &res) == FAIL)
8906 return;
8907 profile_sub(&res, &start);
8908 }
8909
8910 if (rettv_list_alloc(rettv) == OK)
8911 {
8912 long n1, n2;
8913
Bram Moolenaar4f974752019-02-17 17:44:42 +01008914# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 n1 = res.HighPart;
8916 n2 = res.LowPart;
8917# else
8918 n1 = res.tv_sec;
8919 n2 = res.tv_usec;
8920# endif
8921 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8922 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8923 }
8924#endif
8925}
8926
8927#ifdef FEAT_FLOAT
8928/*
8929 * "reltimefloat()" function
8930 */
8931 static void
8932f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8933{
8934# ifdef FEAT_RELTIME
8935 proftime_T tm;
8936# endif
8937
8938 rettv->v_type = VAR_FLOAT;
8939 rettv->vval.v_float = 0;
8940# ifdef FEAT_RELTIME
8941 if (list2proftime(&argvars[0], &tm) == OK)
8942 rettv->vval.v_float = profile_float(&tm);
8943# endif
8944}
8945#endif
8946
8947/*
8948 * "reltimestr()" function
8949 */
8950 static void
8951f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8952{
8953#ifdef FEAT_RELTIME
8954 proftime_T tm;
8955#endif
8956
8957 rettv->v_type = VAR_STRING;
8958 rettv->vval.v_string = NULL;
8959#ifdef FEAT_RELTIME
8960 if (list2proftime(&argvars[0], &tm) == OK)
8961 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8962#endif
8963}
8964
8965#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 static void
8967make_connection(void)
8968{
8969 if (X_DISPLAY == NULL
8970# ifdef FEAT_GUI
8971 && !gui.in_use
8972# endif
8973 )
8974 {
8975 x_force_connect = TRUE;
8976 setup_term_clip();
8977 x_force_connect = FALSE;
8978 }
8979}
8980
8981 static int
8982check_connection(void)
8983{
8984 make_connection();
8985 if (X_DISPLAY == NULL)
8986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008987 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 return FAIL;
8989 }
8990 return OK;
8991}
8992#endif
8993
8994#ifdef FEAT_CLIENTSERVER
8995 static void
8996remote_common(typval_T *argvars, typval_T *rettv, int expr)
8997{
8998 char_u *server_name;
8999 char_u *keys;
9000 char_u *r = NULL;
9001 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009002 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009003# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 HWND w;
9005# else
9006 Window w;
9007# endif
9008
9009 if (check_restricted() || check_secure())
9010 return;
9011
9012# ifdef FEAT_X11
9013 if (check_connection() == FAIL)
9014 return;
9015# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009016 if (argvars[2].v_type != VAR_UNKNOWN
9017 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009018 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009020 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 if (server_name == NULL)
9022 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009023 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009024# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009025 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009027 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9028 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029# endif
9030 {
9031 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009033 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009034 vim_free(r);
9035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009037 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 return;
9039 }
9040
9041 rettv->vval.v_string = r;
9042
9043 if (argvars[2].v_type != VAR_UNKNOWN)
9044 {
9045 dictitem_T v;
9046 char_u str[30];
9047 char_u *idvar;
9048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009049 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009050 if (idvar != NULL && *idvar != NUL)
9051 {
9052 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9053 v.di_tv.v_type = VAR_STRING;
9054 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009056 vim_free(v.di_tv.vval.v_string);
9057 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 }
9059}
9060#endif
9061
9062/*
9063 * "remote_expr()" function
9064 */
9065 static void
9066f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9067{
9068 rettv->v_type = VAR_STRING;
9069 rettv->vval.v_string = NULL;
9070#ifdef FEAT_CLIENTSERVER
9071 remote_common(argvars, rettv, TRUE);
9072#endif
9073}
9074
9075/*
9076 * "remote_foreground()" function
9077 */
9078 static void
9079f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9080{
9081#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009082# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 /* On Win32 it's done in this application. */
9084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086
9087 if (server_name != NULL)
9088 serverForeground(server_name);
9089 }
9090# else
9091 /* Send a foreground() expression to the server. */
9092 argvars[1].v_type = VAR_STRING;
9093 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9094 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009095 rettv->v_type = VAR_STRING;
9096 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 remote_common(argvars, rettv, TRUE);
9098 vim_free(argvars[1].vval.v_string);
9099# endif
9100#endif
9101}
9102
9103 static void
9104f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9105{
9106#ifdef FEAT_CLIENTSERVER
9107 dictitem_T v;
9108 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009109# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 long_u n = 0;
9111# endif
9112 char_u *serverid;
9113
9114 if (check_restricted() || check_secure())
9115 {
9116 rettv->vval.v_number = -1;
9117 return;
9118 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009119 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120 if (serverid == NULL)
9121 {
9122 rettv->vval.v_number = -1;
9123 return; /* type error; errmsg already given */
9124 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009125# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9127 if (n == 0)
9128 rettv->vval.v_number = -1;
9129 else
9130 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009131 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132 rettv->vval.v_number = (s != NULL);
9133 }
9134# else
9135 if (check_connection() == FAIL)
9136 return;
9137
9138 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9139 serverStrToWin(serverid), &s);
9140# endif
9141
9142 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9143 {
9144 char_u *retvar;
9145
9146 v.di_tv.v_type = VAR_STRING;
9147 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009148 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 if (retvar != NULL)
9150 set_var(retvar, &v.di_tv, FALSE);
9151 vim_free(v.di_tv.vval.v_string);
9152 }
9153#else
9154 rettv->vval.v_number = -1;
9155#endif
9156}
9157
9158 static void
9159f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9160{
9161 char_u *r = NULL;
9162
9163#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009164 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165
9166 if (serverid != NULL && !check_restricted() && !check_secure())
9167 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009168 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009169# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009170 /* The server's HWND is encoded in the 'id' parameter */
9171 long_u n = 0;
9172# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009173
9174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009175 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009176
Bram Moolenaar4f974752019-02-17 17:44:42 +01009177# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9179 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009180 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 if (r == NULL)
9182# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009183 if (check_connection() == FAIL
9184 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9185 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009187 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009188 }
9189#endif
9190 rettv->v_type = VAR_STRING;
9191 rettv->vval.v_string = r;
9192}
9193
9194/*
9195 * "remote_send()" function
9196 */
9197 static void
9198f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9199{
9200 rettv->v_type = VAR_STRING;
9201 rettv->vval.v_string = NULL;
9202#ifdef FEAT_CLIENTSERVER
9203 remote_common(argvars, rettv, FALSE);
9204#endif
9205}
9206
9207/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009208 * "remote_startserver()" function
9209 */
9210 static void
9211f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9212{
9213#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009214 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009215
9216 if (server == NULL)
9217 return; /* type error; errmsg already given */
9218 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009219 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009220 else
9221 {
9222# ifdef FEAT_X11
9223 if (check_connection() == OK)
9224 serverRegisterName(X_DISPLAY, server);
9225# else
9226 serverSetName(server);
9227# endif
9228 }
9229#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009230 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009231#endif
9232}
9233
9234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235 * "remove()" function
9236 */
9237 static void
9238f_remove(typval_T *argvars, typval_T *rettv)
9239{
9240 list_T *l;
9241 listitem_T *item, *item2;
9242 listitem_T *li;
9243 long idx;
9244 long end;
9245 char_u *key;
9246 dict_T *d;
9247 dictitem_T *di;
9248 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009249 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250
9251 if (argvars[0].v_type == VAR_DICT)
9252 {
9253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009254 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009255 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009256 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009258 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009259 if (key != NULL)
9260 {
9261 di = dict_find(d, key, -1);
9262 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009263 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9265 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9266 {
9267 *rettv = di->di_tv;
9268 init_tv(&di->di_tv);
9269 dictitem_remove(d, di);
9270 }
9271 }
9272 }
9273 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009274 else if (argvars[0].v_type == VAR_BLOB)
9275 {
9276 idx = (long)tv_get_number_chk(&argvars[1], &error);
9277 if (!error)
9278 {
9279 blob_T *b = argvars[0].vval.v_blob;
9280 int len = blob_len(b);
9281 char_u *p;
9282
9283 if (idx < 0)
9284 // count from the end
9285 idx = len + idx;
9286 if (idx < 0 || idx >= len)
9287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009288 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009289 return;
9290 }
9291 if (argvars[2].v_type == VAR_UNKNOWN)
9292 {
9293 // Remove one item, return its value.
9294 p = (char_u *)b->bv_ga.ga_data;
9295 rettv->vval.v_number = (varnumber_T) *(p + idx);
9296 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9297 --b->bv_ga.ga_len;
9298 }
9299 else
9300 {
9301 blob_T *blob;
9302
9303 // Remove range of items, return list with values.
9304 end = (long)tv_get_number_chk(&argvars[2], &error);
9305 if (error)
9306 return;
9307 if (end < 0)
9308 // count from the end
9309 end = len + end;
9310 if (end >= len || idx > end)
9311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009312 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009313 return;
9314 }
9315 blob = blob_alloc();
9316 if (blob == NULL)
9317 return;
9318 blob->bv_ga.ga_len = end - idx + 1;
9319 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9320 {
9321 vim_free(blob);
9322 return;
9323 }
9324 p = (char_u *)b->bv_ga.ga_data;
9325 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9326 (size_t)(end - idx + 1));
9327 ++blob->bv_refcount;
9328 rettv->v_type = VAR_BLOB;
9329 rettv->vval.v_blob = blob;
9330
9331 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9332 b->bv_ga.ga_len -= end - idx + 1;
9333 }
9334 }
9335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009337 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009339 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009341 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009343 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009345 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 else
9347 {
9348 if (argvars[2].v_type == VAR_UNKNOWN)
9349 {
9350 /* Remove one item, return its value. */
9351 vimlist_remove(l, item, item);
9352 *rettv = item->li_tv;
9353 vim_free(item);
9354 }
9355 else
9356 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009357 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009358 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009360 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009362 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 else
9364 {
9365 int cnt = 0;
9366
9367 for (li = item; li != NULL; li = li->li_next)
9368 {
9369 ++cnt;
9370 if (li == item2)
9371 break;
9372 }
9373 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009374 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 else
9376 {
9377 vimlist_remove(l, item, item2);
9378 if (rettv_list_alloc(rettv) == OK)
9379 {
9380 l = rettv->vval.v_list;
9381 l->lv_first = item;
9382 l->lv_last = item2;
9383 item->li_prev = NULL;
9384 item2->li_next = NULL;
9385 l->lv_len = cnt;
9386 }
9387 }
9388 }
9389 }
9390 }
9391 }
9392}
9393
9394/*
9395 * "rename({from}, {to})" function
9396 */
9397 static void
9398f_rename(typval_T *argvars, typval_T *rettv)
9399{
9400 char_u buf[NUMBUFLEN];
9401
9402 if (check_restricted() || check_secure())
9403 rettv->vval.v_number = -1;
9404 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009405 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9406 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407}
9408
9409/*
9410 * "repeat()" function
9411 */
9412 static void
9413f_repeat(typval_T *argvars, typval_T *rettv)
9414{
9415 char_u *p;
9416 int n;
9417 int slen;
9418 int len;
9419 char_u *r;
9420 int i;
9421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009422 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423 if (argvars[0].v_type == VAR_LIST)
9424 {
9425 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9426 while (n-- > 0)
9427 if (list_extend(rettv->vval.v_list,
9428 argvars[0].vval.v_list, NULL) == FAIL)
9429 break;
9430 }
9431 else
9432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 rettv->v_type = VAR_STRING;
9435 rettv->vval.v_string = NULL;
9436
9437 slen = (int)STRLEN(p);
9438 len = slen * n;
9439 if (len <= 0)
9440 return;
9441
9442 r = alloc(len + 1);
9443 if (r != NULL)
9444 {
9445 for (i = 0; i < n; i++)
9446 mch_memmove(r + i * slen, p, (size_t)slen);
9447 r[len] = NUL;
9448 }
9449
9450 rettv->vval.v_string = r;
9451 }
9452}
9453
9454/*
9455 * "resolve()" function
9456 */
9457 static void
9458f_resolve(typval_T *argvars, typval_T *rettv)
9459{
9460 char_u *p;
9461#ifdef HAVE_READLINK
9462 char_u *buf = NULL;
9463#endif
9464
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009465 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466#ifdef FEAT_SHORTCUT
9467 {
9468 char_u *v = NULL;
9469
Bram Moolenaardce1e892019-02-10 23:18:53 +01009470 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 if (v != NULL)
9472 rettv->vval.v_string = v;
9473 else
9474 rettv->vval.v_string = vim_strsave(p);
9475 }
9476#else
9477# ifdef HAVE_READLINK
9478 {
9479 char_u *cpy;
9480 int len;
9481 char_u *remain = NULL;
9482 char_u *q;
9483 int is_relative_to_current = FALSE;
9484 int has_trailing_pathsep = FALSE;
9485 int limit = 100;
9486
9487 p = vim_strsave(p);
9488
9489 if (p[0] == '.' && (vim_ispathsep(p[1])
9490 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9491 is_relative_to_current = TRUE;
9492
9493 len = STRLEN(p);
9494 if (len > 0 && after_pathsep(p, p + len))
9495 {
9496 has_trailing_pathsep = TRUE;
9497 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9498 }
9499
9500 q = getnextcomp(p);
9501 if (*q != NUL)
9502 {
9503 /* Separate the first path component in "p", and keep the
9504 * remainder (beginning with the path separator). */
9505 remain = vim_strsave(q - 1);
9506 q[-1] = NUL;
9507 }
9508
9509 buf = alloc(MAXPATHL + 1);
9510 if (buf == NULL)
9511 goto fail;
9512
9513 for (;;)
9514 {
9515 for (;;)
9516 {
9517 len = readlink((char *)p, (char *)buf, MAXPATHL);
9518 if (len <= 0)
9519 break;
9520 buf[len] = NUL;
9521
9522 if (limit-- == 0)
9523 {
9524 vim_free(p);
9525 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009526 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 rettv->vval.v_string = NULL;
9528 goto fail;
9529 }
9530
9531 /* Ensure that the result will have a trailing path separator
9532 * if the argument has one. */
9533 if (remain == NULL && has_trailing_pathsep)
9534 add_pathsep(buf);
9535
9536 /* Separate the first path component in the link value and
9537 * concatenate the remainders. */
9538 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9539 if (*q != NUL)
9540 {
9541 if (remain == NULL)
9542 remain = vim_strsave(q - 1);
9543 else
9544 {
9545 cpy = concat_str(q - 1, remain);
9546 if (cpy != NULL)
9547 {
9548 vim_free(remain);
9549 remain = cpy;
9550 }
9551 }
9552 q[-1] = NUL;
9553 }
9554
9555 q = gettail(p);
9556 if (q > p && *q == NUL)
9557 {
9558 /* Ignore trailing path separator. */
9559 q[-1] = NUL;
9560 q = gettail(p);
9561 }
9562 if (q > p && !mch_isFullName(buf))
9563 {
9564 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009565 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 if (cpy != NULL)
9567 {
9568 STRCPY(cpy, p);
9569 STRCPY(gettail(cpy), buf);
9570 vim_free(p);
9571 p = cpy;
9572 }
9573 }
9574 else
9575 {
9576 vim_free(p);
9577 p = vim_strsave(buf);
9578 }
9579 }
9580
9581 if (remain == NULL)
9582 break;
9583
9584 /* Append the first path component of "remain" to "p". */
9585 q = getnextcomp(remain + 1);
9586 len = q - remain - (*q != NUL);
9587 cpy = vim_strnsave(p, STRLEN(p) + len);
9588 if (cpy != NULL)
9589 {
9590 STRNCAT(cpy, remain, len);
9591 vim_free(p);
9592 p = cpy;
9593 }
9594 /* Shorten "remain". */
9595 if (*q != NUL)
9596 STRMOVE(remain, q - 1);
9597 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009598 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 }
9600
9601 /* If the result is a relative path name, make it explicitly relative to
9602 * the current directory if and only if the argument had this form. */
9603 if (!vim_ispathsep(*p))
9604 {
9605 if (is_relative_to_current
9606 && *p != NUL
9607 && !(p[0] == '.'
9608 && (p[1] == NUL
9609 || vim_ispathsep(p[1])
9610 || (p[1] == '.'
9611 && (p[2] == NUL
9612 || vim_ispathsep(p[2]))))))
9613 {
9614 /* Prepend "./". */
9615 cpy = concat_str((char_u *)"./", p);
9616 if (cpy != NULL)
9617 {
9618 vim_free(p);
9619 p = cpy;
9620 }
9621 }
9622 else if (!is_relative_to_current)
9623 {
9624 /* Strip leading "./". */
9625 q = p;
9626 while (q[0] == '.' && vim_ispathsep(q[1]))
9627 q += 2;
9628 if (q > p)
9629 STRMOVE(p, p + 2);
9630 }
9631 }
9632
9633 /* Ensure that the result will have no trailing path separator
9634 * if the argument had none. But keep "/" or "//". */
9635 if (!has_trailing_pathsep)
9636 {
9637 q = p + STRLEN(p);
9638 if (after_pathsep(p, q))
9639 *gettail_sep(p) = NUL;
9640 }
9641
9642 rettv->vval.v_string = p;
9643 }
9644# else
9645 rettv->vval.v_string = vim_strsave(p);
9646# endif
9647#endif
9648
9649 simplify_filename(rettv->vval.v_string);
9650
9651#ifdef HAVE_READLINK
9652fail:
9653 vim_free(buf);
9654#endif
9655 rettv->v_type = VAR_STRING;
9656}
9657
9658/*
9659 * "reverse({list})" function
9660 */
9661 static void
9662f_reverse(typval_T *argvars, typval_T *rettv)
9663{
9664 list_T *l;
9665 listitem_T *li, *ni;
9666
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009667 if (argvars[0].v_type == VAR_BLOB)
9668 {
9669 blob_T *b = argvars[0].vval.v_blob;
9670 int i, len = blob_len(b);
9671
9672 for (i = 0; i < len / 2; i++)
9673 {
9674 int tmp = blob_get(b, i);
9675
9676 blob_set(b, i, blob_get(b, len - i - 1));
9677 blob_set(b, len - i - 1, tmp);
9678 }
9679 rettv_blob_set(rettv, b);
9680 return;
9681 }
9682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009684 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009686 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 (char_u *)N_("reverse() argument"), TRUE))
9688 {
9689 li = l->lv_last;
9690 l->lv_first = l->lv_last = NULL;
9691 l->lv_len = 0;
9692 while (li != NULL)
9693 {
9694 ni = li->li_prev;
9695 list_append(l, li);
9696 li = ni;
9697 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009698 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 l->lv_idx = l->lv_len - l->lv_idx - 1;
9700 }
9701}
9702
9703#define SP_NOMOVE 0x01 /* don't move cursor */
9704#define SP_REPEAT 0x02 /* repeat to find outer pair */
9705#define SP_RETCOUNT 0x04 /* return matchcount */
9706#define SP_SETPCMARK 0x08 /* set previous context mark */
9707#define SP_START 0x10 /* accept match at start position */
9708#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9709#define SP_END 0x40 /* leave cursor at end of match */
9710#define SP_COLUMN 0x80 /* start at cursor column */
9711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712/*
9713 * Get flags for a search function.
9714 * Possibly sets "p_ws".
9715 * Returns BACKWARD, FORWARD or zero (for an error).
9716 */
9717 static int
9718get_search_arg(typval_T *varp, int *flagsp)
9719{
9720 int dir = FORWARD;
9721 char_u *flags;
9722 char_u nbuf[NUMBUFLEN];
9723 int mask;
9724
9725 if (varp->v_type != VAR_UNKNOWN)
9726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009727 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009728 if (flags == NULL)
9729 return 0; /* type error; errmsg already given */
9730 while (*flags != NUL)
9731 {
9732 switch (*flags)
9733 {
9734 case 'b': dir = BACKWARD; break;
9735 case 'w': p_ws = TRUE; break;
9736 case 'W': p_ws = FALSE; break;
9737 default: mask = 0;
9738 if (flagsp != NULL)
9739 switch (*flags)
9740 {
9741 case 'c': mask = SP_START; break;
9742 case 'e': mask = SP_END; break;
9743 case 'm': mask = SP_RETCOUNT; break;
9744 case 'n': mask = SP_NOMOVE; break;
9745 case 'p': mask = SP_SUBPAT; break;
9746 case 'r': mask = SP_REPEAT; break;
9747 case 's': mask = SP_SETPCMARK; break;
9748 case 'z': mask = SP_COLUMN; break;
9749 }
9750 if (mask == 0)
9751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009752 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009753 dir = 0;
9754 }
9755 else
9756 *flagsp |= mask;
9757 }
9758 if (dir == 0)
9759 break;
9760 ++flags;
9761 }
9762 }
9763 return dir;
9764}
9765
9766/*
9767 * Shared by search() and searchpos() functions.
9768 */
9769 static int
9770search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9771{
9772 int flags;
9773 char_u *pat;
9774 pos_T pos;
9775 pos_T save_cursor;
9776 int save_p_ws = p_ws;
9777 int dir;
9778 int retval = 0; /* default: FAIL */
9779 long lnum_stop = 0;
9780 proftime_T tm;
9781#ifdef FEAT_RELTIME
9782 long time_limit = 0;
9783#endif
9784 int options = SEARCH_KEEP;
9785 int subpatnum;
9786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009787 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9789 if (dir == 0)
9790 goto theend;
9791 flags = *flagsp;
9792 if (flags & SP_START)
9793 options |= SEARCH_START;
9794 if (flags & SP_END)
9795 options |= SEARCH_END;
9796 if (flags & SP_COLUMN)
9797 options |= SEARCH_COL;
9798
9799 /* Optional arguments: line number to stop searching and timeout. */
9800 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9801 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009802 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 if (lnum_stop < 0)
9804 goto theend;
9805#ifdef FEAT_RELTIME
9806 if (argvars[3].v_type != VAR_UNKNOWN)
9807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009808 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809 if (time_limit < 0)
9810 goto theend;
9811 }
9812#endif
9813 }
9814
9815#ifdef FEAT_RELTIME
9816 /* Set the time limit, if there is one. */
9817 profile_setlimit(time_limit, &tm);
9818#endif
9819
9820 /*
9821 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9822 * Check to make sure only those flags are set.
9823 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9824 * flags cannot be set. Check for that condition also.
9825 */
9826 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9827 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009829 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 goto theend;
9831 }
9832
9833 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009834 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009835 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836 if (subpatnum != FAIL)
9837 {
9838 if (flags & SP_SUBPAT)
9839 retval = subpatnum;
9840 else
9841 retval = pos.lnum;
9842 if (flags & SP_SETPCMARK)
9843 setpcmark();
9844 curwin->w_cursor = pos;
9845 if (match_pos != NULL)
9846 {
9847 /* Store the match cursor position */
9848 match_pos->lnum = pos.lnum;
9849 match_pos->col = pos.col + 1;
9850 }
9851 /* "/$" will put the cursor after the end of the line, may need to
9852 * correct that here */
9853 check_cursor();
9854 }
9855
9856 /* If 'n' flag is used: restore cursor position. */
9857 if (flags & SP_NOMOVE)
9858 curwin->w_cursor = save_cursor;
9859 else
9860 curwin->w_set_curswant = TRUE;
9861theend:
9862 p_ws = save_p_ws;
9863
9864 return retval;
9865}
9866
9867#ifdef FEAT_FLOAT
9868
9869/*
9870 * round() is not in C90, use ceil() or floor() instead.
9871 */
9872 float_T
9873vim_round(float_T f)
9874{
9875 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9876}
9877
9878/*
9879 * "round({float})" function
9880 */
9881 static void
9882f_round(typval_T *argvars, typval_T *rettv)
9883{
9884 float_T f = 0.0;
9885
9886 rettv->v_type = VAR_FLOAT;
9887 if (get_float_arg(argvars, &f) == OK)
9888 rettv->vval.v_float = vim_round(f);
9889 else
9890 rettv->vval.v_float = 0.0;
9891}
9892#endif
9893
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009894#ifdef FEAT_RUBY
9895/*
9896 * "rubyeval()" function
9897 */
9898 static void
9899f_rubyeval(typval_T *argvars, typval_T *rettv)
9900{
9901 char_u *str;
9902 char_u buf[NUMBUFLEN];
9903
9904 str = tv_get_string_buf(&argvars[0], buf);
9905 do_rubyeval(str, rettv);
9906}
9907#endif
9908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909/*
9910 * "screenattr()" function
9911 */
9912 static void
9913f_screenattr(typval_T *argvars, typval_T *rettv)
9914{
9915 int row;
9916 int col;
9917 int c;
9918
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009919 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9920 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 if (row < 0 || row >= screen_Rows
9922 || col < 0 || col >= screen_Columns)
9923 c = -1;
9924 else
9925 c = ScreenAttrs[LineOffset[row] + col];
9926 rettv->vval.v_number = c;
9927}
9928
9929/*
9930 * "screenchar()" function
9931 */
9932 static void
9933f_screenchar(typval_T *argvars, typval_T *rettv)
9934{
9935 int row;
9936 int col;
9937 int off;
9938 int c;
9939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009940 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9941 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009942 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 c = -1;
9944 else
9945 {
9946 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009947 if (enc_utf8 && ScreenLinesUC[off] != 0)
9948 c = ScreenLinesUC[off];
9949 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 c = ScreenLines[off];
9951 }
9952 rettv->vval.v_number = c;
9953}
9954
9955/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009956 * "screenchars()" function
9957 */
9958 static void
9959f_screenchars(typval_T *argvars, typval_T *rettv)
9960{
9961 int row;
9962 int col;
9963 int off;
9964 int c;
9965 int i;
9966
9967 if (rettv_list_alloc(rettv) == FAIL)
9968 return;
9969 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9970 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9971 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9972 return;
9973
9974 off = LineOffset[row] + col;
9975 if (enc_utf8 && ScreenLinesUC[off] != 0)
9976 c = ScreenLinesUC[off];
9977 else
9978 c = ScreenLines[off];
9979 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9980
9981 if (enc_utf8)
9982
9983 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9984 list_append_number(rettv->vval.v_list,
9985 (varnumber_T)ScreenLinesC[i][off]);
9986}
9987
9988/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989 * "screencol()" function
9990 *
9991 * First column is 1 to be consistent with virtcol().
9992 */
9993 static void
9994f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9995{
9996 rettv->vval.v_number = screen_screencol() + 1;
9997}
9998
9999/*
10000 * "screenrow()" function
10001 */
10002 static void
10003f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10004{
10005 rettv->vval.v_number = screen_screenrow() + 1;
10006}
10007
10008/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010009 * "screenstring()" function
10010 */
10011 static void
10012f_screenstring(typval_T *argvars, typval_T *rettv)
10013{
10014 int row;
10015 int col;
10016 int off;
10017 int c;
10018 int i;
10019 char_u buf[MB_MAXBYTES + 1];
10020 int buflen = 0;
10021
10022 rettv->vval.v_string = NULL;
10023 rettv->v_type = VAR_STRING;
10024
10025 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10026 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10027 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10028 return;
10029
10030 off = LineOffset[row] + col;
10031 if (enc_utf8 && ScreenLinesUC[off] != 0)
10032 c = ScreenLinesUC[off];
10033 else
10034 c = ScreenLines[off];
10035 buflen += mb_char2bytes(c, buf);
10036
10037 if (enc_utf8)
10038 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10039 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10040
10041 buf[buflen] = NUL;
10042 rettv->vval.v_string = vim_strsave(buf);
10043}
10044
10045/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010046 * "search()" function
10047 */
10048 static void
10049f_search(typval_T *argvars, typval_T *rettv)
10050{
10051 int flags = 0;
10052
10053 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10054}
10055
10056/*
10057 * "searchdecl()" function
10058 */
10059 static void
10060f_searchdecl(typval_T *argvars, typval_T *rettv)
10061{
10062 int locally = 1;
10063 int thisblock = 0;
10064 int error = FALSE;
10065 char_u *name;
10066
10067 rettv->vval.v_number = 1; /* default: FAIL */
10068
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010069 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010070 if (argvars[1].v_type != VAR_UNKNOWN)
10071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010072 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010074 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010075 }
10076 if (!error && name != NULL)
10077 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10078 locally, thisblock, SEARCH_KEEP) == FAIL;
10079}
10080
10081/*
10082 * Used by searchpair() and searchpairpos()
10083 */
10084 static int
10085searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10086{
10087 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010088 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089 int save_p_ws = p_ws;
10090 int dir;
10091 int flags = 0;
10092 char_u nbuf1[NUMBUFLEN];
10093 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 int retval = 0; /* default: FAIL */
10095 long lnum_stop = 0;
10096 long time_limit = 0;
10097
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010098 /* Get the three pattern arguments: start, middle, end. Will result in an
10099 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010100 spat = tv_get_string_chk(&argvars[0]);
10101 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10102 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010103 if (spat == NULL || mpat == NULL || epat == NULL)
10104 goto theend; /* type error */
10105
10106 /* Handle the optional fourth argument: flags */
10107 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10108 if (dir == 0)
10109 goto theend;
10110
10111 /* Don't accept SP_END or SP_SUBPAT.
10112 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10113 */
10114 if ((flags & (SP_END | SP_SUBPAT)) != 0
10115 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010117 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118 goto theend;
10119 }
10120
10121 /* Using 'r' implies 'W', otherwise it doesn't work. */
10122 if (flags & SP_REPEAT)
10123 p_ws = FALSE;
10124
10125 /* Optional fifth argument: skip expression */
10126 if (argvars[3].v_type == VAR_UNKNOWN
10127 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010128 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 else
10130 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010131 skip = &argvars[4];
10132 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10133 && skip->v_type != VAR_STRING)
10134 {
10135 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010136 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010137 goto theend;
10138 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010139 if (argvars[5].v_type != VAR_UNKNOWN)
10140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010141 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010142 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010144 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010146 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147#ifdef FEAT_RELTIME
10148 if (argvars[6].v_type != VAR_UNKNOWN)
10149 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010150 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010153 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010155 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010156 }
10157#endif
10158 }
10159 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160
10161 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10162 match_pos, lnum_stop, time_limit);
10163
10164theend:
10165 p_ws = save_p_ws;
10166
10167 return retval;
10168}
10169
10170/*
10171 * "searchpair()" function
10172 */
10173 static void
10174f_searchpair(typval_T *argvars, typval_T *rettv)
10175{
10176 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10177}
10178
10179/*
10180 * "searchpairpos()" function
10181 */
10182 static void
10183f_searchpairpos(typval_T *argvars, typval_T *rettv)
10184{
10185 pos_T match_pos;
10186 int lnum = 0;
10187 int col = 0;
10188
10189 if (rettv_list_alloc(rettv) == FAIL)
10190 return;
10191
10192 if (searchpair_cmn(argvars, &match_pos) > 0)
10193 {
10194 lnum = match_pos.lnum;
10195 col = match_pos.col;
10196 }
10197
10198 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10199 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10200}
10201
10202/*
10203 * Search for a start/middle/end thing.
10204 * Used by searchpair(), see its documentation for the details.
10205 * Returns 0 or -1 for no match,
10206 */
10207 long
10208do_searchpair(
10209 char_u *spat, /* start pattern */
10210 char_u *mpat, /* middle pattern */
10211 char_u *epat, /* end pattern */
10212 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010213 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010214 int flags, /* SP_SETPCMARK and other SP_ values */
10215 pos_T *match_pos,
10216 linenr_T lnum_stop, /* stop at this line if not zero */
10217 long time_limit UNUSED) /* stop after this many msec */
10218{
10219 char_u *save_cpo;
10220 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10221 long retval = 0;
10222 pos_T pos;
10223 pos_T firstpos;
10224 pos_T foundpos;
10225 pos_T save_cursor;
10226 pos_T save_pos;
10227 int n;
10228 int r;
10229 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010230 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231 int err;
10232 int options = SEARCH_KEEP;
10233 proftime_T tm;
10234
10235 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10236 save_cpo = p_cpo;
10237 p_cpo = empty_option;
10238
10239#ifdef FEAT_RELTIME
10240 /* Set the time limit, if there is one. */
10241 profile_setlimit(time_limit, &tm);
10242#endif
10243
10244 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10245 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010246 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10247 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010248 if (pat2 == NULL || pat3 == NULL)
10249 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010250 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010251 if (*mpat == NUL)
10252 STRCPY(pat3, pat2);
10253 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010254 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 spat, epat, mpat);
10256 if (flags & SP_START)
10257 options |= SEARCH_START;
10258
Bram Moolenaar48570482017-10-30 21:48:41 +010010259 if (skip != NULL)
10260 {
10261 /* Empty string means to not use the skip expression. */
10262 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10263 use_skip = skip->vval.v_string != NULL
10264 && *skip->vval.v_string != NUL;
10265 }
10266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 save_cursor = curwin->w_cursor;
10268 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010269 CLEAR_POS(&firstpos);
10270 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271 pat = pat3;
10272 for (;;)
10273 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010274 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010275 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010276 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 /* didn't find it or found the first match again: FAIL */
10278 break;
10279
10280 if (firstpos.lnum == 0)
10281 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010282 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 {
10284 /* Found the same position again. Can happen with a pattern that
10285 * has "\zs" at the end and searching backwards. Advance one
10286 * character and try again. */
10287 if (dir == BACKWARD)
10288 decl(&pos);
10289 else
10290 incl(&pos);
10291 }
10292 foundpos = pos;
10293
10294 /* clear the start flag to avoid getting stuck here */
10295 options &= ~SEARCH_START;
10296
10297 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010298 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 {
10300 save_pos = curwin->w_cursor;
10301 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010302 err = FALSE;
10303 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 curwin->w_cursor = save_pos;
10305 if (err)
10306 {
10307 /* Evaluating {skip} caused an error, break here. */
10308 curwin->w_cursor = save_cursor;
10309 retval = -1;
10310 break;
10311 }
10312 if (r)
10313 continue;
10314 }
10315
10316 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10317 {
10318 /* Found end when searching backwards or start when searching
10319 * forward: nested pair. */
10320 ++nest;
10321 pat = pat2; /* nested, don't search for middle */
10322 }
10323 else
10324 {
10325 /* Found end when searching forward or start when searching
10326 * backward: end of (nested) pair; or found middle in outer pair. */
10327 if (--nest == 1)
10328 pat = pat3; /* outer level, search for middle */
10329 }
10330
10331 if (nest == 0)
10332 {
10333 /* Found the match: return matchcount or line number. */
10334 if (flags & SP_RETCOUNT)
10335 ++retval;
10336 else
10337 retval = pos.lnum;
10338 if (flags & SP_SETPCMARK)
10339 setpcmark();
10340 curwin->w_cursor = pos;
10341 if (!(flags & SP_REPEAT))
10342 break;
10343 nest = 1; /* search for next unmatched */
10344 }
10345 }
10346
10347 if (match_pos != NULL)
10348 {
10349 /* Store the match cursor position */
10350 match_pos->lnum = curwin->w_cursor.lnum;
10351 match_pos->col = curwin->w_cursor.col + 1;
10352 }
10353
10354 /* If 'n' flag is used or search failed: restore cursor position. */
10355 if ((flags & SP_NOMOVE) || retval == 0)
10356 curwin->w_cursor = save_cursor;
10357
10358theend:
10359 vim_free(pat2);
10360 vim_free(pat3);
10361 if (p_cpo == empty_option)
10362 p_cpo = save_cpo;
10363 else
10364 /* Darn, evaluating the {skip} expression changed the value. */
10365 free_string_option(save_cpo);
10366
10367 return retval;
10368}
10369
10370/*
10371 * "searchpos()" function
10372 */
10373 static void
10374f_searchpos(typval_T *argvars, typval_T *rettv)
10375{
10376 pos_T match_pos;
10377 int lnum = 0;
10378 int col = 0;
10379 int n;
10380 int flags = 0;
10381
10382 if (rettv_list_alloc(rettv) == FAIL)
10383 return;
10384
10385 n = search_cmn(argvars, &match_pos, &flags);
10386 if (n > 0)
10387 {
10388 lnum = match_pos.lnum;
10389 col = match_pos.col;
10390 }
10391
10392 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10393 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10394 if (flags & SP_SUBPAT)
10395 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10396}
10397
10398 static void
10399f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10400{
10401#ifdef FEAT_CLIENTSERVER
10402 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010403 char_u *server = tv_get_string_chk(&argvars[0]);
10404 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405
10406 rettv->vval.v_number = -1;
10407 if (server == NULL || reply == NULL)
10408 return;
10409 if (check_restricted() || check_secure())
10410 return;
10411# ifdef FEAT_X11
10412 if (check_connection() == FAIL)
10413 return;
10414# endif
10415
10416 if (serverSendReply(server, reply) < 0)
10417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010418 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010419 return;
10420 }
10421 rettv->vval.v_number = 0;
10422#else
10423 rettv->vval.v_number = -1;
10424#endif
10425}
10426
10427 static void
10428f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10429{
10430 char_u *r = NULL;
10431
10432#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010433# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010434 r = serverGetVimNames();
10435# else
10436 make_connection();
10437 if (X_DISPLAY != NULL)
10438 r = serverGetVimNames(X_DISPLAY);
10439# endif
10440#endif
10441 rettv->v_type = VAR_STRING;
10442 rettv->vval.v_string = r;
10443}
10444
10445/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010446 * "setbufline()" function
10447 */
10448 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010449f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010450{
10451 linenr_T lnum;
10452 buf_T *buf;
10453
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010454 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010455 if (buf == NULL)
10456 rettv->vval.v_number = 1; /* FAIL */
10457 else
10458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010459 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010460 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010461 }
10462}
10463
10464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 * "setbufvar()" function
10466 */
10467 static void
10468f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10469{
10470 buf_T *buf;
10471 char_u *varname, *bufvarname;
10472 typval_T *varp;
10473 char_u nbuf[NUMBUFLEN];
10474
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010475 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010477 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10478 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010479 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 varp = &argvars[2];
10481
10482 if (buf != NULL && varname != NULL && varp != NULL)
10483 {
10484 if (*varname == '&')
10485 {
10486 long numval;
10487 char_u *strval;
10488 int error = FALSE;
10489 aco_save_T aco;
10490
10491 /* set curbuf to be our buf, temporarily */
10492 aucmd_prepbuf(&aco, buf);
10493
10494 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010495 numval = (long)tv_get_number_chk(varp, &error);
10496 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010497 if (!error && strval != NULL)
10498 set_option_value(varname, numval, strval, OPT_LOCAL);
10499
10500 /* reset notion of buffer */
10501 aucmd_restbuf(&aco);
10502 }
10503 else
10504 {
10505 buf_T *save_curbuf = curbuf;
10506
Bram Moolenaar964b3742019-05-24 18:54:09 +020010507 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010508 if (bufvarname != NULL)
10509 {
10510 curbuf = buf;
10511 STRCPY(bufvarname, "b:");
10512 STRCPY(bufvarname + 2, varname);
10513 set_var(bufvarname, varp, TRUE);
10514 vim_free(bufvarname);
10515 curbuf = save_curbuf;
10516 }
10517 }
10518 }
10519}
10520
10521 static void
10522f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10523{
10524 dict_T *d;
10525 dictitem_T *di;
10526 char_u *csearch;
10527
10528 if (argvars[0].v_type != VAR_DICT)
10529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010530 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010531 return;
10532 }
10533
10534 if ((d = argvars[0].vval.v_dict) != NULL)
10535 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010536 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010537 if (csearch != NULL)
10538 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010539 if (enc_utf8)
10540 {
10541 int pcc[MAX_MCO];
10542 int c = utfc_ptr2char(csearch, pcc);
10543
10544 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10545 }
10546 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 set_last_csearch(PTR2CHAR(csearch),
10548 csearch, MB_PTR2LEN(csearch));
10549 }
10550
10551 di = dict_find(d, (char_u *)"forward", -1);
10552 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010553 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010554 ? FORWARD : BACKWARD);
10555
10556 di = dict_find(d, (char_u *)"until", -1);
10557 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010558 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 }
10560}
10561
10562/*
10563 * "setcmdpos()" function
10564 */
10565 static void
10566f_setcmdpos(typval_T *argvars, typval_T *rettv)
10567{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010568 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569
10570 if (pos >= 0)
10571 rettv->vval.v_number = set_cmdline_pos(pos);
10572}
10573
10574/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010575 * "setenv()" function
10576 */
10577 static void
10578f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10579{
10580 char_u namebuf[NUMBUFLEN];
10581 char_u valbuf[NUMBUFLEN];
10582 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10583
10584 if (argvars[1].v_type == VAR_SPECIAL
10585 && argvars[1].vval.v_number == VVAL_NULL)
10586 vim_unsetenv(name);
10587 else
10588 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10589}
10590
10591/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 * "setfperm({fname}, {mode})" function
10593 */
10594 static void
10595f_setfperm(typval_T *argvars, typval_T *rettv)
10596{
10597 char_u *fname;
10598 char_u modebuf[NUMBUFLEN];
10599 char_u *mode_str;
10600 int i;
10601 int mask;
10602 int mode = 0;
10603
10604 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010605 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 if (fname == NULL)
10607 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010608 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 if (mode_str == NULL)
10610 return;
10611 if (STRLEN(mode_str) != 9)
10612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010613 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 return;
10615 }
10616
10617 mask = 1;
10618 for (i = 8; i >= 0; --i)
10619 {
10620 if (mode_str[i] != '-')
10621 mode |= mask;
10622 mask = mask << 1;
10623 }
10624 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10625}
10626
10627/*
10628 * "setline()" function
10629 */
10630 static void
10631f_setline(typval_T *argvars, typval_T *rettv)
10632{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010633 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634
Bram Moolenaarca851592018-06-06 21:04:07 +020010635 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636}
10637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638/*
10639 * Used by "setqflist()" and "setloclist()" functions
10640 */
10641 static void
10642set_qf_ll_list(
10643 win_T *wp UNUSED,
10644 typval_T *list_arg UNUSED,
10645 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010646 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010647 typval_T *rettv)
10648{
10649#ifdef FEAT_QUICKFIX
10650 static char *e_invact = N_("E927: Invalid action: '%s'");
10651 char_u *act;
10652 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010653 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654#endif
10655
10656 rettv->vval.v_number = -1;
10657
10658#ifdef FEAT_QUICKFIX
10659 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010660 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010661 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010662 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 else
10664 {
10665 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010666 dict_T *d = NULL;
10667 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668
10669 if (action_arg->v_type == VAR_STRING)
10670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010671 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672 if (act == NULL)
10673 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010674 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10675 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676 action = *act;
10677 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010678 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010679 }
10680 else if (action_arg->v_type == VAR_UNKNOWN)
10681 action = ' ';
10682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010683 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684
Bram Moolenaard823fa92016-08-12 16:29:27 +020010685 if (action_arg->v_type != VAR_UNKNOWN
10686 && what_arg->v_type != VAR_UNKNOWN)
10687 {
10688 if (what_arg->v_type == VAR_DICT)
10689 d = what_arg->vval.v_dict;
10690 else
10691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010692 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010693 valid_dict = FALSE;
10694 }
10695 }
10696
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010697 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010698 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010699 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10700 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010701 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010702 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 }
10704#endif
10705}
10706
10707/*
10708 * "setloclist()" function
10709 */
10710 static void
10711f_setloclist(typval_T *argvars, typval_T *rettv)
10712{
10713 win_T *win;
10714
10715 rettv->vval.v_number = -1;
10716
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010717 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010718 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010719 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010720}
10721
10722/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010723 * "setpos()" function
10724 */
10725 static void
10726f_setpos(typval_T *argvars, typval_T *rettv)
10727{
10728 pos_T pos;
10729 int fnum;
10730 char_u *name;
10731 colnr_T curswant = -1;
10732
10733 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010734 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 if (name != NULL)
10736 {
10737 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10738 {
10739 if (--pos.col < 0)
10740 pos.col = 0;
10741 if (name[0] == '.' && name[1] == NUL)
10742 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010743 /* set cursor; "fnum" is ignored */
10744 curwin->w_cursor = pos;
10745 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010747 curwin->w_curswant = curswant - 1;
10748 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010749 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010750 check_cursor();
10751 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 }
10753 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10754 {
10755 /* set mark */
10756 if (setmark_pos(name[1], &pos, fnum) == OK)
10757 rettv->vval.v_number = 0;
10758 }
10759 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010760 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761 }
10762 }
10763}
10764
10765/*
10766 * "setqflist()" function
10767 */
10768 static void
10769f_setqflist(typval_T *argvars, typval_T *rettv)
10770{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010771 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772}
10773
10774/*
10775 * "setreg()" function
10776 */
10777 static void
10778f_setreg(typval_T *argvars, typval_T *rettv)
10779{
10780 int regname;
10781 char_u *strregname;
10782 char_u *stropt;
10783 char_u *strval;
10784 int append;
10785 char_u yank_type;
10786 long block_len;
10787
10788 block_len = -1;
10789 yank_type = MAUTO;
10790 append = FALSE;
10791
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010792 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 rettv->vval.v_number = 1; /* FAIL is default */
10794
10795 if (strregname == NULL)
10796 return; /* type error; errmsg already given */
10797 regname = *strregname;
10798 if (regname == 0 || regname == '@')
10799 regname = '"';
10800
10801 if (argvars[2].v_type != VAR_UNKNOWN)
10802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010803 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 if (stropt == NULL)
10805 return; /* type error */
10806 for (; *stropt != NUL; ++stropt)
10807 switch (*stropt)
10808 {
10809 case 'a': case 'A': /* append */
10810 append = TRUE;
10811 break;
10812 case 'v': case 'c': /* character-wise selection */
10813 yank_type = MCHAR;
10814 break;
10815 case 'V': case 'l': /* line-wise selection */
10816 yank_type = MLINE;
10817 break;
10818 case 'b': case Ctrl_V: /* block-wise selection */
10819 yank_type = MBLOCK;
10820 if (VIM_ISDIGIT(stropt[1]))
10821 {
10822 ++stropt;
10823 block_len = getdigits(&stropt) - 1;
10824 --stropt;
10825 }
10826 break;
10827 }
10828 }
10829
10830 if (argvars[1].v_type == VAR_LIST)
10831 {
10832 char_u **lstval;
10833 char_u **allocval;
10834 char_u buf[NUMBUFLEN];
10835 char_u **curval;
10836 char_u **curallocval;
10837 list_T *ll = argvars[1].vval.v_list;
10838 listitem_T *li;
10839 int len;
10840
10841 /* If the list is NULL handle like an empty list. */
10842 len = ll == NULL ? 0 : ll->lv_len;
10843
10844 /* First half: use for pointers to result lines; second half: use for
10845 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010846 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010847 if (lstval == NULL)
10848 return;
10849 curval = lstval;
10850 allocval = lstval + len + 2;
10851 curallocval = allocval;
10852
10853 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10854 li = li->li_next)
10855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010856 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010857 if (strval == NULL)
10858 goto free_lstval;
10859 if (strval == buf)
10860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010861 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010862 * overwrite the string. */
10863 strval = vim_strsave(buf);
10864 if (strval == NULL)
10865 goto free_lstval;
10866 *curallocval++ = strval;
10867 }
10868 *curval++ = strval;
10869 }
10870 *curval++ = NULL;
10871
10872 write_reg_contents_lst(regname, lstval, -1,
10873 append, yank_type, block_len);
10874free_lstval:
10875 while (curallocval > allocval)
10876 vim_free(*--curallocval);
10877 vim_free(lstval);
10878 }
10879 else
10880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010881 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010882 if (strval == NULL)
10883 return;
10884 write_reg_contents_ex(regname, strval, -1,
10885 append, yank_type, block_len);
10886 }
10887 rettv->vval.v_number = 0;
10888}
10889
10890/*
10891 * "settabvar()" function
10892 */
10893 static void
10894f_settabvar(typval_T *argvars, typval_T *rettv)
10895{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 tabpage_T *save_curtab;
10897 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 char_u *varname, *tabvarname;
10899 typval_T *varp;
10900
10901 rettv->vval.v_number = 0;
10902
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010903 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 return;
10905
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010906 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10907 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 varp = &argvars[2];
10909
Bram Moolenaar4033c552017-09-16 20:54:51 +020010910 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 save_curtab = curtab;
10913 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914
Bram Moolenaar964b3742019-05-24 18:54:09 +020010915 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 if (tabvarname != NULL)
10917 {
10918 STRCPY(tabvarname, "t:");
10919 STRCPY(tabvarname + 2, varname);
10920 set_var(tabvarname, varp, TRUE);
10921 vim_free(tabvarname);
10922 }
10923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 /* Restore current tabpage */
10925 if (valid_tabpage(save_curtab))
10926 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 }
10928}
10929
10930/*
10931 * "settabwinvar()" function
10932 */
10933 static void
10934f_settabwinvar(typval_T *argvars, typval_T *rettv)
10935{
10936 setwinvar(argvars, rettv, 1);
10937}
10938
10939/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010940 * "settagstack()" function
10941 */
10942 static void
10943f_settagstack(typval_T *argvars, typval_T *rettv)
10944{
10945 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10946 win_T *wp;
10947 dict_T *d;
10948 int action = 'r';
10949
10950 rettv->vval.v_number = -1;
10951
10952 // first argument: window number or id
10953 wp = find_win_by_nr_or_id(&argvars[0]);
10954 if (wp == NULL)
10955 return;
10956
10957 // second argument: dict with items to set in the tag stack
10958 if (argvars[1].v_type != VAR_DICT)
10959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010960 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010961 return;
10962 }
10963 d = argvars[1].vval.v_dict;
10964 if (d == NULL)
10965 return;
10966
10967 // third argument: action - 'a' for append and 'r' for replace.
10968 // default is to replace the stack.
10969 if (argvars[2].v_type == VAR_UNKNOWN)
10970 action = 'r';
10971 else if (argvars[2].v_type == VAR_STRING)
10972 {
10973 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010975 if (actstr == NULL)
10976 return;
10977 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10978 action = *actstr;
10979 else
10980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010981 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010982 return;
10983 }
10984 }
10985 else
10986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010987 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010988 return;
10989 }
10990
10991 if (set_tagstack(wp, d, action) == OK)
10992 rettv->vval.v_number = 0;
10993}
10994
10995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 * "setwinvar()" function
10997 */
10998 static void
10999f_setwinvar(typval_T *argvars, typval_T *rettv)
11000{
11001 setwinvar(argvars, rettv, 0);
11002}
11003
11004#ifdef FEAT_CRYPT
11005/*
11006 * "sha256({string})" function
11007 */
11008 static void
11009f_sha256(typval_T *argvars, typval_T *rettv)
11010{
11011 char_u *p;
11012
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011013 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014 rettv->vval.v_string = vim_strsave(
11015 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11016 rettv->v_type = VAR_STRING;
11017}
11018#endif /* FEAT_CRYPT */
11019
11020/*
11021 * "shellescape({string})" function
11022 */
11023 static void
11024f_shellescape(typval_T *argvars, typval_T *rettv)
11025{
Bram Moolenaar20615522017-06-05 18:46:26 +020011026 int do_special = non_zero_arg(&argvars[1]);
11027
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011029 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 rettv->v_type = VAR_STRING;
11031}
11032
11033/*
11034 * shiftwidth() function
11035 */
11036 static void
11037f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11038{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011039 rettv->vval.v_number = 0;
11040
11041 if (argvars[0].v_type != VAR_UNKNOWN)
11042 {
11043 long col;
11044
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011045 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011046 if (col < 0)
11047 return; // type error; errmsg already given
11048#ifdef FEAT_VARTABS
11049 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11050 return;
11051#endif
11052 }
11053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054 rettv->vval.v_number = get_sw_value(curbuf);
11055}
11056
11057/*
11058 * "simplify()" function
11059 */
11060 static void
11061f_simplify(typval_T *argvars, typval_T *rettv)
11062{
11063 char_u *p;
11064
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011065 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 rettv->vval.v_string = vim_strsave(p);
11067 simplify_filename(rettv->vval.v_string); /* simplify in place */
11068 rettv->v_type = VAR_STRING;
11069}
11070
11071#ifdef FEAT_FLOAT
11072/*
11073 * "sin()" function
11074 */
11075 static void
11076f_sin(typval_T *argvars, typval_T *rettv)
11077{
11078 float_T f = 0.0;
11079
11080 rettv->v_type = VAR_FLOAT;
11081 if (get_float_arg(argvars, &f) == OK)
11082 rettv->vval.v_float = sin(f);
11083 else
11084 rettv->vval.v_float = 0.0;
11085}
11086
11087/*
11088 * "sinh()" function
11089 */
11090 static void
11091f_sinh(typval_T *argvars, typval_T *rettv)
11092{
11093 float_T f = 0.0;
11094
11095 rettv->v_type = VAR_FLOAT;
11096 if (get_float_arg(argvars, &f) == OK)
11097 rettv->vval.v_float = sinh(f);
11098 else
11099 rettv->vval.v_float = 0.0;
11100}
11101#endif
11102
Bram Moolenaareae1b912019-05-09 15:12:55 +020011103static int item_compare(const void *s1, const void *s2);
11104static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105
11106/* struct used in the array that's given to qsort() */
11107typedef struct
11108{
11109 listitem_T *item;
11110 int idx;
11111} sortItem_T;
11112
11113/* struct storing information about current sort */
11114typedef struct
11115{
11116 int item_compare_ic;
11117 int item_compare_numeric;
11118 int item_compare_numbers;
11119#ifdef FEAT_FLOAT
11120 int item_compare_float;
11121#endif
11122 char_u *item_compare_func;
11123 partial_T *item_compare_partial;
11124 dict_T *item_compare_selfdict;
11125 int item_compare_func_err;
11126 int item_compare_keep_zero;
11127} sortinfo_T;
11128static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129#define ITEM_COMPARE_FAIL 999
11130
11131/*
11132 * Compare functions for f_sort() and f_uniq() below.
11133 */
11134 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135item_compare(const void *s1, const void *s2)
11136{
11137 sortItem_T *si1, *si2;
11138 typval_T *tv1, *tv2;
11139 char_u *p1, *p2;
11140 char_u *tofree1 = NULL, *tofree2 = NULL;
11141 int res;
11142 char_u numbuf1[NUMBUFLEN];
11143 char_u numbuf2[NUMBUFLEN];
11144
11145 si1 = (sortItem_T *)s1;
11146 si2 = (sortItem_T *)s2;
11147 tv1 = &si1->item->li_tv;
11148 tv2 = &si2->item->li_tv;
11149
11150 if (sortinfo->item_compare_numbers)
11151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011152 varnumber_T v1 = tv_get_number(tv1);
11153 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154
11155 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11156 }
11157
11158#ifdef FEAT_FLOAT
11159 if (sortinfo->item_compare_float)
11160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011161 float_T v1 = tv_get_float(tv1);
11162 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011163
11164 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11165 }
11166#endif
11167
11168 /* tv2string() puts quotes around a string and allocates memory. Don't do
11169 * that for string variables. Use a single quote when comparing with a
11170 * non-string to do what the docs promise. */
11171 if (tv1->v_type == VAR_STRING)
11172 {
11173 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11174 p1 = (char_u *)"'";
11175 else
11176 p1 = tv1->vval.v_string;
11177 }
11178 else
11179 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11180 if (tv2->v_type == VAR_STRING)
11181 {
11182 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11183 p2 = (char_u *)"'";
11184 else
11185 p2 = tv2->vval.v_string;
11186 }
11187 else
11188 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11189 if (p1 == NULL)
11190 p1 = (char_u *)"";
11191 if (p2 == NULL)
11192 p2 = (char_u *)"";
11193 if (!sortinfo->item_compare_numeric)
11194 {
11195 if (sortinfo->item_compare_ic)
11196 res = STRICMP(p1, p2);
11197 else
11198 res = STRCMP(p1, p2);
11199 }
11200 else
11201 {
11202 double n1, n2;
11203 n1 = strtod((char *)p1, (char **)&p1);
11204 n2 = strtod((char *)p2, (char **)&p2);
11205 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11206 }
11207
11208 /* When the result would be zero, compare the item indexes. Makes the
11209 * sort stable. */
11210 if (res == 0 && !sortinfo->item_compare_keep_zero)
11211 res = si1->idx > si2->idx ? 1 : -1;
11212
11213 vim_free(tofree1);
11214 vim_free(tofree2);
11215 return res;
11216}
11217
11218 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219item_compare2(const void *s1, const void *s2)
11220{
11221 sortItem_T *si1, *si2;
11222 int res;
11223 typval_T rettv;
11224 typval_T argv[3];
11225 int dummy;
11226 char_u *func_name;
11227 partial_T *partial = sortinfo->item_compare_partial;
11228
11229 /* shortcut after failure in previous call; compare all items equal */
11230 if (sortinfo->item_compare_func_err)
11231 return 0;
11232
11233 si1 = (sortItem_T *)s1;
11234 si2 = (sortItem_T *)s2;
11235
11236 if (partial == NULL)
11237 func_name = sortinfo->item_compare_func;
11238 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011239 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240
11241 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11242 * in the copy without changing the original list items. */
11243 copy_tv(&si1->item->li_tv, &argv[0]);
11244 copy_tv(&si2->item->li_tv, &argv[1]);
11245
11246 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011247 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011248 partial, sortinfo->item_compare_selfdict);
11249 clear_tv(&argv[0]);
11250 clear_tv(&argv[1]);
11251
11252 if (res == FAIL)
11253 res = ITEM_COMPARE_FAIL;
11254 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011255 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011256 if (sortinfo->item_compare_func_err)
11257 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11258 clear_tv(&rettv);
11259
11260 /* When the result would be zero, compare the pointers themselves. Makes
11261 * the sort stable. */
11262 if (res == 0 && !sortinfo->item_compare_keep_zero)
11263 res = si1->idx > si2->idx ? 1 : -1;
11264
11265 return res;
11266}
11267
11268/*
11269 * "sort({list})" function
11270 */
11271 static void
11272do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11273{
11274 list_T *l;
11275 listitem_T *li;
11276 sortItem_T *ptrs;
11277 sortinfo_T *old_sortinfo;
11278 sortinfo_T info;
11279 long len;
11280 long i;
11281
11282 /* Pointer to current info struct used in compare function. Save and
11283 * restore the current one for nested calls. */
11284 old_sortinfo = sortinfo;
11285 sortinfo = &info;
11286
11287 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011288 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011289 else
11290 {
11291 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011292 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11294 TRUE))
11295 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011296 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011297
11298 len = list_len(l);
11299 if (len <= 1)
11300 goto theend; /* short list sorts pretty quickly */
11301
11302 info.item_compare_ic = FALSE;
11303 info.item_compare_numeric = FALSE;
11304 info.item_compare_numbers = FALSE;
11305#ifdef FEAT_FLOAT
11306 info.item_compare_float = FALSE;
11307#endif
11308 info.item_compare_func = NULL;
11309 info.item_compare_partial = NULL;
11310 info.item_compare_selfdict = NULL;
11311 if (argvars[1].v_type != VAR_UNKNOWN)
11312 {
11313 /* optional second argument: {func} */
11314 if (argvars[1].v_type == VAR_FUNC)
11315 info.item_compare_func = argvars[1].vval.v_string;
11316 else if (argvars[1].v_type == VAR_PARTIAL)
11317 info.item_compare_partial = argvars[1].vval.v_partial;
11318 else
11319 {
11320 int error = FALSE;
11321
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011322 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 if (error)
11324 goto theend; /* type error; errmsg already given */
11325 if (i == 1)
11326 info.item_compare_ic = TRUE;
11327 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011328 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 else if (i != 0)
11330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011331 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 goto theend;
11333 }
11334 if (info.item_compare_func != NULL)
11335 {
11336 if (*info.item_compare_func == NUL)
11337 {
11338 /* empty string means default sort */
11339 info.item_compare_func = NULL;
11340 }
11341 else if (STRCMP(info.item_compare_func, "n") == 0)
11342 {
11343 info.item_compare_func = NULL;
11344 info.item_compare_numeric = TRUE;
11345 }
11346 else if (STRCMP(info.item_compare_func, "N") == 0)
11347 {
11348 info.item_compare_func = NULL;
11349 info.item_compare_numbers = TRUE;
11350 }
11351#ifdef FEAT_FLOAT
11352 else if (STRCMP(info.item_compare_func, "f") == 0)
11353 {
11354 info.item_compare_func = NULL;
11355 info.item_compare_float = TRUE;
11356 }
11357#endif
11358 else if (STRCMP(info.item_compare_func, "i") == 0)
11359 {
11360 info.item_compare_func = NULL;
11361 info.item_compare_ic = TRUE;
11362 }
11363 }
11364 }
11365
11366 if (argvars[2].v_type != VAR_UNKNOWN)
11367 {
11368 /* optional third argument: {dict} */
11369 if (argvars[2].v_type != VAR_DICT)
11370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011371 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 goto theend;
11373 }
11374 info.item_compare_selfdict = argvars[2].vval.v_dict;
11375 }
11376 }
11377
11378 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011379 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 if (ptrs == NULL)
11381 goto theend;
11382
11383 i = 0;
11384 if (sort)
11385 {
11386 /* sort(): ptrs will be the list to sort */
11387 for (li = l->lv_first; li != NULL; li = li->li_next)
11388 {
11389 ptrs[i].item = li;
11390 ptrs[i].idx = i;
11391 ++i;
11392 }
11393
11394 info.item_compare_func_err = FALSE;
11395 info.item_compare_keep_zero = FALSE;
11396 /* test the compare function */
11397 if ((info.item_compare_func != NULL
11398 || info.item_compare_partial != NULL)
11399 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11400 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011401 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402 else
11403 {
11404 /* Sort the array with item pointers. */
11405 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11406 info.item_compare_func == NULL
11407 && info.item_compare_partial == NULL
11408 ? item_compare : item_compare2);
11409
11410 if (!info.item_compare_func_err)
11411 {
11412 /* Clear the List and append the items in sorted order. */
11413 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11414 l->lv_len = 0;
11415 for (i = 0; i < len; ++i)
11416 list_append(l, ptrs[i].item);
11417 }
11418 }
11419 }
11420 else
11421 {
11422 int (*item_compare_func_ptr)(const void *, const void *);
11423
11424 /* f_uniq(): ptrs will be a stack of items to remove */
11425 info.item_compare_func_err = FALSE;
11426 info.item_compare_keep_zero = TRUE;
11427 item_compare_func_ptr = info.item_compare_func != NULL
11428 || info.item_compare_partial != NULL
11429 ? item_compare2 : item_compare;
11430
11431 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11432 li = li->li_next)
11433 {
11434 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11435 == 0)
11436 ptrs[i++].item = li;
11437 if (info.item_compare_func_err)
11438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011439 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 break;
11441 }
11442 }
11443
11444 if (!info.item_compare_func_err)
11445 {
11446 while (--i >= 0)
11447 {
11448 li = ptrs[i].item->li_next;
11449 ptrs[i].item->li_next = li->li_next;
11450 if (li->li_next != NULL)
11451 li->li_next->li_prev = ptrs[i].item;
11452 else
11453 l->lv_last = ptrs[i].item;
11454 list_fix_watch(l, li);
11455 listitem_free(li);
11456 l->lv_len--;
11457 }
11458 }
11459 }
11460
11461 vim_free(ptrs);
11462 }
11463theend:
11464 sortinfo = old_sortinfo;
11465}
11466
11467/*
11468 * "sort({list})" function
11469 */
11470 static void
11471f_sort(typval_T *argvars, typval_T *rettv)
11472{
11473 do_sort_uniq(argvars, rettv, TRUE);
11474}
11475
11476/*
11477 * "uniq({list})" function
11478 */
11479 static void
11480f_uniq(typval_T *argvars, typval_T *rettv)
11481{
11482 do_sort_uniq(argvars, rettv, FALSE);
11483}
11484
11485/*
11486 * "soundfold({word})" function
11487 */
11488 static void
11489f_soundfold(typval_T *argvars, typval_T *rettv)
11490{
11491 char_u *s;
11492
11493 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011494 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495#ifdef FEAT_SPELL
11496 rettv->vval.v_string = eval_soundfold(s);
11497#else
11498 rettv->vval.v_string = vim_strsave(s);
11499#endif
11500}
11501
11502/*
11503 * "spellbadword()" function
11504 */
11505 static void
11506f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11507{
11508 char_u *word = (char_u *)"";
11509 hlf_T attr = HLF_COUNT;
11510 int len = 0;
11511
11512 if (rettv_list_alloc(rettv) == FAIL)
11513 return;
11514
11515#ifdef FEAT_SPELL
11516 if (argvars[0].v_type == VAR_UNKNOWN)
11517 {
11518 /* Find the start and length of the badly spelled word. */
11519 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11520 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011521 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011522 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011523 curwin->w_set_curswant = TRUE;
11524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011525 }
11526 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011528 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011529 int capcol = -1;
11530
11531 if (str != NULL)
11532 {
11533 /* Check the argument for spelling. */
11534 while (*str != NUL)
11535 {
11536 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11537 if (attr != HLF_COUNT)
11538 {
11539 word = str;
11540 break;
11541 }
11542 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011543 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544 }
11545 }
11546 }
11547#endif
11548
11549 list_append_string(rettv->vval.v_list, word, len);
11550 list_append_string(rettv->vval.v_list, (char_u *)(
11551 attr == HLF_SPB ? "bad" :
11552 attr == HLF_SPR ? "rare" :
11553 attr == HLF_SPL ? "local" :
11554 attr == HLF_SPC ? "caps" :
11555 ""), -1);
11556}
11557
11558/*
11559 * "spellsuggest()" function
11560 */
11561 static void
11562f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11563{
11564#ifdef FEAT_SPELL
11565 char_u *str;
11566 int typeerr = FALSE;
11567 int maxcount;
11568 garray_T ga;
11569 int i;
11570 listitem_T *li;
11571 int need_capital = FALSE;
11572#endif
11573
11574 if (rettv_list_alloc(rettv) == FAIL)
11575 return;
11576
11577#ifdef FEAT_SPELL
11578 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011580 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 if (argvars[1].v_type != VAR_UNKNOWN)
11582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011583 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 if (maxcount <= 0)
11585 return;
11586 if (argvars[2].v_type != VAR_UNKNOWN)
11587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011588 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589 if (typeerr)
11590 return;
11591 }
11592 }
11593 else
11594 maxcount = 25;
11595
11596 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11597
11598 for (i = 0; i < ga.ga_len; ++i)
11599 {
11600 str = ((char_u **)ga.ga_data)[i];
11601
11602 li = listitem_alloc();
11603 if (li == NULL)
11604 vim_free(str);
11605 else
11606 {
11607 li->li_tv.v_type = VAR_STRING;
11608 li->li_tv.v_lock = 0;
11609 li->li_tv.vval.v_string = str;
11610 list_append(rettv->vval.v_list, li);
11611 }
11612 }
11613 ga_clear(&ga);
11614 }
11615#endif
11616}
11617
11618 static void
11619f_split(typval_T *argvars, typval_T *rettv)
11620{
11621 char_u *str;
11622 char_u *end;
11623 char_u *pat = NULL;
11624 regmatch_T regmatch;
11625 char_u patbuf[NUMBUFLEN];
11626 char_u *save_cpo;
11627 int match;
11628 colnr_T col = 0;
11629 int keepempty = FALSE;
11630 int typeerr = FALSE;
11631
11632 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11633 save_cpo = p_cpo;
11634 p_cpo = (char_u *)"";
11635
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011636 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637 if (argvars[1].v_type != VAR_UNKNOWN)
11638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011639 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 if (pat == NULL)
11641 typeerr = TRUE;
11642 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011643 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 }
11645 if (pat == NULL || *pat == NUL)
11646 pat = (char_u *)"[\\x01- ]\\+";
11647
11648 if (rettv_list_alloc(rettv) == FAIL)
11649 return;
11650 if (typeerr)
11651 return;
11652
11653 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11654 if (regmatch.regprog != NULL)
11655 {
11656 regmatch.rm_ic = FALSE;
11657 while (*str != NUL || keepempty)
11658 {
11659 if (*str == NUL)
11660 match = FALSE; /* empty item at the end */
11661 else
11662 match = vim_regexec_nl(&regmatch, str, col);
11663 if (match)
11664 end = regmatch.startp[0];
11665 else
11666 end = str + STRLEN(str);
11667 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11668 && *str != NUL && match && end < regmatch.endp[0]))
11669 {
11670 if (list_append_string(rettv->vval.v_list, str,
11671 (int)(end - str)) == FAIL)
11672 break;
11673 }
11674 if (!match)
11675 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011676 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011677 if (regmatch.endp[0] > str)
11678 col = 0;
11679 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011680 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011681 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682 str = regmatch.endp[0];
11683 }
11684
11685 vim_regfree(regmatch.regprog);
11686 }
11687
11688 p_cpo = save_cpo;
11689}
11690
11691#ifdef FEAT_FLOAT
11692/*
11693 * "sqrt()" function
11694 */
11695 static void
11696f_sqrt(typval_T *argvars, typval_T *rettv)
11697{
11698 float_T f = 0.0;
11699
11700 rettv->v_type = VAR_FLOAT;
11701 if (get_float_arg(argvars, &f) == OK)
11702 rettv->vval.v_float = sqrt(f);
11703 else
11704 rettv->vval.v_float = 0.0;
11705}
11706
11707/*
11708 * "str2float()" function
11709 */
11710 static void
11711f_str2float(typval_T *argvars, typval_T *rettv)
11712{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011713 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011714 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715
Bram Moolenaar08243d22017-01-10 16:12:29 +010011716 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 p = skipwhite(p + 1);
11718 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011719 if (isneg)
11720 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011721 rettv->v_type = VAR_FLOAT;
11722}
11723#endif
11724
11725/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011726 * "str2list()" function
11727 */
11728 static void
11729f_str2list(typval_T *argvars, typval_T *rettv)
11730{
11731 char_u *p;
11732 int utf8 = FALSE;
11733
11734 if (rettv_list_alloc(rettv) == FAIL)
11735 return;
11736
11737 if (argvars[1].v_type != VAR_UNKNOWN)
11738 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11739
11740 p = tv_get_string(&argvars[0]);
11741
11742 if (has_mbyte || utf8)
11743 {
11744 int (*ptr2len)(char_u *);
11745 int (*ptr2char)(char_u *);
11746
11747 if (utf8 || enc_utf8)
11748 {
11749 ptr2len = utf_ptr2len;
11750 ptr2char = utf_ptr2char;
11751 }
11752 else
11753 {
11754 ptr2len = mb_ptr2len;
11755 ptr2char = mb_ptr2char;
11756 }
11757
11758 for ( ; *p != NUL; p += (*ptr2len)(p))
11759 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11760 }
11761 else
11762 for ( ; *p != NUL; ++p)
11763 list_append_number(rettv->vval.v_list, *p);
11764}
11765
11766/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 * "str2nr()" function
11768 */
11769 static void
11770f_str2nr(typval_T *argvars, typval_T *rettv)
11771{
11772 int base = 10;
11773 char_u *p;
11774 varnumber_T n;
11775 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011776 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011777
11778 if (argvars[1].v_type != VAR_UNKNOWN)
11779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011780 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011781 if (base != 2 && base != 8 && base != 10 && base != 16)
11782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011783 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011784 return;
11785 }
11786 }
11787
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011788 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011789 isneg = (*p == '-');
11790 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011791 p = skipwhite(p + 1);
11792 switch (base)
11793 {
11794 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11795 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11796 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11797 default: what = 0;
11798 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011799 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11800 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011801 if (isneg)
11802 rettv->vval.v_number = -n;
11803 else
11804 rettv->vval.v_number = n;
11805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806}
11807
11808#ifdef HAVE_STRFTIME
11809/*
11810 * "strftime({format}[, {time}])" function
11811 */
11812 static void
11813f_strftime(typval_T *argvars, typval_T *rettv)
11814{
11815 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011816 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817 struct tm *curtime;
11818 time_t seconds;
11819 char_u *p;
11820
11821 rettv->v_type = VAR_STRING;
11822
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011823 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824 if (argvars[1].v_type == VAR_UNKNOWN)
11825 seconds = time(NULL);
11826 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011827 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011828 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829 /* MSVC returns NULL for an invalid value of seconds. */
11830 if (curtime == NULL)
11831 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11832 else
11833 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 vimconv_T conv;
11835 char_u *enc;
11836
11837 conv.vc_type = CONV_NONE;
11838 enc = enc_locale();
11839 convert_setup(&conv, p_enc, enc);
11840 if (conv.vc_type != CONV_NONE)
11841 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 if (p != NULL)
11843 (void)strftime((char *)result_buf, sizeof(result_buf),
11844 (char *)p, curtime);
11845 else
11846 result_buf[0] = NUL;
11847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011848 if (conv.vc_type != CONV_NONE)
11849 vim_free(p);
11850 convert_setup(&conv, enc, p_enc);
11851 if (conv.vc_type != CONV_NONE)
11852 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11853 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 rettv->vval.v_string = vim_strsave(result_buf);
11855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 /* Release conversion descriptors */
11857 convert_setup(&conv, NULL, NULL);
11858 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 }
11860}
11861#endif
11862
11863/*
11864 * "strgetchar()" function
11865 */
11866 static void
11867f_strgetchar(typval_T *argvars, typval_T *rettv)
11868{
11869 char_u *str;
11870 int len;
11871 int error = FALSE;
11872 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011873 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011874
11875 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011876 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011877 if (str == NULL)
11878 return;
11879 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011880 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011881 if (error)
11882 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883
Bram Moolenaar13505972019-01-24 15:04:48 +010011884 while (charidx >= 0 && byteidx < len)
11885 {
11886 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011887 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011888 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11889 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011890 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011891 --charidx;
11892 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011893 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011894}
11895
11896/*
11897 * "stridx()" function
11898 */
11899 static void
11900f_stridx(typval_T *argvars, typval_T *rettv)
11901{
11902 char_u buf[NUMBUFLEN];
11903 char_u *needle;
11904 char_u *haystack;
11905 char_u *save_haystack;
11906 char_u *pos;
11907 int start_idx;
11908
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011909 needle = tv_get_string_chk(&argvars[1]);
11910 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011911 rettv->vval.v_number = -1;
11912 if (needle == NULL || haystack == NULL)
11913 return; /* type error; errmsg already given */
11914
11915 if (argvars[2].v_type != VAR_UNKNOWN)
11916 {
11917 int error = FALSE;
11918
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011919 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 if (error || start_idx >= (int)STRLEN(haystack))
11921 return;
11922 if (start_idx >= 0)
11923 haystack += start_idx;
11924 }
11925
11926 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11927 if (pos != NULL)
11928 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11929}
11930
11931/*
11932 * "string()" function
11933 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011934 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935f_string(typval_T *argvars, typval_T *rettv)
11936{
11937 char_u *tofree;
11938 char_u numbuf[NUMBUFLEN];
11939
11940 rettv->v_type = VAR_STRING;
11941 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11942 get_copyID());
11943 /* Make a copy if we have a value but it's not in allocated memory. */
11944 if (rettv->vval.v_string != NULL && tofree == NULL)
11945 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11946}
11947
11948/*
11949 * "strlen()" function
11950 */
11951 static void
11952f_strlen(typval_T *argvars, typval_T *rettv)
11953{
11954 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011955 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011956}
11957
11958/*
11959 * "strchars()" function
11960 */
11961 static void
11962f_strchars(typval_T *argvars, typval_T *rettv)
11963{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011964 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011965 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011966 varnumber_T len = 0;
11967 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011968
11969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011970 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011972 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973 else
11974 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11976 while (*s != NUL)
11977 {
11978 func_mb_ptr2char_adv(&s);
11979 ++len;
11980 }
11981 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011982 }
11983}
11984
11985/*
11986 * "strdisplaywidth()" function
11987 */
11988 static void
11989f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11990{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011991 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011992 int col = 0;
11993
11994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011995 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011996
11997 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11998}
11999
12000/*
12001 * "strwidth()" function
12002 */
12003 static void
12004f_strwidth(typval_T *argvars, typval_T *rettv)
12005{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012006 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012007
Bram Moolenaar13505972019-01-24 15:04:48 +010012008 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012009}
12010
12011/*
12012 * "strcharpart()" function
12013 */
12014 static void
12015f_strcharpart(typval_T *argvars, typval_T *rettv)
12016{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012017 char_u *p;
12018 int nchar;
12019 int nbyte = 0;
12020 int charlen;
12021 int len = 0;
12022 int slen;
12023 int error = FALSE;
12024
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012025 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012026 slen = (int)STRLEN(p);
12027
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012028 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012029 if (!error)
12030 {
12031 if (nchar > 0)
12032 while (nchar > 0 && nbyte < slen)
12033 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012034 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012035 --nchar;
12036 }
12037 else
12038 nbyte = nchar;
12039 if (argvars[2].v_type != VAR_UNKNOWN)
12040 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012041 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012042 while (charlen > 0 && nbyte + len < slen)
12043 {
12044 int off = nbyte + len;
12045
12046 if (off < 0)
12047 len += 1;
12048 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012049 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050 --charlen;
12051 }
12052 }
12053 else
12054 len = slen - nbyte; /* default: all bytes that are available. */
12055 }
12056
12057 /*
12058 * Only return the overlap between the specified part and the actual
12059 * string.
12060 */
12061 if (nbyte < 0)
12062 {
12063 len += nbyte;
12064 nbyte = 0;
12065 }
12066 else if (nbyte > slen)
12067 nbyte = slen;
12068 if (len < 0)
12069 len = 0;
12070 else if (nbyte + len > slen)
12071 len = slen - nbyte;
12072
12073 rettv->v_type = VAR_STRING;
12074 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012075}
12076
12077/*
12078 * "strpart()" function
12079 */
12080 static void
12081f_strpart(typval_T *argvars, typval_T *rettv)
12082{
12083 char_u *p;
12084 int n;
12085 int len;
12086 int slen;
12087 int error = FALSE;
12088
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012089 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012090 slen = (int)STRLEN(p);
12091
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012092 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012093 if (error)
12094 len = 0;
12095 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012096 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097 else
12098 len = slen - n; /* default len: all bytes that are available. */
12099
12100 /*
12101 * Only return the overlap between the specified part and the actual
12102 * string.
12103 */
12104 if (n < 0)
12105 {
12106 len += n;
12107 n = 0;
12108 }
12109 else if (n > slen)
12110 n = slen;
12111 if (len < 0)
12112 len = 0;
12113 else if (n + len > slen)
12114 len = slen - n;
12115
12116 rettv->v_type = VAR_STRING;
12117 rettv->vval.v_string = vim_strnsave(p + n, len);
12118}
12119
12120/*
12121 * "strridx()" function
12122 */
12123 static void
12124f_strridx(typval_T *argvars, typval_T *rettv)
12125{
12126 char_u buf[NUMBUFLEN];
12127 char_u *needle;
12128 char_u *haystack;
12129 char_u *rest;
12130 char_u *lastmatch = NULL;
12131 int haystack_len, end_idx;
12132
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012133 needle = tv_get_string_chk(&argvars[1]);
12134 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012135
12136 rettv->vval.v_number = -1;
12137 if (needle == NULL || haystack == NULL)
12138 return; /* type error; errmsg already given */
12139
12140 haystack_len = (int)STRLEN(haystack);
12141 if (argvars[2].v_type != VAR_UNKNOWN)
12142 {
12143 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012144 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012145 if (end_idx < 0)
12146 return; /* can never find a match */
12147 }
12148 else
12149 end_idx = haystack_len;
12150
12151 if (*needle == NUL)
12152 {
12153 /* Empty string matches past the end. */
12154 lastmatch = haystack + end_idx;
12155 }
12156 else
12157 {
12158 for (rest = haystack; *rest != '\0'; ++rest)
12159 {
12160 rest = (char_u *)strstr((char *)rest, (char *)needle);
12161 if (rest == NULL || rest > haystack + end_idx)
12162 break;
12163 lastmatch = rest;
12164 }
12165 }
12166
12167 if (lastmatch == NULL)
12168 rettv->vval.v_number = -1;
12169 else
12170 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12171}
12172
12173/*
12174 * "strtrans()" function
12175 */
12176 static void
12177f_strtrans(typval_T *argvars, typval_T *rettv)
12178{
12179 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012180 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012181}
12182
12183/*
12184 * "submatch()" function
12185 */
12186 static void
12187f_submatch(typval_T *argvars, typval_T *rettv)
12188{
12189 int error = FALSE;
12190 int no;
12191 int retList = 0;
12192
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012193 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012194 if (error)
12195 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012196 if (no < 0 || no >= NSUBEXP)
12197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012198 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012199 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012200 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012201 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012202 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012203 if (error)
12204 return;
12205
12206 if (retList == 0)
12207 {
12208 rettv->v_type = VAR_STRING;
12209 rettv->vval.v_string = reg_submatch(no);
12210 }
12211 else
12212 {
12213 rettv->v_type = VAR_LIST;
12214 rettv->vval.v_list = reg_submatch_list(no);
12215 }
12216}
12217
12218/*
12219 * "substitute()" function
12220 */
12221 static void
12222f_substitute(typval_T *argvars, typval_T *rettv)
12223{
12224 char_u patbuf[NUMBUFLEN];
12225 char_u subbuf[NUMBUFLEN];
12226 char_u flagsbuf[NUMBUFLEN];
12227
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 char_u *str = tv_get_string_chk(&argvars[0]);
12229 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012230 char_u *sub = NULL;
12231 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012232 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012233
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012234 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12235 expr = &argvars[2];
12236 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012237 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012239 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012240 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12241 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012242 rettv->vval.v_string = NULL;
12243 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012244 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012245}
12246
12247/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012248 * "swapinfo(swap_filename)" function
12249 */
12250 static void
12251f_swapinfo(typval_T *argvars, typval_T *rettv)
12252{
12253 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012254 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012255}
12256
12257/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012258 * "swapname(expr)" function
12259 */
12260 static void
12261f_swapname(typval_T *argvars, typval_T *rettv)
12262{
12263 buf_T *buf;
12264
12265 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012266 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012267 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12268 || buf->b_ml.ml_mfp->mf_fname == NULL)
12269 rettv->vval.v_string = NULL;
12270 else
12271 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12272}
12273
12274/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012275 * "synID(lnum, col, trans)" function
12276 */
12277 static void
12278f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12279{
12280 int id = 0;
12281#ifdef FEAT_SYN_HL
12282 linenr_T lnum;
12283 colnr_T col;
12284 int trans;
12285 int transerr = FALSE;
12286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012287 lnum = tv_get_lnum(argvars); /* -1 on type error */
12288 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12289 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290
12291 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12292 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12293 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12294#endif
12295
12296 rettv->vval.v_number = id;
12297}
12298
12299/*
12300 * "synIDattr(id, what [, mode])" function
12301 */
12302 static void
12303f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12304{
12305 char_u *p = NULL;
12306#ifdef FEAT_SYN_HL
12307 int id;
12308 char_u *what;
12309 char_u *mode;
12310 char_u modebuf[NUMBUFLEN];
12311 int modec;
12312
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012313 id = (int)tv_get_number(&argvars[0]);
12314 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012315 if (argvars[2].v_type != VAR_UNKNOWN)
12316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012317 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012318 modec = TOLOWER_ASC(mode[0]);
12319 if (modec != 't' && modec != 'c' && modec != 'g')
12320 modec = 0; /* replace invalid with current */
12321 }
12322 else
12323 {
12324#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12325 if (USE_24BIT)
12326 modec = 'g';
12327 else
12328#endif
12329 if (t_colors > 1)
12330 modec = 'c';
12331 else
12332 modec = 't';
12333 }
12334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012335 switch (TOLOWER_ASC(what[0]))
12336 {
12337 case 'b':
12338 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12339 p = highlight_color(id, what, modec);
12340 else /* bold */
12341 p = highlight_has_attr(id, HL_BOLD, modec);
12342 break;
12343
12344 case 'f': /* fg[#] or font */
12345 p = highlight_color(id, what, modec);
12346 break;
12347
12348 case 'i':
12349 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12350 p = highlight_has_attr(id, HL_INVERSE, modec);
12351 else /* italic */
12352 p = highlight_has_attr(id, HL_ITALIC, modec);
12353 break;
12354
12355 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012356 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012357 break;
12358
12359 case 'r': /* reverse */
12360 p = highlight_has_attr(id, HL_INVERSE, modec);
12361 break;
12362
12363 case 's':
12364 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12365 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012366 /* strikeout */
12367 else if (TOLOWER_ASC(what[1]) == 't' &&
12368 TOLOWER_ASC(what[2]) == 'r')
12369 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370 else /* standout */
12371 p = highlight_has_attr(id, HL_STANDOUT, modec);
12372 break;
12373
12374 case 'u':
12375 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12376 /* underline */
12377 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12378 else
12379 /* undercurl */
12380 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12381 break;
12382 }
12383
12384 if (p != NULL)
12385 p = vim_strsave(p);
12386#endif
12387 rettv->v_type = VAR_STRING;
12388 rettv->vval.v_string = p;
12389}
12390
12391/*
12392 * "synIDtrans(id)" function
12393 */
12394 static void
12395f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12396{
12397 int id;
12398
12399#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012400 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012401
12402 if (id > 0)
12403 id = syn_get_final_id(id);
12404 else
12405#endif
12406 id = 0;
12407
12408 rettv->vval.v_number = id;
12409}
12410
12411/*
12412 * "synconcealed(lnum, col)" function
12413 */
12414 static void
12415f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12416{
12417#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12418 linenr_T lnum;
12419 colnr_T col;
12420 int syntax_flags = 0;
12421 int cchar;
12422 int matchid = 0;
12423 char_u str[NUMBUFLEN];
12424#endif
12425
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012426 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427
12428#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012429 lnum = tv_get_lnum(argvars); /* -1 on type error */
12430 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431
12432 vim_memset(str, NUL, sizeof(str));
12433
12434 if (rettv_list_alloc(rettv) != FAIL)
12435 {
12436 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12437 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12438 && curwin->w_p_cole > 0)
12439 {
12440 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12441 syntax_flags = get_syntax_info(&matchid);
12442
12443 /* get the conceal character */
12444 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12445 {
12446 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012447 if (cchar == NUL && curwin->w_p_cole == 1)
12448 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012449 if (cchar != NUL)
12450 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451 if (has_mbyte)
12452 (*mb_char2bytes)(cchar, str);
12453 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 str[0] = cchar;
12455 }
12456 }
12457 }
12458
12459 list_append_number(rettv->vval.v_list,
12460 (syntax_flags & HL_CONCEAL) != 0);
12461 /* -1 to auto-determine strlen */
12462 list_append_string(rettv->vval.v_list, str, -1);
12463 list_append_number(rettv->vval.v_list, matchid);
12464 }
12465#endif
12466}
12467
12468/*
12469 * "synstack(lnum, col)" function
12470 */
12471 static void
12472f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12473{
12474#ifdef FEAT_SYN_HL
12475 linenr_T lnum;
12476 colnr_T col;
12477 int i;
12478 int id;
12479#endif
12480
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012481 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482
12483#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012484 lnum = tv_get_lnum(argvars); /* -1 on type error */
12485 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012486
12487 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12488 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12489 && rettv_list_alloc(rettv) != FAIL)
12490 {
12491 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12492 for (i = 0; ; ++i)
12493 {
12494 id = syn_get_stack_item(i);
12495 if (id < 0)
12496 break;
12497 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12498 break;
12499 }
12500 }
12501#endif
12502}
12503
12504 static void
12505get_cmd_output_as_rettv(
12506 typval_T *argvars,
12507 typval_T *rettv,
12508 int retlist)
12509{
12510 char_u *res = NULL;
12511 char_u *p;
12512 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012513 int err = FALSE;
12514 FILE *fd;
12515 list_T *list = NULL;
12516 int flags = SHELL_SILENT;
12517
12518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = NULL;
12520 if (check_restricted() || check_secure())
12521 goto errret;
12522
12523 if (argvars[1].v_type != VAR_UNKNOWN)
12524 {
12525 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012526 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012527 * command.
12528 */
12529 if ((infile = vim_tempname('i', TRUE)) == NULL)
12530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012531 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012532 goto errret;
12533 }
12534
12535 fd = mch_fopen((char *)infile, WRITEBIN);
12536 if (fd == NULL)
12537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012538 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 goto errret;
12540 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012541 if (argvars[1].v_type == VAR_NUMBER)
12542 {
12543 linenr_T lnum;
12544 buf_T *buf;
12545
12546 buf = buflist_findnr(argvars[1].vval.v_number);
12547 if (buf == NULL)
12548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012549 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012550 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012551 goto errret;
12552 }
12553
12554 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12555 {
12556 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12557 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12558 {
12559 err = TRUE;
12560 break;
12561 }
12562 if (putc(NL, fd) == EOF)
12563 {
12564 err = TRUE;
12565 break;
12566 }
12567 }
12568 }
12569 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 {
12571 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12572 err = TRUE;
12573 }
12574 else
12575 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012576 size_t len;
12577 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012578
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012579 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 if (p == NULL)
12581 {
12582 fclose(fd);
12583 goto errret; /* type error; errmsg already given */
12584 }
12585 len = STRLEN(p);
12586 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12587 err = TRUE;
12588 }
12589 if (fclose(fd) != 0)
12590 err = TRUE;
12591 if (err)
12592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012593 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594 goto errret;
12595 }
12596 }
12597
12598 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12599 * echoes typeahead, that messes up the display. */
12600 if (!msg_silent)
12601 flags += SHELL_COOKED;
12602
12603 if (retlist)
12604 {
12605 int len;
12606 listitem_T *li;
12607 char_u *s = NULL;
12608 char_u *start;
12609 char_u *end;
12610 int i;
12611
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012612 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 if (res == NULL)
12614 goto errret;
12615
12616 list = list_alloc();
12617 if (list == NULL)
12618 goto errret;
12619
12620 for (i = 0; i < len; ++i)
12621 {
12622 start = res + i;
12623 while (i < len && res[i] != NL)
12624 ++i;
12625 end = res + i;
12626
Bram Moolenaar964b3742019-05-24 18:54:09 +020012627 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 if (s == NULL)
12629 goto errret;
12630
12631 for (p = s; start < end; ++p, ++start)
12632 *p = *start == NUL ? NL : *start;
12633 *p = NUL;
12634
12635 li = listitem_alloc();
12636 if (li == NULL)
12637 {
12638 vim_free(s);
12639 goto errret;
12640 }
12641 li->li_tv.v_type = VAR_STRING;
12642 li->li_tv.v_lock = 0;
12643 li->li_tv.vval.v_string = s;
12644 list_append(list, li);
12645 }
12646
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012647 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648 list = NULL;
12649 }
12650 else
12651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012652 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010012653#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012654 /* translate <CR><NL> into <NL> */
12655 if (res != NULL)
12656 {
12657 char_u *s, *d;
12658
12659 d = res;
12660 for (s = res; *s; ++s)
12661 {
12662 if (s[0] == CAR && s[1] == NL)
12663 ++s;
12664 *d++ = *s;
12665 }
12666 *d = NUL;
12667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668#endif
12669 rettv->vval.v_string = res;
12670 res = NULL;
12671 }
12672
12673errret:
12674 if (infile != NULL)
12675 {
12676 mch_remove(infile);
12677 vim_free(infile);
12678 }
12679 if (res != NULL)
12680 vim_free(res);
12681 if (list != NULL)
12682 list_free(list);
12683}
12684
12685/*
12686 * "system()" function
12687 */
12688 static void
12689f_system(typval_T *argvars, typval_T *rettv)
12690{
12691 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12692}
12693
12694/*
12695 * "systemlist()" function
12696 */
12697 static void
12698f_systemlist(typval_T *argvars, typval_T *rettv)
12699{
12700 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12701}
12702
12703/*
12704 * "tabpagebuflist()" function
12705 */
12706 static void
12707f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12708{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012709 tabpage_T *tp;
12710 win_T *wp = NULL;
12711
12712 if (argvars[0].v_type == VAR_UNKNOWN)
12713 wp = firstwin;
12714 else
12715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012716 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012717 if (tp != NULL)
12718 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12719 }
12720 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12721 {
12722 for (; wp != NULL; wp = wp->w_next)
12723 if (list_append_number(rettv->vval.v_list,
12724 wp->w_buffer->b_fnum) == FAIL)
12725 break;
12726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012727}
12728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012729/*
12730 * "tabpagenr()" function
12731 */
12732 static void
12733f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12734{
12735 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 char_u *arg;
12737
12738 if (argvars[0].v_type != VAR_UNKNOWN)
12739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012740 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 nr = 0;
12742 if (arg != NULL)
12743 {
12744 if (STRCMP(arg, "$") == 0)
12745 nr = tabpage_index(NULL) - 1;
12746 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012747 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012748 }
12749 }
12750 else
12751 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 rettv->vval.v_number = nr;
12753}
12754
12755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756/*
12757 * Common code for tabpagewinnr() and winnr().
12758 */
12759 static int
12760get_winnr(tabpage_T *tp, typval_T *argvar)
12761{
12762 win_T *twin;
12763 int nr = 1;
12764 win_T *wp;
12765 char_u *arg;
12766
12767 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12768 if (argvar->v_type != VAR_UNKNOWN)
12769 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012770 int invalid_arg = FALSE;
12771
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012772 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 if (arg == NULL)
12774 nr = 0; /* type error; errmsg already given */
12775 else if (STRCMP(arg, "$") == 0)
12776 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12777 else if (STRCMP(arg, "#") == 0)
12778 {
12779 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12780 if (twin == NULL)
12781 nr = 0;
12782 }
12783 else
12784 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012785 long count;
12786 char_u *endp;
12787
12788 // Extract the window count (if specified). e.g. winnr('3j')
12789 count = strtol((char *)arg, (char **)&endp, 10);
12790 if (count <= 0)
12791 count = 1; // if count is not specified, default to 1
12792 if (endp != NULL && *endp != '\0')
12793 {
12794 if (STRCMP(endp, "j") == 0)
12795 twin = win_vert_neighbor(tp, twin, FALSE, count);
12796 else if (STRCMP(endp, "k") == 0)
12797 twin = win_vert_neighbor(tp, twin, TRUE, count);
12798 else if (STRCMP(endp, "h") == 0)
12799 twin = win_horz_neighbor(tp, twin, TRUE, count);
12800 else if (STRCMP(endp, "l") == 0)
12801 twin = win_horz_neighbor(tp, twin, FALSE, count);
12802 else
12803 invalid_arg = TRUE;
12804 }
12805 else
12806 invalid_arg = TRUE;
12807 }
12808
12809 if (invalid_arg)
12810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012811 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012812 nr = 0;
12813 }
12814 }
12815
12816 if (nr > 0)
12817 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12818 wp != twin; wp = wp->w_next)
12819 {
12820 if (wp == NULL)
12821 {
12822 /* didn't find it in this tabpage */
12823 nr = 0;
12824 break;
12825 }
12826 ++nr;
12827 }
12828 return nr;
12829}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830
12831/*
12832 * "tabpagewinnr()" function
12833 */
12834 static void
12835f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12836{
12837 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012838 tabpage_T *tp;
12839
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012840 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841 if (tp == NULL)
12842 nr = 0;
12843 else
12844 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012845 rettv->vval.v_number = nr;
12846}
12847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012848/*
12849 * "tagfiles()" function
12850 */
12851 static void
12852f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12853{
12854 char_u *fname;
12855 tagname_T tn;
12856 int first;
12857
12858 if (rettv_list_alloc(rettv) == FAIL)
12859 return;
12860 fname = alloc(MAXPATHL);
12861 if (fname == NULL)
12862 return;
12863
12864 for (first = TRUE; ; first = FALSE)
12865 if (get_tagfname(&tn, first, fname) == FAIL
12866 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12867 break;
12868 tagname_free(&tn);
12869 vim_free(fname);
12870}
12871
12872/*
12873 * "taglist()" function
12874 */
12875 static void
12876f_taglist(typval_T *argvars, typval_T *rettv)
12877{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012878 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012879 char_u *tag_pattern;
12880
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012881 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012882
12883 rettv->vval.v_number = FALSE;
12884 if (*tag_pattern == NUL)
12885 return;
12886
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012888 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012889 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012890 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891}
12892
12893/*
12894 * "tempname()" function
12895 */
12896 static void
12897f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12898{
12899 static int x = 'A';
12900
12901 rettv->v_type = VAR_STRING;
12902 rettv->vval.v_string = vim_tempname(x, FALSE);
12903
12904 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12905 * names. Skip 'I' and 'O', they are used for shell redirection. */
12906 do
12907 {
12908 if (x == 'Z')
12909 x = '0';
12910 else if (x == '9')
12911 x = 'A';
12912 else
12913 {
12914#ifdef EBCDIC
12915 if (x == 'I')
12916 x = 'J';
12917 else if (x == 'R')
12918 x = 'S';
12919 else
12920#endif
12921 ++x;
12922 }
12923 } while (x == 'I' || x == 'O');
12924}
12925
12926#ifdef FEAT_FLOAT
12927/*
12928 * "tan()" function
12929 */
12930 static void
12931f_tan(typval_T *argvars, typval_T *rettv)
12932{
12933 float_T f = 0.0;
12934
12935 rettv->v_type = VAR_FLOAT;
12936 if (get_float_arg(argvars, &f) == OK)
12937 rettv->vval.v_float = tan(f);
12938 else
12939 rettv->vval.v_float = 0.0;
12940}
12941
12942/*
12943 * "tanh()" function
12944 */
12945 static void
12946f_tanh(typval_T *argvars, typval_T *rettv)
12947{
12948 float_T f = 0.0;
12949
12950 rettv->v_type = VAR_FLOAT;
12951 if (get_float_arg(argvars, &f) == OK)
12952 rettv->vval.v_float = tanh(f);
12953 else
12954 rettv->vval.v_float = 0.0;
12955}
12956#endif
12957
12958/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012959 * Get a callback from "arg". It can be a Funcref or a function name.
12960 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012961 * "cb_name" is not allocated.
12962 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012964 callback_T
12965get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012966{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012967 callback_T res;
12968
12969 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012970 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12971 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012972 res.cb_partial = arg->vval.v_partial;
12973 ++res.cb_partial->pt_refcount;
12974 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012976 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012978 res.cb_partial = NULL;
12979 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12980 {
12981 // Note that we don't make a copy of the string.
12982 res.cb_name = arg->vval.v_string;
12983 func_ref(res.cb_name);
12984 }
12985 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12986 {
12987 res.cb_name = (char_u *)"";
12988 }
12989 else
12990 {
12991 emsg(_("E921: Invalid callback argument"));
12992 res.cb_name = NULL;
12993 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012994 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012995 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996}
12997
12998/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012999 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000 */
13001 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013002put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013003{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013004 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013006 tv->v_type = VAR_PARTIAL;
13007 tv->vval.v_partial = cb->cb_partial;
13008 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013010 else
13011 {
13012 tv->v_type = VAR_FUNC;
13013 tv->vval.v_string = vim_strsave(cb->cb_name);
13014 func_ref(cb->cb_name);
13015 }
13016}
13017
13018/*
13019 * Make a copy of "src" into "dest", allocating the function name if needed,
13020 * without incrementing the refcount.
13021 */
13022 void
13023set_callback(callback_T *dest, callback_T *src)
13024{
13025 if (src->cb_partial == NULL)
13026 {
13027 // just a function name, make a copy
13028 dest->cb_name = vim_strsave(src->cb_name);
13029 dest->cb_free_name = TRUE;
13030 }
13031 else
13032 {
13033 // cb_name is a pointer into cb_partial
13034 dest->cb_name = src->cb_name;
13035 dest->cb_free_name = FALSE;
13036 }
13037 dest->cb_partial = src->cb_partial;
13038}
13039
13040/*
13041 * Unref/free "callback" returned by get_callback() or set_callback().
13042 */
13043 void
13044free_callback(callback_T *callback)
13045{
13046 if (callback->cb_partial != NULL)
13047 {
13048 partial_unref(callback->cb_partial);
13049 callback->cb_partial = NULL;
13050 }
13051 else if (callback->cb_name != NULL)
13052 func_unref(callback->cb_name);
13053 if (callback->cb_free_name)
13054 {
13055 vim_free(callback->cb_name);
13056 callback->cb_free_name = FALSE;
13057 }
13058 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013060
13061#ifdef FEAT_TIMERS
13062/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013063 * "timer_info([timer])" function
13064 */
13065 static void
13066f_timer_info(typval_T *argvars, typval_T *rettv)
13067{
13068 timer_T *timer = NULL;
13069
13070 if (rettv_list_alloc(rettv) != OK)
13071 return;
13072 if (argvars[0].v_type != VAR_UNKNOWN)
13073 {
13074 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013075 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013076 else
13077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013078 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013079 if (timer != NULL)
13080 add_timer_info(rettv, timer);
13081 }
13082 }
13083 else
13084 add_timer_info_all(rettv);
13085}
13086
13087/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013088 * "timer_pause(timer, paused)" function
13089 */
13090 static void
13091f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13092{
13093 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013094 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013095
13096 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013097 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013098 else
13099 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013100 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013101 if (timer != NULL)
13102 timer->tr_paused = paused;
13103 }
13104}
13105
13106/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013107 * "timer_start(time, callback [, options])" function
13108 */
13109 static void
13110f_timer_start(typval_T *argvars, typval_T *rettv)
13111{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013112 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013113 timer_T *timer;
13114 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013115 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013116 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117
Bram Moolenaar75537a92016-09-05 22:45:28 +020013118 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119 if (check_secure())
13120 return;
13121 if (argvars[2].v_type != VAR_UNKNOWN)
13122 {
13123 if (argvars[2].v_type != VAR_DICT
13124 || (dict = argvars[2].vval.v_dict) == NULL)
13125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013126 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127 return;
13128 }
13129 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013130 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 }
13132
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013133 callback = get_callback(&argvars[1]);
13134 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013135 return;
13136
13137 timer = create_timer(msec, repeat);
13138 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013139 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013140 else
13141 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013142 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013143 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144 }
13145}
13146
13147/*
13148 * "timer_stop(timer)" function
13149 */
13150 static void
13151f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13152{
13153 timer_T *timer;
13154
13155 if (argvars[0].v_type != VAR_NUMBER)
13156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013157 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 return;
13159 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013160 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 if (timer != NULL)
13162 stop_timer(timer);
13163}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013164
13165/*
13166 * "timer_stopall()" function
13167 */
13168 static void
13169f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13170{
13171 stop_all_timers();
13172}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013173#endif
13174
13175/*
13176 * "tolower(string)" function
13177 */
13178 static void
13179f_tolower(typval_T *argvars, typval_T *rettv)
13180{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013182 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183}
13184
13185/*
13186 * "toupper(string)" function
13187 */
13188 static void
13189f_toupper(typval_T *argvars, typval_T *rettv)
13190{
13191 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013192 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193}
13194
13195/*
13196 * "tr(string, fromstr, tostr)" function
13197 */
13198 static void
13199f_tr(typval_T *argvars, typval_T *rettv)
13200{
13201 char_u *in_str;
13202 char_u *fromstr;
13203 char_u *tostr;
13204 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205 int inlen;
13206 int fromlen;
13207 int tolen;
13208 int idx;
13209 char_u *cpstr;
13210 int cplen;
13211 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212 char_u buf[NUMBUFLEN];
13213 char_u buf2[NUMBUFLEN];
13214 garray_T ga;
13215
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013216 in_str = tv_get_string(&argvars[0]);
13217 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13218 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219
13220 /* Default return value: empty string. */
13221 rettv->v_type = VAR_STRING;
13222 rettv->vval.v_string = NULL;
13223 if (fromstr == NULL || tostr == NULL)
13224 return; /* type error; errmsg already given */
13225 ga_init2(&ga, (int)sizeof(char), 80);
13226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013227 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 /* not multi-byte: fromstr and tostr must be the same length */
13229 if (STRLEN(fromstr) != STRLEN(tostr))
13230 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013232 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 ga_clear(&ga);
13234 return;
13235 }
13236
13237 /* fromstr and tostr have to contain the same number of chars */
13238 while (*in_str != NUL)
13239 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 if (has_mbyte)
13241 {
13242 inlen = (*mb_ptr2len)(in_str);
13243 cpstr = in_str;
13244 cplen = inlen;
13245 idx = 0;
13246 for (p = fromstr; *p != NUL; p += fromlen)
13247 {
13248 fromlen = (*mb_ptr2len)(p);
13249 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13250 {
13251 for (p = tostr; *p != NUL; p += tolen)
13252 {
13253 tolen = (*mb_ptr2len)(p);
13254 if (idx-- == 0)
13255 {
13256 cplen = tolen;
13257 cpstr = p;
13258 break;
13259 }
13260 }
13261 if (*p == NUL) /* tostr is shorter than fromstr */
13262 goto error;
13263 break;
13264 }
13265 ++idx;
13266 }
13267
13268 if (first && cpstr == in_str)
13269 {
13270 /* Check that fromstr and tostr have the same number of
13271 * (multi-byte) characters. Done only once when a character
13272 * of in_str doesn't appear in fromstr. */
13273 first = FALSE;
13274 for (p = tostr; *p != NUL; p += tolen)
13275 {
13276 tolen = (*mb_ptr2len)(p);
13277 --idx;
13278 }
13279 if (idx != 0)
13280 goto error;
13281 }
13282
13283 (void)ga_grow(&ga, cplen);
13284 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13285 ga.ga_len += cplen;
13286
13287 in_str += inlen;
13288 }
13289 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013290 {
13291 /* When not using multi-byte chars we can do it faster. */
13292 p = vim_strchr(fromstr, *in_str);
13293 if (p != NULL)
13294 ga_append(&ga, tostr[p - fromstr]);
13295 else
13296 ga_append(&ga, *in_str);
13297 ++in_str;
13298 }
13299 }
13300
13301 /* add a terminating NUL */
13302 (void)ga_grow(&ga, 1);
13303 ga_append(&ga, NUL);
13304
13305 rettv->vval.v_string = ga.ga_data;
13306}
13307
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013308/*
13309 * "trim({expr})" function
13310 */
13311 static void
13312f_trim(typval_T *argvars, typval_T *rettv)
13313{
13314 char_u buf1[NUMBUFLEN];
13315 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013316 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013317 char_u *mask = NULL;
13318 char_u *tail;
13319 char_u *prev;
13320 char_u *p;
13321 int c1;
13322
13323 rettv->v_type = VAR_STRING;
13324 if (head == NULL)
13325 {
13326 rettv->vval.v_string = NULL;
13327 return;
13328 }
13329
13330 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013332
13333 while (*head != NUL)
13334 {
13335 c1 = PTR2CHAR(head);
13336 if (mask == NULL)
13337 {
13338 if (c1 > ' ' && c1 != 0xa0)
13339 break;
13340 }
13341 else
13342 {
13343 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13344 if (c1 == PTR2CHAR(p))
13345 break;
13346 if (*p == NUL)
13347 break;
13348 }
13349 MB_PTR_ADV(head);
13350 }
13351
13352 for (tail = head + STRLEN(head); tail > head; tail = prev)
13353 {
13354 prev = tail;
13355 MB_PTR_BACK(head, prev);
13356 c1 = PTR2CHAR(prev);
13357 if (mask == NULL)
13358 {
13359 if (c1 > ' ' && c1 != 0xa0)
13360 break;
13361 }
13362 else
13363 {
13364 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13365 if (c1 == PTR2CHAR(p))
13366 break;
13367 if (*p == NUL)
13368 break;
13369 }
13370 }
13371 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13372}
13373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374#ifdef FEAT_FLOAT
13375/*
13376 * "trunc({float})" function
13377 */
13378 static void
13379f_trunc(typval_T *argvars, typval_T *rettv)
13380{
13381 float_T f = 0.0;
13382
13383 rettv->v_type = VAR_FLOAT;
13384 if (get_float_arg(argvars, &f) == OK)
13385 /* trunc() is not in C90, use floor() or ceil() instead. */
13386 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13387 else
13388 rettv->vval.v_float = 0.0;
13389}
13390#endif
13391
13392/*
13393 * "type(expr)" function
13394 */
13395 static void
13396f_type(typval_T *argvars, typval_T *rettv)
13397{
13398 int n = -1;
13399
13400 switch (argvars[0].v_type)
13401 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013402 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13403 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013404 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013405 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13406 case VAR_LIST: n = VAR_TYPE_LIST; break;
13407 case VAR_DICT: n = VAR_TYPE_DICT; break;
13408 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 case VAR_SPECIAL:
13410 if (argvars[0].vval.v_number == VVAL_FALSE
13411 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013412 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013414 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013416 case VAR_JOB: n = VAR_TYPE_JOB; break;
13417 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013418 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013420 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421 n = -1;
13422 break;
13423 }
13424 rettv->vval.v_number = n;
13425}
13426
13427/*
13428 * "undofile(name)" function
13429 */
13430 static void
13431f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13432{
13433 rettv->v_type = VAR_STRING;
13434#ifdef FEAT_PERSISTENT_UNDO
13435 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013436 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437
13438 if (*fname == NUL)
13439 {
13440 /* If there is no file name there will be no undo file. */
13441 rettv->vval.v_string = NULL;
13442 }
13443 else
13444 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020013445 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013446
13447 if (ffname != NULL)
13448 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13449 vim_free(ffname);
13450 }
13451 }
13452#else
13453 rettv->vval.v_string = NULL;
13454#endif
13455}
13456
13457/*
13458 * "undotree()" function
13459 */
13460 static void
13461f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13462{
13463 if (rettv_dict_alloc(rettv) == OK)
13464 {
13465 dict_T *dict = rettv->vval.v_dict;
13466 list_T *list;
13467
Bram Moolenaare0be1672018-07-08 16:50:37 +020013468 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13469 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13470 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13471 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13472 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13473 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013474
13475 list = list_alloc();
13476 if (list != NULL)
13477 {
13478 u_eval_tree(curbuf->b_u_oldhead, list);
13479 dict_add_list(dict, "entries", list);
13480 }
13481 }
13482}
13483
13484/*
13485 * "values(dict)" function
13486 */
13487 static void
13488f_values(typval_T *argvars, typval_T *rettv)
13489{
13490 dict_list(argvars, rettv, 1);
13491}
13492
13493/*
13494 * "virtcol(string)" function
13495 */
13496 static void
13497f_virtcol(typval_T *argvars, typval_T *rettv)
13498{
13499 colnr_T vcol = 0;
13500 pos_T *fp;
13501 int fnum = curbuf->b_fnum;
13502
13503 fp = var2fpos(&argvars[0], FALSE, &fnum);
13504 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13505 && fnum == curbuf->b_fnum)
13506 {
13507 getvvcol(curwin, fp, NULL, NULL, &vcol);
13508 ++vcol;
13509 }
13510
13511 rettv->vval.v_number = vcol;
13512}
13513
13514/*
13515 * "visualmode()" function
13516 */
13517 static void
13518f_visualmode(typval_T *argvars, typval_T *rettv)
13519{
13520 char_u str[2];
13521
13522 rettv->v_type = VAR_STRING;
13523 str[0] = curbuf->b_visual_mode_eval;
13524 str[1] = NUL;
13525 rettv->vval.v_string = vim_strsave(str);
13526
13527 /* A non-zero number or non-empty string argument: reset mode. */
13528 if (non_zero_arg(&argvars[0]))
13529 curbuf->b_visual_mode_eval = NUL;
13530}
13531
13532/*
13533 * "wildmenumode()" function
13534 */
13535 static void
13536f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13537{
13538#ifdef FEAT_WILDMENU
13539 if (wild_menu_showing)
13540 rettv->vval.v_number = 1;
13541#endif
13542}
13543
13544/*
13545 * "winbufnr(nr)" function
13546 */
13547 static void
13548f_winbufnr(typval_T *argvars, typval_T *rettv)
13549{
13550 win_T *wp;
13551
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013552 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 if (wp == NULL)
13554 rettv->vval.v_number = -1;
13555 else
13556 rettv->vval.v_number = wp->w_buffer->b_fnum;
13557}
13558
13559/*
13560 * "wincol()" function
13561 */
13562 static void
13563f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13564{
13565 validate_cursor();
13566 rettv->vval.v_number = curwin->w_wcol + 1;
13567}
13568
13569/*
13570 * "winheight(nr)" function
13571 */
13572 static void
13573f_winheight(typval_T *argvars, typval_T *rettv)
13574{
13575 win_T *wp;
13576
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013577 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 if (wp == NULL)
13579 rettv->vval.v_number = -1;
13580 else
13581 rettv->vval.v_number = wp->w_height;
13582}
13583
13584/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013585 * "winlayout()" function
13586 */
13587 static void
13588f_winlayout(typval_T *argvars, typval_T *rettv)
13589{
13590 tabpage_T *tp;
13591
13592 if (rettv_list_alloc(rettv) != OK)
13593 return;
13594
13595 if (argvars[0].v_type == VAR_UNKNOWN)
13596 tp = curtab;
13597 else
13598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013599 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013600 if (tp == NULL)
13601 return;
13602 }
13603
13604 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13605}
13606
13607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 * "winline()" function
13609 */
13610 static void
13611f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13612{
13613 validate_cursor();
13614 rettv->vval.v_number = curwin->w_wrow + 1;
13615}
13616
13617/*
13618 * "winnr()" function
13619 */
13620 static void
13621f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13622{
13623 int nr = 1;
13624
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013626 rettv->vval.v_number = nr;
13627}
13628
13629/*
13630 * "winrestcmd()" function
13631 */
13632 static void
13633f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13634{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013635 win_T *wp;
13636 int winnr = 1;
13637 garray_T ga;
13638 char_u buf[50];
13639
13640 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013641 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013642 {
13643 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13644 ga_concat(&ga, buf);
13645 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13646 ga_concat(&ga, buf);
13647 ++winnr;
13648 }
13649 ga_append(&ga, NUL);
13650
13651 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013652 rettv->v_type = VAR_STRING;
13653}
13654
13655/*
13656 * "winrestview()" function
13657 */
13658 static void
13659f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13660{
13661 dict_T *dict;
13662
13663 if (argvars[0].v_type != VAR_DICT
13664 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013665 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013666 else
13667 {
13668 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013669 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013670 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013671 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013672 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013673 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13675 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010013676 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013677 curwin->w_set_curswant = FALSE;
13678 }
13679
13680 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013681 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013682#ifdef FEAT_DIFF
13683 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013684 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685#endif
13686 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013687 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013688 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013689 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013690
13691 check_cursor();
13692 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013693 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013694 changed_window_setting();
13695
13696 if (curwin->w_topline <= 0)
13697 curwin->w_topline = 1;
13698 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13699 curwin->w_topline = curbuf->b_ml.ml_line_count;
13700#ifdef FEAT_DIFF
13701 check_topfill(curwin, TRUE);
13702#endif
13703 }
13704}
13705
13706/*
13707 * "winsaveview()" function
13708 */
13709 static void
13710f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13711{
13712 dict_T *dict;
13713
13714 if (rettv_dict_alloc(rettv) == FAIL)
13715 return;
13716 dict = rettv->vval.v_dict;
13717
Bram Moolenaare0be1672018-07-08 16:50:37 +020013718 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13719 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013720 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013721 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013722 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013723
Bram Moolenaare0be1672018-07-08 16:50:37 +020013724 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013725#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013726 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013727#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013728 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13729 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730}
13731
13732/*
13733 * "winwidth(nr)" function
13734 */
13735 static void
13736f_winwidth(typval_T *argvars, typval_T *rettv)
13737{
13738 win_T *wp;
13739
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013740 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013741 if (wp == NULL)
13742 rettv->vval.v_number = -1;
13743 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013744 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013745}
13746
13747/*
13748 * "wordcount()" function
13749 */
13750 static void
13751f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13752{
13753 if (rettv_dict_alloc(rettv) == FAIL)
13754 return;
13755 cursor_pos_info(rettv->vval.v_dict);
13756}
13757
13758/*
13759 * "writefile()" function
13760 */
13761 static void
13762f_writefile(typval_T *argvars, typval_T *rettv)
13763{
13764 int binary = FALSE;
13765 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013766#ifdef HAVE_FSYNC
13767 int do_fsync = p_fs;
13768#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013769 char_u *fname;
13770 FILE *fd;
13771 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013772 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013773 list_T *list = NULL;
13774 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013776 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013777 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778 return;
13779
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013780 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013781 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013782 list = argvars[0].vval.v_list;
13783 if (list == NULL)
13784 return;
13785 for (li = list->lv_first; li != NULL; li = li->li_next)
13786 if (tv_get_string_chk(&li->li_tv) == NULL)
13787 return;
13788 }
13789 else if (argvars[0].v_type == VAR_BLOB)
13790 {
13791 blob = argvars[0].vval.v_blob;
13792 if (blob == NULL)
13793 return;
13794 }
13795 else
13796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013797 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013798 return;
13799 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013800
13801 if (argvars[2].v_type != VAR_UNKNOWN)
13802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013803 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013804
13805 if (arg2 == NULL)
13806 return;
13807 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013808 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013809 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013810 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013811#ifdef HAVE_FSYNC
13812 if (vim_strchr(arg2, 's') != NULL)
13813 do_fsync = TRUE;
13814 else if (vim_strchr(arg2, 'S') != NULL)
13815 do_fsync = FALSE;
13816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013817 }
13818
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013819 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013820 if (fname == NULL)
13821 return;
13822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013823 /* Always open the file in binary mode, library functions have a mind of
13824 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013825 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13826 append ? APPENDBIN : WRITEBIN)) == NULL)
13827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013828 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 ret = -1;
13830 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013831 else if (blob)
13832 {
13833 if (write_blob(fd, blob) == FAIL)
13834 ret = -1;
13835#ifdef HAVE_FSYNC
13836 else if (do_fsync)
13837 // Ignore the error, the user wouldn't know what to do about it.
13838 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013839 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013840#endif
13841 fclose(fd);
13842 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013843 else
13844 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013845 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013846 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013847#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013848 else if (do_fsync)
13849 /* Ignore the error, the user wouldn't know what to do about it.
13850 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013851 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013852#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013853 fclose(fd);
13854 }
13855
13856 rettv->vval.v_number = ret;
13857}
13858
13859/*
13860 * "xor(expr, expr)" function
13861 */
13862 static void
13863f_xor(typval_T *argvars, typval_T *rettv)
13864{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013865 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13866 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867}
13868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869#endif /* FEAT_EVAL */