blob: 0c522dd51b4c02900bce8666674bfc51c34a96e1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010027static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029
30#ifdef FEAT_FLOAT
31static void f_abs(typval_T *argvars, typval_T *rettv);
32static void f_acos(typval_T *argvars, typval_T *rettv);
33#endif
34static void f_add(typval_T *argvars, typval_T *rettv);
35static void f_and(typval_T *argvars, typval_T *rettv);
36static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020037static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020038static void f_argc(typval_T *argvars, typval_T *rettv);
39static void f_argidx(typval_T *argvars, typval_T *rettv);
40static void f_arglistid(typval_T *argvars, typval_T *rettv);
41static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042#ifdef FEAT_FLOAT
43static void f_asin(typval_T *argvars, typval_T *rettv);
44static void f_atan(typval_T *argvars, typval_T *rettv);
45static void f_atan2(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010047#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020048static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010049static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010050# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010051static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010052# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010053#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_browse(typval_T *argvars, typval_T *rettv);
55static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020056static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_bufexists(typval_T *argvars, typval_T *rettv);
58static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020059static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_bufloaded(typval_T *argvars, typval_T *rettv);
61static void f_bufname(typval_T *argvars, typval_T *rettv);
62static void f_bufnr(typval_T *argvars, typval_T *rettv);
63static void f_bufwinid(typval_T *argvars, typval_T *rettv);
64static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
65static void f_byte2line(typval_T *argvars, typval_T *rettv);
66static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
67static void f_byteidx(typval_T *argvars, typval_T *rettv);
68static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
69static void f_call(typval_T *argvars, typval_T *rettv);
70#ifdef FEAT_FLOAT
71static void f_ceil(typval_T *argvars, typval_T *rettv);
72#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020073static void f_changenr(typval_T *argvars, typval_T *rettv);
74static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020075static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_cindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077static void f_col(typval_T *argvars, typval_T *rettv);
78#if defined(FEAT_INS_EXPAND)
79static void f_complete(typval_T *argvars, typval_T *rettv);
80static void f_complete_add(typval_T *argvars, typval_T *rettv);
81static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010082static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083#endif
84static void f_confirm(typval_T *argvars, typval_T *rettv);
85static void f_copy(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_cos(typval_T *argvars, typval_T *rettv);
88static void f_cosh(typval_T *argvars, typval_T *rettv);
89#endif
90static void f_count(typval_T *argvars, typval_T *rettv);
91static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
92static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010093#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020094static void f_debugbreak(typval_T *argvars, typval_T *rettv);
95#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_deepcopy(typval_T *argvars, typval_T *rettv);
97static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +020098static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_did_filetype(typval_T *argvars, typval_T *rettv);
100static void f_diff_filler(typval_T *argvars, typval_T *rettv);
101static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
102static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200103static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200104static void f_escape(typval_T *argvars, typval_T *rettv);
105static void f_eval(typval_T *argvars, typval_T *rettv);
106static void f_eventhandler(typval_T *argvars, typval_T *rettv);
107static void f_executable(typval_T *argvars, typval_T *rettv);
108static void f_execute(typval_T *argvars, typval_T *rettv);
109static void f_exepath(typval_T *argvars, typval_T *rettv);
110static void f_exists(typval_T *argvars, typval_T *rettv);
111#ifdef FEAT_FLOAT
112static void f_exp(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200115static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_extend(typval_T *argvars, typval_T *rettv);
117static void f_feedkeys(typval_T *argvars, typval_T *rettv);
118static void f_filereadable(typval_T *argvars, typval_T *rettv);
119static void f_filewritable(typval_T *argvars, typval_T *rettv);
120static void f_filter(typval_T *argvars, typval_T *rettv);
121static void f_finddir(typval_T *argvars, typval_T *rettv);
122static void f_findfile(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_float2nr(typval_T *argvars, typval_T *rettv);
125static void f_floor(typval_T *argvars, typval_T *rettv);
126static void f_fmod(typval_T *argvars, typval_T *rettv);
127#endif
128static void f_fnameescape(typval_T *argvars, typval_T *rettv);
129static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
130static void f_foldclosed(typval_T *argvars, typval_T *rettv);
131static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
132static void f_foldlevel(typval_T *argvars, typval_T *rettv);
133static void f_foldtext(typval_T *argvars, typval_T *rettv);
134static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
135static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200136static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200137static void f_function(typval_T *argvars, typval_T *rettv);
138static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
139static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200140static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_getbufline(typval_T *argvars, typval_T *rettv);
142static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100143static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200144static void f_getchar(typval_T *argvars, typval_T *rettv);
145static void f_getcharmod(typval_T *argvars, typval_T *rettv);
146static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
147static void f_getcmdline(typval_T *argvars, typval_T *rettv);
148#if defined(FEAT_CMDL_COMPL)
149static void f_getcompletion(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
152static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
153static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
154static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200155static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getfontname(typval_T *argvars, typval_T *rettv);
157static void f_getfperm(typval_T *argvars, typval_T *rettv);
158static void f_getfsize(typval_T *argvars, typval_T *rettv);
159static void f_getftime(typval_T *argvars, typval_T *rettv);
160static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100161static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200163static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_getpid(typval_T *argvars, typval_T *rettv);
165static void f_getcurpos(typval_T *argvars, typval_T *rettv);
166static void f_getpos(typval_T *argvars, typval_T *rettv);
167static void f_getqflist(typval_T *argvars, typval_T *rettv);
168static void f_getreg(typval_T *argvars, typval_T *rettv);
169static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200170static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_gettabvar(typval_T *argvars, typval_T *rettv);
172static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100173static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200174static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100175static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200176static void f_getwinposx(typval_T *argvars, typval_T *rettv);
177static void f_getwinposy(typval_T *argvars, typval_T *rettv);
178static void f_getwinvar(typval_T *argvars, typval_T *rettv);
179static void f_glob(typval_T *argvars, typval_T *rettv);
180static void f_globpath(typval_T *argvars, typval_T *rettv);
181static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
182static void f_has(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
184static void f_hasmapto(typval_T *argvars, typval_T *rettv);
185static void f_histadd(typval_T *argvars, typval_T *rettv);
186static void f_histdel(typval_T *argvars, typval_T *rettv);
187static void f_histget(typval_T *argvars, typval_T *rettv);
188static void f_histnr(typval_T *argvars, typval_T *rettv);
189static void f_hlID(typval_T *argvars, typval_T *rettv);
190static void f_hlexists(typval_T *argvars, typval_T *rettv);
191static void f_hostname(typval_T *argvars, typval_T *rettv);
192static void f_iconv(typval_T *argvars, typval_T *rettv);
193static void f_indent(typval_T *argvars, typval_T *rettv);
194static void f_index(typval_T *argvars, typval_T *rettv);
195static void f_input(typval_T *argvars, typval_T *rettv);
196static void f_inputdialog(typval_T *argvars, typval_T *rettv);
197static void f_inputlist(typval_T *argvars, typval_T *rettv);
198static void f_inputrestore(typval_T *argvars, typval_T *rettv);
199static void f_inputsave(typval_T *argvars, typval_T *rettv);
200static void f_inputsecret(typval_T *argvars, typval_T *rettv);
201static void f_insert(typval_T *argvars, typval_T *rettv);
202static void f_invert(typval_T *argvars, typval_T *rettv);
203static void f_isdirectory(typval_T *argvars, typval_T *rettv);
204static void f_islocked(typval_T *argvars, typval_T *rettv);
205#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200206static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_isnan(typval_T *argvars, typval_T *rettv);
208#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
210static void f_len(typval_T *argvars, typval_T *rettv);
211static void f_libcall(typval_T *argvars, typval_T *rettv);
212static void f_libcallnr(typval_T *argvars, typval_T *rettv);
213static void f_line(typval_T *argvars, typval_T *rettv);
214static void f_line2byte(typval_T *argvars, typval_T *rettv);
215static void f_lispindent(typval_T *argvars, typval_T *rettv);
216static void f_localtime(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_log(typval_T *argvars, typval_T *rettv);
219static void f_log10(typval_T *argvars, typval_T *rettv);
220#endif
221#ifdef FEAT_LUA
222static void f_luaeval(typval_T *argvars, typval_T *rettv);
223#endif
224static void f_map(typval_T *argvars, typval_T *rettv);
225static void f_maparg(typval_T *argvars, typval_T *rettv);
226static void f_mapcheck(typval_T *argvars, typval_T *rettv);
227static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_matchend(typval_T *argvars, typval_T *rettv);
229static void f_matchlist(typval_T *argvars, typval_T *rettv);
230static void f_matchstr(typval_T *argvars, typval_T *rettv);
231static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
232static void f_max(typval_T *argvars, typval_T *rettv);
233static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200235static void f_mode(typval_T *argvars, typval_T *rettv);
236#ifdef FEAT_MZSCHEME
237static void f_mzeval(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
240static void f_nr2char(typval_T *argvars, typval_T *rettv);
241static void f_or(typval_T *argvars, typval_T *rettv);
242static void f_pathshorten(typval_T *argvars, typval_T *rettv);
243#ifdef FEAT_PERL
244static void f_perleval(typval_T *argvars, typval_T *rettv);
245#endif
246#ifdef FEAT_FLOAT
247static void f_pow(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
250static void f_printf(typval_T *argvars, typval_T *rettv);
251static void f_pumvisible(typval_T *argvars, typval_T *rettv);
252#ifdef FEAT_PYTHON3
253static void f_py3eval(typval_T *argvars, typval_T *rettv);
254#endif
255#ifdef FEAT_PYTHON
256static void f_pyeval(typval_T *argvars, typval_T *rettv);
257#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100258#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
259static void f_pyxeval(typval_T *argvars, typval_T *rettv);
260#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200262static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200263static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200264static void f_reg_executing(typval_T *argvars, typval_T *rettv);
265static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200266static void f_reltime(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_FLOAT
268static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_reltimestr(typval_T *argvars, typval_T *rettv);
271static void f_remote_expr(typval_T *argvars, typval_T *rettv);
272static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
273static void f_remote_peek(typval_T *argvars, typval_T *rettv);
274static void f_remote_read(typval_T *argvars, typval_T *rettv);
275static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100276static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200277static void f_remove(typval_T *argvars, typval_T *rettv);
278static void f_rename(typval_T *argvars, typval_T *rettv);
279static void f_repeat(typval_T *argvars, typval_T *rettv);
280static void f_resolve(typval_T *argvars, typval_T *rettv);
281static void f_reverse(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_FLOAT
283static void f_round(typval_T *argvars, typval_T *rettv);
284#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100285#ifdef FEAT_RUBY
286static void f_rubyeval(typval_T *argvars, typval_T *rettv);
287#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200288static void f_screenattr(typval_T *argvars, typval_T *rettv);
289static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100290static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200291static void f_screencol(typval_T *argvars, typval_T *rettv);
292static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100293static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_search(typval_T *argvars, typval_T *rettv);
295static void f_searchdecl(typval_T *argvars, typval_T *rettv);
296static void f_searchpair(typval_T *argvars, typval_T *rettv);
297static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
298static void f_searchpos(typval_T *argvars, typval_T *rettv);
299static void f_server2client(typval_T *argvars, typval_T *rettv);
300static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200301static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200302static void f_setbufvar(typval_T *argvars, typval_T *rettv);
303static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
304static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200305static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_setfperm(typval_T *argvars, typval_T *rettv);
307static void f_setline(typval_T *argvars, typval_T *rettv);
308static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200309static void f_setpos(typval_T *argvars, typval_T *rettv);
310static void f_setqflist(typval_T *argvars, typval_T *rettv);
311static void f_setreg(typval_T *argvars, typval_T *rettv);
312static void f_settabvar(typval_T *argvars, typval_T *rettv);
313static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100314static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200315static void f_setwinvar(typval_T *argvars, typval_T *rettv);
316#ifdef FEAT_CRYPT
317static void f_sha256(typval_T *argvars, typval_T *rettv);
318#endif /* FEAT_CRYPT */
319static void f_shellescape(typval_T *argvars, typval_T *rettv);
320static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
321static void f_simplify(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_sin(typval_T *argvars, typval_T *rettv);
324static void f_sinh(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_soundfold(typval_T *argvars, typval_T *rettv);
327static void f_spellbadword(typval_T *argvars, typval_T *rettv);
328static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
329static void f_split(typval_T *argvars, typval_T *rettv);
330#ifdef FEAT_FLOAT
331static void f_sqrt(typval_T *argvars, typval_T *rettv);
332static void f_str2float(typval_T *argvars, typval_T *rettv);
333#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200334static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_str2nr(typval_T *argvars, typval_T *rettv);
336static void f_strchars(typval_T *argvars, typval_T *rettv);
337#ifdef HAVE_STRFTIME
338static void f_strftime(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_strgetchar(typval_T *argvars, typval_T *rettv);
341static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_strlen(typval_T *argvars, typval_T *rettv);
343static void f_strcharpart(typval_T *argvars, typval_T *rettv);
344static void f_strpart(typval_T *argvars, typval_T *rettv);
345static void f_strridx(typval_T *argvars, typval_T *rettv);
346static void f_strtrans(typval_T *argvars, typval_T *rettv);
347static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
348static void f_strwidth(typval_T *argvars, typval_T *rettv);
349static void f_submatch(typval_T *argvars, typval_T *rettv);
350static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200351static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200352static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_synID(typval_T *argvars, typval_T *rettv);
354static void f_synIDattr(typval_T *argvars, typval_T *rettv);
355static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
356static void f_synstack(typval_T *argvars, typval_T *rettv);
357static void f_synconcealed(typval_T *argvars, typval_T *rettv);
358static void f_system(typval_T *argvars, typval_T *rettv);
359static void f_systemlist(typval_T *argvars, typval_T *rettv);
360static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
361static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
362static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
363static void f_taglist(typval_T *argvars, typval_T *rettv);
364static void f_tagfiles(typval_T *argvars, typval_T *rettv);
365static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366#ifdef FEAT_FLOAT
367static void f_tan(typval_T *argvars, typval_T *rettv);
368static void f_tanh(typval_T *argvars, typval_T *rettv);
369#endif
370#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200371static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200372static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200373static void f_timer_start(typval_T *argvars, typval_T *rettv);
374static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200375static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376#endif
377static void f_tolower(typval_T *argvars, typval_T *rettv);
378static void f_toupper(typval_T *argvars, typval_T *rettv);
379static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100380static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381#ifdef FEAT_FLOAT
382static void f_trunc(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_type(typval_T *argvars, typval_T *rettv);
385static void f_undofile(typval_T *argvars, typval_T *rettv);
386static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387static void f_virtcol(typval_T *argvars, typval_T *rettv);
388static void f_visualmode(typval_T *argvars, typval_T *rettv);
389static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200390static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
392static void f_win_getid(typval_T *argvars, typval_T *rettv);
393static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
394static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
395static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100396static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_winbufnr(typval_T *argvars, typval_T *rettv);
398static void f_wincol(typval_T *argvars, typval_T *rettv);
399static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200400static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_winline(typval_T *argvars, typval_T *rettv);
402static void f_winnr(typval_T *argvars, typval_T *rettv);
403static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
404static void f_winrestview(typval_T *argvars, typval_T *rettv);
405static void f_winsaveview(typval_T *argvars, typval_T *rettv);
406static void f_winwidth(typval_T *argvars, typval_T *rettv);
407static void f_writefile(typval_T *argvars, typval_T *rettv);
408static void f_wordcount(typval_T *argvars, typval_T *rettv);
409static void f_xor(typval_T *argvars, typval_T *rettv);
410
411/*
412 * Array with names and number of arguments of all internal functions
413 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
414 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200415typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200417 char *f_name; // function name
418 char f_min_argc; // minimal number of arguments
419 char f_max_argc; // maximal number of arguments
420 char f_argtype; // for method: FEARG_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200422 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200423} funcentry_T;
424
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200425// values for f_argtype; zero means it cannot be used as a method
426#define FEARG_1 1 // base is the first argument
427#define FEARG_2 2 // base is the second argument
428#define FEARG_LAST 9 // base is the last argument
429
Bram Moolenaarac92e252019-08-03 21:58:38 +0200430static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431{
432#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200433 {"abs", 1, 1, 0, f_abs},
434 {"acos", 1, 1, 0, f_acos}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200435#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200436 {"add", 2, 2, FEARG_1, f_add},
437 {"and", 2, 2, 0, f_and},
438 {"append", 2, 2, FEARG_LAST, f_append},
439 {"appendbufline", 3, 3, FEARG_LAST, f_appendbufline},
440 {"argc", 0, 1, 0, f_argc},
441 {"argidx", 0, 0, 0, f_argidx},
442 {"arglistid", 0, 2, 0, f_arglistid},
443 {"argv", 0, 2, 0, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200445 {"asin", 1, 1, 0, f_asin}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200447 {"assert_beeps", 1, 2, 0, f_assert_beeps},
448 {"assert_equal", 2, 3, FEARG_2, f_assert_equal},
449 {"assert_equalfile", 2, 2, 0, f_assert_equalfile},
450 {"assert_exception", 1, 2, 0, f_assert_exception},
451 {"assert_fails", 1, 3, 0, f_assert_fails},
452 {"assert_false", 1, 2, 0, f_assert_false},
453 {"assert_inrange", 3, 4, 0, f_assert_inrange},
454 {"assert_match", 2, 3, 0, f_assert_match},
455 {"assert_notequal", 2, 3, FEARG_2, f_assert_notequal},
456 {"assert_notmatch", 2, 3, 0, f_assert_notmatch},
457 {"assert_report", 1, 1, 0, f_assert_report},
458 {"assert_true", 1, 2, 0, f_assert_true},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200460 {"atan", 1, 1, 0, f_atan},
461 {"atan2", 2, 2, 0, f_atan2},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100463#ifdef FEAT_BEVAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200464 {"balloon_gettext", 0, 0, 0, f_balloon_gettext},
465 {"balloon_show", 1, 1, 0, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100466# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200467 {"balloon_split", 1, 1, 0, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100468# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100469#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200470 {"browse", 4, 4, 0, f_browse},
471 {"browsedir", 2, 2, 0, f_browsedir},
472 {"bufadd", 1, 1, 0, f_bufadd},
473 {"bufexists", 1, 1, 0, f_bufexists},
474 {"buffer_exists", 1, 1, 0, f_bufexists}, // obsolete
475 {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
476 {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
477 {"buflisted", 1, 1, 0, f_buflisted},
478 {"bufload", 1, 1, 0, f_bufload},
479 {"bufloaded", 1, 1, 0, f_bufloaded},
480 {"bufname", 1, 1, 0, f_bufname},
481 {"bufnr", 1, 2, 0, f_bufnr},
482 {"bufwinid", 1, 1, 0, f_bufwinid},
483 {"bufwinnr", 1, 1, 0, f_bufwinnr},
484 {"byte2line", 1, 1, 0, f_byte2line},
485 {"byteidx", 2, 2, 0, f_byteidx},
486 {"byteidxcomp", 2, 2, 0, f_byteidxcomp},
487 {"call", 2, 3, 0, f_call},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200489 {"ceil", 1, 1, 0, f_ceil},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200490#endif
491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200492 {"ch_canread", 1, 1, 0, f_ch_canread},
493 {"ch_close", 1, 1, 0, f_ch_close},
494 {"ch_close_in", 1, 1, 0, f_ch_close_in},
495 {"ch_evalexpr", 2, 3, 0, f_ch_evalexpr},
496 {"ch_evalraw", 2, 3, 0, f_ch_evalraw},
497 {"ch_getbufnr", 2, 2, 0, f_ch_getbufnr},
498 {"ch_getjob", 1, 1, 0, f_ch_getjob},
499 {"ch_info", 1, 1, 0, f_ch_info},
500 {"ch_log", 1, 2, 0, f_ch_log},
501 {"ch_logfile", 1, 2, 0, f_ch_logfile},
502 {"ch_open", 1, 2, 0, f_ch_open},
503 {"ch_read", 1, 2, 0, f_ch_read},
504 {"ch_readblob", 1, 2, 0, f_ch_readblob},
505 {"ch_readraw", 1, 2, 0, f_ch_readraw},
506 {"ch_sendexpr", 2, 3, 0, f_ch_sendexpr},
507 {"ch_sendraw", 2, 3, 0, f_ch_sendraw},
508 {"ch_setoptions", 2, 2, 0, f_ch_setoptions},
509 {"ch_status", 1, 2, 0, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200511 {"changenr", 0, 0, 0, f_changenr},
512 {"char2nr", 1, 2, 0, f_char2nr},
513 {"chdir", 1, 1, 0, f_chdir},
514 {"cindent", 1, 1, 0, f_cindent},
515 {"clearmatches", 0, 1, 0, f_clearmatches},
516 {"col", 1, 1, 0, f_col},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517#if defined(FEAT_INS_EXPAND)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200518 {"complete", 2, 2, 0, f_complete},
519 {"complete_add", 1, 1, 0, f_complete_add},
520 {"complete_check", 0, 0, 0, f_complete_check},
521 {"complete_info", 0, 1, 0, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200523 {"confirm", 1, 4, 0, f_confirm},
524 {"copy", 1, 1, FEARG_1, f_copy},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200526 {"cos", 1, 1, 0, f_cos},
527 {"cosh", 1, 1, 0, f_cosh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200529 {"count", 2, 4, FEARG_1, f_count},
530 {"cscope_connection",0,3, 0, f_cscope_connection},
531 {"cursor", 1, 3, 0, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100532#ifdef MSWIN
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200533 {"debugbreak", 1, 1, 0, f_debugbreak},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200534#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200535 {"deepcopy", 1, 2, 0, f_deepcopy},
536 {"delete", 1, 2, 0, f_delete},
537 {"deletebufline", 2, 3, 0, f_deletebufline},
538 {"did_filetype", 0, 0, 0, f_did_filetype},
539 {"diff_filler", 1, 1, 0, f_diff_filler},
540 {"diff_hlID", 2, 2, 0, f_diff_hlID},
541 {"empty", 1, 1, FEARG_1, f_empty},
542 {"environ", 0, 0, 0, f_environ},
543 {"escape", 2, 2, 0, f_escape},
544 {"eval", 1, 1, FEARG_1, f_eval},
545 {"eventhandler", 0, 0, 0, f_eventhandler},
546 {"executable", 1, 1, 0, f_executable},
547 {"execute", 1, 2, 0, f_execute},
548 {"exepath", 1, 1, 0, f_exepath},
549 {"exists", 1, 1, 0, f_exists},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200551 {"exp", 1, 1, 0, f_exp},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200553 {"expand", 1, 3, 0, f_expand},
554 {"expandcmd", 1, 1, 0, f_expandcmd},
555 {"extend", 2, 3, FEARG_1, f_extend},
556 {"feedkeys", 1, 2, 0, f_feedkeys},
557 {"file_readable", 1, 1, 0, f_filereadable}, // obsolete
558 {"filereadable", 1, 1, 0, f_filereadable},
559 {"filewritable", 1, 1, 0, f_filewritable},
560 {"filter", 2, 2, FEARG_1, f_filter},
561 {"finddir", 1, 3, 0, f_finddir},
562 {"findfile", 1, 3, 0, f_findfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200564 {"float2nr", 1, 1, 0, f_float2nr},
565 {"floor", 1, 1, 0, f_floor},
566 {"fmod", 2, 2, 0, f_fmod},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200568 {"fnameescape", 1, 1, 0, f_fnameescape},
569 {"fnamemodify", 2, 2, 0, f_fnamemodify},
570 {"foldclosed", 1, 1, 0, f_foldclosed},
571 {"foldclosedend", 1, 1, 0, f_foldclosedend},
572 {"foldlevel", 1, 1, 0, f_foldlevel},
573 {"foldtext", 0, 0, 0, f_foldtext},
574 {"foldtextresult", 1, 1, 0, f_foldtextresult},
575 {"foreground", 0, 0, 0, f_foreground},
576 {"funcref", 1, 3, 0, f_funcref},
577 {"function", 1, 3, 0, f_function},
578 {"garbagecollect", 0, 1, 0, f_garbagecollect},
579 {"get", 2, 3, FEARG_1, f_get},
580 {"getbufinfo", 0, 1, 0, f_getbufinfo},
581 {"getbufline", 2, 3, 0, f_getbufline},
582 {"getbufvar", 2, 3, 0, f_getbufvar},
583 {"getchangelist", 1, 1, 0, f_getchangelist},
584 {"getchar", 0, 1, 0, f_getchar},
585 {"getcharmod", 0, 0, 0, f_getcharmod},
586 {"getcharsearch", 0, 0, 0, f_getcharsearch},
587 {"getcmdline", 0, 0, 0, f_getcmdline},
588 {"getcmdpos", 0, 0, 0, f_getcmdpos},
589 {"getcmdtype", 0, 0, 0, f_getcmdtype},
590 {"getcmdwintype", 0, 0, 0, f_getcmdwintype},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200592 {"getcompletion", 2, 3, 0, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200594 {"getcurpos", 0, 0, 0, f_getcurpos},
595 {"getcwd", 0, 2, 0, f_getcwd},
596 {"getenv", 1, 1, 0, f_getenv},
597 {"getfontname", 0, 1, 0, f_getfontname},
598 {"getfperm", 1, 1, 0, f_getfperm},
599 {"getfsize", 1, 1, 0, f_getfsize},
600 {"getftime", 1, 1, 0, f_getftime},
601 {"getftype", 1, 1, 0, f_getftype},
602 {"getjumplist", 0, 2, 0, f_getjumplist},
603 {"getline", 1, 2, 0, f_getline},
604 {"getloclist", 1, 2, 0, f_getloclist},
605 {"getmatches", 0, 1, 0, f_getmatches},
606 {"getpid", 0, 0, 0, f_getpid},
607 {"getpos", 1, 1, 0, f_getpos},
608 {"getqflist", 0, 1, 0, f_getqflist},
609 {"getreg", 0, 3, 0, f_getreg},
610 {"getregtype", 0, 1, 0, f_getregtype},
611 {"gettabinfo", 0, 1, 0, f_gettabinfo},
612 {"gettabvar", 2, 3, 0, f_gettabvar},
613 {"gettabwinvar", 3, 4, 0, f_gettabwinvar},
614 {"gettagstack", 0, 1, 0, f_gettagstack},
615 {"getwininfo", 0, 1, 0, f_getwininfo},
616 {"getwinpos", 0, 1, 0, f_getwinpos},
617 {"getwinposx", 0, 0, 0, f_getwinposx},
618 {"getwinposy", 0, 0, 0, f_getwinposy},
619 {"getwinvar", 2, 3, 0, f_getwinvar},
620 {"glob", 1, 4, 0, f_glob},
621 {"glob2regpat", 1, 1, 0, f_glob2regpat},
622 {"globpath", 2, 5, 0, f_globpath},
623 {"has", 1, 1, 0, f_has},
624 {"has_key", 2, 2, FEARG_1, f_has_key},
625 {"haslocaldir", 0, 2, 0, f_haslocaldir},
626 {"hasmapto", 1, 3, 0, f_hasmapto},
627 {"highlightID", 1, 1, 0, f_hlID}, // obsolete
628 {"highlight_exists",1, 1, 0, f_hlexists}, // obsolete
629 {"histadd", 2, 2, 0, f_histadd},
630 {"histdel", 1, 2, 0, f_histdel},
631 {"histget", 1, 2, 0, f_histget},
632 {"histnr", 1, 1, 0, f_histnr},
633 {"hlID", 1, 1, 0, f_hlID},
634 {"hlexists", 1, 1, 0, f_hlexists},
635 {"hostname", 0, 0, 0, f_hostname},
636 {"iconv", 3, 3, 0, f_iconv},
637 {"indent", 1, 1, 0, f_indent},
638 {"index", 2, 4, FEARG_1, f_index},
639 {"input", 1, 3, 0, f_input},
640 {"inputdialog", 1, 3, 0, f_inputdialog},
641 {"inputlist", 1, 1, 0, f_inputlist},
642 {"inputrestore", 0, 0, 0, f_inputrestore},
643 {"inputsave", 0, 0, 0, f_inputsave},
644 {"inputsecret", 1, 2, 0, f_inputsecret},
645 {"insert", 2, 3, FEARG_1, f_insert},
646 {"invert", 1, 1, 0, f_invert},
647 {"isdirectory", 1, 1, 0, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200648#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200649 {"isinf", 1, 1, 0, f_isinf},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200650#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200651 {"islocked", 1, 1, 0, f_islocked},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200653 {"isnan", 1, 1, 0, f_isnan},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200654#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200655 {"items", 1, 1, FEARG_1, f_items},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200657 {"job_getchannel", 1, 1, 0, f_job_getchannel},
658 {"job_info", 0, 1, 0, f_job_info},
659 {"job_setoptions", 2, 2, 0, f_job_setoptions},
660 {"job_start", 1, 2, 0, f_job_start},
661 {"job_status", 1, 1, 0, f_job_status},
662 {"job_stop", 1, 2, 0, f_job_stop},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200664 {"join", 1, 2, FEARG_1, f_join},
665 {"js_decode", 1, 1, 0, f_js_decode},
666 {"js_encode", 1, 1, 0, f_js_encode},
667 {"json_decode", 1, 1, 0, f_json_decode},
668 {"json_encode", 1, 1, 0, f_json_encode},
669 {"keys", 1, 1, FEARG_1, f_keys},
670 {"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
671 {"len", 1, 1, FEARG_1, f_len},
672 {"libcall", 3, 3, 0, f_libcall},
673 {"libcallnr", 3, 3, 0, f_libcallnr},
674 {"line", 1, 1, 0, f_line},
675 {"line2byte", 1, 1, 0, f_line2byte},
676 {"lispindent", 1, 1, 0, f_lispindent},
677 {"list2str", 1, 2, 0, f_list2str},
678 {"listener_add", 1, 2, 0, f_listener_add},
679 {"listener_flush", 0, 1, 0, f_listener_flush},
680 {"listener_remove", 1, 1, 0, f_listener_remove},
681 {"localtime", 0, 0, 0, f_localtime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200682#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200683 {"log", 1, 1, 0, f_log},
684 {"log10", 1, 1, 0, f_log10},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200685#endif
686#ifdef FEAT_LUA
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200687 {"luaeval", 1, 2, 0, f_luaeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200688#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200689 {"map", 2, 2, FEARG_1, f_map},
690 {"maparg", 1, 4, 0, f_maparg},
691 {"mapcheck", 1, 3, 0, f_mapcheck},
692 {"match", 2, 4, 0, f_match},
693 {"matchadd", 2, 5, 0, f_matchadd},
694 {"matchaddpos", 2, 5, 0, f_matchaddpos},
695 {"matcharg", 1, 1, 0, f_matcharg},
696 {"matchdelete", 1, 2, 0, f_matchdelete},
697 {"matchend", 2, 4, 0, f_matchend},
698 {"matchlist", 2, 4, 0, f_matchlist},
699 {"matchstr", 2, 4, 0, f_matchstr},
700 {"matchstrpos", 2, 4, 0, f_matchstrpos},
701 {"max", 1, 1, FEARG_1, f_max},
702 {"min", 1, 1, FEARG_1, f_min},
703 {"mkdir", 1, 3, 0, f_mkdir},
704 {"mode", 0, 1, 0, f_mode},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705#ifdef FEAT_MZSCHEME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200706 {"mzeval", 1, 1, 0, f_mzeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200707#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200708 {"nextnonblank", 1, 1, 0, f_nextnonblank},
709 {"nr2char", 1, 2, 0, f_nr2char},
710 {"or", 2, 2, 0, f_or},
711 {"pathshorten", 1, 1, 0, f_pathshorten},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200712#ifdef FEAT_PERL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200713 {"perleval", 1, 1, 0, f_perleval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200714#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200715#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200716 {"popup_atcursor", 2, 2, 0, f_popup_atcursor},
717 {"popup_beval", 2, 2, 0, f_popup_beval},
718 {"popup_clear", 0, 0, 0, f_popup_clear},
719 {"popup_close", 1, 2, 0, f_popup_close},
720 {"popup_create", 2, 2, 0, f_popup_create},
721 {"popup_dialog", 2, 2, 0, f_popup_dialog},
722 {"popup_filter_menu", 2, 2, 0, f_popup_filter_menu},
723 {"popup_filter_yesno", 2, 2, 0, f_popup_filter_yesno},
724 {"popup_getoptions", 1, 1, 0, f_popup_getoptions},
725 {"popup_getpos", 1, 1, 0, f_popup_getpos},
726 {"popup_getpreview", 0, 0, 0, f_popup_getpreview},
727 {"popup_hide", 1, 1, 0, f_popup_hide},
728 {"popup_locate", 2, 2, 0, f_popup_locate},
729 {"popup_menu", 2, 2, 0, f_popup_menu},
730 {"popup_move", 2, 2, 0, f_popup_move},
731 {"popup_notification", 2, 2, 0, f_popup_notification},
732 {"popup_setoptions", 2, 2, 0, f_popup_setoptions},
733 {"popup_settext", 2, 2, 0, f_popup_settext},
734 {"popup_show", 1, 1, 0, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200735#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200737 {"pow", 2, 2, 0, f_pow},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200738#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200739 {"prevnonblank", 1, 1, 0, f_prevnonblank},
740 {"printf", 1, 19, 0, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200741#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200742 {"prompt_setcallback", 2, 2, 0, f_prompt_setcallback},
743 {"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt},
744 {"prompt_setprompt", 2, 2, 0, f_prompt_setprompt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200745#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100746#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200747 {"prop_add", 3, 3, 0, f_prop_add},
748 {"prop_clear", 1, 3, 0, f_prop_clear},
749 {"prop_list", 1, 2, 0, f_prop_list},
750 {"prop_remove", 1, 3, 0, f_prop_remove},
751 {"prop_type_add", 2, 2, 0, f_prop_type_add},
752 {"prop_type_change", 2, 2, 0, f_prop_type_change},
753 {"prop_type_delete", 1, 2, 0, f_prop_type_delete},
754 {"prop_type_get", 1, 2, 0, f_prop_type_get},
755 {"prop_type_list", 0, 1, 0, f_prop_type_list},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100756#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200757 {"pumvisible", 0, 0, 0, f_pumvisible},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758#ifdef FEAT_PYTHON3
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200759 {"py3eval", 1, 1, 0, f_py3eval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200760#endif
761#ifdef FEAT_PYTHON
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200762 {"pyeval", 1, 1, 0, f_pyeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100764#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200765 {"pyxeval", 1, 1, 0, f_pyxeval},
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100766#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200767 {"range", 1, 3, 0, f_range},
768 {"readdir", 1, 2, 0, f_readdir},
769 {"readfile", 1, 3, 0, f_readfile},
770 {"reg_executing", 0, 0, 0, f_reg_executing},
771 {"reg_recording", 0, 0, 0, f_reg_recording},
772 {"reltime", 0, 2, 0, f_reltime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200774 {"reltimefloat", 1, 1, 0, f_reltimefloat},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200776 {"reltimestr", 1, 1, 0, f_reltimestr},
777 {"remote_expr", 2, 4, 0, f_remote_expr},
778 {"remote_foreground", 1, 1, 0, f_remote_foreground},
779 {"remote_peek", 1, 2, 0, f_remote_peek},
780 {"remote_read", 1, 2, 0, f_remote_read},
781 {"remote_send", 2, 3, 0, f_remote_send},
782 {"remote_startserver", 1, 1, 0, f_remote_startserver},
783 {"remove", 2, 3, FEARG_1, f_remove},
784 {"rename", 2, 2, 0, f_rename},
785 {"repeat", 2, 2, FEARG_1, f_repeat},
786 {"resolve", 1, 1, 0, f_resolve},
787 {"reverse", 1, 1, FEARG_1, f_reverse},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200789 {"round", 1, 1, 0, f_round},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100791#ifdef FEAT_RUBY
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200792 {"rubyeval", 1, 1, 0, f_rubyeval},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100793#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200794 {"screenattr", 2, 2, 0, f_screenattr},
795 {"screenchar", 2, 2, 0, f_screenchar},
796 {"screenchars", 2, 2, 0, f_screenchars},
797 {"screencol", 0, 0, 0, f_screencol},
798 {"screenpos", 3, 3, 0, f_screenpos},
799 {"screenrow", 0, 0, 0, f_screenrow},
800 {"screenstring", 2, 2, 0, f_screenstring},
801 {"search", 1, 4, 0, f_search},
802 {"searchdecl", 1, 3, 0, f_searchdecl},
803 {"searchpair", 3, 7, 0, f_searchpair},
804 {"searchpairpos", 3, 7, 0, f_searchpairpos},
805 {"searchpos", 1, 4, 0, f_searchpos},
806 {"server2client", 2, 2, 0, f_server2client},
807 {"serverlist", 0, 0, 0, f_serverlist},
808 {"setbufline", 3, 3, 0, f_setbufline},
809 {"setbufvar", 3, 3, 0, f_setbufvar},
810 {"setcharsearch", 1, 1, 0, f_setcharsearch},
811 {"setcmdpos", 1, 1, 0, f_setcmdpos},
812 {"setenv", 2, 2, 0, f_setenv},
813 {"setfperm", 2, 2, 0, f_setfperm},
814 {"setline", 2, 2, 0, f_setline},
815 {"setloclist", 2, 4, 0, f_setloclist},
816 {"setmatches", 1, 2, 0, f_setmatches},
817 {"setpos", 2, 2, 0, f_setpos},
818 {"setqflist", 1, 3, 0, f_setqflist},
819 {"setreg", 2, 3, 0, f_setreg},
820 {"settabvar", 3, 3, 0, f_settabvar},
821 {"settabwinvar", 4, 4, 0, f_settabwinvar},
822 {"settagstack", 2, 3, 0, f_settagstack},
823 {"setwinvar", 3, 3, 0, f_setwinvar},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824#ifdef FEAT_CRYPT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200825 {"sha256", 1, 1, 0, f_sha256},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200827 {"shellescape", 1, 2, 0, f_shellescape},
828 {"shiftwidth", 0, 1, 0, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100829#ifdef FEAT_SIGNS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200830 {"sign_define", 1, 2, 0, f_sign_define},
831 {"sign_getdefined", 0, 1, 0, f_sign_getdefined},
832 {"sign_getplaced", 0, 2, 0, f_sign_getplaced},
833 {"sign_jump", 3, 3, 0, f_sign_jump},
834 {"sign_place", 4, 5, 0, f_sign_place},
835 {"sign_placelist", 1, 1, 0, f_sign_placelist},
836 {"sign_undefine", 0, 1, 0, f_sign_undefine},
837 {"sign_unplace", 1, 2, 0, f_sign_unplace},
838 {"sign_unplacelist", 1, 2, 0, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100839#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200840 {"simplify", 1, 1, 0, f_simplify},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200842 {"sin", 1, 1, 0, f_sin},
843 {"sinh", 1, 1, 0, f_sinh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200845 {"sort", 1, 3, FEARG_1, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200846#ifdef FEAT_SOUND
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200847 {"sound_clear", 0, 0, 0, f_sound_clear},
848 {"sound_playevent", 1, 2, 0, f_sound_playevent},
849 {"sound_playfile", 1, 2, 0, f_sound_playfile},
850 {"sound_stop", 1, 1, 0, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200851#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200852 {"soundfold", 1, 1, 0, f_soundfold},
853 {"spellbadword", 0, 1, 0, f_spellbadword},
854 {"spellsuggest", 1, 3, 0, f_spellsuggest},
855 {"split", 1, 3, FEARG_1, f_split},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200857 {"sqrt", 1, 1, 0, f_sqrt},
858 {"str2float", 1, 1, 0, f_str2float},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200859#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200860 {"str2list", 1, 2, FEARG_1, f_str2list},
861 {"str2nr", 1, 2, 0, f_str2nr},
862 {"strcharpart", 2, 3, 0, f_strcharpart},
863 {"strchars", 1, 2, 0, f_strchars},
864 {"strdisplaywidth", 1, 2, 0, f_strdisplaywidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865#ifdef HAVE_STRFTIME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200866 {"strftime", 1, 2, 0, f_strftime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200868 {"strgetchar", 2, 2, 0, f_strgetchar},
869 {"stridx", 2, 3, 0, f_stridx},
870 {"string", 1, 1, FEARG_1, f_string},
871 {"strlen", 1, 1, FEARG_1, f_strlen},
872 {"strpart", 2, 3, 0, f_strpart},
873 {"strridx", 2, 3, 0, f_strridx},
874 {"strtrans", 1, 1, FEARG_1, f_strtrans},
875 {"strwidth", 1, 1, FEARG_1, f_strwidth},
876 {"submatch", 1, 2, 0, f_submatch},
877 {"substitute", 4, 4, FEARG_1, f_substitute},
878 {"swapinfo", 1, 1, 0, f_swapinfo},
879 {"swapname", 1, 1, 0, f_swapname},
880 {"synID", 3, 3, 0, f_synID},
881 {"synIDattr", 2, 3, FEARG_1, f_synIDattr},
882 {"synIDtrans", 1, 1, FEARG_1, f_synIDtrans},
883 {"synconcealed", 2, 2, 0, f_synconcealed},
884 {"synstack", 2, 2, 0, f_synstack},
885 {"system", 1, 2, FEARG_1, f_system},
886 {"systemlist", 1, 2, FEARG_1, f_systemlist},
887 {"tabpagebuflist", 0, 1, 0, f_tabpagebuflist},
888 {"tabpagenr", 0, 1, 0, f_tabpagenr},
889 {"tabpagewinnr", 1, 2, 0, f_tabpagewinnr},
890 {"tagfiles", 0, 0, 0, f_tagfiles},
891 {"taglist", 1, 2, 0, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200893 {"tan", 1, 1, 0, f_tan},
894 {"tanh", 1, 1, 0, f_tanh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200896 {"tempname", 0, 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897#ifdef FEAT_TERMINAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200898 {"term_dumpdiff", 2, 3, 0, f_term_dumpdiff},
899 {"term_dumpload", 1, 2, 0, f_term_dumpload},
900 {"term_dumpwrite", 2, 3, 0, f_term_dumpwrite},
901 {"term_getaltscreen", 1, 1, 0, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200902# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200903 {"term_getansicolors", 1, 1, 0, f_term_getansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200904# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200905 {"term_getattr", 2, 2, 0, f_term_getattr},
906 {"term_getcursor", 1, 1, 0, f_term_getcursor},
907 {"term_getjob", 1, 1, 0, f_term_getjob},
908 {"term_getline", 2, 2, 0, f_term_getline},
909 {"term_getscrolled", 1, 1, 0, f_term_getscrolled},
910 {"term_getsize", 1, 1, 0, f_term_getsize},
911 {"term_getstatus", 1, 1, 0, f_term_getstatus},
912 {"term_gettitle", 1, 1, 0, f_term_gettitle},
913 {"term_gettty", 1, 2, 0, f_term_gettty},
914 {"term_list", 0, 0, 0, f_term_list},
915 {"term_scrape", 2, 2, 0, f_term_scrape},
916 {"term_sendkeys", 2, 2, 0, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200917# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200918 {"term_setansicolors", 2, 2, 0, f_term_setansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200919# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200920 {"term_setkill", 2, 2, 0, f_term_setkill},
921 {"term_setrestore", 2, 2, 0, f_term_setrestore},
922 {"term_setsize", 3, 3, 0, f_term_setsize},
923 {"term_start", 1, 2, 0, f_term_start},
924 {"term_wait", 1, 2, 0, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200925#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200926 {"test_alloc_fail", 3, 3, 0, f_test_alloc_fail},
927 {"test_autochdir", 0, 0, 0, f_test_autochdir},
928 {"test_feedinput", 1, 1, 0, f_test_feedinput},
929 {"test_garbagecollect_now", 0, 0, 0, f_test_garbagecollect_now},
930 {"test_garbagecollect_soon", 0, 0, 0, f_test_garbagecollect_soon},
931 {"test_getvalue", 1, 1, 0, f_test_getvalue},
932 {"test_ignore_error", 1, 1, 0, f_test_ignore_error},
933 {"test_null_blob", 0, 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200935 {"test_null_channel", 0, 0, 0, f_test_null_channel},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200936#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200937 {"test_null_dict", 0, 0, 0, f_test_null_dict},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200939 {"test_null_job", 0, 0, 0, f_test_null_job},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200941 {"test_null_list", 0, 0, 0, f_test_null_list},
942 {"test_null_partial", 0, 0, 0, f_test_null_partial},
943 {"test_null_string", 0, 0, 0, f_test_null_string},
944 {"test_option_not_set", 1, 1, 0, f_test_option_not_set},
945 {"test_override", 2, 2, 0, f_test_override},
946 {"test_refcount", 1, 1, 0, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200947#ifdef FEAT_GUI
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200948 {"test_scrollbar", 3, 3, 0, f_test_scrollbar},
Bram Moolenaarab186732018-09-14 21:27:06 +0200949#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200950#ifdef FEAT_MOUSE
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200951 {"test_setmouse", 2, 2, 0, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200952#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200953 {"test_settime", 1, 1, 0, f_test_settime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200954#ifdef FEAT_TIMERS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200955 {"timer_info", 0, 1, 0, f_timer_info},
956 {"timer_pause", 2, 2, 0, f_timer_pause},
957 {"timer_start", 2, 3, 0, f_timer_start},
958 {"timer_stop", 1, 1, 0, f_timer_stop},
959 {"timer_stopall", 0, 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200960#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200961 {"tolower", 1, 1, 0, f_tolower},
962 {"toupper", 1, 1, 0, f_toupper},
963 {"tr", 3, 3, 0, f_tr},
964 {"trim", 1, 2, 0, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200966 {"trunc", 1, 1, 0, f_trunc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200967#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200968 {"type", 1, 1, FEARG_1, f_type},
969 {"undofile", 1, 1, 0, f_undofile},
970 {"undotree", 0, 0, 0, f_undotree},
971 {"uniq", 1, 3, FEARG_1, f_uniq},
972 {"values", 1, 1, FEARG_1, f_values},
973 {"virtcol", 1, 1, 0, f_virtcol},
974 {"visualmode", 0, 1, 0, f_visualmode},
975 {"wildmenumode", 0, 0, 0, f_wildmenumode},
976 {"win_execute", 2, 3, 0, f_win_execute},
977 {"win_findbuf", 1, 1, 0, f_win_findbuf},
978 {"win_getid", 0, 2, 0, f_win_getid},
979 {"win_gotoid", 1, 1, 0, f_win_gotoid},
980 {"win_id2tabwin", 1, 1, 0, f_win_id2tabwin},
981 {"win_id2win", 1, 1, 0, f_win_id2win},
982 {"win_screenpos", 1, 1, 0, f_win_screenpos},
983 {"winbufnr", 1, 1, 0, f_winbufnr},
984 {"wincol", 0, 0, 0, f_wincol},
985 {"winheight", 1, 1, 0, f_winheight},
986 {"winlayout", 0, 1, 0, f_winlayout},
987 {"winline", 0, 0, 0, f_winline},
988 {"winnr", 0, 1, 0, f_winnr},
989 {"winrestcmd", 0, 0, 0, f_winrestcmd},
990 {"winrestview", 1, 1, 0, f_winrestview},
991 {"winsaveview", 0, 0, 0, f_winsaveview},
992 {"winwidth", 1, 1, 0, f_winwidth},
993 {"wordcount", 0, 0, 0, f_wordcount},
994 {"writefile", 2, 3, 0, f_writefile},
995 {"xor", 2, 2, 0, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +0200996};
997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200998#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
999
1000/*
1001 * Function given to ExpandGeneric() to obtain the list of internal
1002 * or user defined function names.
1003 */
1004 char_u *
1005get_function_name(expand_T *xp, int idx)
1006{
1007 static int intidx = -1;
1008 char_u *name;
1009
1010 if (idx == 0)
1011 intidx = -1;
1012 if (intidx < 0)
1013 {
1014 name = get_user_func_name(xp, idx);
1015 if (name != NULL)
1016 return name;
1017 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001018 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001019 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001020 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001022 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001023 STRCAT(IObuff, ")");
1024 return IObuff;
1025 }
1026
1027 return NULL;
1028}
1029
1030/*
1031 * Function given to ExpandGeneric() to obtain the list of internal or
1032 * user defined variable or function names.
1033 */
1034 char_u *
1035get_expr_name(expand_T *xp, int idx)
1036{
1037 static int intidx = -1;
1038 char_u *name;
1039
1040 if (idx == 0)
1041 intidx = -1;
1042 if (intidx < 0)
1043 {
1044 name = get_function_name(xp, idx);
1045 if (name != NULL)
1046 return name;
1047 }
1048 return get_user_var_name(xp, ++intidx);
1049}
1050
1051#endif /* FEAT_CMDL_COMPL */
1052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001053/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001054 * Find internal function "name" in table "global_functions".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001055 * Return index, or -1 if not found
1056 */
Bram Moolenaarac92e252019-08-03 21:58:38 +02001057 static int
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001058find_internal_func(char_u *name)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001059{
1060 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001061 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001062 int cmp;
1063 int x;
1064
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001065 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001066
1067 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001068 while (first <= last)
1069 {
1070 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001071 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001072 if (cmp < 0)
1073 last = x - 1;
1074 else if (cmp > 0)
1075 first = x + 1;
1076 else
1077 return x;
1078 }
1079 return -1;
1080}
1081
1082 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001083has_internal_func(char_u *name)
1084{
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001085 return find_internal_func(name) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001086}
1087
1088 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001089call_internal_func(
1090 char_u *name,
1091 int argcount,
1092 typval_T *argvars,
1093 typval_T *rettv)
1094{
1095 int i;
1096
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001097 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001098 if (i < 0)
1099 return ERROR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001100 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001101 return ERROR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001102 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001103 return ERROR_TOOMANY;
1104 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001105 global_functions[i].f_func(argvars, rettv);
1106 return ERROR_NONE;
1107}
1108
1109/*
1110 * Invoke a method for base->method().
1111 */
1112 int
1113call_internal_method(
1114 char_u *name,
1115 int argcount,
1116 typval_T *argvars,
1117 typval_T *rettv,
1118 typval_T *basetv)
1119{
1120 int i;
1121 int fi;
1122 typval_T argv[MAX_FUNC_ARGS + 1];
1123
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001124 fi = find_internal_func(name);
1125 if (fi < 0 || global_functions[fi].f_argtype == 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001126 return ERROR_UNKNOWN;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001127 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001128 return ERROR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001129 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001130 return ERROR_TOOMANY;
1131
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001132 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001133 {
1134 // base value goes last
1135 for (i = 0; i < argcount; ++i)
1136 argv[i] = argvars[i];
1137 argv[argcount] = *basetv;
1138 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001139 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001140 {
1141 // base value goes second
1142 argv[0] = argvars[0];
1143 argv[1] = *basetv;
1144 for (i = 1; i < argcount; ++i)
1145 argv[i + 1] = argvars[i];
1146 }
1147 else
1148 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001149 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001150 argv[0] = *basetv;
1151 for (i = 0; i < argcount; ++i)
1152 argv[i + 1] = argvars[i];
1153 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001154 argv[argcount + 1].v_type = VAR_UNKNOWN;
1155
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001156 global_functions[fi].f_func(argv, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001157 return ERROR_NONE;
1158}
1159
1160/*
1161 * Return TRUE for a non-zero Number and a non-empty String.
1162 */
1163 static int
1164non_zero_arg(typval_T *argvars)
1165{
1166 return ((argvars[0].v_type == VAR_NUMBER
1167 && argvars[0].vval.v_number != 0)
1168 || (argvars[0].v_type == VAR_SPECIAL
1169 && argvars[0].vval.v_number == VVAL_TRUE)
1170 || (argvars[0].v_type == VAR_STRING
1171 && argvars[0].vval.v_string != NULL
1172 && *argvars[0].vval.v_string != NUL));
1173}
1174
1175/*
1176 * Get the lnum from the first argument.
1177 * Also accepts ".", "$", etc., but that only works for the current buffer.
1178 * Returns -1 on error.
1179 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001180 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182{
1183 typval_T rettv;
1184 linenr_T lnum;
1185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001186 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001187 if (lnum == 0) /* no valid number, try using line() */
1188 {
1189 rettv.v_type = VAR_NUMBER;
1190 f_line(argvars, &rettv);
1191 lnum = (linenr_T)rettv.vval.v_number;
1192 clear_tv(&rettv);
1193 }
1194 return lnum;
1195}
1196
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001197/*
1198 * Get the lnum from the first argument.
1199 * Also accepts "$", then "buf" is used.
1200 * Returns 0 on error.
1201 */
1202 static linenr_T
1203tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1204{
1205 if (argvars[0].v_type == VAR_STRING
1206 && argvars[0].vval.v_string != NULL
1207 && argvars[0].vval.v_string[0] == '$'
1208 && buf != NULL)
1209 return buf->b_ml.ml_line_count;
1210 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1211}
1212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001214/*
1215 * Get the float value of "argvars[0]" into "f".
1216 * Returns FAIL when the argument is not a Number or Float.
1217 */
1218 static int
1219get_float_arg(typval_T *argvars, float_T *f)
1220{
1221 if (argvars[0].v_type == VAR_FLOAT)
1222 {
1223 *f = argvars[0].vval.v_float;
1224 return OK;
1225 }
1226 if (argvars[0].v_type == VAR_NUMBER)
1227 {
1228 *f = (float_T)argvars[0].vval.v_number;
1229 return OK;
1230 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001232 return FAIL;
1233}
1234
1235/*
1236 * "abs(expr)" function
1237 */
1238 static void
1239f_abs(typval_T *argvars, typval_T *rettv)
1240{
1241 if (argvars[0].v_type == VAR_FLOAT)
1242 {
1243 rettv->v_type = VAR_FLOAT;
1244 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1245 }
1246 else
1247 {
1248 varnumber_T n;
1249 int error = FALSE;
1250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001251 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252 if (error)
1253 rettv->vval.v_number = -1;
1254 else if (n > 0)
1255 rettv->vval.v_number = n;
1256 else
1257 rettv->vval.v_number = -n;
1258 }
1259}
1260
1261/*
1262 * "acos()" function
1263 */
1264 static void
1265f_acos(typval_T *argvars, typval_T *rettv)
1266{
1267 float_T f = 0.0;
1268
1269 rettv->v_type = VAR_FLOAT;
1270 if (get_float_arg(argvars, &f) == OK)
1271 rettv->vval.v_float = acos(f);
1272 else
1273 rettv->vval.v_float = 0.0;
1274}
1275#endif
1276
1277/*
1278 * "add(list, item)" function
1279 */
1280 static void
1281f_add(typval_T *argvars, typval_T *rettv)
1282{
1283 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285
1286 rettv->vval.v_number = 1; /* Default: Failed */
1287 if (argvars[0].v_type == VAR_LIST)
1288 {
1289 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001290 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001291 (char_u *)N_("add() argument"), TRUE)
1292 && list_append_tv(l, &argvars[1]) == OK)
1293 copy_tv(&argvars[0], rettv);
1294 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 else if (argvars[0].v_type == VAR_BLOB)
1296 {
1297 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001298 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 (char_u *)N_("add() argument"), TRUE))
1300 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001301 int error = FALSE;
1302 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1303
1304 if (!error)
1305 {
1306 ga_append(&b->bv_ga, (int)n);
1307 copy_tv(&argvars[0], rettv);
1308 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001309 }
1310 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001312 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313}
1314
1315/*
1316 * "and(expr, expr)" function
1317 */
1318 static void
1319f_and(typval_T *argvars, typval_T *rettv)
1320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001321 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1322 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001323}
1324
1325/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001326 * If there is a window for "curbuf", make it the current window.
1327 */
1328 static void
1329find_win_for_curbuf(void)
1330{
1331 wininfo_T *wip;
1332
1333 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1334 {
1335 if (wip->wi_win != NULL)
1336 {
1337 curwin = wip->wi_win;
1338 break;
1339 }
1340 }
1341}
1342
1343/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001344 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 */
1346 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001347set_buffer_lines(
1348 buf_T *buf,
1349 linenr_T lnum_arg,
1350 int append,
1351 typval_T *lines,
1352 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353{
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1355 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356 list_T *l = NULL;
1357 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 linenr_T append_lnum;
1360 buf_T *curbuf_save = NULL;
1361 win_T *curwin_save = NULL;
1362 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 /* When using the current buffer ml_mfp will be set if needed. Useful when
1365 * setline() is used on startup. For other buffers the buffer must be
1366 * loaded. */
1367 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 rettv->vval.v_number = 1; /* FAIL */
1370 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 }
1372
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001375 curbuf_save = curbuf;
1376 curwin_save = curwin;
1377 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001378 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001379 }
1380
1381 if (append)
1382 // appendbufline() uses the line number below which we insert
1383 append_lnum = lnum - 1;
1384 else
1385 // setbufline() uses the line number above which we insert, we only
1386 // append if it's below the last line
1387 append_lnum = curbuf->b_ml.ml_line_count;
1388
1389 if (lines->v_type == VAR_LIST)
1390 {
1391 l = lines->vval.v_list;
1392 li = l->lv_first;
1393 }
1394 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001395 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001396
1397 /* default result is zero == OK */
1398 for (;;)
1399 {
1400 if (l != NULL)
1401 {
1402 /* list argument, get next string */
1403 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001405 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001406 li = li->li_next;
1407 }
1408
Bram Moolenaarca851592018-06-06 21:04:07 +02001409 rettv->vval.v_number = 1; /* FAIL */
1410 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1411 break;
1412
1413 /* When coming here from Insert mode, sync undo, so that this can be
1414 * undone separately from what was previously inserted. */
1415 if (u_sync_once == 2)
1416 {
1417 u_sync_once = 1; /* notify that u_sync() was called */
1418 u_sync(TRUE);
1419 }
1420
1421 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1422 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001423 // Existing line, replace it.
1424 // Removes any existing text properties.
1425 if (u_savesub(lnum) == OK && ml_replace_len(
1426 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001427 {
1428 changed_bytes(lnum, 0);
1429 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1430 check_cursor_col();
1431 rettv->vval.v_number = 0; /* OK */
1432 }
1433 }
1434 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1435 {
1436 /* append the line */
1437 ++added;
1438 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1439 rettv->vval.v_number = 0; /* OK */
1440 }
1441
1442 if (l == NULL) /* only one string argument */
1443 break;
1444 ++lnum;
1445 }
1446
1447 if (added > 0)
1448 {
1449 win_T *wp;
1450 tabpage_T *tp;
1451
1452 appended_lines_mark(append_lnum, added);
1453 FOR_ALL_TAB_WINDOWS(tp, wp)
1454 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1455 wp->w_cursor.lnum += added;
1456 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001457 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001459
1460 if (!is_curbuf)
1461 {
1462 curbuf = curbuf_save;
1463 curwin = curwin_save;
1464 }
1465}
1466
1467/*
1468 * "append(lnum, string/list)" function
1469 */
1470 static void
1471f_append(typval_T *argvars, typval_T *rettv)
1472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001474
1475 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1476}
1477
1478/*
1479 * "appendbufline(buf, lnum, string/list)" function
1480 */
1481 static void
1482f_appendbufline(typval_T *argvars, typval_T *rettv)
1483{
1484 linenr_T lnum;
1485 buf_T *buf;
1486
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001487 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 if (buf == NULL)
1489 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499 */
1500 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 win_T *wp;
1504
1505 if (argvars[0].v_type == VAR_UNKNOWN)
1506 // use the current window
1507 rettv->vval.v_number = ARGCOUNT;
1508 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 // use the global argument list
1511 rettv->vval.v_number = GARGCOUNT;
1512 else
1513 {
1514 // use the argument list of the specified window
1515 wp = find_win_by_nr_or_id(&argvars[0]);
1516 if (wp != NULL)
1517 rettv->vval.v_number = WARGCOUNT(wp);
1518 else
1519 rettv->vval.v_number = -1;
1520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521}
1522
1523/*
1524 * "argidx()" function
1525 */
1526 static void
1527f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1528{
1529 rettv->vval.v_number = curwin->w_arg_idx;
1530}
1531
1532/*
1533 * "arglistid()" function
1534 */
1535 static void
1536f_arglistid(typval_T *argvars, typval_T *rettv)
1537{
1538 win_T *wp;
1539
1540 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001541 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 if (wp != NULL)
1543 rettv->vval.v_number = wp->w_alist->id;
1544}
1545
1546/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 * Get the argument list for a given window
1548 */
1549 static void
1550get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1551{
1552 int idx;
1553
1554 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1555 for (idx = 0; idx < argcount; ++idx)
1556 list_append_string(rettv->vval.v_list,
1557 alist_name(&arglist[idx]), -1);
1558}
1559
1560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 * "argv(nr)" function
1562 */
1563 static void
1564f_argv(typval_T *argvars, typval_T *rettv)
1565{
1566 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001567 aentry_T *arglist = NULL;
1568 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569
1570 if (argvars[0].v_type != VAR_UNKNOWN)
1571 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001572 if (argvars[1].v_type == VAR_UNKNOWN)
1573 {
1574 arglist = ARGLIST;
1575 argcount = ARGCOUNT;
1576 }
1577 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001578 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 {
1580 arglist = GARGLIST;
1581 argcount = GARGCOUNT;
1582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 {
1585 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1586
1587 if (wp != NULL)
1588 {
1589 /* Use the argument list of the specified window */
1590 arglist = WARGLIST(wp);
1591 argcount = WARGCOUNT(wp);
1592 }
1593 }
1594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 if (arglist != NULL && idx >= 0 && idx < argcount)
1599 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1600 else if (idx == -1)
1601 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 else
1604 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605}
1606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607#ifdef FEAT_FLOAT
1608/*
1609 * "asin()" function
1610 */
1611 static void
1612f_asin(typval_T *argvars, typval_T *rettv)
1613{
1614 float_T f = 0.0;
1615
1616 rettv->v_type = VAR_FLOAT;
1617 if (get_float_arg(argvars, &f) == OK)
1618 rettv->vval.v_float = asin(f);
1619 else
1620 rettv->vval.v_float = 0.0;
1621}
1622
1623/*
1624 * "atan()" function
1625 */
1626 static void
1627f_atan(typval_T *argvars, typval_T *rettv)
1628{
1629 float_T f = 0.0;
1630
1631 rettv->v_type = VAR_FLOAT;
1632 if (get_float_arg(argvars, &f) == OK)
1633 rettv->vval.v_float = atan(f);
1634 else
1635 rettv->vval.v_float = 0.0;
1636}
1637
1638/*
1639 * "atan2()" function
1640 */
1641 static void
1642f_atan2(typval_T *argvars, typval_T *rettv)
1643{
1644 float_T fx = 0.0, fy = 0.0;
1645
1646 rettv->v_type = VAR_FLOAT;
1647 if (get_float_arg(argvars, &fx) == OK
1648 && get_float_arg(&argvars[1], &fy) == OK)
1649 rettv->vval.v_float = atan2(fx, fy);
1650 else
1651 rettv->vval.v_float = 0.0;
1652}
1653#endif
1654
1655/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001656 * "balloon_show()" function
1657 */
1658#ifdef FEAT_BEVAL
1659 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001660f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1661{
1662 rettv->v_type = VAR_STRING;
1663 if (balloonEval != NULL)
1664 {
1665 if (balloonEval->msg == NULL)
1666 rettv->vval.v_string = NULL;
1667 else
1668 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1669 }
1670}
1671
1672 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001673f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1674{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001675 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001676 {
1677 if (argvars[0].v_type == VAR_LIST
1678# ifdef FEAT_GUI
1679 && !gui.in_use
1680# endif
1681 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001682 {
1683 list_T *l = argvars[0].vval.v_list;
1684
1685 // empty list removes the balloon
1686 post_balloon(balloonEval, NULL,
1687 l == NULL || l->lv_len == 0 ? NULL : l);
1688 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001689 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001690 {
1691 char_u *mesg = tv_get_string_chk(&argvars[0]);
1692
1693 if (mesg != NULL)
1694 // empty string removes the balloon
1695 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1696 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001697 }
1698}
1699
Bram Moolenaar669a8282017-11-19 20:13:05 +01001700# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001701 static void
1702f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1703{
1704 if (rettv_list_alloc(rettv) == OK)
1705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001706 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001707
1708 if (msg != NULL)
1709 {
1710 pumitem_T *array;
1711 int size = split_message(msg, &array);
1712 int i;
1713
1714 /* Skip the first and last item, they are always empty. */
1715 for (i = 1; i < size - 1; ++i)
1716 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001717 while (size > 0)
1718 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001719 vim_free(array);
1720 }
1721 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001722}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001723# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001724#endif
1725
1726/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727 * "browse(save, title, initdir, default)" function
1728 */
1729 static void
1730f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1731{
1732#ifdef FEAT_BROWSE
1733 int save;
1734 char_u *title;
1735 char_u *initdir;
1736 char_u *defname;
1737 char_u buf[NUMBUFLEN];
1738 char_u buf2[NUMBUFLEN];
1739 int error = FALSE;
1740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001741 save = (int)tv_get_number_chk(&argvars[0], &error);
1742 title = tv_get_string_chk(&argvars[1]);
1743 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1744 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001745
1746 if (error || title == NULL || initdir == NULL || defname == NULL)
1747 rettv->vval.v_string = NULL;
1748 else
1749 rettv->vval.v_string =
1750 do_browse(save ? BROWSE_SAVE : 0,
1751 title, defname, NULL, initdir, NULL, curbuf);
1752#else
1753 rettv->vval.v_string = NULL;
1754#endif
1755 rettv->v_type = VAR_STRING;
1756}
1757
1758/*
1759 * "browsedir(title, initdir)" function
1760 */
1761 static void
1762f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1763{
1764#ifdef FEAT_BROWSE
1765 char_u *title;
1766 char_u *initdir;
1767 char_u buf[NUMBUFLEN];
1768
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001769 title = tv_get_string_chk(&argvars[0]);
1770 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001771
1772 if (title == NULL || initdir == NULL)
1773 rettv->vval.v_string = NULL;
1774 else
1775 rettv->vval.v_string = do_browse(BROWSE_DIR,
1776 title, NULL, NULL, initdir, NULL, curbuf);
1777#else
1778 rettv->vval.v_string = NULL;
1779#endif
1780 rettv->v_type = VAR_STRING;
1781}
1782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001783/*
1784 * Find a buffer by number or exact name.
1785 */
1786 static buf_T *
1787find_buffer(typval_T *avar)
1788{
1789 buf_T *buf = NULL;
1790
1791 if (avar->v_type == VAR_NUMBER)
1792 buf = buflist_findnr((int)avar->vval.v_number);
1793 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1794 {
1795 buf = buflist_findname_exp(avar->vval.v_string);
1796 if (buf == NULL)
1797 {
1798 /* No full path name match, try a match with a URL or a "nofile"
1799 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001800 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001801 if (buf->b_fname != NULL
1802 && (path_with_url(buf->b_fname)
1803#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001804 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001805#endif
1806 )
1807 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1808 break;
1809 }
1810 }
1811 return buf;
1812}
1813
1814/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001815 * "bufadd(expr)" function
1816 */
1817 static void
1818f_bufadd(typval_T *argvars, typval_T *rettv)
1819{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001820 char_u *name = tv_get_string(&argvars[0]);
1821
1822 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001823}
1824
1825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001826 * "bufexists(expr)" function
1827 */
1828 static void
1829f_bufexists(typval_T *argvars, typval_T *rettv)
1830{
1831 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1832}
1833
1834/*
1835 * "buflisted(expr)" function
1836 */
1837 static void
1838f_buflisted(typval_T *argvars, typval_T *rettv)
1839{
1840 buf_T *buf;
1841
1842 buf = find_buffer(&argvars[0]);
1843 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1844}
1845
1846/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001847 * "bufload(expr)" function
1848 */
1849 static void
1850f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1851{
1852 buf_T *buf = get_buf_arg(&argvars[0]);
1853
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001854 if (buf != NULL)
1855 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001856}
1857
1858/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001859 * "bufloaded(expr)" function
1860 */
1861 static void
1862f_bufloaded(typval_T *argvars, typval_T *rettv)
1863{
1864 buf_T *buf;
1865
1866 buf = find_buffer(&argvars[0]);
1867 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1868}
1869
1870 buf_T *
1871buflist_find_by_name(char_u *name, int curtab_only)
1872{
1873 int save_magic;
1874 char_u *save_cpo;
1875 buf_T *buf;
1876
1877 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1878 save_magic = p_magic;
1879 p_magic = TRUE;
1880 save_cpo = p_cpo;
1881 p_cpo = (char_u *)"";
1882
1883 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1884 TRUE, FALSE, curtab_only));
1885
1886 p_magic = save_magic;
1887 p_cpo = save_cpo;
1888 return buf;
1889}
1890
1891/*
1892 * Get buffer by number or pattern.
1893 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001894 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001895tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896{
1897 char_u *name = tv->vval.v_string;
1898 buf_T *buf;
1899
1900 if (tv->v_type == VAR_NUMBER)
1901 return buflist_findnr((int)tv->vval.v_number);
1902 if (tv->v_type != VAR_STRING)
1903 return NULL;
1904 if (name == NULL || *name == NUL)
1905 return curbuf;
1906 if (name[0] == '$' && name[1] == NUL)
1907 return lastbuf;
1908
1909 buf = buflist_find_by_name(name, curtab_only);
1910
1911 /* If not found, try expanding the name, like done for bufexists(). */
1912 if (buf == NULL)
1913 buf = find_buffer(tv);
1914
1915 return buf;
1916}
1917
1918/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001919 * Get the buffer from "arg" and give an error and return NULL if it is not
1920 * valid.
1921 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001922 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001923get_buf_arg(typval_T *arg)
1924{
1925 buf_T *buf;
1926
1927 ++emsg_off;
1928 buf = tv_get_buf(arg, FALSE);
1929 --emsg_off;
1930 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001931 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001932 return buf;
1933}
1934
1935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936 * "bufname(expr)" function
1937 */
1938 static void
1939f_bufname(typval_T *argvars, typval_T *rettv)
1940{
1941 buf_T *buf;
1942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001943 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001944 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001945 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001946 rettv->v_type = VAR_STRING;
1947 if (buf != NULL && buf->b_fname != NULL)
1948 rettv->vval.v_string = vim_strsave(buf->b_fname);
1949 else
1950 rettv->vval.v_string = NULL;
1951 --emsg_off;
1952}
1953
1954/*
1955 * "bufnr(expr)" function
1956 */
1957 static void
1958f_bufnr(typval_T *argvars, typval_T *rettv)
1959{
1960 buf_T *buf;
1961 int error = FALSE;
1962 char_u *name;
1963
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001964 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001965 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001966 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967 --emsg_off;
1968
1969 /* If the buffer isn't found and the second argument is not zero create a
1970 * new buffer. */
1971 if (buf == NULL
1972 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001975 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 && !error)
1977 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1978
1979 if (buf != NULL)
1980 rettv->vval.v_number = buf->b_fnum;
1981 else
1982 rettv->vval.v_number = -1;
1983}
1984
1985 static void
1986buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1987{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 win_T *wp;
1989 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990 buf_T *buf;
1991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001992 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001994 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001995 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001996 {
1997 ++winnr;
1998 if (wp->w_buffer == buf)
1999 break;
2000 }
2001 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 --emsg_off;
2003}
2004
2005/*
2006 * "bufwinid(nr)" function
2007 */
2008 static void
2009f_bufwinid(typval_T *argvars, typval_T *rettv)
2010{
2011 buf_win_common(argvars, rettv, FALSE);
2012}
2013
2014/*
2015 * "bufwinnr(nr)" function
2016 */
2017 static void
2018f_bufwinnr(typval_T *argvars, typval_T *rettv)
2019{
2020 buf_win_common(argvars, rettv, TRUE);
2021}
2022
2023/*
2024 * "byte2line(byte)" function
2025 */
2026 static void
2027f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2028{
2029#ifndef FEAT_BYTEOFF
2030 rettv->vval.v_number = -1;
2031#else
2032 long boff = 0;
2033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002034 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 if (boff < 0)
2036 rettv->vval.v_number = -1;
2037 else
2038 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2039 (linenr_T)0, &boff);
2040#endif
2041}
2042
2043 static void
2044byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2045{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 char_u *str;
2048 varnumber_T idx;
2049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002050 str = tv_get_string_chk(&argvars[0]);
2051 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 rettv->vval.v_number = -1;
2053 if (str == NULL || idx < 0)
2054 return;
2055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 t = str;
2057 for ( ; idx > 0; idx--)
2058 {
2059 if (*t == NUL) /* EOL reached */
2060 return;
2061 if (enc_utf8 && comp)
2062 t += utf_ptr2len(t);
2063 else
2064 t += (*mb_ptr2len)(t);
2065 }
2066 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067}
2068
2069/*
2070 * "byteidx()" function
2071 */
2072 static void
2073f_byteidx(typval_T *argvars, typval_T *rettv)
2074{
2075 byteidx(argvars, rettv, FALSE);
2076}
2077
2078/*
2079 * "byteidxcomp()" function
2080 */
2081 static void
2082f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2083{
2084 byteidx(argvars, rettv, TRUE);
2085}
2086
2087/*
2088 * "call(func, arglist [, dict])" function
2089 */
2090 static void
2091f_call(typval_T *argvars, typval_T *rettv)
2092{
2093 char_u *func;
2094 partial_T *partial = NULL;
2095 dict_T *selfdict = NULL;
2096
2097 if (argvars[1].v_type != VAR_LIST)
2098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002099 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 return;
2101 }
2102 if (argvars[1].vval.v_list == NULL)
2103 return;
2104
2105 if (argvars[0].v_type == VAR_FUNC)
2106 func = argvars[0].vval.v_string;
2107 else if (argvars[0].v_type == VAR_PARTIAL)
2108 {
2109 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002110 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 }
2112 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002113 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 if (*func == NUL)
2115 return; /* type error or empty name */
2116
2117 if (argvars[2].v_type != VAR_UNKNOWN)
2118 {
2119 if (argvars[2].v_type != VAR_DICT)
2120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002122 return;
2123 }
2124 selfdict = argvars[2].vval.v_dict;
2125 }
2126
2127 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2128}
2129
2130#ifdef FEAT_FLOAT
2131/*
2132 * "ceil({float})" function
2133 */
2134 static void
2135f_ceil(typval_T *argvars, typval_T *rettv)
2136{
2137 float_T f = 0.0;
2138
2139 rettv->v_type = VAR_FLOAT;
2140 if (get_float_arg(argvars, &f) == OK)
2141 rettv->vval.v_float = ceil(f);
2142 else
2143 rettv->vval.v_float = 0.0;
2144}
2145#endif
2146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147/*
2148 * "changenr()" function
2149 */
2150 static void
2151f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2152{
2153 rettv->vval.v_number = curbuf->b_u_seq_cur;
2154}
2155
2156/*
2157 * "char2nr(string)" function
2158 */
2159 static void
2160f_char2nr(typval_T *argvars, typval_T *rettv)
2161{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 if (has_mbyte)
2163 {
2164 int utf8 = 0;
2165
2166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002167 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168
2169 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002170 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002172 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 }
2174 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002175 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176}
2177
2178/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002179 * "chdir(dir)" function
2180 */
2181 static void
2182f_chdir(typval_T *argvars, typval_T *rettv)
2183{
2184 char_u *cwd;
2185 cdscope_T scope = CDSCOPE_GLOBAL;
2186
2187 rettv->v_type = VAR_STRING;
2188 rettv->vval.v_string = NULL;
2189
2190 if (argvars[0].v_type != VAR_STRING)
2191 return;
2192
2193 // Return the current directory
2194 cwd = alloc(MAXPATHL);
2195 if (cwd != NULL)
2196 {
2197 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2198 {
2199#ifdef BACKSLASH_IN_FILENAME
2200 slash_adjust(cwd);
2201#endif
2202 rettv->vval.v_string = vim_strsave(cwd);
2203 }
2204 vim_free(cwd);
2205 }
2206
2207 if (curwin->w_localdir != NULL)
2208 scope = CDSCOPE_WINDOW;
2209 else if (curtab->tp_localdir != NULL)
2210 scope = CDSCOPE_TABPAGE;
2211
2212 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2213 // Directory change failed
2214 VIM_CLEAR(rettv->vval.v_string);
2215}
2216
2217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 * "cindent(lnum)" function
2219 */
2220 static void
2221f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2222{
2223#ifdef FEAT_CINDENT
2224 pos_T pos;
2225 linenr_T lnum;
2226
2227 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002228 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2230 {
2231 curwin->w_cursor.lnum = lnum;
2232 rettv->vval.v_number = get_c_indent();
2233 curwin->w_cursor = pos;
2234 }
2235 else
2236#endif
2237 rettv->vval.v_number = -1;
2238}
2239
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002240 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002241get_optional_window(typval_T *argvars, int idx)
2242{
2243 win_T *win = curwin;
2244
2245 if (argvars[idx].v_type != VAR_UNKNOWN)
2246 {
2247 win = find_win_by_nr_or_id(&argvars[idx]);
2248 if (win == NULL)
2249 {
2250 emsg(_(e_invalwindow));
2251 return NULL;
2252 }
2253 }
2254 return win;
2255}
2256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002257/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 * "col(string)" function
2259 */
2260 static void
2261f_col(typval_T *argvars, typval_T *rettv)
2262{
2263 colnr_T col = 0;
2264 pos_T *fp;
2265 int fnum = curbuf->b_fnum;
2266
2267 fp = var2fpos(&argvars[0], FALSE, &fnum);
2268 if (fp != NULL && fnum == curbuf->b_fnum)
2269 {
2270 if (fp->col == MAXCOL)
2271 {
2272 /* '> can be MAXCOL, get the length of the line then */
2273 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2274 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2275 else
2276 col = MAXCOL;
2277 }
2278 else
2279 {
2280 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 /* col(".") when the cursor is on the NUL at the end of the line
2282 * because of "coladd" can be seen as an extra column. */
2283 if (virtual_active() && fp == &curwin->w_cursor)
2284 {
2285 char_u *p = ml_get_cursor();
2286
2287 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2288 curwin->w_virtcol - curwin->w_cursor.coladd))
2289 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002290 int l;
2291
2292 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2293 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294 }
2295 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 }
2297 }
2298 rettv->vval.v_number = col;
2299}
2300
2301#if defined(FEAT_INS_EXPAND)
2302/*
2303 * "complete()" function
2304 */
2305 static void
2306f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2307{
2308 int startcol;
2309
2310 if ((State & INSERT) == 0)
2311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002312 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 return;
2314 }
2315
2316 /* Check for undo allowed here, because if something was already inserted
2317 * the line was already saved for undo and this check isn't done. */
2318 if (!undo_allowed())
2319 return;
2320
2321 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002324 return;
2325 }
2326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002327 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002328 if (startcol <= 0)
2329 return;
2330
2331 set_completion(startcol - 1, argvars[1].vval.v_list);
2332}
2333
2334/*
2335 * "complete_add()" function
2336 */
2337 static void
2338f_complete_add(typval_T *argvars, typval_T *rettv)
2339{
2340 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2341}
2342
2343/*
2344 * "complete_check()" function
2345 */
2346 static void
2347f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2348{
2349 int saved = RedrawingDisabled;
2350
2351 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002352 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002353 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 RedrawingDisabled = saved;
2355}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002356
2357/*
2358 * "complete_info()" function
2359 */
2360 static void
2361f_complete_info(typval_T *argvars, typval_T *rettv)
2362{
2363 list_T *what_list = NULL;
2364
2365 if (rettv_dict_alloc(rettv) != OK)
2366 return;
2367
2368 if (argvars[0].v_type != VAR_UNKNOWN)
2369 {
2370 if (argvars[0].v_type != VAR_LIST)
2371 {
2372 emsg(_(e_listreq));
2373 return;
2374 }
2375 what_list = argvars[0].vval.v_list;
2376 }
2377 get_complete_info(what_list, rettv->vval.v_dict);
2378}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379#endif
2380
2381/*
2382 * "confirm(message, buttons[, default [, type]])" function
2383 */
2384 static void
2385f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2386{
2387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2388 char_u *message;
2389 char_u *buttons = NULL;
2390 char_u buf[NUMBUFLEN];
2391 char_u buf2[NUMBUFLEN];
2392 int def = 1;
2393 int type = VIM_GENERIC;
2394 char_u *typestr;
2395 int error = FALSE;
2396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002397 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 if (message == NULL)
2399 error = TRUE;
2400 if (argvars[1].v_type != VAR_UNKNOWN)
2401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002402 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403 if (buttons == NULL)
2404 error = TRUE;
2405 if (argvars[2].v_type != VAR_UNKNOWN)
2406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002407 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 if (argvars[3].v_type != VAR_UNKNOWN)
2409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 if (typestr == NULL)
2412 error = TRUE;
2413 else
2414 {
2415 switch (TOUPPER_ASC(*typestr))
2416 {
2417 case 'E': type = VIM_ERROR; break;
2418 case 'Q': type = VIM_QUESTION; break;
2419 case 'I': type = VIM_INFO; break;
2420 case 'W': type = VIM_WARNING; break;
2421 case 'G': type = VIM_GENERIC; break;
2422 }
2423 }
2424 }
2425 }
2426 }
2427
2428 if (buttons == NULL || *buttons == NUL)
2429 buttons = (char_u *)_("&Ok");
2430
2431 if (!error)
2432 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2433 def, NULL, FALSE);
2434#endif
2435}
2436
2437/*
2438 * "copy()" function
2439 */
2440 static void
2441f_copy(typval_T *argvars, typval_T *rettv)
2442{
2443 item_copy(&argvars[0], rettv, FALSE, 0);
2444}
2445
2446#ifdef FEAT_FLOAT
2447/*
2448 * "cos()" function
2449 */
2450 static void
2451f_cos(typval_T *argvars, typval_T *rettv)
2452{
2453 float_T f = 0.0;
2454
2455 rettv->v_type = VAR_FLOAT;
2456 if (get_float_arg(argvars, &f) == OK)
2457 rettv->vval.v_float = cos(f);
2458 else
2459 rettv->vval.v_float = 0.0;
2460}
2461
2462/*
2463 * "cosh()" function
2464 */
2465 static void
2466f_cosh(typval_T *argvars, typval_T *rettv)
2467{
2468 float_T f = 0.0;
2469
2470 rettv->v_type = VAR_FLOAT;
2471 if (get_float_arg(argvars, &f) == OK)
2472 rettv->vval.v_float = cosh(f);
2473 else
2474 rettv->vval.v_float = 0.0;
2475}
2476#endif
2477
2478/*
2479 * "count()" function
2480 */
2481 static void
2482f_count(typval_T *argvars, typval_T *rettv)
2483{
2484 long n = 0;
2485 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002486 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487
Bram Moolenaar9966b212017-07-28 16:46:57 +02002488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002489 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002490
2491 if (argvars[0].v_type == VAR_STRING)
2492 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002493 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002494 char_u *p = argvars[0].vval.v_string;
2495 char_u *next;
2496
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002497 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002498 {
2499 if (ic)
2500 {
2501 size_t len = STRLEN(expr);
2502
2503 while (*p != NUL)
2504 {
2505 if (MB_STRNICMP(p, expr, len) == 0)
2506 {
2507 ++n;
2508 p += len;
2509 }
2510 else
2511 MB_PTR_ADV(p);
2512 }
2513 }
2514 else
2515 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2516 != NULL)
2517 {
2518 ++n;
2519 p = next + STRLEN(expr);
2520 }
2521 }
2522
2523 }
2524 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 {
2526 listitem_T *li;
2527 list_T *l;
2528 long idx;
2529
2530 if ((l = argvars[0].vval.v_list) != NULL)
2531 {
2532 li = l->lv_first;
2533 if (argvars[2].v_type != VAR_UNKNOWN)
2534 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 if (argvars[3].v_type != VAR_UNKNOWN)
2536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002537 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538 if (!error)
2539 {
2540 li = list_find(l, idx);
2541 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002542 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 }
2544 }
2545 if (error)
2546 li = NULL;
2547 }
2548
2549 for ( ; li != NULL; li = li->li_next)
2550 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2551 ++n;
2552 }
2553 }
2554 else if (argvars[0].v_type == VAR_DICT)
2555 {
2556 int todo;
2557 dict_T *d;
2558 hashitem_T *hi;
2559
2560 if ((d = argvars[0].vval.v_dict) != NULL)
2561 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 if (argvars[2].v_type != VAR_UNKNOWN)
2563 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002564 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002566 }
2567
2568 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2569 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2570 {
2571 if (!HASHITEM_EMPTY(hi))
2572 {
2573 --todo;
2574 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2575 ++n;
2576 }
2577 }
2578 }
2579 }
2580 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 rettv->vval.v_number = n;
2583}
2584
2585/*
2586 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2587 *
2588 * Checks the existence of a cscope connection.
2589 */
2590 static void
2591f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2592{
2593#ifdef FEAT_CSCOPE
2594 int num = 0;
2595 char_u *dbpath = NULL;
2596 char_u *prepend = NULL;
2597 char_u buf[NUMBUFLEN];
2598
2599 if (argvars[0].v_type != VAR_UNKNOWN
2600 && argvars[1].v_type != VAR_UNKNOWN)
2601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002602 num = (int)tv_get_number(&argvars[0]);
2603 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002605 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 }
2607
2608 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2609#endif
2610}
2611
2612/*
2613 * "cursor(lnum, col)" function, or
2614 * "cursor(list)"
2615 *
2616 * Moves the cursor to the specified line and column.
2617 * Returns 0 when the position could be set, -1 otherwise.
2618 */
2619 static void
2620f_cursor(typval_T *argvars, typval_T *rettv)
2621{
2622 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002623 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 int set_curswant = TRUE;
2625
2626 rettv->vval.v_number = -1;
2627 if (argvars[1].v_type == VAR_UNKNOWN)
2628 {
2629 pos_T pos;
2630 colnr_T curswant = -1;
2631
2632 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002634 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002635 return;
2636 }
2637 line = pos.lnum;
2638 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002640 if (curswant >= 0)
2641 {
2642 curwin->w_curswant = curswant - 1;
2643 set_curswant = FALSE;
2644 }
2645 }
2646 else
2647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002648 line = tv_get_lnum(argvars);
2649 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002651 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002652 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002653 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002654 return; /* type error; errmsg already given */
2655 if (line > 0)
2656 curwin->w_cursor.lnum = line;
2657 if (col > 0)
2658 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660
2661 /* Make sure the cursor is in a valid position. */
2662 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 /* Correct cursor for multi-byte character. */
2664 if (has_mbyte)
2665 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666
2667 curwin->w_set_curswant = set_curswant;
2668 rettv->vval.v_number = 0;
2669}
2670
Bram Moolenaar4f974752019-02-17 17:44:42 +01002671#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002672/*
2673 * "debugbreak()" function
2674 */
2675 static void
2676f_debugbreak(typval_T *argvars, typval_T *rettv)
2677{
2678 int pid;
2679
2680 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002682 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002683 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002684 else
2685 {
2686 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2687
2688 if (hProcess != NULL)
2689 {
2690 DebugBreakProcess(hProcess);
2691 CloseHandle(hProcess);
2692 rettv->vval.v_number = OK;
2693 }
2694 }
2695}
2696#endif
2697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698/*
2699 * "deepcopy()" function
2700 */
2701 static void
2702f_deepcopy(typval_T *argvars, typval_T *rettv)
2703{
2704 int noref = 0;
2705 int copyID;
2706
2707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002708 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002710 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 else
2712 {
2713 copyID = get_copyID();
2714 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2715 }
2716}
2717
2718/*
2719 * "delete()" function
2720 */
2721 static void
2722f_delete(typval_T *argvars, typval_T *rettv)
2723{
2724 char_u nbuf[NUMBUFLEN];
2725 char_u *name;
2726 char_u *flags;
2727
2728 rettv->vval.v_number = -1;
2729 if (check_restricted() || check_secure())
2730 return;
2731
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002732 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 if (name == NULL || *name == NUL)
2734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002735 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002736 return;
2737 }
2738
2739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002740 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002741 else
2742 flags = (char_u *)"";
2743
2744 if (*flags == NUL)
2745 /* delete a file */
2746 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2747 else if (STRCMP(flags, "d") == 0)
2748 /* delete an empty directory */
2749 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2750 else if (STRCMP(flags, "rf") == 0)
2751 /* delete a directory recursively */
2752 rettv->vval.v_number = delete_recursive(name);
2753 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002754 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755}
2756
2757/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002758 * "deletebufline()" function
2759 */
2760 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002761f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002762{
2763 buf_T *buf;
2764 linenr_T first, last;
2765 linenr_T lnum;
2766 long count;
2767 int is_curbuf;
2768 buf_T *curbuf_save = NULL;
2769 win_T *curwin_save = NULL;
2770 tabpage_T *tp;
2771 win_T *wp;
2772
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002773 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002774 if (buf == NULL)
2775 {
2776 rettv->vval.v_number = 1; /* FAIL */
2777 return;
2778 }
2779 is_curbuf = buf == curbuf;
2780
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002783 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002784 else
2785 last = first;
2786
2787 if (buf->b_ml.ml_mfp == NULL || first < 1
2788 || first > buf->b_ml.ml_line_count || last < first)
2789 {
2790 rettv->vval.v_number = 1; /* FAIL */
2791 return;
2792 }
2793
2794 if (!is_curbuf)
2795 {
2796 curbuf_save = curbuf;
2797 curwin_save = curwin;
2798 curbuf = buf;
2799 find_win_for_curbuf();
2800 }
2801 if (last > curbuf->b_ml.ml_line_count)
2802 last = curbuf->b_ml.ml_line_count;
2803 count = last - first + 1;
2804
2805 // When coming here from Insert mode, sync undo, so that this can be
2806 // undone separately from what was previously inserted.
2807 if (u_sync_once == 2)
2808 {
2809 u_sync_once = 1; // notify that u_sync() was called
2810 u_sync(TRUE);
2811 }
2812
2813 if (u_save(first - 1, last + 1) == FAIL)
2814 {
2815 rettv->vval.v_number = 1; /* FAIL */
2816 return;
2817 }
2818
2819 for (lnum = first; lnum <= last; ++lnum)
2820 ml_delete(first, TRUE);
2821
2822 FOR_ALL_TAB_WINDOWS(tp, wp)
2823 if (wp->w_buffer == buf)
2824 {
2825 if (wp->w_cursor.lnum > last)
2826 wp->w_cursor.lnum -= count;
2827 else if (wp->w_cursor.lnum> first)
2828 wp->w_cursor.lnum = first;
2829 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2830 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2831 }
2832 check_cursor_col();
2833 deleted_lines_mark(first, count);
2834
2835 if (!is_curbuf)
2836 {
2837 curbuf = curbuf_save;
2838 curwin = curwin_save;
2839 }
2840}
2841
2842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002843 * "did_filetype()" function
2844 */
2845 static void
2846f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2847{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002849}
2850
2851/*
2852 * "diff_filler()" function
2853 */
2854 static void
2855f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2856{
2857#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002858 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859#endif
2860}
2861
2862/*
2863 * "diff_hlID()" function
2864 */
2865 static void
2866f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2867{
2868#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002869 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002871 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872 static int fnum = 0;
2873 static int change_start = 0;
2874 static int change_end = 0;
2875 static hlf_T hlID = (hlf_T)0;
2876 int filler_lines;
2877 int col;
2878
2879 if (lnum < 0) /* ignore type error in {lnum} arg */
2880 lnum = 0;
2881 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002882 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002883 || fnum != curbuf->b_fnum)
2884 {
2885 /* New line, buffer, change: need to get the values. */
2886 filler_lines = diff_check(curwin, lnum);
2887 if (filler_lines < 0)
2888 {
2889 if (filler_lines == -1)
2890 {
2891 change_start = MAXCOL;
2892 change_end = -1;
2893 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2894 hlID = HLF_ADD; /* added line */
2895 else
2896 hlID = HLF_CHD; /* changed line */
2897 }
2898 else
2899 hlID = HLF_ADD; /* added line */
2900 }
2901 else
2902 hlID = (hlf_T)0;
2903 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002904 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 fnum = curbuf->b_fnum;
2906 }
2907
2908 if (hlID == HLF_CHD || hlID == HLF_TXD)
2909 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002910 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 if (col >= change_start && col <= change_end)
2912 hlID = HLF_TXD; /* changed text */
2913 else
2914 hlID = HLF_CHD; /* changed line */
2915 }
2916 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2917#endif
2918}
2919
2920/*
2921 * "empty({expr})" function
2922 */
2923 static void
2924f_empty(typval_T *argvars, typval_T *rettv)
2925{
2926 int n = FALSE;
2927
2928 switch (argvars[0].v_type)
2929 {
2930 case VAR_STRING:
2931 case VAR_FUNC:
2932 n = argvars[0].vval.v_string == NULL
2933 || *argvars[0].vval.v_string == NUL;
2934 break;
2935 case VAR_PARTIAL:
2936 n = FALSE;
2937 break;
2938 case VAR_NUMBER:
2939 n = argvars[0].vval.v_number == 0;
2940 break;
2941 case VAR_FLOAT:
2942#ifdef FEAT_FLOAT
2943 n = argvars[0].vval.v_float == 0.0;
2944 break;
2945#endif
2946 case VAR_LIST:
2947 n = argvars[0].vval.v_list == NULL
2948 || argvars[0].vval.v_list->lv_first == NULL;
2949 break;
2950 case VAR_DICT:
2951 n = argvars[0].vval.v_dict == NULL
2952 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2953 break;
2954 case VAR_SPECIAL:
2955 n = argvars[0].vval.v_number != VVAL_TRUE;
2956 break;
2957
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002958 case VAR_BLOB:
2959 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002960 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2961 break;
2962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 case VAR_JOB:
2964#ifdef FEAT_JOB_CHANNEL
2965 n = argvars[0].vval.v_job == NULL
2966 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2967 break;
2968#endif
2969 case VAR_CHANNEL:
2970#ifdef FEAT_JOB_CHANNEL
2971 n = argvars[0].vval.v_channel == NULL
2972 || !channel_is_open(argvars[0].vval.v_channel);
2973 break;
2974#endif
2975 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002976 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 n = TRUE;
2978 break;
2979 }
2980
2981 rettv->vval.v_number = n;
2982}
2983
2984/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002985 * "environ()" function
2986 */
2987 static void
2988f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2989{
2990#if !defined(AMIGA)
2991 int i = 0;
2992 char_u *entry, *value;
2993# ifdef MSWIN
2994 extern wchar_t **_wenviron;
2995# else
2996 extern char **environ;
2997# endif
2998
2999 if (rettv_dict_alloc(rettv) != OK)
3000 return;
3001
3002# ifdef MSWIN
3003 if (*_wenviron == NULL)
3004 return;
3005# else
3006 if (*environ == NULL)
3007 return;
3008# endif
3009
3010 for (i = 0; ; ++i)
3011 {
3012# ifdef MSWIN
3013 short_u *p;
3014
3015 if ((p = (short_u *)_wenviron[i]) == NULL)
3016 return;
3017 entry = utf16_to_enc(p, NULL);
3018# else
3019 if ((entry = (char_u *)environ[i]) == NULL)
3020 return;
3021 entry = vim_strsave(entry);
3022# endif
3023 if (entry == NULL) // out of memory
3024 return;
3025 if ((value = vim_strchr(entry, '=')) == NULL)
3026 {
3027 vim_free(entry);
3028 continue;
3029 }
3030 *value++ = NUL;
3031 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3032 vim_free(entry);
3033 }
3034#endif
3035}
3036
3037/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 * "escape({string}, {chars})" function
3039 */
3040 static void
3041f_escape(typval_T *argvars, typval_T *rettv)
3042{
3043 char_u buf[NUMBUFLEN];
3044
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003045 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3046 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 rettv->v_type = VAR_STRING;
3048}
3049
3050/*
3051 * "eval()" function
3052 */
3053 static void
3054f_eval(typval_T *argvars, typval_T *rettv)
3055{
3056 char_u *s, *p;
3057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003058 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 if (s != NULL)
3060 s = skipwhite(s);
3061
3062 p = s;
3063 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3064 {
3065 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003066 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003067 need_clr_eos = FALSE;
3068 rettv->v_type = VAR_NUMBER;
3069 rettv->vval.v_number = 0;
3070 }
3071 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003072 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073}
3074
3075/*
3076 * "eventhandler()" function
3077 */
3078 static void
3079f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3080{
3081 rettv->vval.v_number = vgetc_busy;
3082}
3083
3084/*
3085 * "executable()" function
3086 */
3087 static void
3088f_executable(typval_T *argvars, typval_T *rettv)
3089{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091
3092 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003093 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094}
3095
3096static garray_T redir_execute_ga;
3097
3098/*
3099 * Append "value[value_len]" to the execute() output.
3100 */
3101 void
3102execute_redir_str(char_u *value, int value_len)
3103{
3104 int len;
3105
3106 if (value_len == -1)
3107 len = (int)STRLEN(value); /* Append the entire string */
3108 else
3109 len = value_len; /* Append only "value_len" characters */
3110 if (ga_grow(&redir_execute_ga, len) == OK)
3111 {
3112 mch_memmove((char *)redir_execute_ga.ga_data
3113 + redir_execute_ga.ga_len, value, len);
3114 redir_execute_ga.ga_len += len;
3115 }
3116}
3117
3118/*
3119 * Get next line from a list.
3120 * Called by do_cmdline() to get the next line.
3121 * Returns allocated string, or NULL for end of function.
3122 */
3123
3124 static char_u *
3125get_list_line(
3126 int c UNUSED,
3127 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003128 int indent UNUSED,
3129 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130{
3131 listitem_T **p = (listitem_T **)cookie;
3132 listitem_T *item = *p;
3133 char_u buf[NUMBUFLEN];
3134 char_u *s;
3135
3136 if (item == NULL)
3137 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003138 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 *p = item->li_next;
3140 return s == NULL ? NULL : vim_strsave(s);
3141}
3142
3143/*
3144 * "execute()" function
3145 */
3146 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003147execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148{
3149 char_u *cmd = NULL;
3150 list_T *list = NULL;
3151 int save_msg_silent = msg_silent;
3152 int save_emsg_silent = emsg_silent;
3153 int save_emsg_noredir = emsg_noredir;
3154 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003155 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003157 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003158 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003159
3160 rettv->vval.v_string = NULL;
3161 rettv->v_type = VAR_STRING;
3162
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003163 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003165 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 if (list == NULL || list->lv_first == NULL)
3167 /* empty list, no commands, empty output */
3168 return;
3169 ++list->lv_refcount;
3170 }
3171 else
3172 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003173 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 if (cmd == NULL)
3175 return;
3176 }
3177
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003178 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 {
3180 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003181 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182
3183 if (s == NULL)
3184 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003185 if (*s == NUL)
3186 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 if (STRNCMP(s, "silent", 6) == 0)
3188 ++msg_silent;
3189 if (STRCMP(s, "silent!") == 0)
3190 {
3191 emsg_silent = TRUE;
3192 emsg_noredir = TRUE;
3193 }
3194 }
3195 else
3196 ++msg_silent;
3197
3198 if (redir_execute)
3199 save_ga = redir_execute_ga;
3200 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3201 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003202 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003203 if (!echo_output)
3204 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205
3206 if (cmd != NULL)
3207 do_cmdline_cmd(cmd);
3208 else
3209 {
3210 listitem_T *item = list->lv_first;
3211
3212 do_cmdline(NULL, get_list_line, (void *)&item,
3213 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3214 --list->lv_refcount;
3215 }
3216
Bram Moolenaard297f352017-01-29 20:31:21 +01003217 /* Need to append a NUL to the result. */
3218 if (ga_grow(&redir_execute_ga, 1) == OK)
3219 {
3220 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3221 rettv->vval.v_string = redir_execute_ga.ga_data;
3222 }
3223 else
3224 {
3225 ga_clear(&redir_execute_ga);
3226 rettv->vval.v_string = NULL;
3227 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003228 msg_silent = save_msg_silent;
3229 emsg_silent = save_emsg_silent;
3230 emsg_noredir = save_emsg_noredir;
3231
3232 redir_execute = save_redir_execute;
3233 if (redir_execute)
3234 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003235 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003237 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003238 if (echo_output)
3239 // When not working silently: put it in column zero. A following
3240 // "echon" will overwrite the message, unavoidably.
3241 msg_col = 0;
3242 else
3243 // When working silently: Put it back where it was, since nothing
3244 // should have been written.
3245 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246}
3247
3248/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003249 * "execute()" function
3250 */
3251 static void
3252f_execute(typval_T *argvars, typval_T *rettv)
3253{
3254 execute_common(argvars, rettv, 0);
3255}
3256
3257/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 * "exepath()" function
3259 */
3260 static void
3261f_exepath(typval_T *argvars, typval_T *rettv)
3262{
3263 char_u *p = NULL;
3264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003265 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 rettv->v_type = VAR_STRING;
3267 rettv->vval.v_string = p;
3268}
3269
3270/*
3271 * "exists()" function
3272 */
3273 static void
3274f_exists(typval_T *argvars, typval_T *rettv)
3275{
3276 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003279 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 if (*p == '$') /* environment variable */
3281 {
3282 /* first try "normal" environment variables (fast) */
3283 if (mch_getenv(p + 1) != NULL)
3284 n = TRUE;
3285 else
3286 {
3287 /* try expanding things like $VIM and ${HOME} */
3288 p = expand_env_save(p);
3289 if (p != NULL && *p != '$')
3290 n = TRUE;
3291 vim_free(p);
3292 }
3293 }
3294 else if (*p == '&' || *p == '+') /* option */
3295 {
3296 n = (get_option_tv(&p, NULL, TRUE) == OK);
3297 if (*skipwhite(p) != NUL)
3298 n = FALSE; /* trailing garbage */
3299 }
3300 else if (*p == '*') /* internal or user defined function */
3301 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003302 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 }
3304 else if (*p == ':')
3305 {
3306 n = cmd_exists(p + 1);
3307 }
3308 else if (*p == '#')
3309 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (p[1] == '#')
3311 n = autocmd_supported(p + 2);
3312 else
3313 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 }
3315 else /* internal variable */
3316 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003317 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318 }
3319
3320 rettv->vval.v_number = n;
3321}
3322
3323#ifdef FEAT_FLOAT
3324/*
3325 * "exp()" function
3326 */
3327 static void
3328f_exp(typval_T *argvars, typval_T *rettv)
3329{
3330 float_T f = 0.0;
3331
3332 rettv->v_type = VAR_FLOAT;
3333 if (get_float_arg(argvars, &f) == OK)
3334 rettv->vval.v_float = exp(f);
3335 else
3336 rettv->vval.v_float = 0.0;
3337}
3338#endif
3339
3340/*
3341 * "expand()" function
3342 */
3343 static void
3344f_expand(typval_T *argvars, typval_T *rettv)
3345{
3346 char_u *s;
3347 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003348 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3350 expand_T xpc;
3351 int error = FALSE;
3352 char_u *result;
3353
3354 rettv->v_type = VAR_STRING;
3355 if (argvars[1].v_type != VAR_UNKNOWN
3356 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003357 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003358 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003359 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003361 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362 if (*s == '%' || *s == '#' || *s == '<')
3363 {
3364 ++emsg_off;
3365 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3366 --emsg_off;
3367 if (rettv->v_type == VAR_LIST)
3368 {
3369 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3370 list_append_string(rettv->vval.v_list, result, -1);
3371 else
3372 vim_free(result);
3373 }
3374 else
3375 rettv->vval.v_string = result;
3376 }
3377 else
3378 {
3379 /* When the optional second argument is non-zero, don't remove matches
3380 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3381 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003382 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003383 options |= WILD_KEEP_ALL;
3384 if (!error)
3385 {
3386 ExpandInit(&xpc);
3387 xpc.xp_context = EXPAND_FILES;
3388 if (p_wic)
3389 options += WILD_ICASE;
3390 if (rettv->v_type == VAR_STRING)
3391 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3392 options, WILD_ALL);
3393 else if (rettv_list_alloc(rettv) != FAIL)
3394 {
3395 int i;
3396
3397 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3398 for (i = 0; i < xpc.xp_numfiles; i++)
3399 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3400 ExpandCleanup(&xpc);
3401 }
3402 }
3403 else
3404 rettv->vval.v_string = NULL;
3405 }
3406}
3407
3408/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003409 * "expandcmd()" function
3410 * Expand all the special characters in a command string.
3411 */
3412 static void
3413f_expandcmd(typval_T *argvars, typval_T *rettv)
3414{
3415 exarg_T eap;
3416 char_u *cmdstr;
3417 char *errormsg = NULL;
3418
3419 rettv->v_type = VAR_STRING;
3420 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3421
3422 memset(&eap, 0, sizeof(eap));
3423 eap.cmd = cmdstr;
3424 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003425 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003426 eap.usefilter = FALSE;
3427 eap.nextcmd = NULL;
3428 eap.cmdidx = CMD_USER;
3429
3430 expand_filename(&eap, &cmdstr, &errormsg);
3431 if (errormsg != NULL && *errormsg != NUL)
3432 emsg(errormsg);
3433
3434 rettv->vval.v_string = cmdstr;
3435}
3436
3437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 * "extend(list, list [, idx])" function
3439 * "extend(dict, dict [, action])" function
3440 */
3441 static void
3442f_extend(typval_T *argvars, typval_T *rettv)
3443{
3444 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3445
3446 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3447 {
3448 list_T *l1, *l2;
3449 listitem_T *item;
3450 long before;
3451 int error = FALSE;
3452
3453 l1 = argvars[0].vval.v_list;
3454 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003455 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 && l2 != NULL)
3457 {
3458 if (argvars[2].v_type != VAR_UNKNOWN)
3459 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003460 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461 if (error)
3462 return; /* type error; errmsg already given */
3463
3464 if (before == l1->lv_len)
3465 item = NULL;
3466 else
3467 {
3468 item = list_find(l1, before);
3469 if (item == NULL)
3470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 return;
3473 }
3474 }
3475 }
3476 else
3477 item = NULL;
3478 list_extend(l1, l2, item);
3479
3480 copy_tv(&argvars[0], rettv);
3481 }
3482 }
3483 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3484 {
3485 dict_T *d1, *d2;
3486 char_u *action;
3487 int i;
3488
3489 d1 = argvars[0].vval.v_dict;
3490 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003491 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003492 && d2 != NULL)
3493 {
3494 /* Check the third argument. */
3495 if (argvars[2].v_type != VAR_UNKNOWN)
3496 {
3497 static char *(av[]) = {"keep", "force", "error"};
3498
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003499 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500 if (action == NULL)
3501 return; /* type error; errmsg already given */
3502 for (i = 0; i < 3; ++i)
3503 if (STRCMP(action, av[i]) == 0)
3504 break;
3505 if (i == 3)
3506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003507 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508 return;
3509 }
3510 }
3511 else
3512 action = (char_u *)"force";
3513
3514 dict_extend(d1, d2, action);
3515
3516 copy_tv(&argvars[0], rettv);
3517 }
3518 }
3519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003520 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521}
3522
3523/*
3524 * "feedkeys()" function
3525 */
3526 static void
3527f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3528{
3529 int remap = TRUE;
3530 int insert = FALSE;
3531 char_u *keys, *flags;
3532 char_u nbuf[NUMBUFLEN];
3533 int typed = FALSE;
3534 int execute = FALSE;
3535 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003536 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537 char_u *keys_esc;
3538
3539 /* This is not allowed in the sandbox. If the commands would still be
3540 * executed in the sandbox it would be OK, but it probably happens later,
3541 * when "sandbox" is no longer set. */
3542 if (check_secure())
3543 return;
3544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003545 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546
3547 if (argvars[1].v_type != VAR_UNKNOWN)
3548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003549 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550 for ( ; *flags != NUL; ++flags)
3551 {
3552 switch (*flags)
3553 {
3554 case 'n': remap = FALSE; break;
3555 case 'm': remap = TRUE; break;
3556 case 't': typed = TRUE; break;
3557 case 'i': insert = TRUE; break;
3558 case 'x': execute = TRUE; break;
3559 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003560 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 }
3562 }
3563 }
3564
3565 if (*keys != NUL || execute)
3566 {
3567 /* Need to escape K_SPECIAL and CSI before putting the string in the
3568 * typeahead buffer. */
3569 keys_esc = vim_strsave_escape_csi(keys);
3570 if (keys_esc != NULL)
3571 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003572 if (lowlevel)
3573 {
3574#ifdef USE_INPUT_BUF
3575 add_to_input_buf(keys, (int)STRLEN(keys));
3576#else
3577 emsg(_("E980: lowlevel input not supported"));
3578#endif
3579 }
3580 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003581 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003582 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003584 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003585#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003586 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003587#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003588 )
3589 typebuf_was_filled = TRUE;
3590 }
3591 vim_free(keys_esc);
3592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 if (execute)
3594 {
3595 int save_msg_scroll = msg_scroll;
3596
3597 /* Avoid a 1 second delay when the keys start Insert mode. */
3598 msg_scroll = FALSE;
3599
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003600 if (!dangerous)
3601 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003602 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003603 if (!dangerous)
3604 --ex_normal_busy;
3605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 msg_scroll |= save_msg_scroll;
3607 }
3608 }
3609 }
3610}
3611
3612/*
3613 * "filereadable()" function
3614 */
3615 static void
3616f_filereadable(typval_T *argvars, typval_T *rettv)
3617{
3618 int fd;
3619 char_u *p;
3620 int n;
3621
3622#ifndef O_NONBLOCK
3623# define O_NONBLOCK 0
3624#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003625 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3627 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3628 {
3629 n = TRUE;
3630 close(fd);
3631 }
3632 else
3633 n = FALSE;
3634
3635 rettv->vval.v_number = n;
3636}
3637
3638/*
3639 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3640 * rights to write into.
3641 */
3642 static void
3643f_filewritable(typval_T *argvars, typval_T *rettv)
3644{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003645 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646}
3647
3648 static void
3649findfilendir(
3650 typval_T *argvars UNUSED,
3651 typval_T *rettv,
3652 int find_what UNUSED)
3653{
3654#ifdef FEAT_SEARCHPATH
3655 char_u *fname;
3656 char_u *fresult = NULL;
3657 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3658 char_u *p;
3659 char_u pathbuf[NUMBUFLEN];
3660 int count = 1;
3661 int first = TRUE;
3662 int error = FALSE;
3663#endif
3664
3665 rettv->vval.v_string = NULL;
3666 rettv->v_type = VAR_STRING;
3667
3668#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003669 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003670
3671 if (argvars[1].v_type != VAR_UNKNOWN)
3672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003673 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 if (p == NULL)
3675 error = TRUE;
3676 else
3677 {
3678 if (*p != NUL)
3679 path = p;
3680
3681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003682 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 }
3684 }
3685
3686 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3687 error = TRUE;
3688
3689 if (*fname != NUL && !error)
3690 {
3691 do
3692 {
3693 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3694 vim_free(fresult);
3695 fresult = find_file_in_path_option(first ? fname : NULL,
3696 first ? (int)STRLEN(fname) : 0,
3697 0, first, path,
3698 find_what,
3699 curbuf->b_ffname,
3700 find_what == FINDFILE_DIR
3701 ? (char_u *)"" : curbuf->b_p_sua);
3702 first = FALSE;
3703
3704 if (fresult != NULL && rettv->v_type == VAR_LIST)
3705 list_append_string(rettv->vval.v_list, fresult, -1);
3706
3707 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3708 }
3709
3710 if (rettv->v_type == VAR_STRING)
3711 rettv->vval.v_string = fresult;
3712#endif
3713}
3714
3715/*
3716 * "filter()" function
3717 */
3718 static void
3719f_filter(typval_T *argvars, typval_T *rettv)
3720{
3721 filter_map(argvars, rettv, FALSE);
3722}
3723
3724/*
3725 * "finddir({fname}[, {path}[, {count}]])" function
3726 */
3727 static void
3728f_finddir(typval_T *argvars, typval_T *rettv)
3729{
3730 findfilendir(argvars, rettv, FINDFILE_DIR);
3731}
3732
3733/*
3734 * "findfile({fname}[, {path}[, {count}]])" function
3735 */
3736 static void
3737f_findfile(typval_T *argvars, typval_T *rettv)
3738{
3739 findfilendir(argvars, rettv, FINDFILE_FILE);
3740}
3741
3742#ifdef FEAT_FLOAT
3743/*
3744 * "float2nr({float})" function
3745 */
3746 static void
3747f_float2nr(typval_T *argvars, typval_T *rettv)
3748{
3749 float_T f = 0.0;
3750
3751 if (get_float_arg(argvars, &f) == OK)
3752 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003753 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003754 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003755 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003756 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 else
3758 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003759 }
3760}
3761
3762/*
3763 * "floor({float})" function
3764 */
3765 static void
3766f_floor(typval_T *argvars, typval_T *rettv)
3767{
3768 float_T f = 0.0;
3769
3770 rettv->v_type = VAR_FLOAT;
3771 if (get_float_arg(argvars, &f) == OK)
3772 rettv->vval.v_float = floor(f);
3773 else
3774 rettv->vval.v_float = 0.0;
3775}
3776
3777/*
3778 * "fmod()" function
3779 */
3780 static void
3781f_fmod(typval_T *argvars, typval_T *rettv)
3782{
3783 float_T fx = 0.0, fy = 0.0;
3784
3785 rettv->v_type = VAR_FLOAT;
3786 if (get_float_arg(argvars, &fx) == OK
3787 && get_float_arg(&argvars[1], &fy) == OK)
3788 rettv->vval.v_float = fmod(fx, fy);
3789 else
3790 rettv->vval.v_float = 0.0;
3791}
3792#endif
3793
3794/*
3795 * "fnameescape({string})" function
3796 */
3797 static void
3798f_fnameescape(typval_T *argvars, typval_T *rettv)
3799{
3800 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003801 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 rettv->v_type = VAR_STRING;
3803}
3804
3805/*
3806 * "fnamemodify({fname}, {mods})" function
3807 */
3808 static void
3809f_fnamemodify(typval_T *argvars, typval_T *rettv)
3810{
3811 char_u *fname;
3812 char_u *mods;
3813 int usedlen = 0;
3814 int len;
3815 char_u *fbuf = NULL;
3816 char_u buf[NUMBUFLEN];
3817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003818 fname = tv_get_string_chk(&argvars[0]);
3819 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003820 if (fname == NULL || mods == NULL)
3821 fname = NULL;
3822 else
3823 {
3824 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003825 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 }
3827
3828 rettv->v_type = VAR_STRING;
3829 if (fname == NULL)
3830 rettv->vval.v_string = NULL;
3831 else
3832 rettv->vval.v_string = vim_strnsave(fname, len);
3833 vim_free(fbuf);
3834}
3835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003836/*
3837 * "foldclosed()" function
3838 */
3839 static void
3840foldclosed_both(
3841 typval_T *argvars UNUSED,
3842 typval_T *rettv,
3843 int end UNUSED)
3844{
3845#ifdef FEAT_FOLDING
3846 linenr_T lnum;
3847 linenr_T first, last;
3848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003849 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3851 {
3852 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3853 {
3854 if (end)
3855 rettv->vval.v_number = (varnumber_T)last;
3856 else
3857 rettv->vval.v_number = (varnumber_T)first;
3858 return;
3859 }
3860 }
3861#endif
3862 rettv->vval.v_number = -1;
3863}
3864
3865/*
3866 * "foldclosed()" function
3867 */
3868 static void
3869f_foldclosed(typval_T *argvars, typval_T *rettv)
3870{
3871 foldclosed_both(argvars, rettv, FALSE);
3872}
3873
3874/*
3875 * "foldclosedend()" function
3876 */
3877 static void
3878f_foldclosedend(typval_T *argvars, typval_T *rettv)
3879{
3880 foldclosed_both(argvars, rettv, TRUE);
3881}
3882
3883/*
3884 * "foldlevel()" function
3885 */
3886 static void
3887f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3888{
3889#ifdef FEAT_FOLDING
3890 linenr_T lnum;
3891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003892 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003893 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3894 rettv->vval.v_number = foldLevel(lnum);
3895#endif
3896}
3897
3898/*
3899 * "foldtext()" function
3900 */
3901 static void
3902f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3903{
3904#ifdef FEAT_FOLDING
3905 linenr_T foldstart;
3906 linenr_T foldend;
3907 char_u *dashes;
3908 linenr_T lnum;
3909 char_u *s;
3910 char_u *r;
3911 int len;
3912 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003913 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914#endif
3915
3916 rettv->v_type = VAR_STRING;
3917 rettv->vval.v_string = NULL;
3918#ifdef FEAT_FOLDING
3919 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3920 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3921 dashes = get_vim_var_str(VV_FOLDDASHES);
3922 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3923 && dashes != NULL)
3924 {
3925 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003926 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927 if (!linewhite(lnum))
3928 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929
3930 /* Find interesting text in this line. */
3931 s = skipwhite(ml_get(lnum));
3932 /* skip C comment-start */
3933 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3934 {
3935 s = skipwhite(s + 2);
3936 if (*skipwhite(s) == NUL
3937 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3938 {
3939 s = skipwhite(ml_get(lnum + 1));
3940 if (*s == '*')
3941 s = skipwhite(s + 1);
3942 }
3943 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003944 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003945 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003946 r = alloc(STRLEN(txt)
3947 + STRLEN(dashes) // for %s
3948 + 20 // for %3ld
3949 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 if (r != NULL)
3951 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003952 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 len = (int)STRLEN(r);
3954 STRCAT(r, s);
3955 /* remove 'foldmarker' and 'commentstring' */
3956 foldtext_cleanup(r + len);
3957 rettv->vval.v_string = r;
3958 }
3959 }
3960#endif
3961}
3962
3963/*
3964 * "foldtextresult(lnum)" function
3965 */
3966 static void
3967f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3968{
3969#ifdef FEAT_FOLDING
3970 linenr_T lnum;
3971 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003972 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 foldinfo_T foldinfo;
3974 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003975 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976#endif
3977
3978 rettv->v_type = VAR_STRING;
3979 rettv->vval.v_string = NULL;
3980#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003981 if (entered)
3982 return; /* reject recursive use */
3983 entered = TRUE;
3984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003985 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003986 /* treat illegal types and illegal string values for {lnum} the same */
3987 if (lnum < 0)
3988 lnum = 0;
3989 fold_count = foldedCount(curwin, lnum, &foldinfo);
3990 if (fold_count > 0)
3991 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003992 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3993 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994 if (text == buf)
3995 text = vim_strsave(text);
3996 rettv->vval.v_string = text;
3997 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003998
3999 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000#endif
4001}
4002
4003/*
4004 * "foreground()" function
4005 */
4006 static void
4007f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4008{
4009#ifdef FEAT_GUI
4010 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004011 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004013 return;
4014 }
4015#endif
4016#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018#endif
4019}
4020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004023{
4024 char_u *s;
4025 char_u *name;
4026 int use_string = FALSE;
4027 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004028 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004029
4030 if (argvars[0].v_type == VAR_FUNC)
4031 {
4032 /* function(MyFunc, [arg], dict) */
4033 s = argvars[0].vval.v_string;
4034 }
4035 else if (argvars[0].v_type == VAR_PARTIAL
4036 && argvars[0].vval.v_partial != NULL)
4037 {
4038 /* function(dict.MyFunc, [arg]) */
4039 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004040 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041 }
4042 else
4043 {
4044 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004045 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046 use_string = TRUE;
4047 }
4048
Bram Moolenaar843b8842016-08-21 14:36:15 +02004049 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004050 {
4051 name = s;
4052 trans_name = trans_function_name(&name, FALSE,
4053 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4054 if (*name != NUL)
4055 s = NULL;
4056 }
4057
Bram Moolenaar843b8842016-08-21 14:36:15 +02004058 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4059 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004062 else if (trans_name != NULL && (is_funcref
4063 ? find_func(trans_name) == NULL
4064 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004065 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 else
4067 {
4068 int dict_idx = 0;
4069 int arg_idx = 0;
4070 list_T *list = NULL;
4071
4072 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4073 {
4074 char sid_buf[25];
4075 int off = *s == 's' ? 2 : 5;
4076
4077 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4078 * also be called from another script. Using trans_function_name()
4079 * would also work, but some plugins depend on the name being
4080 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004081 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004082 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004083 if (name != NULL)
4084 {
4085 STRCPY(name, sid_buf);
4086 STRCAT(name, s + off);
4087 }
4088 }
4089 else
4090 name = vim_strsave(s);
4091
4092 if (argvars[1].v_type != VAR_UNKNOWN)
4093 {
4094 if (argvars[2].v_type != VAR_UNKNOWN)
4095 {
4096 /* function(name, [args], dict) */
4097 arg_idx = 1;
4098 dict_idx = 2;
4099 }
4100 else if (argvars[1].v_type == VAR_DICT)
4101 /* function(name, dict) */
4102 dict_idx = 1;
4103 else
4104 /* function(name, [args]) */
4105 arg_idx = 1;
4106 if (dict_idx > 0)
4107 {
4108 if (argvars[dict_idx].v_type != VAR_DICT)
4109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004110 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004112 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113 }
4114 if (argvars[dict_idx].vval.v_dict == NULL)
4115 dict_idx = 0;
4116 }
4117 if (arg_idx > 0)
4118 {
4119 if (argvars[arg_idx].v_type != VAR_LIST)
4120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004121 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004122 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004123 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004124 }
4125 list = argvars[arg_idx].vval.v_list;
4126 if (list == NULL || list->lv_len == 0)
4127 arg_idx = 0;
4128 }
4129 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004131 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004132 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004133
4134 /* result is a VAR_PARTIAL */
4135 if (pt == NULL)
4136 vim_free(name);
4137 else
4138 {
4139 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4140 {
4141 listitem_T *li;
4142 int i = 0;
4143 int arg_len = 0;
4144 int lv_len = 0;
4145
4146 if (arg_pt != NULL)
4147 arg_len = arg_pt->pt_argc;
4148 if (list != NULL)
4149 lv_len = list->lv_len;
4150 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004151 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 if (pt->pt_argv == NULL)
4153 {
4154 vim_free(pt);
4155 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004158 for (i = 0; i < arg_len; i++)
4159 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4160 if (lv_len > 0)
4161 for (li = list->lv_first; li != NULL;
4162 li = li->li_next)
4163 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164 }
4165
4166 /* For "function(dict.func, [], dict)" and "func" is a partial
4167 * use "dict". That is backwards compatible. */
4168 if (dict_idx > 0)
4169 {
4170 /* The dict is bound explicitly, pt_auto is FALSE. */
4171 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4172 ++pt->pt_dict->dv_refcount;
4173 }
4174 else if (arg_pt != NULL)
4175 {
4176 /* If the dict was bound automatically the result is also
4177 * bound automatically. */
4178 pt->pt_dict = arg_pt->pt_dict;
4179 pt->pt_auto = arg_pt->pt_auto;
4180 if (pt->pt_dict != NULL)
4181 ++pt->pt_dict->dv_refcount;
4182 }
4183
4184 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004185 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4186 {
4187 pt->pt_func = arg_pt->pt_func;
4188 func_ptr_ref(pt->pt_func);
4189 vim_free(name);
4190 }
4191 else if (is_funcref)
4192 {
4193 pt->pt_func = find_func(trans_name);
4194 func_ptr_ref(pt->pt_func);
4195 vim_free(name);
4196 }
4197 else
4198 {
4199 pt->pt_name = name;
4200 func_ref(name);
4201 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 }
4203 rettv->v_type = VAR_PARTIAL;
4204 rettv->vval.v_partial = pt;
4205 }
4206 else
4207 {
4208 /* result is a VAR_FUNC */
4209 rettv->v_type = VAR_FUNC;
4210 rettv->vval.v_string = name;
4211 func_ref(name);
4212 }
4213 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004214theend:
4215 vim_free(trans_name);
4216}
4217
4218/*
4219 * "funcref()" function
4220 */
4221 static void
4222f_funcref(typval_T *argvars, typval_T *rettv)
4223{
4224 common_function(argvars, rettv, TRUE);
4225}
4226
4227/*
4228 * "function()" function
4229 */
4230 static void
4231f_function(typval_T *argvars, typval_T *rettv)
4232{
4233 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234}
4235
4236/*
4237 * "garbagecollect()" function
4238 */
4239 static void
4240f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4241{
4242 /* This is postponed until we are back at the toplevel, because we may be
4243 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4244 want_garbage_collect = TRUE;
4245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004246 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 garbage_collect_at_exit = TRUE;
4248}
4249
4250/*
4251 * "get()" function
4252 */
4253 static void
4254f_get(typval_T *argvars, typval_T *rettv)
4255{
4256 listitem_T *li;
4257 list_T *l;
4258 dictitem_T *di;
4259 dict_T *d;
4260 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004261 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004263 if (argvars[0].v_type == VAR_BLOB)
4264 {
4265 int error = FALSE;
4266 int idx = tv_get_number_chk(&argvars[1], &error);
4267
4268 if (!error)
4269 {
4270 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004271 if (idx < 0)
4272 idx = blob_len(argvars[0].vval.v_blob) + idx;
4273 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4274 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004275 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004276 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004277 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004278 tv = rettv;
4279 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004280 }
4281 }
4282 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004283 {
4284 if ((l = argvars[0].vval.v_list) != NULL)
4285 {
4286 int error = FALSE;
4287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004288 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 if (!error && li != NULL)
4290 tv = &li->li_tv;
4291 }
4292 }
4293 else if (argvars[0].v_type == VAR_DICT)
4294 {
4295 if ((d = argvars[0].vval.v_dict) != NULL)
4296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004297 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 if (di != NULL)
4299 tv = &di->di_tv;
4300 }
4301 }
4302 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4303 {
4304 partial_T *pt;
4305 partial_T fref_pt;
4306
4307 if (argvars[0].v_type == VAR_PARTIAL)
4308 pt = argvars[0].vval.v_partial;
4309 else
4310 {
4311 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4312 fref_pt.pt_name = argvars[0].vval.v_string;
4313 pt = &fref_pt;
4314 }
4315
4316 if (pt != NULL)
4317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004318 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004319 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320
4321 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4322 {
4323 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004324 n = partial_name(pt);
4325 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326 rettv->vval.v_string = NULL;
4327 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004328 {
4329 rettv->vval.v_string = vim_strsave(n);
4330 if (rettv->v_type == VAR_FUNC)
4331 func_ref(rettv->vval.v_string);
4332 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004333 }
4334 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004335 {
4336 what_is_dict = TRUE;
4337 if (pt->pt_dict != NULL)
4338 rettv_dict_set(rettv, pt->pt_dict);
4339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004340 else if (STRCMP(what, "args") == 0)
4341 {
4342 rettv->v_type = VAR_LIST;
4343 if (rettv_list_alloc(rettv) == OK)
4344 {
4345 int i;
4346
4347 for (i = 0; i < pt->pt_argc; ++i)
4348 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4349 }
4350 }
4351 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004352 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004353
4354 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4355 // third argument
4356 if (!what_is_dict)
4357 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358 }
4359 }
4360 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004361 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362
4363 if (tv == NULL)
4364 {
4365 if (argvars[2].v_type != VAR_UNKNOWN)
4366 copy_tv(&argvars[2], rettv);
4367 }
4368 else
4369 copy_tv(tv, rettv);
4370}
4371
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004372/*
4373 * Returns buffer options, variables and other attributes in a dictionary.
4374 */
4375 static dict_T *
4376get_buffer_info(buf_T *buf)
4377{
4378 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004379 tabpage_T *tp;
4380 win_T *wp;
4381 list_T *windows;
4382
4383 dict = dict_alloc();
4384 if (dict == NULL)
4385 return NULL;
4386
Bram Moolenaare0be1672018-07-08 16:50:37 +02004387 dict_add_number(dict, "bufnr", buf->b_fnum);
4388 dict_add_string(dict, "name", buf->b_ffname);
4389 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4390 : buflist_findlnum(buf));
4391 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4392 dict_add_number(dict, "listed", buf->b_p_bl);
4393 dict_add_number(dict, "changed", bufIsChanged(buf));
4394 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4395 dict_add_number(dict, "hidden",
4396 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004397
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004398 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004399 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004400
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004401 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004402 windows = list_alloc();
4403 if (windows != NULL)
4404 {
4405 FOR_ALL_TAB_WINDOWS(tp, wp)
4406 if (wp->w_buffer == buf)
4407 list_append_number(windows, (varnumber_T)wp->w_id);
4408 dict_add_list(dict, "windows", windows);
4409 }
4410
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004411#ifdef FEAT_TEXT_PROP
4412 // List of popup windows displaying this buffer
4413 windows = list_alloc();
4414 if (windows != NULL)
4415 {
4416 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4417 if (wp->w_buffer == buf)
4418 list_append_number(windows, (varnumber_T)wp->w_id);
4419 FOR_ALL_TABPAGES(tp)
4420 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4421 if (wp->w_buffer == buf)
4422 list_append_number(windows, (varnumber_T)wp->w_id);
4423
4424 dict_add_list(dict, "popups", windows);
4425 }
4426#endif
4427
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004428#ifdef FEAT_SIGNS
4429 if (buf->b_signlist != NULL)
4430 {
4431 /* List of signs placed in this buffer */
4432 list_T *signs = list_alloc();
4433 if (signs != NULL)
4434 {
4435 get_buffer_signs(buf, signs);
4436 dict_add_list(dict, "signs", signs);
4437 }
4438 }
4439#endif
4440
4441 return dict;
4442}
4443
4444/*
4445 * "getbufinfo()" function
4446 */
4447 static void
4448f_getbufinfo(typval_T *argvars, typval_T *rettv)
4449{
4450 buf_T *buf = NULL;
4451 buf_T *argbuf = NULL;
4452 dict_T *d;
4453 int filtered = FALSE;
4454 int sel_buflisted = FALSE;
4455 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004456 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004457
4458 if (rettv_list_alloc(rettv) != OK)
4459 return;
4460
4461 /* List of all the buffers or selected buffers */
4462 if (argvars[0].v_type == VAR_DICT)
4463 {
4464 dict_T *sel_d = argvars[0].vval.v_dict;
4465
4466 if (sel_d != NULL)
4467 {
4468 dictitem_T *di;
4469
4470 filtered = TRUE;
4471
4472 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004473 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004474 sel_buflisted = TRUE;
4475
4476 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004477 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004478 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004479
4480 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004481 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004482 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004483 }
4484 }
4485 else if (argvars[0].v_type != VAR_UNKNOWN)
4486 {
4487 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004488 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004489 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004490 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004491 --emsg_off;
4492 if (argbuf == NULL)
4493 return;
4494 }
4495
4496 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004497 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004498 {
4499 if (argbuf != NULL && argbuf != buf)
4500 continue;
4501 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004502 || (sel_buflisted && !buf->b_p_bl)
4503 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004504 continue;
4505
4506 d = get_buffer_info(buf);
4507 if (d != NULL)
4508 list_append_dict(rettv->vval.v_list, d);
4509 if (argbuf != NULL)
4510 return;
4511 }
4512}
4513
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514/*
4515 * Get line or list of lines from buffer "buf" into "rettv".
4516 * Return a range (from start to end) of lines in rettv from the specified
4517 * buffer.
4518 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4519 */
4520 static void
4521get_buffer_lines(
4522 buf_T *buf,
4523 linenr_T start,
4524 linenr_T end,
4525 int retlist,
4526 typval_T *rettv)
4527{
4528 char_u *p;
4529
4530 rettv->v_type = VAR_STRING;
4531 rettv->vval.v_string = NULL;
4532 if (retlist && rettv_list_alloc(rettv) == FAIL)
4533 return;
4534
4535 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4536 return;
4537
4538 if (!retlist)
4539 {
4540 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4541 p = ml_get_buf(buf, start, FALSE);
4542 else
4543 p = (char_u *)"";
4544 rettv->vval.v_string = vim_strsave(p);
4545 }
4546 else
4547 {
4548 if (end < start)
4549 return;
4550
4551 if (start < 1)
4552 start = 1;
4553 if (end > buf->b_ml.ml_line_count)
4554 end = buf->b_ml.ml_line_count;
4555 while (start <= end)
4556 if (list_append_string(rettv->vval.v_list,
4557 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4558 break;
4559 }
4560}
4561
4562/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563 * "getbufline()" function
4564 */
4565 static void
4566f_getbufline(typval_T *argvars, typval_T *rettv)
4567{
4568 linenr_T lnum;
4569 linenr_T end;
4570 buf_T *buf;
4571
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004572 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004574 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575 --emsg_off;
4576
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004578 if (argvars[2].v_type == VAR_UNKNOWN)
4579 end = lnum;
4580 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004581 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582
4583 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4584}
4585
4586/*
4587 * "getbufvar()" function
4588 */
4589 static void
4590f_getbufvar(typval_T *argvars, typval_T *rettv)
4591{
4592 buf_T *buf;
4593 buf_T *save_curbuf;
4594 char_u *varname;
4595 dictitem_T *v;
4596 int done = FALSE;
4597
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004598 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4599 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004600 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004601 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004602
4603 rettv->v_type = VAR_STRING;
4604 rettv->vval.v_string = NULL;
4605
4606 if (buf != NULL && varname != NULL)
4607 {
4608 /* set curbuf to be our buf, temporarily */
4609 save_curbuf = curbuf;
4610 curbuf = buf;
4611
Bram Moolenaar30567352016-08-27 21:25:44 +02004612 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004614 if (varname[1] == NUL)
4615 {
4616 /* get all buffer-local options in a dict */
4617 dict_T *opts = get_winbuf_options(TRUE);
4618
4619 if (opts != NULL)
4620 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004621 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004622 done = TRUE;
4623 }
4624 }
4625 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4626 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 done = TRUE;
4628 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 else
4630 {
4631 /* Look up the variable. */
4632 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4633 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4634 'b', varname, FALSE);
4635 if (v != NULL)
4636 {
4637 copy_tv(&v->di_tv, rettv);
4638 done = TRUE;
4639 }
4640 }
4641
4642 /* restore previous notion of curbuf */
4643 curbuf = save_curbuf;
4644 }
4645
4646 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4647 /* use the default value */
4648 copy_tv(&argvars[2], rettv);
4649
4650 --emsg_off;
4651}
4652
4653/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004654 * "getchangelist()" function
4655 */
4656 static void
4657f_getchangelist(typval_T *argvars, typval_T *rettv)
4658{
4659#ifdef FEAT_JUMPLIST
4660 buf_T *buf;
4661 int i;
4662 list_T *l;
4663 dict_T *d;
4664#endif
4665
4666 if (rettv_list_alloc(rettv) != OK)
4667 return;
4668
4669#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004670 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004671 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004672 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004673 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004674 if (buf == NULL)
4675 return;
4676
4677 l = list_alloc();
4678 if (l == NULL)
4679 return;
4680
4681 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4682 return;
4683 /*
4684 * The current window change list index tracks only the position in the
4685 * current buffer change list. For other buffers, use the change list
4686 * length as the current index.
4687 */
4688 list_append_number(rettv->vval.v_list,
4689 (varnumber_T)((buf == curwin->w_buffer)
4690 ? curwin->w_changelistidx : buf->b_changelistlen));
4691
4692 for (i = 0; i < buf->b_changelistlen; ++i)
4693 {
4694 if (buf->b_changelist[i].lnum == 0)
4695 continue;
4696 if ((d = dict_alloc()) == NULL)
4697 return;
4698 if (list_append_dict(l, d) == FAIL)
4699 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004700 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4701 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004702 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004703 }
4704#endif
4705}
4706/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004707 * "getchar()" function
4708 */
4709 static void
4710f_getchar(typval_T *argvars, typval_T *rettv)
4711{
4712 varnumber_T n;
4713 int error = FALSE;
4714
Bram Moolenaar84d93902018-09-11 20:10:20 +02004715#ifdef MESSAGE_QUEUE
4716 // vpeekc() used to check for messages, but that caused problems, invoking
4717 // a callback where it was not expected. Some plugins use getchar(1) in a
4718 // loop to await a message, therefore make sure we check for messages here.
4719 parse_queued_messages();
4720#endif
4721
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004722 /* Position the cursor. Needed after a message that ends in a space. */
4723 windgoto(msg_row, msg_col);
4724
4725 ++no_mapping;
4726 ++allow_keys;
4727 for (;;)
4728 {
4729 if (argvars[0].v_type == VAR_UNKNOWN)
4730 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004731 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004732 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004733 /* getchar(1): only check if char avail */
4734 n = vpeekc_any();
4735 else if (error || vpeekc_any() == NUL)
4736 /* illegal argument or getchar(0) and no char avail: return zero */
4737 n = 0;
4738 else
4739 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004740 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741
4742 if (n == K_IGNORE)
4743 continue;
4744 break;
4745 }
4746 --no_mapping;
4747 --allow_keys;
4748
4749 set_vim_var_nr(VV_MOUSE_WIN, 0);
4750 set_vim_var_nr(VV_MOUSE_WINID, 0);
4751 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4752 set_vim_var_nr(VV_MOUSE_COL, 0);
4753
4754 rettv->vval.v_number = n;
4755 if (IS_SPECIAL(n) || mod_mask != 0)
4756 {
4757 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4758 int i = 0;
4759
4760 /* Turn a special key into three bytes, plus modifier. */
4761 if (mod_mask != 0)
4762 {
4763 temp[i++] = K_SPECIAL;
4764 temp[i++] = KS_MODIFIER;
4765 temp[i++] = mod_mask;
4766 }
4767 if (IS_SPECIAL(n))
4768 {
4769 temp[i++] = K_SPECIAL;
4770 temp[i++] = K_SECOND(n);
4771 temp[i++] = K_THIRD(n);
4772 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773 else if (has_mbyte)
4774 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 else
4776 temp[i++] = n;
4777 temp[i++] = NUL;
4778 rettv->v_type = VAR_STRING;
4779 rettv->vval.v_string = vim_strsave(temp);
4780
4781#ifdef FEAT_MOUSE
4782 if (is_mouse_key(n))
4783 {
4784 int row = mouse_row;
4785 int col = mouse_col;
4786 win_T *win;
4787 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004788 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 int winnr = 1;
4790
4791 if (row >= 0 && col >= 0)
4792 {
4793 /* Find the window at the mouse coordinates and compute the
4794 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004795 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004796 if (win == NULL)
4797 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004798 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004799# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004800 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004801 winnr = 0;
4802 else
4803# endif
4804 for (wp = firstwin; wp != win && wp != NULL;
4805 wp = wp->w_next)
4806 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004807 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4808 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4809 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4810 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4811 }
4812 }
4813#endif
4814 }
4815}
4816
4817/*
4818 * "getcharmod()" function
4819 */
4820 static void
4821f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4822{
4823 rettv->vval.v_number = mod_mask;
4824}
4825
4826/*
4827 * "getcharsearch()" function
4828 */
4829 static void
4830f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4831{
4832 if (rettv_dict_alloc(rettv) != FAIL)
4833 {
4834 dict_T *dict = rettv->vval.v_dict;
4835
Bram Moolenaare0be1672018-07-08 16:50:37 +02004836 dict_add_string(dict, "char", last_csearch());
4837 dict_add_number(dict, "forward", last_csearch_forward());
4838 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 }
4840}
4841
4842/*
4843 * "getcmdline()" function
4844 */
4845 static void
4846f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4847{
4848 rettv->v_type = VAR_STRING;
4849 rettv->vval.v_string = get_cmdline_str();
4850}
4851
4852/*
4853 * "getcmdpos()" function
4854 */
4855 static void
4856f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4857{
4858 rettv->vval.v_number = get_cmdline_pos() + 1;
4859}
4860
4861/*
4862 * "getcmdtype()" function
4863 */
4864 static void
4865f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4866{
4867 rettv->v_type = VAR_STRING;
4868 rettv->vval.v_string = alloc(2);
4869 if (rettv->vval.v_string != NULL)
4870 {
4871 rettv->vval.v_string[0] = get_cmdline_type();
4872 rettv->vval.v_string[1] = NUL;
4873 }
4874}
4875
4876/*
4877 * "getcmdwintype()" function
4878 */
4879 static void
4880f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4881{
4882 rettv->v_type = VAR_STRING;
4883 rettv->vval.v_string = NULL;
4884#ifdef FEAT_CMDWIN
4885 rettv->vval.v_string = alloc(2);
4886 if (rettv->vval.v_string != NULL)
4887 {
4888 rettv->vval.v_string[0] = cmdwin_type;
4889 rettv->vval.v_string[1] = NUL;
4890 }
4891#endif
4892}
4893
4894#if defined(FEAT_CMDL_COMPL)
4895/*
4896 * "getcompletion()" function
4897 */
4898 static void
4899f_getcompletion(typval_T *argvars, typval_T *rettv)
4900{
4901 char_u *pat;
4902 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004903 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004904 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4905 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004907 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004910 if (p_wic)
4911 options |= WILD_ICASE;
4912
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004913 /* For filtered results, 'wildignore' is used */
4914 if (!filtered)
4915 options |= WILD_KEEP_ALL;
4916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004918 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004919 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004920 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921 if (xpc.xp_context == EXPAND_NOTHING)
4922 {
4923 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004924 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004926 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 return;
4928 }
4929
4930# if defined(FEAT_MENU)
4931 if (xpc.xp_context == EXPAND_MENUS)
4932 {
4933 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4934 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4935 }
4936# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004937#ifdef FEAT_CSCOPE
4938 if (xpc.xp_context == EXPAND_CSCOPE)
4939 {
4940 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4941 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4942 }
4943#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004944#ifdef FEAT_SIGNS
4945 if (xpc.xp_context == EXPAND_SIGN)
4946 {
4947 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4948 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4949 }
4950#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951
4952 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4953 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4954 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004955 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956
4957 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4958
4959 for (i = 0; i < xpc.xp_numfiles; i++)
4960 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4961 }
4962 vim_free(pat);
4963 ExpandCleanup(&xpc);
4964}
4965#endif
4966
4967/*
4968 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004969 *
4970 * Return the current working directory of a window in a tab page.
4971 * First optional argument 'winnr' is the window number or -1 and the second
4972 * optional argument 'tabnr' is the tab page number.
4973 *
4974 * If no arguments are supplied, then return the directory of the current
4975 * window.
4976 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4977 * the specified window.
4978 * If 'winnr' is 0 then return the directory of the current window.
4979 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4980 * directory of the specified tab page. Otherwise return the directory of the
4981 * specified window in the specified tab page.
4982 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004983 */
4984 static void
4985f_getcwd(typval_T *argvars, typval_T *rettv)
4986{
4987 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004988 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004990 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991
4992 rettv->v_type = VAR_STRING;
4993 rettv->vval.v_string = NULL;
4994
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004995 if (argvars[0].v_type == VAR_NUMBER
4996 && argvars[0].vval.v_number == -1
4997 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004998 global = TRUE;
4999 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005000 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005001
5002 if (wp != NULL && wp->w_localdir != NULL)
5003 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005004 else if (tp != NULL && tp->tp_localdir != NULL)
5005 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5006 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005007 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005008 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 rettv->vval.v_string = vim_strsave(globaldir);
5010 else
5011 {
5012 cwd = alloc(MAXPATHL);
5013 if (cwd != NULL)
5014 {
5015 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5016 rettv->vval.v_string = vim_strsave(cwd);
5017 vim_free(cwd);
5018 }
5019 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005021#ifdef BACKSLASH_IN_FILENAME
5022 if (rettv->vval.v_string != NULL)
5023 slash_adjust(rettv->vval.v_string);
5024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025}
5026
5027/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005028 * "getenv()" function
5029 */
5030 static void
5031f_getenv(typval_T *argvars, typval_T *rettv)
5032{
5033 int mustfree = FALSE;
5034 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5035
5036 if (p == NULL)
5037 {
5038 rettv->v_type = VAR_SPECIAL;
5039 rettv->vval.v_number = VVAL_NULL;
5040 return;
5041 }
5042 if (!mustfree)
5043 p = vim_strsave(p);
5044 rettv->vval.v_string = p;
5045 rettv->v_type = VAR_STRING;
5046}
5047
5048/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005049 * "getfontname()" function
5050 */
5051 static void
5052f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5053{
5054 rettv->v_type = VAR_STRING;
5055 rettv->vval.v_string = NULL;
5056#ifdef FEAT_GUI
5057 if (gui.in_use)
5058 {
5059 GuiFont font;
5060 char_u *name = NULL;
5061
5062 if (argvars[0].v_type == VAR_UNKNOWN)
5063 {
5064 /* Get the "Normal" font. Either the name saved by
5065 * hl_set_font_name() or from the font ID. */
5066 font = gui.norm_font;
5067 name = hl_get_font_name();
5068 }
5069 else
5070 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005071 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5073 return;
5074 font = gui_mch_get_font(name, FALSE);
5075 if (font == NOFONT)
5076 return; /* Invalid font name, return empty string. */
5077 }
5078 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5079 if (argvars[0].v_type != VAR_UNKNOWN)
5080 gui_mch_free_font(font);
5081 }
5082#endif
5083}
5084
5085/*
5086 * "getfperm({fname})" function
5087 */
5088 static void
5089f_getfperm(typval_T *argvars, typval_T *rettv)
5090{
5091 char_u *fname;
5092 stat_T st;
5093 char_u *perm = NULL;
5094 char_u flags[] = "rwx";
5095 int i;
5096
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098
5099 rettv->v_type = VAR_STRING;
5100 if (mch_stat((char *)fname, &st) >= 0)
5101 {
5102 perm = vim_strsave((char_u *)"---------");
5103 if (perm != NULL)
5104 {
5105 for (i = 0; i < 9; i++)
5106 {
5107 if (st.st_mode & (1 << (8 - i)))
5108 perm[i] = flags[i % 3];
5109 }
5110 }
5111 }
5112 rettv->vval.v_string = perm;
5113}
5114
5115/*
5116 * "getfsize({fname})" function
5117 */
5118 static void
5119f_getfsize(typval_T *argvars, typval_T *rettv)
5120{
5121 char_u *fname;
5122 stat_T st;
5123
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005124 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125
5126 rettv->v_type = VAR_NUMBER;
5127
5128 if (mch_stat((char *)fname, &st) >= 0)
5129 {
5130 if (mch_isdir(fname))
5131 rettv->vval.v_number = 0;
5132 else
5133 {
5134 rettv->vval.v_number = (varnumber_T)st.st_size;
5135
5136 /* non-perfect check for overflow */
5137 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5138 rettv->vval.v_number = -2;
5139 }
5140 }
5141 else
5142 rettv->vval.v_number = -1;
5143}
5144
5145/*
5146 * "getftime({fname})" function
5147 */
5148 static void
5149f_getftime(typval_T *argvars, typval_T *rettv)
5150{
5151 char_u *fname;
5152 stat_T st;
5153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005154 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155
5156 if (mch_stat((char *)fname, &st) >= 0)
5157 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5158 else
5159 rettv->vval.v_number = -1;
5160}
5161
5162/*
5163 * "getftype({fname})" function
5164 */
5165 static void
5166f_getftype(typval_T *argvars, typval_T *rettv)
5167{
5168 char_u *fname;
5169 stat_T st;
5170 char_u *type = NULL;
5171 char *t;
5172
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005173 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005174
5175 rettv->v_type = VAR_STRING;
5176 if (mch_lstat((char *)fname, &st) >= 0)
5177 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005178 if (S_ISREG(st.st_mode))
5179 t = "file";
5180 else if (S_ISDIR(st.st_mode))
5181 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005182 else if (S_ISLNK(st.st_mode))
5183 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005184 else if (S_ISBLK(st.st_mode))
5185 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005186 else if (S_ISCHR(st.st_mode))
5187 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 else if (S_ISFIFO(st.st_mode))
5189 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005190 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005191 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 else
5193 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005194 type = vim_strsave((char_u *)t);
5195 }
5196 rettv->vval.v_string = type;
5197}
5198
5199/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005200 * "getjumplist()" function
5201 */
5202 static void
5203f_getjumplist(typval_T *argvars, typval_T *rettv)
5204{
5205#ifdef FEAT_JUMPLIST
5206 win_T *wp;
5207 int i;
5208 list_T *l;
5209 dict_T *d;
5210#endif
5211
5212 if (rettv_list_alloc(rettv) != OK)
5213 return;
5214
5215#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005216 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005217 if (wp == NULL)
5218 return;
5219
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005220 cleanup_jumplist(wp, TRUE);
5221
Bram Moolenaar4f505882018-02-10 21:06:32 +01005222 l = list_alloc();
5223 if (l == NULL)
5224 return;
5225
5226 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5227 return;
5228 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5229
5230 for (i = 0; i < wp->w_jumplistlen; ++i)
5231 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005232 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5233 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005234 if ((d = dict_alloc()) == NULL)
5235 return;
5236 if (list_append_dict(l, d) == FAIL)
5237 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005238 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5239 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005240 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005241 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005242 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005243 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005244 }
5245#endif
5246}
5247
5248/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249 * "getline(lnum, [end])" function
5250 */
5251 static void
5252f_getline(typval_T *argvars, typval_T *rettv)
5253{
5254 linenr_T lnum;
5255 linenr_T end;
5256 int retlist;
5257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005258 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 if (argvars[1].v_type == VAR_UNKNOWN)
5260 {
5261 end = 0;
5262 retlist = FALSE;
5263 }
5264 else
5265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005266 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005267 retlist = TRUE;
5268 }
5269
5270 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5271}
5272
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005273#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005274 static void
5275get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5276{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005277 if (what_arg->v_type == VAR_UNKNOWN)
5278 {
5279 if (rettv_list_alloc(rettv) == OK)
5280 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005281 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005282 }
5283 else
5284 {
5285 if (rettv_dict_alloc(rettv) == OK)
5286 if (is_qf || (wp != NULL))
5287 {
5288 if (what_arg->v_type == VAR_DICT)
5289 {
5290 dict_T *d = what_arg->vval.v_dict;
5291
5292 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005293 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005294 }
5295 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005296 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005297 }
5298 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005299}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005300#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005301
5302/*
5303 * "getloclist()" function
5304 */
5305 static void
5306f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5307{
5308#ifdef FEAT_QUICKFIX
5309 win_T *wp;
5310
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005311 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005312 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5313#endif
5314}
5315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317 * "getpid()" function
5318 */
5319 static void
5320f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5321{
5322 rettv->vval.v_number = mch_get_pid();
5323}
5324
5325 static void
5326getpos_both(
5327 typval_T *argvars,
5328 typval_T *rettv,
5329 int getcurpos)
5330{
5331 pos_T *fp;
5332 list_T *l;
5333 int fnum = -1;
5334
5335 if (rettv_list_alloc(rettv) == OK)
5336 {
5337 l = rettv->vval.v_list;
5338 if (getcurpos)
5339 fp = &curwin->w_cursor;
5340 else
5341 fp = var2fpos(&argvars[0], TRUE, &fnum);
5342 if (fnum != -1)
5343 list_append_number(l, (varnumber_T)fnum);
5344 else
5345 list_append_number(l, (varnumber_T)0);
5346 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5347 : (varnumber_T)0);
5348 list_append_number(l, (fp != NULL)
5349 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5350 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005351 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352 (varnumber_T)0);
5353 if (getcurpos)
5354 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005355 int save_set_curswant = curwin->w_set_curswant;
5356 colnr_T save_curswant = curwin->w_curswant;
5357 colnr_T save_virtcol = curwin->w_virtcol;
5358
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359 update_curswant();
5360 list_append_number(l, curwin->w_curswant == MAXCOL ?
5361 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005362
5363 // Do not change "curswant", as it is unexpected that a get
5364 // function has a side effect.
5365 if (save_set_curswant)
5366 {
5367 curwin->w_set_curswant = save_set_curswant;
5368 curwin->w_curswant = save_curswant;
5369 curwin->w_virtcol = save_virtcol;
5370 curwin->w_valid &= ~VALID_VIRTCOL;
5371 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372 }
5373 }
5374 else
5375 rettv->vval.v_number = FALSE;
5376}
5377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378/*
5379 * "getcurpos()" function
5380 */
5381 static void
5382f_getcurpos(typval_T *argvars, typval_T *rettv)
5383{
5384 getpos_both(argvars, rettv, TRUE);
5385}
5386
5387/*
5388 * "getpos(string)" function
5389 */
5390 static void
5391f_getpos(typval_T *argvars, typval_T *rettv)
5392{
5393 getpos_both(argvars, rettv, FALSE);
5394}
5395
5396/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005397 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005398 */
5399 static void
5400f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5401{
5402#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005403 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404#endif
5405}
5406
5407/*
5408 * "getreg()" function
5409 */
5410 static void
5411f_getreg(typval_T *argvars, typval_T *rettv)
5412{
5413 char_u *strregname;
5414 int regname;
5415 int arg2 = FALSE;
5416 int return_list = FALSE;
5417 int error = FALSE;
5418
5419 if (argvars[0].v_type != VAR_UNKNOWN)
5420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005421 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005422 error = strregname == NULL;
5423 if (argvars[1].v_type != VAR_UNKNOWN)
5424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005425 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005427 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 }
5429 }
5430 else
5431 strregname = get_vim_var_str(VV_REG);
5432
5433 if (error)
5434 return;
5435
5436 regname = (strregname == NULL ? '"' : *strregname);
5437 if (regname == 0)
5438 regname = '"';
5439
5440 if (return_list)
5441 {
5442 rettv->v_type = VAR_LIST;
5443 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5444 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5445 if (rettv->vval.v_list == NULL)
5446 (void)rettv_list_alloc(rettv);
5447 else
5448 ++rettv->vval.v_list->lv_refcount;
5449 }
5450 else
5451 {
5452 rettv->v_type = VAR_STRING;
5453 rettv->vval.v_string = get_reg_contents(regname,
5454 arg2 ? GREG_EXPR_SRC : 0);
5455 }
5456}
5457
5458/*
5459 * "getregtype()" function
5460 */
5461 static void
5462f_getregtype(typval_T *argvars, typval_T *rettv)
5463{
5464 char_u *strregname;
5465 int regname;
5466 char_u buf[NUMBUFLEN + 2];
5467 long reglen = 0;
5468
5469 if (argvars[0].v_type != VAR_UNKNOWN)
5470 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005471 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005472 if (strregname == NULL) /* type error; errmsg already given */
5473 {
5474 rettv->v_type = VAR_STRING;
5475 rettv->vval.v_string = NULL;
5476 return;
5477 }
5478 }
5479 else
5480 /* Default to v:register */
5481 strregname = get_vim_var_str(VV_REG);
5482
5483 regname = (strregname == NULL ? '"' : *strregname);
5484 if (regname == 0)
5485 regname = '"';
5486
5487 buf[0] = NUL;
5488 buf[1] = NUL;
5489 switch (get_reg_type(regname, &reglen))
5490 {
5491 case MLINE: buf[0] = 'V'; break;
5492 case MCHAR: buf[0] = 'v'; break;
5493 case MBLOCK:
5494 buf[0] = Ctrl_V;
5495 sprintf((char *)buf + 1, "%ld", reglen + 1);
5496 break;
5497 }
5498 rettv->v_type = VAR_STRING;
5499 rettv->vval.v_string = vim_strsave(buf);
5500}
5501
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005502/*
5503 * Returns information (variables, options, etc.) about a tab page
5504 * as a dictionary.
5505 */
5506 static dict_T *
5507get_tabpage_info(tabpage_T *tp, int tp_idx)
5508{
5509 win_T *wp;
5510 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005511 list_T *l;
5512
5513 dict = dict_alloc();
5514 if (dict == NULL)
5515 return NULL;
5516
Bram Moolenaare0be1672018-07-08 16:50:37 +02005517 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005518
5519 l = list_alloc();
5520 if (l != NULL)
5521 {
5522 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005523 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005524 list_append_number(l, (varnumber_T)wp->w_id);
5525 dict_add_list(dict, "windows", l);
5526 }
5527
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005528 /* Make a reference to tabpage variables */
5529 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005530
5531 return dict;
5532}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005533
5534/*
5535 * "gettabinfo()" function
5536 */
5537 static void
5538f_gettabinfo(typval_T *argvars, typval_T *rettv)
5539{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005540 tabpage_T *tp, *tparg = NULL;
5541 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005542 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005543
5544 if (rettv_list_alloc(rettv) != OK)
5545 return;
5546
5547 if (argvars[0].v_type != VAR_UNKNOWN)
5548 {
5549 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005550 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551 if (tparg == NULL)
5552 return;
5553 }
5554
5555 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005556 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005558 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005559 if (tparg != NULL && tp != tparg)
5560 continue;
5561 d = get_tabpage_info(tp, tpnr);
5562 if (d != NULL)
5563 list_append_dict(rettv->vval.v_list, d);
5564 if (tparg != NULL)
5565 return;
5566 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005567}
5568
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005569/*
5570 * "gettabvar()" function
5571 */
5572 static void
5573f_gettabvar(typval_T *argvars, typval_T *rettv)
5574{
5575 win_T *oldcurwin;
5576 tabpage_T *tp, *oldtabpage;
5577 dictitem_T *v;
5578 char_u *varname;
5579 int done = FALSE;
5580
5581 rettv->v_type = VAR_STRING;
5582 rettv->vval.v_string = NULL;
5583
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 varname = tv_get_string_chk(&argvars[1]);
5585 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 if (tp != NULL && varname != NULL)
5587 {
5588 /* Set tp to be our tabpage, temporarily. Also set the window to the
5589 * first window in the tabpage, otherwise the window is not valid. */
5590 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005591 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5592 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593 {
5594 /* look up the variable */
5595 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5596 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5597 if (v != NULL)
5598 {
5599 copy_tv(&v->di_tv, rettv);
5600 done = TRUE;
5601 }
5602 }
5603
5604 /* restore previous notion of curwin */
5605 restore_win(oldcurwin, oldtabpage, TRUE);
5606 }
5607
5608 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5609 copy_tv(&argvars[2], rettv);
5610}
5611
5612/*
5613 * "gettabwinvar()" function
5614 */
5615 static void
5616f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5617{
5618 getwinvar(argvars, rettv, 1);
5619}
5620
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005621/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005622 * "gettagstack()" function
5623 */
5624 static void
5625f_gettagstack(typval_T *argvars, typval_T *rettv)
5626{
5627 win_T *wp = curwin; // default is current window
5628
5629 if (rettv_dict_alloc(rettv) != OK)
5630 return;
5631
5632 if (argvars[0].v_type != VAR_UNKNOWN)
5633 {
5634 wp = find_win_by_nr_or_id(&argvars[0]);
5635 if (wp == NULL)
5636 return;
5637 }
5638
5639 get_tagstack(wp, rettv->vval.v_dict);
5640}
5641
5642/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643 * Returns information about a window as a dictionary.
5644 */
5645 static dict_T *
5646get_win_info(win_T *wp, short tpnr, short winnr)
5647{
5648 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649
5650 dict = dict_alloc();
5651 if (dict == NULL)
5652 return NULL;
5653
Bram Moolenaare0be1672018-07-08 16:50:37 +02005654 dict_add_number(dict, "tabnr", tpnr);
5655 dict_add_number(dict, "winnr", winnr);
5656 dict_add_number(dict, "winid", wp->w_id);
5657 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005658 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005659 dict_add_number(dict, "topline", wp->w_topline);
5660 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005661#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005662 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005663#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005664 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005665 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005666 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005667
Bram Moolenaar69905d12017-08-13 18:14:47 +02005668#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005669 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005670#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005671#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005672 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5673 dict_add_number(dict, "loclist",
5674 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005675#endif
5676
Bram Moolenaar30567352016-08-27 21:25:44 +02005677 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005678 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005679
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005680 return dict;
5681}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682
5683/*
5684 * "getwininfo()" function
5685 */
5686 static void
5687f_getwininfo(typval_T *argvars, typval_T *rettv)
5688{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005689 tabpage_T *tp;
5690 win_T *wp = NULL, *wparg = NULL;
5691 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005692 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005693
5694 if (rettv_list_alloc(rettv) != OK)
5695 return;
5696
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005697 if (argvars[0].v_type != VAR_UNKNOWN)
5698 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005699 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005700 if (wparg == NULL)
5701 return;
5702 }
5703
5704 /* Collect information about either all the windows across all the tab
5705 * pages or one particular window.
5706 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005707 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005708 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005709 tabnr++;
5710 winnr = 0;
5711 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005712 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005713 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714 if (wparg != NULL && wp != wparg)
5715 continue;
5716 d = get_win_info(wp, tabnr, winnr);
5717 if (d != NULL)
5718 list_append_dict(rettv->vval.v_list, d);
5719 if (wparg != NULL)
5720 /* found information about a specific window */
5721 return;
5722 }
5723 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005724}
5725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005726/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005727 * "win_execute()" function
5728 */
5729 static void
5730f_win_execute(typval_T *argvars, typval_T *rettv)
5731{
5732 int id = (int)tv_get_number(argvars);
5733 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005734 win_T *save_curwin;
5735 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005736
5737 if (wp != NULL)
5738 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005739 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5740 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005741 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005742 check_cursor();
5743 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005744 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005745 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005746 }
5747}
5748
5749/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750 * "win_findbuf()" function
5751 */
5752 static void
5753f_win_findbuf(typval_T *argvars, typval_T *rettv)
5754{
5755 if (rettv_list_alloc(rettv) != FAIL)
5756 win_findbuf(argvars, rettv->vval.v_list);
5757}
5758
5759/*
5760 * "win_getid()" function
5761 */
5762 static void
5763f_win_getid(typval_T *argvars, typval_T *rettv)
5764{
5765 rettv->vval.v_number = win_getid(argvars);
5766}
5767
5768/*
5769 * "win_gotoid()" function
5770 */
5771 static void
5772f_win_gotoid(typval_T *argvars, typval_T *rettv)
5773{
5774 rettv->vval.v_number = win_gotoid(argvars);
5775}
5776
5777/*
5778 * "win_id2tabwin()" function
5779 */
5780 static void
5781f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5782{
5783 if (rettv_list_alloc(rettv) != FAIL)
5784 win_id2tabwin(argvars, rettv->vval.v_list);
5785}
5786
5787/*
5788 * "win_id2win()" function
5789 */
5790 static void
5791f_win_id2win(typval_T *argvars, typval_T *rettv)
5792{
5793 rettv->vval.v_number = win_id2win(argvars);
5794}
5795
5796/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005797 * "win_screenpos()" function
5798 */
5799 static void
5800f_win_screenpos(typval_T *argvars, typval_T *rettv)
5801{
5802 win_T *wp;
5803
5804 if (rettv_list_alloc(rettv) == FAIL)
5805 return;
5806
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005807 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005808 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5809 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5810}
5811
5812/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005813 * "getwinpos({timeout})" function
5814 */
5815 static void
5816f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5817{
5818 int x = -1;
5819 int y = -1;
5820
5821 if (rettv_list_alloc(rettv) == FAIL)
5822 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005823#if defined(FEAT_GUI) \
5824 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5825 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005826 {
5827 varnumber_T timeout = 100;
5828
5829 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005831
5832 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005833 }
5834#endif
5835 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5836 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5837}
5838
5839
5840/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005841 * "getwinposx()" function
5842 */
5843 static void
5844f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5845{
5846 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005847#if defined(FEAT_GUI) \
5848 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5849 || defined(MSWIN)
5850
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005851 {
5852 int x, y;
5853
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005854 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005855 rettv->vval.v_number = x;
5856 }
5857#endif
5858}
5859
5860/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 * "getwinposy()" function
5862 */
5863 static void
5864f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5865{
5866 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005867#if defined(FEAT_GUI) \
5868 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5869 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005870 {
5871 int x, y;
5872
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005873 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005874 rettv->vval.v_number = y;
5875 }
5876#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005877}
5878
5879/*
5880 * "getwinvar()" function
5881 */
5882 static void
5883f_getwinvar(typval_T *argvars, typval_T *rettv)
5884{
5885 getwinvar(argvars, rettv, 0);
5886}
5887
5888/*
5889 * "glob()" function
5890 */
5891 static void
5892f_glob(typval_T *argvars, typval_T *rettv)
5893{
5894 int options = WILD_SILENT|WILD_USE_NL;
5895 expand_T xpc;
5896 int error = FALSE;
5897
5898 /* When the optional second argument is non-zero, don't remove matches
5899 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5900 rettv->v_type = VAR_STRING;
5901 if (argvars[1].v_type != VAR_UNKNOWN)
5902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005903 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904 options |= WILD_KEEP_ALL;
5905 if (argvars[2].v_type != VAR_UNKNOWN)
5906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005908 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005910 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911 options |= WILD_ALLLINKS;
5912 }
5913 }
5914 if (!error)
5915 {
5916 ExpandInit(&xpc);
5917 xpc.xp_context = EXPAND_FILES;
5918 if (p_wic)
5919 options += WILD_ICASE;
5920 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005921 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005922 NULL, options, WILD_ALL);
5923 else if (rettv_list_alloc(rettv) != FAIL)
5924 {
5925 int i;
5926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005927 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 NULL, options, WILD_ALL_KEEP);
5929 for (i = 0; i < xpc.xp_numfiles; i++)
5930 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5931
5932 ExpandCleanup(&xpc);
5933 }
5934 }
5935 else
5936 rettv->vval.v_string = NULL;
5937}
5938
5939/*
5940 * "globpath()" function
5941 */
5942 static void
5943f_globpath(typval_T *argvars, typval_T *rettv)
5944{
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005945 int flags = WILD_IGNORE_COMPLETESLASH;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005947 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948 int error = FALSE;
5949 garray_T ga;
5950 int i;
5951
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005952 // When the optional second argument is non-zero, don't remove matches
5953 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 rettv->v_type = VAR_STRING;
5955 if (argvars[2].v_type != VAR_UNKNOWN)
5956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005957 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 flags |= WILD_KEEP_ALL;
5959 if (argvars[3].v_type != VAR_UNKNOWN)
5960 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005961 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005962 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005964 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 flags |= WILD_ALLLINKS;
5966 }
5967 }
5968 if (file != NULL && !error)
5969 {
5970 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005971 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (rettv->v_type == VAR_STRING)
5973 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5974 else if (rettv_list_alloc(rettv) != FAIL)
5975 for (i = 0; i < ga.ga_len; ++i)
5976 list_append_string(rettv->vval.v_list,
5977 ((char_u **)(ga.ga_data))[i], -1);
5978 ga_clear_strings(&ga);
5979 }
5980 else
5981 rettv->vval.v_string = NULL;
5982}
5983
5984/*
5985 * "glob2regpat()" function
5986 */
5987 static void
5988f_glob2regpat(typval_T *argvars, typval_T *rettv)
5989{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005990 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991
5992 rettv->v_type = VAR_STRING;
5993 rettv->vval.v_string = (pat == NULL)
5994 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5995}
5996
5997/* for VIM_VERSION_ defines */
5998#include "version.h"
5999
6000/*
6001 * "has()" function
6002 */
6003 static void
6004f_has(typval_T *argvars, typval_T *rettv)
6005{
6006 int i;
6007 char_u *name;
6008 int n = FALSE;
6009 static char *(has_list[]) =
6010 {
6011#ifdef AMIGA
6012 "amiga",
6013# ifdef FEAT_ARP
6014 "arp",
6015# endif
6016#endif
6017#ifdef __BEOS__
6018 "beos",
6019#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006020#if defined(BSD) && !defined(MACOS_X)
6021 "bsd",
6022#endif
6023#ifdef hpux
6024 "hpux",
6025#endif
6026#ifdef __linux__
6027 "linux",
6028#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006029#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006030 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6031 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006032# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006033 "macunix", /* Mac OS X, with the darwin feature */
6034 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006035# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006036#endif
6037#ifdef __QNX__
6038 "qnx",
6039#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006040#ifdef SUN_SYSTEM
6041 "sun",
6042#else
6043 "moon",
6044#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045#ifdef UNIX
6046 "unix",
6047#endif
6048#ifdef VMS
6049 "vms",
6050#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006051#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052 "win32",
6053#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006054#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 "win32unix",
6056#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006057#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006058 "win64",
6059#endif
6060#ifdef EBCDIC
6061 "ebcdic",
6062#endif
6063#ifndef CASE_INSENSITIVE_FILENAME
6064 "fname_case",
6065#endif
6066#ifdef HAVE_ACL
6067 "acl",
6068#endif
6069#ifdef FEAT_ARABIC
6070 "arabic",
6071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006073#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006074 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006075#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006076#ifdef FEAT_AUTOSERVERNAME
6077 "autoservername",
6078#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006079#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006081# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082 "balloon_multiline",
6083# endif
6084#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006085#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006086 "balloon_eval_term",
6087#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6089 "builtin_terms",
6090# ifdef ALL_BUILTIN_TCAPS
6091 "all_builtin_terms",
6092# endif
6093#endif
6094#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006095 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 || defined(FEAT_GUI_MOTIF))
6097 "browsefilter",
6098#endif
6099#ifdef FEAT_BYTEOFF
6100 "byte_offset",
6101#endif
6102#ifdef FEAT_JOB_CHANNEL
6103 "channel",
6104#endif
6105#ifdef FEAT_CINDENT
6106 "cindent",
6107#endif
6108#ifdef FEAT_CLIENTSERVER
6109 "clientserver",
6110#endif
6111#ifdef FEAT_CLIPBOARD
6112 "clipboard",
6113#endif
6114#ifdef FEAT_CMDL_COMPL
6115 "cmdline_compl",
6116#endif
6117#ifdef FEAT_CMDHIST
6118 "cmdline_hist",
6119#endif
6120#ifdef FEAT_COMMENTS
6121 "comments",
6122#endif
6123#ifdef FEAT_CONCEAL
6124 "conceal",
6125#endif
6126#ifdef FEAT_CRYPT
6127 "cryptv",
6128 "crypt-blowfish",
6129 "crypt-blowfish2",
6130#endif
6131#ifdef FEAT_CSCOPE
6132 "cscope",
6133#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135#ifdef CURSOR_SHAPE
6136 "cursorshape",
6137#endif
6138#ifdef DEBUG
6139 "debug",
6140#endif
6141#ifdef FEAT_CON_DIALOG
6142 "dialog_con",
6143#endif
6144#ifdef FEAT_GUI_DIALOG
6145 "dialog_gui",
6146#endif
6147#ifdef FEAT_DIFF
6148 "diff",
6149#endif
6150#ifdef FEAT_DIGRAPHS
6151 "digraphs",
6152#endif
6153#ifdef FEAT_DIRECTX
6154 "directx",
6155#endif
6156#ifdef FEAT_DND
6157 "dnd",
6158#endif
6159#ifdef FEAT_EMACS_TAGS
6160 "emacs_tags",
6161#endif
6162 "eval", /* always present, of course! */
6163 "ex_extra", /* graduated feature */
6164#ifdef FEAT_SEARCH_EXTRA
6165 "extra_search",
6166#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167#ifdef FEAT_SEARCHPATH
6168 "file_in_path",
6169#endif
6170#ifdef FEAT_FILTERPIPE
6171 "filterpipe",
6172#endif
6173#ifdef FEAT_FIND_ID
6174 "find_in_path",
6175#endif
6176#ifdef FEAT_FLOAT
6177 "float",
6178#endif
6179#ifdef FEAT_FOLDING
6180 "folding",
6181#endif
6182#ifdef FEAT_FOOTER
6183 "footer",
6184#endif
6185#if !defined(USE_SYSTEM) && defined(UNIX)
6186 "fork",
6187#endif
6188#ifdef FEAT_GETTEXT
6189 "gettext",
6190#endif
6191#ifdef FEAT_GUI
6192 "gui",
6193#endif
6194#ifdef FEAT_GUI_ATHENA
6195# ifdef FEAT_GUI_NEXTAW
6196 "gui_neXtaw",
6197# else
6198 "gui_athena",
6199# endif
6200#endif
6201#ifdef FEAT_GUI_GTK
6202 "gui_gtk",
6203# ifdef USE_GTK3
6204 "gui_gtk3",
6205# else
6206 "gui_gtk2",
6207# endif
6208#endif
6209#ifdef FEAT_GUI_GNOME
6210 "gui_gnome",
6211#endif
6212#ifdef FEAT_GUI_MAC
6213 "gui_mac",
6214#endif
6215#ifdef FEAT_GUI_MOTIF
6216 "gui_motif",
6217#endif
6218#ifdef FEAT_GUI_PHOTON
6219 "gui_photon",
6220#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006221#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 "gui_win32",
6223#endif
6224#ifdef FEAT_HANGULIN
6225 "hangul_input",
6226#endif
6227#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6228 "iconv",
6229#endif
6230#ifdef FEAT_INS_EXPAND
6231 "insert_expand",
6232#endif
6233#ifdef FEAT_JOB_CHANNEL
6234 "job",
6235#endif
6236#ifdef FEAT_JUMPLIST
6237 "jumplist",
6238#endif
6239#ifdef FEAT_KEYMAP
6240 "keymap",
6241#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006242 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243#ifdef FEAT_LANGMAP
6244 "langmap",
6245#endif
6246#ifdef FEAT_LIBCALL
6247 "libcall",
6248#endif
6249#ifdef FEAT_LINEBREAK
6250 "linebreak",
6251#endif
6252#ifdef FEAT_LISP
6253 "lispindent",
6254#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "localmap",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257#ifdef FEAT_LUA
6258# ifndef DYNAMIC_LUA
6259 "lua",
6260# endif
6261#endif
6262#ifdef FEAT_MENU
6263 "menu",
6264#endif
6265#ifdef FEAT_SESSION
6266 "mksession",
6267#endif
6268#ifdef FEAT_MODIFY_FNAME
6269 "modify_fname",
6270#endif
6271#ifdef FEAT_MOUSE
6272 "mouse",
6273#endif
6274#ifdef FEAT_MOUSESHAPE
6275 "mouseshape",
6276#endif
6277#if defined(UNIX) || defined(VMS)
6278# ifdef FEAT_MOUSE_DEC
6279 "mouse_dec",
6280# endif
6281# ifdef FEAT_MOUSE_GPM
6282 "mouse_gpm",
6283# endif
6284# ifdef FEAT_MOUSE_JSB
6285 "mouse_jsbterm",
6286# endif
6287# ifdef FEAT_MOUSE_NET
6288 "mouse_netterm",
6289# endif
6290# ifdef FEAT_MOUSE_PTERM
6291 "mouse_pterm",
6292# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006293# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 "mouse_sgr",
6295# endif
6296# ifdef FEAT_SYSMOUSE
6297 "mouse_sysmouse",
6298# endif
6299# ifdef FEAT_MOUSE_URXVT
6300 "mouse_urxvt",
6301# endif
6302# ifdef FEAT_MOUSE_XTERM
6303 "mouse_xterm",
6304# endif
6305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307#ifdef FEAT_MBYTE_IME
6308 "multi_byte_ime",
6309#endif
6310#ifdef FEAT_MULTI_LANG
6311 "multi_lang",
6312#endif
6313#ifdef FEAT_MZSCHEME
6314#ifndef DYNAMIC_MZSCHEME
6315 "mzscheme",
6316#endif
6317#endif
6318#ifdef FEAT_NUM64
6319 "num64",
6320#endif
6321#ifdef FEAT_OLE
6322 "ole",
6323#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006324#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327#ifdef FEAT_PATH_EXTRA
6328 "path_extra",
6329#endif
6330#ifdef FEAT_PERL
6331#ifndef DYNAMIC_PERL
6332 "perl",
6333#endif
6334#endif
6335#ifdef FEAT_PERSISTENT_UNDO
6336 "persistent_undo",
6337#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006338#if defined(FEAT_PYTHON)
6339 "python_compiled",
6340# if defined(DYNAMIC_PYTHON)
6341 "python_dynamic",
6342# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006344 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006345# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006347#if defined(FEAT_PYTHON3)
6348 "python3_compiled",
6349# if defined(DYNAMIC_PYTHON3)
6350 "python3_dynamic",
6351# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006353 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006354# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355#endif
6356#ifdef FEAT_POSTSCRIPT
6357 "postscript",
6358#endif
6359#ifdef FEAT_PRINTER
6360 "printer",
6361#endif
6362#ifdef FEAT_PROFILE
6363 "profile",
6364#endif
6365#ifdef FEAT_RELTIME
6366 "reltime",
6367#endif
6368#ifdef FEAT_QUICKFIX
6369 "quickfix",
6370#endif
6371#ifdef FEAT_RIGHTLEFT
6372 "rightleft",
6373#endif
6374#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6375 "ruby",
6376#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006378#ifdef FEAT_CMDL_INFO
6379 "showcmd",
6380 "cmdline_info",
6381#endif
6382#ifdef FEAT_SIGNS
6383 "signs",
6384#endif
6385#ifdef FEAT_SMARTINDENT
6386 "smartindent",
6387#endif
6388#ifdef STARTUPTIME
6389 "startuptime",
6390#endif
6391#ifdef FEAT_STL_OPT
6392 "statusline",
6393#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394#ifdef FEAT_NETBEANS_INTG
6395 "netbeans_intg",
6396#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006397#ifdef FEAT_SOUND
6398 "sound",
6399#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400#ifdef FEAT_SPELL
6401 "spell",
6402#endif
6403#ifdef FEAT_SYN_HL
6404 "syntax",
6405#endif
6406#if defined(USE_SYSTEM) || !defined(UNIX)
6407 "system",
6408#endif
6409#ifdef FEAT_TAG_BINS
6410 "tag_binary",
6411#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412#ifdef FEAT_TCL
6413# ifndef DYNAMIC_TCL
6414 "tcl",
6415# endif
6416#endif
6417#ifdef FEAT_TERMGUICOLORS
6418 "termguicolors",
6419#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006420#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006421 "terminal",
6422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423#ifdef TERMINFO
6424 "terminfo",
6425#endif
6426#ifdef FEAT_TERMRESPONSE
6427 "termresponse",
6428#endif
6429#ifdef FEAT_TEXTOBJ
6430 "textobjects",
6431#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006432#ifdef FEAT_TEXT_PROP
6433 "textprop",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435#ifdef HAVE_TGETENT
6436 "tgetent",
6437#endif
6438#ifdef FEAT_TIMERS
6439 "timers",
6440#endif
6441#ifdef FEAT_TITLE
6442 "title",
6443#endif
6444#ifdef FEAT_TOOLBAR
6445 "toolbar",
6446#endif
6447#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6448 "unnamedplus",
6449#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006450 "user-commands", /* was accidentally included in 5.4 */
6451 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006452#ifdef FEAT_VARTABS
6453 "vartabs",
6454#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006455 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456#ifdef FEAT_VIMINFO
6457 "viminfo",
6458#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006459 "vimscript-1",
6460 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006461 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006463 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006466#ifdef FEAT_VTP
6467 "vtp",
6468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469#ifdef FEAT_WILDIGN
6470 "wildignore",
6471#endif
6472#ifdef FEAT_WILDMENU
6473 "wildmenu",
6474#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#ifdef FEAT_WAK
6477 "winaltkeys",
6478#endif
6479#ifdef FEAT_WRITEBACKUP
6480 "writebackup",
6481#endif
6482#ifdef FEAT_XIM
6483 "xim",
6484#endif
6485#ifdef FEAT_XFONTSET
6486 "xfontset",
6487#endif
6488#ifdef FEAT_XPM_W32
6489 "xpm",
6490 "xpm_w32", /* for backward compatibility */
6491#else
6492# if defined(HAVE_XPM)
6493 "xpm",
6494# endif
6495#endif
6496#ifdef USE_XSMP
6497 "xsmp",
6498#endif
6499#ifdef USE_XSMP_INTERACT
6500 "xsmp_interact",
6501#endif
6502#ifdef FEAT_XCLIPBOARD
6503 "xterm_clipboard",
6504#endif
6505#ifdef FEAT_XTERM_SAVE
6506 "xterm_save",
6507#endif
6508#if defined(UNIX) && defined(FEAT_X11)
6509 "X11",
6510#endif
6511 NULL
6512 };
6513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006514 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515 for (i = 0; has_list[i] != NULL; ++i)
6516 if (STRICMP(name, has_list[i]) == 0)
6517 {
6518 n = TRUE;
6519 break;
6520 }
6521
6522 if (n == FALSE)
6523 {
6524 if (STRNICMP(name, "patch", 5) == 0)
6525 {
6526 if (name[5] == '-'
6527 && STRLEN(name) >= 11
6528 && vim_isdigit(name[6])
6529 && vim_isdigit(name[8])
6530 && vim_isdigit(name[10]))
6531 {
6532 int major = atoi((char *)name + 6);
6533 int minor = atoi((char *)name + 8);
6534
6535 /* Expect "patch-9.9.01234". */
6536 n = (major < VIM_VERSION_MAJOR
6537 || (major == VIM_VERSION_MAJOR
6538 && (minor < VIM_VERSION_MINOR
6539 || (minor == VIM_VERSION_MINOR
6540 && has_patch(atoi((char *)name + 10))))));
6541 }
6542 else
6543 n = has_patch(atoi((char *)name + 5));
6544 }
6545 else if (STRICMP(name, "vim_starting") == 0)
6546 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006547 else if (STRICMP(name, "ttyin") == 0)
6548 n = mch_input_isatty();
6549 else if (STRICMP(name, "ttyout") == 0)
6550 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 else if (STRICMP(name, "multi_byte_encoding") == 0)
6552 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006553#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554 else if (STRICMP(name, "balloon_multiline") == 0)
6555 n = multiline_balloon_available();
6556#endif
6557#ifdef DYNAMIC_TCL
6558 else if (STRICMP(name, "tcl") == 0)
6559 n = tcl_enabled(FALSE);
6560#endif
6561#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6562 else if (STRICMP(name, "iconv") == 0)
6563 n = iconv_enabled(FALSE);
6564#endif
6565#ifdef DYNAMIC_LUA
6566 else if (STRICMP(name, "lua") == 0)
6567 n = lua_enabled(FALSE);
6568#endif
6569#ifdef DYNAMIC_MZSCHEME
6570 else if (STRICMP(name, "mzscheme") == 0)
6571 n = mzscheme_enabled(FALSE);
6572#endif
6573#ifdef DYNAMIC_RUBY
6574 else if (STRICMP(name, "ruby") == 0)
6575 n = ruby_enabled(FALSE);
6576#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577#ifdef DYNAMIC_PYTHON
6578 else if (STRICMP(name, "python") == 0)
6579 n = python_enabled(FALSE);
6580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581#ifdef DYNAMIC_PYTHON3
6582 else if (STRICMP(name, "python3") == 0)
6583 n = python3_enabled(FALSE);
6584#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006585#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6586 else if (STRICMP(name, "pythonx") == 0)
6587 {
6588# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6589 if (p_pyx == 0)
6590 n = python3_enabled(FALSE) || python_enabled(FALSE);
6591 else if (p_pyx == 3)
6592 n = python3_enabled(FALSE);
6593 else if (p_pyx == 2)
6594 n = python_enabled(FALSE);
6595# elif defined(DYNAMIC_PYTHON)
6596 n = python_enabled(FALSE);
6597# elif defined(DYNAMIC_PYTHON3)
6598 n = python3_enabled(FALSE);
6599# endif
6600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601#endif
6602#ifdef DYNAMIC_PERL
6603 else if (STRICMP(name, "perl") == 0)
6604 n = perl_enabled(FALSE);
6605#endif
6606#ifdef FEAT_GUI
6607 else if (STRICMP(name, "gui_running") == 0)
6608 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609# ifdef FEAT_BROWSE
6610 else if (STRICMP(name, "browse") == 0)
6611 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6612# endif
6613#endif
6614#ifdef FEAT_SYN_HL
6615 else if (STRICMP(name, "syntax_items") == 0)
6616 n = syntax_present(curwin);
6617#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006618#ifdef FEAT_VTP
6619 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006620 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621#endif
6622#ifdef FEAT_NETBEANS_INTG
6623 else if (STRICMP(name, "netbeans_enabled") == 0)
6624 n = netbeans_active();
6625#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006626#ifdef FEAT_MOUSE_GPM
6627 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6628 n = gpm_enabled();
6629#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006630#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006631 else if (STRICMP(name, "terminal") == 0)
6632 n = terminal_enabled();
6633#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006634#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006635 else if (STRICMP(name, "conpty") == 0)
6636 n = use_conpty();
6637#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 }
6639
6640 rettv->vval.v_number = n;
6641}
6642
6643/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 * "haslocaldir()" function
6645 */
6646 static void
6647f_haslocaldir(typval_T *argvars, typval_T *rettv)
6648{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006649 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 win_T *wp = NULL;
6651
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006652 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6653
6654 // Check for window-local and tab-local directories
6655 if (wp != NULL && wp->w_localdir != NULL)
6656 rettv->vval.v_number = 1;
6657 else if (tp != NULL && tp->tp_localdir != NULL)
6658 rettv->vval.v_number = 2;
6659 else
6660 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661}
6662
6663/*
6664 * "hasmapto()" function
6665 */
6666 static void
6667f_hasmapto(typval_T *argvars, typval_T *rettv)
6668{
6669 char_u *name;
6670 char_u *mode;
6671 char_u buf[NUMBUFLEN];
6672 int abbr = FALSE;
6673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006674 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 if (argvars[1].v_type == VAR_UNKNOWN)
6676 mode = (char_u *)"nvo";
6677 else
6678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006679 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006681 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 }
6683
6684 if (map_to_exists(name, mode, abbr))
6685 rettv->vval.v_number = TRUE;
6686 else
6687 rettv->vval.v_number = FALSE;
6688}
6689
6690/*
6691 * "histadd()" function
6692 */
6693 static void
6694f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6695{
6696#ifdef FEAT_CMDHIST
6697 int histype;
6698 char_u *str;
6699 char_u buf[NUMBUFLEN];
6700#endif
6701
6702 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006703 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 return;
6705#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006706 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707 histype = str != NULL ? get_histtype(str) : -1;
6708 if (histype >= 0)
6709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006710 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711 if (*str != NUL)
6712 {
6713 init_history();
6714 add_to_history(histype, str, FALSE, NUL);
6715 rettv->vval.v_number = TRUE;
6716 return;
6717 }
6718 }
6719#endif
6720}
6721
6722/*
6723 * "histdel()" function
6724 */
6725 static void
6726f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6727{
6728#ifdef FEAT_CMDHIST
6729 int n;
6730 char_u buf[NUMBUFLEN];
6731 char_u *str;
6732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006733 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 if (str == NULL)
6735 n = 0;
6736 else if (argvars[1].v_type == VAR_UNKNOWN)
6737 /* only one argument: clear entire history */
6738 n = clr_history(get_histtype(str));
6739 else if (argvars[1].v_type == VAR_NUMBER)
6740 /* index given: remove that entry */
6741 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006742 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 else
6744 /* string given: remove all matching entries */
6745 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006746 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 rettv->vval.v_number = n;
6748#endif
6749}
6750
6751/*
6752 * "histget()" function
6753 */
6754 static void
6755f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6756{
6757#ifdef FEAT_CMDHIST
6758 int type;
6759 int idx;
6760 char_u *str;
6761
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006762 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763 if (str == NULL)
6764 rettv->vval.v_string = NULL;
6765 else
6766 {
6767 type = get_histtype(str);
6768 if (argvars[1].v_type == VAR_UNKNOWN)
6769 idx = get_history_idx(type);
6770 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006771 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 /* -1 on type error */
6773 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6774 }
6775#else
6776 rettv->vval.v_string = NULL;
6777#endif
6778 rettv->v_type = VAR_STRING;
6779}
6780
6781/*
6782 * "histnr()" function
6783 */
6784 static void
6785f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6786{
6787 int i;
6788
6789#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006790 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006791
6792 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6793 if (i >= HIST_CMD && i < HIST_COUNT)
6794 i = get_history_idx(i);
6795 else
6796#endif
6797 i = -1;
6798 rettv->vval.v_number = i;
6799}
6800
6801/*
6802 * "highlightID(name)" function
6803 */
6804 static void
6805f_hlID(typval_T *argvars, typval_T *rettv)
6806{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006807 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808}
6809
6810/*
6811 * "highlight_exists()" function
6812 */
6813 static void
6814f_hlexists(typval_T *argvars, typval_T *rettv)
6815{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006816 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817}
6818
6819/*
6820 * "hostname()" function
6821 */
6822 static void
6823f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6824{
6825 char_u hostname[256];
6826
6827 mch_get_host_name(hostname, 256);
6828 rettv->v_type = VAR_STRING;
6829 rettv->vval.v_string = vim_strsave(hostname);
6830}
6831
6832/*
6833 * iconv() function
6834 */
6835 static void
6836f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6837{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838 char_u buf1[NUMBUFLEN];
6839 char_u buf2[NUMBUFLEN];
6840 char_u *from, *to, *str;
6841 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006842
6843 rettv->v_type = VAR_STRING;
6844 rettv->vval.v_string = NULL;
6845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 str = tv_get_string(&argvars[0]);
6847 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6848 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 vimconv.vc_type = CONV_NONE;
6850 convert_setup(&vimconv, from, to);
6851
6852 /* If the encodings are equal, no conversion needed. */
6853 if (vimconv.vc_type == CONV_NONE)
6854 rettv->vval.v_string = vim_strsave(str);
6855 else
6856 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6857
6858 convert_setup(&vimconv, NULL, NULL);
6859 vim_free(from);
6860 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861}
6862
6863/*
6864 * "indent()" function
6865 */
6866 static void
6867f_indent(typval_T *argvars, typval_T *rettv)
6868{
6869 linenr_T lnum;
6870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6873 rettv->vval.v_number = get_indent_lnum(lnum);
6874 else
6875 rettv->vval.v_number = -1;
6876}
6877
6878/*
6879 * "index()" function
6880 */
6881 static void
6882f_index(typval_T *argvars, typval_T *rettv)
6883{
6884 list_T *l;
6885 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006886 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 long idx = 0;
6888 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006889 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890
6891 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006892 if (argvars[0].v_type == VAR_BLOB)
6893 {
6894 typval_T tv;
6895 int start = 0;
6896
6897 if (argvars[2].v_type != VAR_UNKNOWN)
6898 {
6899 start = tv_get_number_chk(&argvars[2], &error);
6900 if (error)
6901 return;
6902 }
6903 b = argvars[0].vval.v_blob;
6904 if (b == NULL)
6905 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006906 if (start < 0)
6907 {
6908 start = blob_len(b) + start;
6909 if (start < 0)
6910 start = 0;
6911 }
6912
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006913 for (idx = start; idx < blob_len(b); ++idx)
6914 {
6915 tv.v_type = VAR_NUMBER;
6916 tv.vval.v_number = blob_get(b, idx);
6917 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6918 {
6919 rettv->vval.v_number = idx;
6920 return;
6921 }
6922 }
6923 return;
6924 }
6925 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006927 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 return;
6929 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 l = argvars[0].vval.v_list;
6932 if (l != NULL)
6933 {
6934 item = l->lv_first;
6935 if (argvars[2].v_type != VAR_UNKNOWN)
6936 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 /* Start at specified item. Use the cached index that list_find()
6938 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 idx = l->lv_idx;
6941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 if (error)
6944 item = NULL;
6945 }
6946
6947 for ( ; item != NULL; item = item->li_next, ++idx)
6948 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6949 {
6950 rettv->vval.v_number = idx;
6951 break;
6952 }
6953 }
6954}
6955
6956static int inputsecret_flag = 0;
6957
6958/*
6959 * "input()" function
6960 * Also handles inputsecret() when inputsecret is set.
6961 */
6962 static void
6963f_input(typval_T *argvars, typval_T *rettv)
6964{
6965 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6966}
6967
6968/*
6969 * "inputdialog()" function
6970 */
6971 static void
6972f_inputdialog(typval_T *argvars, typval_T *rettv)
6973{
6974#if defined(FEAT_GUI_TEXTDIALOG)
6975 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6976 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6977 {
6978 char_u *message;
6979 char_u buf[NUMBUFLEN];
6980 char_u *defstr = (char_u *)"";
6981
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006982 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006983 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006984 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6986 else
6987 IObuff[0] = NUL;
6988 if (message != NULL && defstr != NULL
6989 && do_dialog(VIM_QUESTION, NULL, message,
6990 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6991 rettv->vval.v_string = vim_strsave(IObuff);
6992 else
6993 {
6994 if (message != NULL && defstr != NULL
6995 && argvars[1].v_type != VAR_UNKNOWN
6996 && argvars[2].v_type != VAR_UNKNOWN)
6997 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006998 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999 else
7000 rettv->vval.v_string = NULL;
7001 }
7002 rettv->v_type = VAR_STRING;
7003 }
7004 else
7005#endif
7006 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7007}
7008
7009/*
7010 * "inputlist()" function
7011 */
7012 static void
7013f_inputlist(typval_T *argvars, typval_T *rettv)
7014{
7015 listitem_T *li;
7016 int selected;
7017 int mouse_used;
7018
7019#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007020 /* While starting up, there is no place to enter text. When running tests
7021 * with --not-a-term we assume feedkeys() will be used. */
7022 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 return;
7024#endif
7025 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007027 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 return;
7029 }
7030
7031 msg_start();
7032 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7033 lines_left = Rows; /* avoid more prompt */
7034 msg_scroll = TRUE;
7035 msg_clr_eos();
7036
7037 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7038 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007039 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 msg_putchar('\n');
7041 }
7042
7043 /* Ask for choice. */
7044 selected = prompt_for_number(&mouse_used);
7045 if (mouse_used)
7046 selected -= lines_left;
7047
7048 rettv->vval.v_number = selected;
7049}
7050
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7052
7053/*
7054 * "inputrestore()" function
7055 */
7056 static void
7057f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7058{
7059 if (ga_userinput.ga_len > 0)
7060 {
7061 --ga_userinput.ga_len;
7062 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7063 + ga_userinput.ga_len);
7064 /* default return is zero == OK */
7065 }
7066 else if (p_verbose > 1)
7067 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007068 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 rettv->vval.v_number = 1; /* Failed */
7070 }
7071}
7072
7073/*
7074 * "inputsave()" function
7075 */
7076 static void
7077f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7078{
7079 /* Add an entry to the stack of typeahead storage. */
7080 if (ga_grow(&ga_userinput, 1) == OK)
7081 {
7082 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7083 + ga_userinput.ga_len);
7084 ++ga_userinput.ga_len;
7085 /* default return is zero == OK */
7086 }
7087 else
7088 rettv->vval.v_number = 1; /* Failed */
7089}
7090
7091/*
7092 * "inputsecret()" function
7093 */
7094 static void
7095f_inputsecret(typval_T *argvars, typval_T *rettv)
7096{
7097 ++cmdline_star;
7098 ++inputsecret_flag;
7099 f_input(argvars, rettv);
7100 --cmdline_star;
7101 --inputsecret_flag;
7102}
7103
7104/*
7105 * "insert()" function
7106 */
7107 static void
7108f_insert(typval_T *argvars, typval_T *rettv)
7109{
7110 long before = 0;
7111 listitem_T *item;
7112 list_T *l;
7113 int error = FALSE;
7114
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007115 if (argvars[0].v_type == VAR_BLOB)
7116 {
7117 int val, len;
7118 char_u *p;
7119
7120 len = blob_len(argvars[0].vval.v_blob);
7121 if (argvars[2].v_type != VAR_UNKNOWN)
7122 {
7123 before = (long)tv_get_number_chk(&argvars[2], &error);
7124 if (error)
7125 return; // type error; errmsg already given
7126 if (before < 0 || before > len)
7127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007128 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007129 return;
7130 }
7131 }
7132 val = tv_get_number_chk(&argvars[1], &error);
7133 if (error)
7134 return;
7135 if (val < 0 || val > 255)
7136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007137 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007138 return;
7139 }
7140
7141 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7142 return;
7143 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7144 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7145 *(p + before) = val;
7146 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7147
7148 copy_tv(&argvars[0], rettv);
7149 }
7150 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007151 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007152 else if ((l = argvars[0].vval.v_list) != NULL
7153 && !var_check_lock(l->lv_lock,
7154 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 {
7156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007157 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158 if (error)
7159 return; /* type error; errmsg already given */
7160
7161 if (before == l->lv_len)
7162 item = NULL;
7163 else
7164 {
7165 item = list_find(l, before);
7166 if (item == NULL)
7167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007168 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 l = NULL;
7170 }
7171 }
7172 if (l != NULL)
7173 {
7174 list_insert_tv(l, &argvars[1], item);
7175 copy_tv(&argvars[0], rettv);
7176 }
7177 }
7178}
7179
7180/*
7181 * "invert(expr)" function
7182 */
7183 static void
7184f_invert(typval_T *argvars, typval_T *rettv)
7185{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187}
7188
7189/*
7190 * "isdirectory()" function
7191 */
7192 static void
7193f_isdirectory(typval_T *argvars, typval_T *rettv)
7194{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196}
7197
7198/*
7199 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7200 * or it refers to a List or Dictionary that is locked.
7201 */
7202 static int
7203tv_islocked(typval_T *tv)
7204{
7205 return (tv->v_lock & VAR_LOCKED)
7206 || (tv->v_type == VAR_LIST
7207 && tv->vval.v_list != NULL
7208 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7209 || (tv->v_type == VAR_DICT
7210 && tv->vval.v_dict != NULL
7211 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7212}
7213
7214/*
7215 * "islocked()" function
7216 */
7217 static void
7218f_islocked(typval_T *argvars, typval_T *rettv)
7219{
7220 lval_T lv;
7221 char_u *end;
7222 dictitem_T *di;
7223
7224 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007225 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007226 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 if (end != NULL && lv.ll_name != NULL)
7228 {
7229 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007230 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 else
7232 {
7233 if (lv.ll_tv == NULL)
7234 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007235 di = find_var(lv.ll_name, NULL, TRUE);
7236 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007238 /* Consider a variable locked when:
7239 * 1. the variable itself is locked
7240 * 2. the value of the variable is locked.
7241 * 3. the List or Dict value is locked.
7242 */
7243 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7244 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 }
7246 }
7247 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007248 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007249 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007250 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 else if (lv.ll_list != NULL)
7252 /* List item. */
7253 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7254 else
7255 /* Dictionary item. */
7256 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7257 }
7258 }
7259
7260 clear_lval(&lv);
7261}
7262
7263#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7264/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007265 * "isinf()" function
7266 */
7267 static void
7268f_isinf(typval_T *argvars, typval_T *rettv)
7269{
7270 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7271 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7272}
7273
7274/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 * "isnan()" function
7276 */
7277 static void
7278f_isnan(typval_T *argvars, typval_T *rettv)
7279{
7280 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7281 && isnan(argvars[0].vval.v_float);
7282}
7283#endif
7284
7285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 * "last_buffer_nr()" function.
7287 */
7288 static void
7289f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7290{
7291 int n = 0;
7292 buf_T *buf;
7293
Bram Moolenaar29323592016-07-24 22:04:11 +02007294 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 if (n < buf->b_fnum)
7296 n = buf->b_fnum;
7297
7298 rettv->vval.v_number = n;
7299}
7300
7301/*
7302 * "len()" function
7303 */
7304 static void
7305f_len(typval_T *argvars, typval_T *rettv)
7306{
7307 switch (argvars[0].v_type)
7308 {
7309 case VAR_STRING:
7310 case VAR_NUMBER:
7311 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007312 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007314 case VAR_BLOB:
7315 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7316 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 case VAR_LIST:
7318 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7319 break;
7320 case VAR_DICT:
7321 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7322 break;
7323 case VAR_UNKNOWN:
7324 case VAR_SPECIAL:
7325 case VAR_FLOAT:
7326 case VAR_FUNC:
7327 case VAR_PARTIAL:
7328 case VAR_JOB:
7329 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 break;
7332 }
7333}
7334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007335 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007336libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007337{
7338#ifdef FEAT_LIBCALL
7339 char_u *string_in;
7340 char_u **string_result;
7341 int nr_result;
7342#endif
7343
7344 rettv->v_type = type;
7345 if (type != VAR_NUMBER)
7346 rettv->vval.v_string = NULL;
7347
7348 if (check_restricted() || check_secure())
7349 return;
7350
7351#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007352 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7354 {
7355 string_in = NULL;
7356 if (argvars[2].v_type == VAR_STRING)
7357 string_in = argvars[2].vval.v_string;
7358 if (type == VAR_NUMBER)
7359 string_result = NULL;
7360 else
7361 string_result = &rettv->vval.v_string;
7362 if (mch_libcall(argvars[0].vval.v_string,
7363 argvars[1].vval.v_string,
7364 string_in,
7365 argvars[2].vval.v_number,
7366 string_result,
7367 &nr_result) == OK
7368 && type == VAR_NUMBER)
7369 rettv->vval.v_number = nr_result;
7370 }
7371#endif
7372}
7373
7374/*
7375 * "libcall()" function
7376 */
7377 static void
7378f_libcall(typval_T *argvars, typval_T *rettv)
7379{
7380 libcall_common(argvars, rettv, VAR_STRING);
7381}
7382
7383/*
7384 * "libcallnr()" function
7385 */
7386 static void
7387f_libcallnr(typval_T *argvars, typval_T *rettv)
7388{
7389 libcall_common(argvars, rettv, VAR_NUMBER);
7390}
7391
7392/*
7393 * "line(string)" function
7394 */
7395 static void
7396f_line(typval_T *argvars, typval_T *rettv)
7397{
7398 linenr_T lnum = 0;
7399 pos_T *fp;
7400 int fnum;
7401
7402 fp = var2fpos(&argvars[0], TRUE, &fnum);
7403 if (fp != NULL)
7404 lnum = fp->lnum;
7405 rettv->vval.v_number = lnum;
7406}
7407
7408/*
7409 * "line2byte(lnum)" function
7410 */
7411 static void
7412f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7413{
7414#ifndef FEAT_BYTEOFF
7415 rettv->vval.v_number = -1;
7416#else
7417 linenr_T lnum;
7418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007419 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007420 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7421 rettv->vval.v_number = -1;
7422 else
7423 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7424 if (rettv->vval.v_number >= 0)
7425 ++rettv->vval.v_number;
7426#endif
7427}
7428
7429/*
7430 * "lispindent(lnum)" function
7431 */
7432 static void
7433f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7434{
7435#ifdef FEAT_LISP
7436 pos_T pos;
7437 linenr_T lnum;
7438
7439 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007440 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7442 {
7443 curwin->w_cursor.lnum = lnum;
7444 rettv->vval.v_number = get_lisp_indent();
7445 curwin->w_cursor = pos;
7446 }
7447 else
7448#endif
7449 rettv->vval.v_number = -1;
7450}
7451
7452/*
7453 * "localtime()" function
7454 */
7455 static void
7456f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7457{
7458 rettv->vval.v_number = (varnumber_T)time(NULL);
7459}
7460
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461#ifdef FEAT_FLOAT
7462/*
7463 * "log()" function
7464 */
7465 static void
7466f_log(typval_T *argvars, typval_T *rettv)
7467{
7468 float_T f = 0.0;
7469
7470 rettv->v_type = VAR_FLOAT;
7471 if (get_float_arg(argvars, &f) == OK)
7472 rettv->vval.v_float = log(f);
7473 else
7474 rettv->vval.v_float = 0.0;
7475}
7476
7477/*
7478 * "log10()" function
7479 */
7480 static void
7481f_log10(typval_T *argvars, typval_T *rettv)
7482{
7483 float_T f = 0.0;
7484
7485 rettv->v_type = VAR_FLOAT;
7486 if (get_float_arg(argvars, &f) == OK)
7487 rettv->vval.v_float = log10(f);
7488 else
7489 rettv->vval.v_float = 0.0;
7490}
7491#endif
7492
7493#ifdef FEAT_LUA
7494/*
7495 * "luaeval()" function
7496 */
7497 static void
7498f_luaeval(typval_T *argvars, typval_T *rettv)
7499{
7500 char_u *str;
7501 char_u buf[NUMBUFLEN];
7502
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007503 if (check_restricted() || check_secure())
7504 return;
7505
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007506 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 do_luaeval(str, argvars + 1, rettv);
7508}
7509#endif
7510
7511/*
7512 * "map()" function
7513 */
7514 static void
7515f_map(typval_T *argvars, typval_T *rettv)
7516{
7517 filter_map(argvars, rettv, TRUE);
7518}
7519
7520/*
7521 * "maparg()" function
7522 */
7523 static void
7524f_maparg(typval_T *argvars, typval_T *rettv)
7525{
7526 get_maparg(argvars, rettv, TRUE);
7527}
7528
7529/*
7530 * "mapcheck()" function
7531 */
7532 static void
7533f_mapcheck(typval_T *argvars, typval_T *rettv)
7534{
7535 get_maparg(argvars, rettv, FALSE);
7536}
7537
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007538typedef enum
7539{
7540 MATCH_END, /* matchend() */
7541 MATCH_MATCH, /* match() */
7542 MATCH_STR, /* matchstr() */
7543 MATCH_LIST, /* matchlist() */
7544 MATCH_POS /* matchstrpos() */
7545} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546
7547 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007548find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549{
7550 char_u *str = NULL;
7551 long len = 0;
7552 char_u *expr = NULL;
7553 char_u *pat;
7554 regmatch_T regmatch;
7555 char_u patbuf[NUMBUFLEN];
7556 char_u strbuf[NUMBUFLEN];
7557 char_u *save_cpo;
7558 long start = 0;
7559 long nth = 1;
7560 colnr_T startcol = 0;
7561 int match = 0;
7562 list_T *l = NULL;
7563 listitem_T *li = NULL;
7564 long idx = 0;
7565 char_u *tofree = NULL;
7566
7567 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7568 save_cpo = p_cpo;
7569 p_cpo = (char_u *)"";
7570
7571 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007572 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007574 /* type MATCH_LIST: return empty list when there are no matches.
7575 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576 if (rettv_list_alloc(rettv) == FAIL)
7577 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007578 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579 && (list_append_string(rettv->vval.v_list,
7580 (char_u *)"", 0) == FAIL
7581 || list_append_number(rettv->vval.v_list,
7582 (varnumber_T)-1) == FAIL
7583 || list_append_number(rettv->vval.v_list,
7584 (varnumber_T)-1) == FAIL
7585 || list_append_number(rettv->vval.v_list,
7586 (varnumber_T)-1) == FAIL))
7587 {
7588 list_free(rettv->vval.v_list);
7589 rettv->vval.v_list = NULL;
7590 goto theend;
7591 }
7592 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007593 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 {
7595 rettv->v_type = VAR_STRING;
7596 rettv->vval.v_string = NULL;
7597 }
7598
7599 if (argvars[0].v_type == VAR_LIST)
7600 {
7601 if ((l = argvars[0].vval.v_list) == NULL)
7602 goto theend;
7603 li = l->lv_first;
7604 }
7605 else
7606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007607 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 len = (long)STRLEN(str);
7609 }
7610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 if (pat == NULL)
7613 goto theend;
7614
7615 if (argvars[2].v_type != VAR_UNKNOWN)
7616 {
7617 int error = FALSE;
7618
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007619 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620 if (error)
7621 goto theend;
7622 if (l != NULL)
7623 {
7624 li = list_find(l, start);
7625 if (li == NULL)
7626 goto theend;
7627 idx = l->lv_idx; /* use the cached index */
7628 }
7629 else
7630 {
7631 if (start < 0)
7632 start = 0;
7633 if (start > len)
7634 goto theend;
7635 /* When "count" argument is there ignore matches before "start",
7636 * otherwise skip part of the string. Differs when pattern is "^"
7637 * or "\<". */
7638 if (argvars[3].v_type != VAR_UNKNOWN)
7639 startcol = start;
7640 else
7641 {
7642 str += start;
7643 len -= start;
7644 }
7645 }
7646
7647 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 if (error)
7650 goto theend;
7651 }
7652
7653 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7654 if (regmatch.regprog != NULL)
7655 {
7656 regmatch.rm_ic = p_ic;
7657
7658 for (;;)
7659 {
7660 if (l != NULL)
7661 {
7662 if (li == NULL)
7663 {
7664 match = FALSE;
7665 break;
7666 }
7667 vim_free(tofree);
7668 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7669 if (str == NULL)
7670 break;
7671 }
7672
7673 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7674
7675 if (match && --nth <= 0)
7676 break;
7677 if (l == NULL && !match)
7678 break;
7679
7680 /* Advance to just after the match. */
7681 if (l != NULL)
7682 {
7683 li = li->li_next;
7684 ++idx;
7685 }
7686 else
7687 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 startcol = (colnr_T)(regmatch.startp[0]
7689 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 if (startcol > (colnr_T)len
7691 || str + startcol <= regmatch.startp[0])
7692 {
7693 match = FALSE;
7694 break;
7695 }
7696 }
7697 }
7698
7699 if (match)
7700 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007701 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702 {
7703 listitem_T *li1 = rettv->vval.v_list->lv_first;
7704 listitem_T *li2 = li1->li_next;
7705 listitem_T *li3 = li2->li_next;
7706 listitem_T *li4 = li3->li_next;
7707
7708 vim_free(li1->li_tv.vval.v_string);
7709 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7710 (int)(regmatch.endp[0] - regmatch.startp[0]));
7711 li3->li_tv.vval.v_number =
7712 (varnumber_T)(regmatch.startp[0] - expr);
7713 li4->li_tv.vval.v_number =
7714 (varnumber_T)(regmatch.endp[0] - expr);
7715 if (l != NULL)
7716 li2->li_tv.vval.v_number = (varnumber_T)idx;
7717 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007718 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 {
7720 int i;
7721
7722 /* return list with matched string and submatches */
7723 for (i = 0; i < NSUBEXP; ++i)
7724 {
7725 if (regmatch.endp[i] == NULL)
7726 {
7727 if (list_append_string(rettv->vval.v_list,
7728 (char_u *)"", 0) == FAIL)
7729 break;
7730 }
7731 else if (list_append_string(rettv->vval.v_list,
7732 regmatch.startp[i],
7733 (int)(regmatch.endp[i] - regmatch.startp[i]))
7734 == FAIL)
7735 break;
7736 }
7737 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007738 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007739 {
7740 /* return matched string */
7741 if (l != NULL)
7742 copy_tv(&li->li_tv, rettv);
7743 else
7744 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7745 (int)(regmatch.endp[0] - regmatch.startp[0]));
7746 }
7747 else if (l != NULL)
7748 rettv->vval.v_number = idx;
7749 else
7750 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007751 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 rettv->vval.v_number =
7753 (varnumber_T)(regmatch.startp[0] - str);
7754 else
7755 rettv->vval.v_number =
7756 (varnumber_T)(regmatch.endp[0] - str);
7757 rettv->vval.v_number += (varnumber_T)(str - expr);
7758 }
7759 }
7760 vim_regfree(regmatch.regprog);
7761 }
7762
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007763theend:
7764 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765 /* matchstrpos() without a list: drop the second item. */
7766 listitem_remove(rettv->vval.v_list,
7767 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007768 vim_free(tofree);
7769 p_cpo = save_cpo;
7770}
7771
7772/*
7773 * "match()" function
7774 */
7775 static void
7776f_match(typval_T *argvars, typval_T *rettv)
7777{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007778 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779}
7780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781/*
7782 * "matchend()" function
7783 */
7784 static void
7785f_matchend(typval_T *argvars, typval_T *rettv)
7786{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007787 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007788}
7789
7790/*
7791 * "matchlist()" function
7792 */
7793 static void
7794f_matchlist(typval_T *argvars, typval_T *rettv)
7795{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007796 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797}
7798
7799/*
7800 * "matchstr()" function
7801 */
7802 static void
7803f_matchstr(typval_T *argvars, typval_T *rettv)
7804{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007805 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806}
7807
7808/*
7809 * "matchstrpos()" function
7810 */
7811 static void
7812f_matchstrpos(typval_T *argvars, typval_T *rettv)
7813{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007814 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815}
7816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 static void
7818max_min(typval_T *argvars, typval_T *rettv, int domax)
7819{
7820 varnumber_T n = 0;
7821 varnumber_T i;
7822 int error = FALSE;
7823
7824 if (argvars[0].v_type == VAR_LIST)
7825 {
7826 list_T *l;
7827 listitem_T *li;
7828
7829 l = argvars[0].vval.v_list;
7830 if (l != NULL)
7831 {
7832 li = l->lv_first;
7833 if (li != NULL)
7834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007835 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 for (;;)
7837 {
7838 li = li->li_next;
7839 if (li == NULL)
7840 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 if (domax ? i > n : i < n)
7843 n = i;
7844 }
7845 }
7846 }
7847 }
7848 else if (argvars[0].v_type == VAR_DICT)
7849 {
7850 dict_T *d;
7851 int first = TRUE;
7852 hashitem_T *hi;
7853 int todo;
7854
7855 d = argvars[0].vval.v_dict;
7856 if (d != NULL)
7857 {
7858 todo = (int)d->dv_hashtab.ht_used;
7859 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7860 {
7861 if (!HASHITEM_EMPTY(hi))
7862 {
7863 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007864 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865 if (first)
7866 {
7867 n = i;
7868 first = FALSE;
7869 }
7870 else if (domax ? i > n : i < n)
7871 n = i;
7872 }
7873 }
7874 }
7875 }
7876 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007877 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 rettv->vval.v_number = error ? 0 : n;
7879}
7880
7881/*
7882 * "max()" function
7883 */
7884 static void
7885f_max(typval_T *argvars, typval_T *rettv)
7886{
7887 max_min(argvars, rettv, TRUE);
7888}
7889
7890/*
7891 * "min()" function
7892 */
7893 static void
7894f_min(typval_T *argvars, typval_T *rettv)
7895{
7896 max_min(argvars, rettv, FALSE);
7897}
7898
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899/*
7900 * Create the directory in which "dir" is located, and higher levels when
7901 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007902 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 */
7904 static int
7905mkdir_recurse(char_u *dir, int prot)
7906{
7907 char_u *p;
7908 char_u *updir;
7909 int r = FAIL;
7910
7911 /* Get end of directory name in "dir".
7912 * We're done when it's "/" or "c:/". */
7913 p = gettail_sep(dir);
7914 if (p <= get_past_head(dir))
7915 return OK;
7916
7917 /* If the directory exists we're done. Otherwise: create it.*/
7918 updir = vim_strnsave(dir, (int)(p - dir));
7919 if (updir == NULL)
7920 return FAIL;
7921 if (mch_isdir(updir))
7922 r = OK;
7923 else if (mkdir_recurse(updir, prot) == OK)
7924 r = vim_mkdir_emsg(updir, prot);
7925 vim_free(updir);
7926 return r;
7927}
7928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929/*
7930 * "mkdir()" function
7931 */
7932 static void
7933f_mkdir(typval_T *argvars, typval_T *rettv)
7934{
7935 char_u *dir;
7936 char_u buf[NUMBUFLEN];
7937 int prot = 0755;
7938
7939 rettv->vval.v_number = FAIL;
7940 if (check_restricted() || check_secure())
7941 return;
7942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007943 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007945 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007947 if (*gettail(dir) == NUL)
7948 /* remove trailing slashes */
7949 *gettail_sep(dir) = NUL;
7950
7951 if (argvars[1].v_type != VAR_UNKNOWN)
7952 {
7953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007955 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007956 if (prot == -1)
7957 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007959 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007960 {
7961 if (mch_isdir(dir))
7962 {
7963 /* With the "p" flag it's OK if the dir already exists. */
7964 rettv->vval.v_number = OK;
7965 return;
7966 }
7967 mkdir_recurse(dir, prot);
7968 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007970 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972
7973/*
7974 * "mode()" function
7975 */
7976 static void
7977f_mode(typval_T *argvars, typval_T *rettv)
7978{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007979 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980
Bram Moolenaar612cc382018-07-29 15:34:26 +02007981 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982
7983 if (time_for_testing == 93784)
7984 {
7985 /* Testing the two-character code. */
7986 buf[0] = 'x';
7987 buf[1] = '!';
7988 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007989#ifdef FEAT_TERMINAL
7990 else if (term_use_loop())
7991 buf[0] = 't';
7992#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 else if (VIsual_active)
7994 {
7995 if (VIsual_select)
7996 buf[0] = VIsual_mode + 's' - 'v';
7997 else
7998 buf[0] = VIsual_mode;
7999 }
8000 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8001 || State == CONFIRM)
8002 {
8003 buf[0] = 'r';
8004 if (State == ASKMORE)
8005 buf[1] = 'm';
8006 else if (State == CONFIRM)
8007 buf[1] = '?';
8008 }
8009 else if (State == EXTERNCMD)
8010 buf[0] = '!';
8011 else if (State & INSERT)
8012 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 if (State & VREPLACE_FLAG)
8014 {
8015 buf[0] = 'R';
8016 buf[1] = 'v';
8017 }
8018 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008019 {
8020 if (State & REPLACE_FLAG)
8021 buf[0] = 'R';
8022 else
8023 buf[0] = 'i';
8024#ifdef FEAT_INS_EXPAND
8025 if (ins_compl_active())
8026 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008027 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008028 buf[1] = 'x';
8029#endif
8030 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008032 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 {
8034 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008035 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008037 else if (exmode_active == EXMODE_NORMAL)
8038 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 }
8040 else
8041 {
8042 buf[0] = 'n';
8043 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008044 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008046 // to be able to detect force-linewise/blockwise/characterwise operations
8047 buf[2] = motion_force;
8048 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008049 else if (restart_edit == 'I' || restart_edit == 'R'
8050 || restart_edit == 'V')
8051 {
8052 buf[1] = 'i';
8053 buf[2] = restart_edit;
8054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 }
8056
8057 /* Clear out the minor mode when the argument is not a non-zero number or
8058 * non-empty string. */
8059 if (!non_zero_arg(&argvars[0]))
8060 buf[1] = NUL;
8061
8062 rettv->vval.v_string = vim_strsave(buf);
8063 rettv->v_type = VAR_STRING;
8064}
8065
8066#if defined(FEAT_MZSCHEME) || defined(PROTO)
8067/*
8068 * "mzeval()" function
8069 */
8070 static void
8071f_mzeval(typval_T *argvars, typval_T *rettv)
8072{
8073 char_u *str;
8074 char_u buf[NUMBUFLEN];
8075
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008076 if (check_restricted() || check_secure())
8077 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008078 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 do_mzeval(str, rettv);
8080}
8081
8082 void
8083mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8084{
8085 typval_T argvars[3];
8086
8087 argvars[0].v_type = VAR_STRING;
8088 argvars[0].vval.v_string = name;
8089 copy_tv(args, &argvars[1]);
8090 argvars[2].v_type = VAR_UNKNOWN;
8091 f_call(argvars, rettv);
8092 clear_tv(&argvars[1]);
8093}
8094#endif
8095
8096/*
8097 * "nextnonblank()" function
8098 */
8099 static void
8100f_nextnonblank(typval_T *argvars, typval_T *rettv)
8101{
8102 linenr_T lnum;
8103
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008104 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 {
8106 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8107 {
8108 lnum = 0;
8109 break;
8110 }
8111 if (*skipwhite(ml_get(lnum)) != NUL)
8112 break;
8113 }
8114 rettv->vval.v_number = lnum;
8115}
8116
8117/*
8118 * "nr2char()" function
8119 */
8120 static void
8121f_nr2char(typval_T *argvars, typval_T *rettv)
8122{
8123 char_u buf[NUMBUFLEN];
8124
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 if (has_mbyte)
8126 {
8127 int utf8 = 0;
8128
8129 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008130 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008132 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008134 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 }
8136 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008138 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 buf[1] = NUL;
8140 }
8141 rettv->v_type = VAR_STRING;
8142 rettv->vval.v_string = vim_strsave(buf);
8143}
8144
8145/*
8146 * "or(expr, expr)" function
8147 */
8148 static void
8149f_or(typval_T *argvars, typval_T *rettv)
8150{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008151 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8152 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153}
8154
8155/*
8156 * "pathshorten()" function
8157 */
8158 static void
8159f_pathshorten(typval_T *argvars, typval_T *rettv)
8160{
8161 char_u *p;
8162
8163 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008164 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 if (p == NULL)
8166 rettv->vval.v_string = NULL;
8167 else
8168 {
8169 p = vim_strsave(p);
8170 rettv->vval.v_string = p;
8171 if (p != NULL)
8172 shorten_dir(p);
8173 }
8174}
8175
8176#ifdef FEAT_PERL
8177/*
8178 * "perleval()" function
8179 */
8180 static void
8181f_perleval(typval_T *argvars, typval_T *rettv)
8182{
8183 char_u *str;
8184 char_u buf[NUMBUFLEN];
8185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008186 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 do_perleval(str, rettv);
8188}
8189#endif
8190
8191#ifdef FEAT_FLOAT
8192/*
8193 * "pow()" function
8194 */
8195 static void
8196f_pow(typval_T *argvars, typval_T *rettv)
8197{
8198 float_T fx = 0.0, fy = 0.0;
8199
8200 rettv->v_type = VAR_FLOAT;
8201 if (get_float_arg(argvars, &fx) == OK
8202 && get_float_arg(&argvars[1], &fy) == OK)
8203 rettv->vval.v_float = pow(fx, fy);
8204 else
8205 rettv->vval.v_float = 0.0;
8206}
8207#endif
8208
8209/*
8210 * "prevnonblank()" function
8211 */
8212 static void
8213f_prevnonblank(typval_T *argvars, typval_T *rettv)
8214{
8215 linenr_T lnum;
8216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008217 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8219 lnum = 0;
8220 else
8221 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8222 --lnum;
8223 rettv->vval.v_number = lnum;
8224}
8225
8226/* This dummy va_list is here because:
8227 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8228 * - locally in the function results in a "used before set" warning
8229 * - using va_start() to initialize it gives "function with fixed args" error */
8230static va_list ap;
8231
8232/*
8233 * "printf()" function
8234 */
8235 static void
8236f_printf(typval_T *argvars, typval_T *rettv)
8237{
8238 char_u buf[NUMBUFLEN];
8239 int len;
8240 char_u *s;
8241 int saved_did_emsg = did_emsg;
8242 char *fmt;
8243
8244 rettv->v_type = VAR_STRING;
8245 rettv->vval.v_string = NULL;
8246
8247 /* Get the required length, allocate the buffer and do it for real. */
8248 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008250 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251 if (!did_emsg)
8252 {
8253 s = alloc(len + 1);
8254 if (s != NULL)
8255 {
8256 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008257 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8258 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 }
8260 }
8261 did_emsg |= saved_did_emsg;
8262}
8263
8264/*
8265 * "pumvisible()" function
8266 */
8267 static void
8268f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8269{
8270#ifdef FEAT_INS_EXPAND
8271 if (pum_visible())
8272 rettv->vval.v_number = 1;
8273#endif
8274}
8275
8276#ifdef FEAT_PYTHON3
8277/*
8278 * "py3eval()" function
8279 */
8280 static void
8281f_py3eval(typval_T *argvars, typval_T *rettv)
8282{
8283 char_u *str;
8284 char_u buf[NUMBUFLEN];
8285
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008286 if (check_restricted() || check_secure())
8287 return;
8288
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008289 if (p_pyx == 0)
8290 p_pyx = 3;
8291
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008292 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 do_py3eval(str, rettv);
8294}
8295#endif
8296
8297#ifdef FEAT_PYTHON
8298/*
8299 * "pyeval()" function
8300 */
8301 static void
8302f_pyeval(typval_T *argvars, typval_T *rettv)
8303{
8304 char_u *str;
8305 char_u buf[NUMBUFLEN];
8306
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008307 if (check_restricted() || check_secure())
8308 return;
8309
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008310 if (p_pyx == 0)
8311 p_pyx = 2;
8312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008313 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 do_pyeval(str, rettv);
8315}
8316#endif
8317
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008318#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8319/*
8320 * "pyxeval()" function
8321 */
8322 static void
8323f_pyxeval(typval_T *argvars, typval_T *rettv)
8324{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008325 if (check_restricted() || check_secure())
8326 return;
8327
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008328# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8329 init_pyxversion();
8330 if (p_pyx == 2)
8331 f_pyeval(argvars, rettv);
8332 else
8333 f_py3eval(argvars, rettv);
8334# elif defined(FEAT_PYTHON)
8335 f_pyeval(argvars, rettv);
8336# elif defined(FEAT_PYTHON3)
8337 f_py3eval(argvars, rettv);
8338# endif
8339}
8340#endif
8341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342/*
8343 * "range()" function
8344 */
8345 static void
8346f_range(typval_T *argvars, typval_T *rettv)
8347{
8348 varnumber_T start;
8349 varnumber_T end;
8350 varnumber_T stride = 1;
8351 varnumber_T i;
8352 int error = FALSE;
8353
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008354 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 if (argvars[1].v_type == VAR_UNKNOWN)
8356 {
8357 end = start - 1;
8358 start = 0;
8359 }
8360 else
8361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008362 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 }
8366
8367 if (error)
8368 return; /* type error; errmsg already given */
8369 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008370 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008372 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 else
8374 {
8375 if (rettv_list_alloc(rettv) == OK)
8376 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8377 if (list_append_number(rettv->vval.v_list,
8378 (varnumber_T)i) == FAIL)
8379 break;
8380 }
8381}
8382
8383/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008384 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008385 */
8386 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008387readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008388{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008389 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008390 typval_T save_val;
8391 typval_T rettv;
8392 typval_T argv[2];
8393 int retval = 0;
8394 int error = FALSE;
8395
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008396 if (expr->v_type == VAR_UNKNOWN)
8397 return 1;
8398
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008399 prepare_vimvar(VV_VAL, &save_val);
8400 set_vim_var_string(VV_VAL, name, -1);
8401 argv[0].v_type = VAR_STRING;
8402 argv[0].vval.v_string = name;
8403
8404 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8405 goto theend;
8406
8407 retval = tv_get_number_chk(&rettv, &error);
8408 if (error)
8409 retval = -1;
8410 clear_tv(&rettv);
8411
8412theend:
8413 set_vim_var_string(VV_VAL, NULL, 0);
8414 restore_vimvar(VV_VAL, &save_val);
8415 return retval;
8416}
8417
8418/*
8419 * "readdir()" function
8420 */
8421 static void
8422f_readdir(typval_T *argvars, typval_T *rettv)
8423{
8424 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008425 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008426 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008427 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008428 garray_T ga;
8429 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008430
8431 if (rettv_list_alloc(rettv) == FAIL)
8432 return;
8433 path = tv_get_string(&argvars[0]);
8434 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008435
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008436 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8437 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008438 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008439 for (i = 0; i < ga.ga_len; i++)
8440 {
8441 p = ((char_u **)ga.ga_data)[i];
8442 list_append_string(rettv->vval.v_list, p, -1);
8443 }
8444 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008445 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008446}
8447
8448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 * "readfile()" function
8450 */
8451 static void
8452f_readfile(typval_T *argvars, typval_T *rettv)
8453{
8454 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008455 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456 int failed = FALSE;
8457 char_u *fname;
8458 FILE *fd;
8459 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8460 int io_size = sizeof(buf);
8461 int readlen; /* size of last fread() */
8462 char_u *prev = NULL; /* previously read bytes, if any */
8463 long prevlen = 0; /* length of data in prev */
8464 long prevsize = 0; /* size of prev buffer */
8465 long maxline = MAXLNUM;
8466 long cnt = 0;
8467 char_u *p; /* position in buf */
8468 char_u *start; /* start of current line */
8469
8470 if (argvars[1].v_type != VAR_UNKNOWN)
8471 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008472 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008474 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8475 blob = TRUE;
8476
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008478 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 }
8480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008481 if (blob)
8482 {
8483 if (rettv_blob_alloc(rettv) == FAIL)
8484 return;
8485 }
8486 else
8487 {
8488 if (rettv_list_alloc(rettv) == FAIL)
8489 return;
8490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491
8492 /* Always open the file in binary mode, library functions have a mind of
8493 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008494 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008497 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 return;
8499 }
8500
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008501 if (blob)
8502 {
8503 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008505 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008506 blob_free(rettv->vval.v_blob);
8507 }
8508 fclose(fd);
8509 return;
8510 }
8511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 while (cnt < maxline || maxline < 0)
8513 {
8514 readlen = (int)fread(buf, 1, io_size, fd);
8515
8516 /* This for loop processes what was read, but is also entered at end
8517 * of file so that either:
8518 * - an incomplete line gets written
8519 * - a "binary" file gets an empty line at the end if it ends in a
8520 * newline. */
8521 for (p = buf, start = buf;
8522 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8523 ++p)
8524 {
8525 if (*p == '\n' || readlen <= 0)
8526 {
8527 listitem_T *li;
8528 char_u *s = NULL;
8529 long_u len = p - start;
8530
8531 /* Finished a line. Remove CRs before NL. */
8532 if (readlen > 0 && !binary)
8533 {
8534 while (len > 0 && start[len - 1] == '\r')
8535 --len;
8536 /* removal may cross back to the "prev" string */
8537 if (len == 0)
8538 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8539 --prevlen;
8540 }
8541 if (prevlen == 0)
8542 s = vim_strnsave(start, (int)len);
8543 else
8544 {
8545 /* Change "prev" buffer to be the right size. This way
8546 * the bytes are only copied once, and very long lines are
8547 * allocated only once. */
8548 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8549 {
8550 mch_memmove(s + prevlen, start, len);
8551 s[prevlen + len] = NUL;
8552 prev = NULL; /* the list will own the string */
8553 prevlen = prevsize = 0;
8554 }
8555 }
8556 if (s == NULL)
8557 {
8558 do_outofmem_msg((long_u) prevlen + len + 1);
8559 failed = TRUE;
8560 break;
8561 }
8562
8563 if ((li = listitem_alloc()) == NULL)
8564 {
8565 vim_free(s);
8566 failed = TRUE;
8567 break;
8568 }
8569 li->li_tv.v_type = VAR_STRING;
8570 li->li_tv.v_lock = 0;
8571 li->li_tv.vval.v_string = s;
8572 list_append(rettv->vval.v_list, li);
8573
8574 start = p + 1; /* step over newline */
8575 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8576 break;
8577 }
8578 else if (*p == NUL)
8579 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8581 * when finding the BF and check the previous two bytes. */
8582 else if (*p == 0xbf && enc_utf8 && !binary)
8583 {
8584 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8585 * + 1, these may be in the "prev" string. */
8586 char_u back1 = p >= buf + 1 ? p[-1]
8587 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8588 char_u back2 = p >= buf + 2 ? p[-2]
8589 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8590 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8591
8592 if (back2 == 0xef && back1 == 0xbb)
8593 {
8594 char_u *dest = p - 2;
8595
8596 /* Usually a BOM is at the beginning of a file, and so at
8597 * the beginning of a line; then we can just step over it.
8598 */
8599 if (start == dest)
8600 start = p + 1;
8601 else
8602 {
8603 /* have to shuffle buf to close gap */
8604 int adjust_prevlen = 0;
8605
8606 if (dest < buf)
8607 {
8608 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8609 dest = buf;
8610 }
8611 if (readlen > p - buf + 1)
8612 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8613 readlen -= 3 - adjust_prevlen;
8614 prevlen -= adjust_prevlen;
8615 p = dest - 1;
8616 }
8617 }
8618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 } /* for */
8620
8621 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8622 break;
8623 if (start < p)
8624 {
8625 /* There's part of a line in buf, store it in "prev". */
8626 if (p - start + prevlen >= prevsize)
8627 {
8628 /* need bigger "prev" buffer */
8629 char_u *newprev;
8630
8631 /* A common use case is ordinary text files and "prev" gets a
8632 * fragment of a line, so the first allocation is made
8633 * small, to avoid repeatedly 'allocing' large and
8634 * 'reallocing' small. */
8635 if (prevsize == 0)
8636 prevsize = (long)(p - start);
8637 else
8638 {
8639 long grow50pc = (prevsize * 3) / 2;
8640 long growmin = (long)((p - start) * 2 + prevlen);
8641 prevsize = grow50pc > growmin ? grow50pc : growmin;
8642 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008643 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (newprev == NULL)
8645 {
8646 do_outofmem_msg((long_u)prevsize);
8647 failed = TRUE;
8648 break;
8649 }
8650 prev = newprev;
8651 }
8652 /* Add the line part to end of "prev". */
8653 mch_memmove(prev + prevlen, start, p - start);
8654 prevlen += (long)(p - start);
8655 }
8656 } /* while */
8657
8658 /*
8659 * For a negative line count use only the lines at the end of the file,
8660 * free the rest.
8661 */
8662 if (!failed && maxline < 0)
8663 while (cnt > -maxline)
8664 {
8665 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8666 --cnt;
8667 }
8668
8669 if (failed)
8670 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008671 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008673 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 }
8675
8676 vim_free(prev);
8677 fclose(fd);
8678}
8679
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008680 static void
8681return_register(int regname, typval_T *rettv)
8682{
8683 char_u buf[2] = {0, 0};
8684
8685 buf[0] = (char_u)regname;
8686 rettv->v_type = VAR_STRING;
8687 rettv->vval.v_string = vim_strsave(buf);
8688}
8689
8690/*
8691 * "reg_executing()" function
8692 */
8693 static void
8694f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8695{
8696 return_register(reg_executing, rettv);
8697}
8698
8699/*
8700 * "reg_recording()" function
8701 */
8702 static void
8703f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8704{
8705 return_register(reg_recording, rettv);
8706}
8707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709/*
8710 * Convert a List to proftime_T.
8711 * Return FAIL when there is something wrong.
8712 */
8713 static int
8714list2proftime(typval_T *arg, proftime_T *tm)
8715{
8716 long n1, n2;
8717 int error = FALSE;
8718
8719 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8720 || arg->vval.v_list->lv_len != 2)
8721 return FAIL;
8722 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8723 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008724# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 tm->HighPart = n1;
8726 tm->LowPart = n2;
8727# else
8728 tm->tv_sec = n1;
8729 tm->tv_usec = n2;
8730# endif
8731 return error ? FAIL : OK;
8732}
8733#endif /* FEAT_RELTIME */
8734
8735/*
8736 * "reltime()" function
8737 */
8738 static void
8739f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8740{
8741#ifdef FEAT_RELTIME
8742 proftime_T res;
8743 proftime_T start;
8744
8745 if (argvars[0].v_type == VAR_UNKNOWN)
8746 {
8747 /* No arguments: get current time. */
8748 profile_start(&res);
8749 }
8750 else if (argvars[1].v_type == VAR_UNKNOWN)
8751 {
8752 if (list2proftime(&argvars[0], &res) == FAIL)
8753 return;
8754 profile_end(&res);
8755 }
8756 else
8757 {
8758 /* Two arguments: compute the difference. */
8759 if (list2proftime(&argvars[0], &start) == FAIL
8760 || list2proftime(&argvars[1], &res) == FAIL)
8761 return;
8762 profile_sub(&res, &start);
8763 }
8764
8765 if (rettv_list_alloc(rettv) == OK)
8766 {
8767 long n1, n2;
8768
Bram Moolenaar4f974752019-02-17 17:44:42 +01008769# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 n1 = res.HighPart;
8771 n2 = res.LowPart;
8772# else
8773 n1 = res.tv_sec;
8774 n2 = res.tv_usec;
8775# endif
8776 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8777 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8778 }
8779#endif
8780}
8781
8782#ifdef FEAT_FLOAT
8783/*
8784 * "reltimefloat()" function
8785 */
8786 static void
8787f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8788{
8789# ifdef FEAT_RELTIME
8790 proftime_T tm;
8791# endif
8792
8793 rettv->v_type = VAR_FLOAT;
8794 rettv->vval.v_float = 0;
8795# ifdef FEAT_RELTIME
8796 if (list2proftime(&argvars[0], &tm) == OK)
8797 rettv->vval.v_float = profile_float(&tm);
8798# endif
8799}
8800#endif
8801
8802/*
8803 * "reltimestr()" function
8804 */
8805 static void
8806f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8807{
8808#ifdef FEAT_RELTIME
8809 proftime_T tm;
8810#endif
8811
8812 rettv->v_type = VAR_STRING;
8813 rettv->vval.v_string = NULL;
8814#ifdef FEAT_RELTIME
8815 if (list2proftime(&argvars[0], &tm) == OK)
8816 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8817#endif
8818}
8819
8820#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 static void
8822make_connection(void)
8823{
8824 if (X_DISPLAY == NULL
8825# ifdef FEAT_GUI
8826 && !gui.in_use
8827# endif
8828 )
8829 {
8830 x_force_connect = TRUE;
8831 setup_term_clip();
8832 x_force_connect = FALSE;
8833 }
8834}
8835
8836 static int
8837check_connection(void)
8838{
8839 make_connection();
8840 if (X_DISPLAY == NULL)
8841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008842 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 return FAIL;
8844 }
8845 return OK;
8846}
8847#endif
8848
8849#ifdef FEAT_CLIENTSERVER
8850 static void
8851remote_common(typval_T *argvars, typval_T *rettv, int expr)
8852{
8853 char_u *server_name;
8854 char_u *keys;
8855 char_u *r = NULL;
8856 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008857 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008858# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 HWND w;
8860# else
8861 Window w;
8862# endif
8863
8864 if (check_restricted() || check_secure())
8865 return;
8866
8867# ifdef FEAT_X11
8868 if (check_connection() == FAIL)
8869 return;
8870# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008871 if (argvars[2].v_type != VAR_UNKNOWN
8872 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008873 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008875 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 if (server_name == NULL)
8877 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008878 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008879# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008880 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008882 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8883 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884# endif
8885 {
8886 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008888 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008889 vim_free(r);
8890 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008892 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 return;
8894 }
8895
8896 rettv->vval.v_string = r;
8897
8898 if (argvars[2].v_type != VAR_UNKNOWN)
8899 {
8900 dictitem_T v;
8901 char_u str[30];
8902 char_u *idvar;
8903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008904 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008905 if (idvar != NULL && *idvar != NUL)
8906 {
8907 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8908 v.di_tv.v_type = VAR_STRING;
8909 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008911 vim_free(v.di_tv.vval.v_string);
8912 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 }
8914}
8915#endif
8916
8917/*
8918 * "remote_expr()" function
8919 */
8920 static void
8921f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8922{
8923 rettv->v_type = VAR_STRING;
8924 rettv->vval.v_string = NULL;
8925#ifdef FEAT_CLIENTSERVER
8926 remote_common(argvars, rettv, TRUE);
8927#endif
8928}
8929
8930/*
8931 * "remote_foreground()" function
8932 */
8933 static void
8934f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8935{
8936#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008937# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 /* On Win32 it's done in this application. */
8939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008940 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941
8942 if (server_name != NULL)
8943 serverForeground(server_name);
8944 }
8945# else
8946 /* Send a foreground() expression to the server. */
8947 argvars[1].v_type = VAR_STRING;
8948 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8949 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008950 rettv->v_type = VAR_STRING;
8951 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952 remote_common(argvars, rettv, TRUE);
8953 vim_free(argvars[1].vval.v_string);
8954# endif
8955#endif
8956}
8957
8958 static void
8959f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8960{
8961#ifdef FEAT_CLIENTSERVER
8962 dictitem_T v;
8963 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008964# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 long_u n = 0;
8966# endif
8967 char_u *serverid;
8968
8969 if (check_restricted() || check_secure())
8970 {
8971 rettv->vval.v_number = -1;
8972 return;
8973 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008974 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 if (serverid == NULL)
8976 {
8977 rettv->vval.v_number = -1;
8978 return; /* type error; errmsg already given */
8979 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008980# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8982 if (n == 0)
8983 rettv->vval.v_number = -1;
8984 else
8985 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008986 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987 rettv->vval.v_number = (s != NULL);
8988 }
8989# else
8990 if (check_connection() == FAIL)
8991 return;
8992
8993 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8994 serverStrToWin(serverid), &s);
8995# endif
8996
8997 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8998 {
8999 char_u *retvar;
9000
9001 v.di_tv.v_type = VAR_STRING;
9002 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009003 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 if (retvar != NULL)
9005 set_var(retvar, &v.di_tv, FALSE);
9006 vim_free(v.di_tv.vval.v_string);
9007 }
9008#else
9009 rettv->vval.v_number = -1;
9010#endif
9011}
9012
9013 static void
9014f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9015{
9016 char_u *r = NULL;
9017
9018#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009019 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020
9021 if (serverid != NULL && !check_restricted() && !check_secure())
9022 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009023 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009024# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009025 /* The server's HWND is encoded in the 'id' parameter */
9026 long_u n = 0;
9027# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009028
9029 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009030 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009031
Bram Moolenaar4f974752019-02-17 17:44:42 +01009032# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9034 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009035 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 if (r == NULL)
9037# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009038 if (check_connection() == FAIL
9039 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9040 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009042 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 }
9044#endif
9045 rettv->v_type = VAR_STRING;
9046 rettv->vval.v_string = r;
9047}
9048
9049/*
9050 * "remote_send()" function
9051 */
9052 static void
9053f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9054{
9055 rettv->v_type = VAR_STRING;
9056 rettv->vval.v_string = NULL;
9057#ifdef FEAT_CLIENTSERVER
9058 remote_common(argvars, rettv, FALSE);
9059#endif
9060}
9061
9062/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009063 * "remote_startserver()" function
9064 */
9065 static void
9066f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9067{
9068#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009070
9071 if (server == NULL)
9072 return; /* type error; errmsg already given */
9073 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009074 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009075 else
9076 {
9077# ifdef FEAT_X11
9078 if (check_connection() == OK)
9079 serverRegisterName(X_DISPLAY, server);
9080# else
9081 serverSetName(server);
9082# endif
9083 }
9084#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009085 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009086#endif
9087}
9088
9089/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 * "remove()" function
9091 */
9092 static void
9093f_remove(typval_T *argvars, typval_T *rettv)
9094{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9096
9097 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009098 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009099 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02009100 blob_remove(argvars, rettv);
9101 else if (argvars[0].v_type == VAR_LIST)
9102 list_remove(argvars, rettv, arg_errmsg);
9103 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009104 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105}
9106
9107/*
9108 * "rename({from}, {to})" function
9109 */
9110 static void
9111f_rename(typval_T *argvars, typval_T *rettv)
9112{
9113 char_u buf[NUMBUFLEN];
9114
9115 if (check_restricted() || check_secure())
9116 rettv->vval.v_number = -1;
9117 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009118 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9119 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120}
9121
9122/*
9123 * "repeat()" function
9124 */
9125 static void
9126f_repeat(typval_T *argvars, typval_T *rettv)
9127{
9128 char_u *p;
9129 int n;
9130 int slen;
9131 int len;
9132 char_u *r;
9133 int i;
9134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009135 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136 if (argvars[0].v_type == VAR_LIST)
9137 {
9138 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9139 while (n-- > 0)
9140 if (list_extend(rettv->vval.v_list,
9141 argvars[0].vval.v_list, NULL) == FAIL)
9142 break;
9143 }
9144 else
9145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009146 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 rettv->v_type = VAR_STRING;
9148 rettv->vval.v_string = NULL;
9149
9150 slen = (int)STRLEN(p);
9151 len = slen * n;
9152 if (len <= 0)
9153 return;
9154
9155 r = alloc(len + 1);
9156 if (r != NULL)
9157 {
9158 for (i = 0; i < n; i++)
9159 mch_memmove(r + i * slen, p, (size_t)slen);
9160 r[len] = NUL;
9161 }
9162
9163 rettv->vval.v_string = r;
9164 }
9165}
9166
9167/*
9168 * "resolve()" function
9169 */
9170 static void
9171f_resolve(typval_T *argvars, typval_T *rettv)
9172{
9173 char_u *p;
9174#ifdef HAVE_READLINK
9175 char_u *buf = NULL;
9176#endif
9177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009178 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179#ifdef FEAT_SHORTCUT
9180 {
9181 char_u *v = NULL;
9182
Bram Moolenaardce1e892019-02-10 23:18:53 +01009183 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184 if (v != NULL)
9185 rettv->vval.v_string = v;
9186 else
9187 rettv->vval.v_string = vim_strsave(p);
9188 }
9189#else
9190# ifdef HAVE_READLINK
9191 {
9192 char_u *cpy;
9193 int len;
9194 char_u *remain = NULL;
9195 char_u *q;
9196 int is_relative_to_current = FALSE;
9197 int has_trailing_pathsep = FALSE;
9198 int limit = 100;
9199
9200 p = vim_strsave(p);
9201
9202 if (p[0] == '.' && (vim_ispathsep(p[1])
9203 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9204 is_relative_to_current = TRUE;
9205
9206 len = STRLEN(p);
9207 if (len > 0 && after_pathsep(p, p + len))
9208 {
9209 has_trailing_pathsep = TRUE;
9210 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9211 }
9212
9213 q = getnextcomp(p);
9214 if (*q != NUL)
9215 {
9216 /* Separate the first path component in "p", and keep the
9217 * remainder (beginning with the path separator). */
9218 remain = vim_strsave(q - 1);
9219 q[-1] = NUL;
9220 }
9221
9222 buf = alloc(MAXPATHL + 1);
9223 if (buf == NULL)
9224 goto fail;
9225
9226 for (;;)
9227 {
9228 for (;;)
9229 {
9230 len = readlink((char *)p, (char *)buf, MAXPATHL);
9231 if (len <= 0)
9232 break;
9233 buf[len] = NUL;
9234
9235 if (limit-- == 0)
9236 {
9237 vim_free(p);
9238 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009239 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 rettv->vval.v_string = NULL;
9241 goto fail;
9242 }
9243
9244 /* Ensure that the result will have a trailing path separator
9245 * if the argument has one. */
9246 if (remain == NULL && has_trailing_pathsep)
9247 add_pathsep(buf);
9248
9249 /* Separate the first path component in the link value and
9250 * concatenate the remainders. */
9251 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9252 if (*q != NUL)
9253 {
9254 if (remain == NULL)
9255 remain = vim_strsave(q - 1);
9256 else
9257 {
9258 cpy = concat_str(q - 1, remain);
9259 if (cpy != NULL)
9260 {
9261 vim_free(remain);
9262 remain = cpy;
9263 }
9264 }
9265 q[-1] = NUL;
9266 }
9267
9268 q = gettail(p);
9269 if (q > p && *q == NUL)
9270 {
9271 /* Ignore trailing path separator. */
9272 q[-1] = NUL;
9273 q = gettail(p);
9274 }
9275 if (q > p && !mch_isFullName(buf))
9276 {
9277 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009278 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009279 if (cpy != NULL)
9280 {
9281 STRCPY(cpy, p);
9282 STRCPY(gettail(cpy), buf);
9283 vim_free(p);
9284 p = cpy;
9285 }
9286 }
9287 else
9288 {
9289 vim_free(p);
9290 p = vim_strsave(buf);
9291 }
9292 }
9293
9294 if (remain == NULL)
9295 break;
9296
9297 /* Append the first path component of "remain" to "p". */
9298 q = getnextcomp(remain + 1);
9299 len = q - remain - (*q != NUL);
9300 cpy = vim_strnsave(p, STRLEN(p) + len);
9301 if (cpy != NULL)
9302 {
9303 STRNCAT(cpy, remain, len);
9304 vim_free(p);
9305 p = cpy;
9306 }
9307 /* Shorten "remain". */
9308 if (*q != NUL)
9309 STRMOVE(remain, q - 1);
9310 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009311 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 }
9313
9314 /* If the result is a relative path name, make it explicitly relative to
9315 * the current directory if and only if the argument had this form. */
9316 if (!vim_ispathsep(*p))
9317 {
9318 if (is_relative_to_current
9319 && *p != NUL
9320 && !(p[0] == '.'
9321 && (p[1] == NUL
9322 || vim_ispathsep(p[1])
9323 || (p[1] == '.'
9324 && (p[2] == NUL
9325 || vim_ispathsep(p[2]))))))
9326 {
9327 /* Prepend "./". */
9328 cpy = concat_str((char_u *)"./", p);
9329 if (cpy != NULL)
9330 {
9331 vim_free(p);
9332 p = cpy;
9333 }
9334 }
9335 else if (!is_relative_to_current)
9336 {
9337 /* Strip leading "./". */
9338 q = p;
9339 while (q[0] == '.' && vim_ispathsep(q[1]))
9340 q += 2;
9341 if (q > p)
9342 STRMOVE(p, p + 2);
9343 }
9344 }
9345
9346 /* Ensure that the result will have no trailing path separator
9347 * if the argument had none. But keep "/" or "//". */
9348 if (!has_trailing_pathsep)
9349 {
9350 q = p + STRLEN(p);
9351 if (after_pathsep(p, q))
9352 *gettail_sep(p) = NUL;
9353 }
9354
9355 rettv->vval.v_string = p;
9356 }
9357# else
9358 rettv->vval.v_string = vim_strsave(p);
9359# endif
9360#endif
9361
9362 simplify_filename(rettv->vval.v_string);
9363
9364#ifdef HAVE_READLINK
9365fail:
9366 vim_free(buf);
9367#endif
9368 rettv->v_type = VAR_STRING;
9369}
9370
9371/*
9372 * "reverse({list})" function
9373 */
9374 static void
9375f_reverse(typval_T *argvars, typval_T *rettv)
9376{
9377 list_T *l;
9378 listitem_T *li, *ni;
9379
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009380 if (argvars[0].v_type == VAR_BLOB)
9381 {
9382 blob_T *b = argvars[0].vval.v_blob;
9383 int i, len = blob_len(b);
9384
9385 for (i = 0; i < len / 2; i++)
9386 {
9387 int tmp = blob_get(b, i);
9388
9389 blob_set(b, i, blob_get(b, len - i - 1));
9390 blob_set(b, len - i - 1, tmp);
9391 }
9392 rettv_blob_set(rettv, b);
9393 return;
9394 }
9395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009396 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009397 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009398 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009399 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 (char_u *)N_("reverse() argument"), TRUE))
9401 {
9402 li = l->lv_last;
9403 l->lv_first = l->lv_last = NULL;
9404 l->lv_len = 0;
9405 while (li != NULL)
9406 {
9407 ni = li->li_prev;
9408 list_append(l, li);
9409 li = ni;
9410 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009411 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 l->lv_idx = l->lv_len - l->lv_idx - 1;
9413 }
9414}
9415
9416#define SP_NOMOVE 0x01 /* don't move cursor */
9417#define SP_REPEAT 0x02 /* repeat to find outer pair */
9418#define SP_RETCOUNT 0x04 /* return matchcount */
9419#define SP_SETPCMARK 0x08 /* set previous context mark */
9420#define SP_START 0x10 /* accept match at start position */
9421#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9422#define SP_END 0x40 /* leave cursor at end of match */
9423#define SP_COLUMN 0x80 /* start at cursor column */
9424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425/*
9426 * Get flags for a search function.
9427 * Possibly sets "p_ws".
9428 * Returns BACKWARD, FORWARD or zero (for an error).
9429 */
9430 static int
9431get_search_arg(typval_T *varp, int *flagsp)
9432{
9433 int dir = FORWARD;
9434 char_u *flags;
9435 char_u nbuf[NUMBUFLEN];
9436 int mask;
9437
9438 if (varp->v_type != VAR_UNKNOWN)
9439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009440 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 if (flags == NULL)
9442 return 0; /* type error; errmsg already given */
9443 while (*flags != NUL)
9444 {
9445 switch (*flags)
9446 {
9447 case 'b': dir = BACKWARD; break;
9448 case 'w': p_ws = TRUE; break;
9449 case 'W': p_ws = FALSE; break;
9450 default: mask = 0;
9451 if (flagsp != NULL)
9452 switch (*flags)
9453 {
9454 case 'c': mask = SP_START; break;
9455 case 'e': mask = SP_END; break;
9456 case 'm': mask = SP_RETCOUNT; break;
9457 case 'n': mask = SP_NOMOVE; break;
9458 case 'p': mask = SP_SUBPAT; break;
9459 case 'r': mask = SP_REPEAT; break;
9460 case 's': mask = SP_SETPCMARK; break;
9461 case 'z': mask = SP_COLUMN; break;
9462 }
9463 if (mask == 0)
9464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009465 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 dir = 0;
9467 }
9468 else
9469 *flagsp |= mask;
9470 }
9471 if (dir == 0)
9472 break;
9473 ++flags;
9474 }
9475 }
9476 return dir;
9477}
9478
9479/*
9480 * Shared by search() and searchpos() functions.
9481 */
9482 static int
9483search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9484{
9485 int flags;
9486 char_u *pat;
9487 pos_T pos;
9488 pos_T save_cursor;
9489 int save_p_ws = p_ws;
9490 int dir;
9491 int retval = 0; /* default: FAIL */
9492 long lnum_stop = 0;
9493 proftime_T tm;
9494#ifdef FEAT_RELTIME
9495 long time_limit = 0;
9496#endif
9497 int options = SEARCH_KEEP;
9498 int subpatnum;
9499
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009500 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9502 if (dir == 0)
9503 goto theend;
9504 flags = *flagsp;
9505 if (flags & SP_START)
9506 options |= SEARCH_START;
9507 if (flags & SP_END)
9508 options |= SEARCH_END;
9509 if (flags & SP_COLUMN)
9510 options |= SEARCH_COL;
9511
9512 /* Optional arguments: line number to stop searching and timeout. */
9513 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009515 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 if (lnum_stop < 0)
9517 goto theend;
9518#ifdef FEAT_RELTIME
9519 if (argvars[3].v_type != VAR_UNKNOWN)
9520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009521 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522 if (time_limit < 0)
9523 goto theend;
9524 }
9525#endif
9526 }
9527
9528#ifdef FEAT_RELTIME
9529 /* Set the time limit, if there is one. */
9530 profile_setlimit(time_limit, &tm);
9531#endif
9532
9533 /*
9534 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9535 * Check to make sure only those flags are set.
9536 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9537 * flags cannot be set. Check for that condition also.
9538 */
9539 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9540 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009542 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543 goto theend;
9544 }
9545
9546 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009547 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009548 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 if (subpatnum != FAIL)
9550 {
9551 if (flags & SP_SUBPAT)
9552 retval = subpatnum;
9553 else
9554 retval = pos.lnum;
9555 if (flags & SP_SETPCMARK)
9556 setpcmark();
9557 curwin->w_cursor = pos;
9558 if (match_pos != NULL)
9559 {
9560 /* Store the match cursor position */
9561 match_pos->lnum = pos.lnum;
9562 match_pos->col = pos.col + 1;
9563 }
9564 /* "/$" will put the cursor after the end of the line, may need to
9565 * correct that here */
9566 check_cursor();
9567 }
9568
9569 /* If 'n' flag is used: restore cursor position. */
9570 if (flags & SP_NOMOVE)
9571 curwin->w_cursor = save_cursor;
9572 else
9573 curwin->w_set_curswant = TRUE;
9574theend:
9575 p_ws = save_p_ws;
9576
9577 return retval;
9578}
9579
9580#ifdef FEAT_FLOAT
9581
9582/*
9583 * round() is not in C90, use ceil() or floor() instead.
9584 */
9585 float_T
9586vim_round(float_T f)
9587{
9588 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9589}
9590
9591/*
9592 * "round({float})" function
9593 */
9594 static void
9595f_round(typval_T *argvars, typval_T *rettv)
9596{
9597 float_T f = 0.0;
9598
9599 rettv->v_type = VAR_FLOAT;
9600 if (get_float_arg(argvars, &f) == OK)
9601 rettv->vval.v_float = vim_round(f);
9602 else
9603 rettv->vval.v_float = 0.0;
9604}
9605#endif
9606
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009607#ifdef FEAT_RUBY
9608/*
9609 * "rubyeval()" function
9610 */
9611 static void
9612f_rubyeval(typval_T *argvars, typval_T *rettv)
9613{
9614 char_u *str;
9615 char_u buf[NUMBUFLEN];
9616
9617 str = tv_get_string_buf(&argvars[0], buf);
9618 do_rubyeval(str, rettv);
9619}
9620#endif
9621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622/*
9623 * "screenattr()" function
9624 */
9625 static void
9626f_screenattr(typval_T *argvars, typval_T *rettv)
9627{
9628 int row;
9629 int col;
9630 int c;
9631
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009632 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9633 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 if (row < 0 || row >= screen_Rows
9635 || col < 0 || col >= screen_Columns)
9636 c = -1;
9637 else
9638 c = ScreenAttrs[LineOffset[row] + col];
9639 rettv->vval.v_number = c;
9640}
9641
9642/*
9643 * "screenchar()" function
9644 */
9645 static void
9646f_screenchar(typval_T *argvars, typval_T *rettv)
9647{
9648 int row;
9649 int col;
9650 int off;
9651 int c;
9652
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009653 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9654 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009655 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009656 c = -1;
9657 else
9658 {
9659 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 if (enc_utf8 && ScreenLinesUC[off] != 0)
9661 c = ScreenLinesUC[off];
9662 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 c = ScreenLines[off];
9664 }
9665 rettv->vval.v_number = c;
9666}
9667
9668/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009669 * "screenchars()" function
9670 */
9671 static void
9672f_screenchars(typval_T *argvars, typval_T *rettv)
9673{
9674 int row;
9675 int col;
9676 int off;
9677 int c;
9678 int i;
9679
9680 if (rettv_list_alloc(rettv) == FAIL)
9681 return;
9682 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9683 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9684 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9685 return;
9686
9687 off = LineOffset[row] + col;
9688 if (enc_utf8 && ScreenLinesUC[off] != 0)
9689 c = ScreenLinesUC[off];
9690 else
9691 c = ScreenLines[off];
9692 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9693
9694 if (enc_utf8)
9695
9696 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9697 list_append_number(rettv->vval.v_list,
9698 (varnumber_T)ScreenLinesC[i][off]);
9699}
9700
9701/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009702 * "screencol()" function
9703 *
9704 * First column is 1 to be consistent with virtcol().
9705 */
9706 static void
9707f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9708{
9709 rettv->vval.v_number = screen_screencol() + 1;
9710}
9711
9712/*
9713 * "screenrow()" function
9714 */
9715 static void
9716f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9717{
9718 rettv->vval.v_number = screen_screenrow() + 1;
9719}
9720
9721/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009722 * "screenstring()" function
9723 */
9724 static void
9725f_screenstring(typval_T *argvars, typval_T *rettv)
9726{
9727 int row;
9728 int col;
9729 int off;
9730 int c;
9731 int i;
9732 char_u buf[MB_MAXBYTES + 1];
9733 int buflen = 0;
9734
9735 rettv->vval.v_string = NULL;
9736 rettv->v_type = VAR_STRING;
9737
9738 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9739 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9740 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9741 return;
9742
9743 off = LineOffset[row] + col;
9744 if (enc_utf8 && ScreenLinesUC[off] != 0)
9745 c = ScreenLinesUC[off];
9746 else
9747 c = ScreenLines[off];
9748 buflen += mb_char2bytes(c, buf);
9749
9750 if (enc_utf8)
9751 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9752 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9753
9754 buf[buflen] = NUL;
9755 rettv->vval.v_string = vim_strsave(buf);
9756}
9757
9758/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759 * "search()" function
9760 */
9761 static void
9762f_search(typval_T *argvars, typval_T *rettv)
9763{
9764 int flags = 0;
9765
9766 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9767}
9768
9769/*
9770 * "searchdecl()" function
9771 */
9772 static void
9773f_searchdecl(typval_T *argvars, typval_T *rettv)
9774{
9775 int locally = 1;
9776 int thisblock = 0;
9777 int error = FALSE;
9778 char_u *name;
9779
9780 rettv->vval.v_number = 1; /* default: FAIL */
9781
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009782 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 if (argvars[1].v_type != VAR_UNKNOWN)
9784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009785 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009787 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 }
9789 if (!error && name != NULL)
9790 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9791 locally, thisblock, SEARCH_KEEP) == FAIL;
9792}
9793
9794/*
9795 * Used by searchpair() and searchpairpos()
9796 */
9797 static int
9798searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9799{
9800 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009801 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 int save_p_ws = p_ws;
9803 int dir;
9804 int flags = 0;
9805 char_u nbuf1[NUMBUFLEN];
9806 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 int retval = 0; /* default: FAIL */
9808 long lnum_stop = 0;
9809 long time_limit = 0;
9810
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009811 /* Get the three pattern arguments: start, middle, end. Will result in an
9812 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 spat = tv_get_string_chk(&argvars[0]);
9814 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9815 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 if (spat == NULL || mpat == NULL || epat == NULL)
9817 goto theend; /* type error */
9818
9819 /* Handle the optional fourth argument: flags */
9820 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9821 if (dir == 0)
9822 goto theend;
9823
9824 /* Don't accept SP_END or SP_SUBPAT.
9825 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9826 */
9827 if ((flags & (SP_END | SP_SUBPAT)) != 0
9828 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009830 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 goto theend;
9832 }
9833
9834 /* Using 'r' implies 'W', otherwise it doesn't work. */
9835 if (flags & SP_REPEAT)
9836 p_ws = FALSE;
9837
9838 /* Optional fifth argument: skip expression */
9839 if (argvars[3].v_type == VAR_UNKNOWN
9840 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009841 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842 else
9843 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009844 skip = &argvars[4];
9845 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9846 && skip->v_type != VAR_STRING)
9847 {
9848 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009849 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009850 goto theend;
9851 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 if (argvars[5].v_type != VAR_UNKNOWN)
9853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009854 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009857 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860#ifdef FEAT_RELTIME
9861 if (argvars[6].v_type != VAR_UNKNOWN)
9862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009863 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009866 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009868 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 }
9870#endif
9871 }
9872 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009873
9874 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9875 match_pos, lnum_stop, time_limit);
9876
9877theend:
9878 p_ws = save_p_ws;
9879
9880 return retval;
9881}
9882
9883/*
9884 * "searchpair()" function
9885 */
9886 static void
9887f_searchpair(typval_T *argvars, typval_T *rettv)
9888{
9889 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9890}
9891
9892/*
9893 * "searchpairpos()" function
9894 */
9895 static void
9896f_searchpairpos(typval_T *argvars, typval_T *rettv)
9897{
9898 pos_T match_pos;
9899 int lnum = 0;
9900 int col = 0;
9901
9902 if (rettv_list_alloc(rettv) == FAIL)
9903 return;
9904
9905 if (searchpair_cmn(argvars, &match_pos) > 0)
9906 {
9907 lnum = match_pos.lnum;
9908 col = match_pos.col;
9909 }
9910
9911 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9912 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9913}
9914
9915/*
9916 * Search for a start/middle/end thing.
9917 * Used by searchpair(), see its documentation for the details.
9918 * Returns 0 or -1 for no match,
9919 */
9920 long
9921do_searchpair(
9922 char_u *spat, /* start pattern */
9923 char_u *mpat, /* middle pattern */
9924 char_u *epat, /* end pattern */
9925 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009926 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 int flags, /* SP_SETPCMARK and other SP_ values */
9928 pos_T *match_pos,
9929 linenr_T lnum_stop, /* stop at this line if not zero */
9930 long time_limit UNUSED) /* stop after this many msec */
9931{
9932 char_u *save_cpo;
9933 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9934 long retval = 0;
9935 pos_T pos;
9936 pos_T firstpos;
9937 pos_T foundpos;
9938 pos_T save_cursor;
9939 pos_T save_pos;
9940 int n;
9941 int r;
9942 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009943 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944 int err;
9945 int options = SEARCH_KEEP;
9946 proftime_T tm;
9947
9948 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9949 save_cpo = p_cpo;
9950 p_cpo = empty_option;
9951
9952#ifdef FEAT_RELTIME
9953 /* Set the time limit, if there is one. */
9954 profile_setlimit(time_limit, &tm);
9955#endif
9956
9957 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9958 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009959 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9960 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961 if (pat2 == NULL || pat3 == NULL)
9962 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009963 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 if (*mpat == NUL)
9965 STRCPY(pat3, pat2);
9966 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009967 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009968 spat, epat, mpat);
9969 if (flags & SP_START)
9970 options |= SEARCH_START;
9971
Bram Moolenaar48570482017-10-30 21:48:41 +01009972 if (skip != NULL)
9973 {
9974 /* Empty string means to not use the skip expression. */
9975 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9976 use_skip = skip->vval.v_string != NULL
9977 && *skip->vval.v_string != NUL;
9978 }
9979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 save_cursor = curwin->w_cursor;
9981 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009982 CLEAR_POS(&firstpos);
9983 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009984 pat = pat3;
9985 for (;;)
9986 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009987 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009988 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009989 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 /* didn't find it or found the first match again: FAIL */
9991 break;
9992
9993 if (firstpos.lnum == 0)
9994 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009995 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 {
9997 /* Found the same position again. Can happen with a pattern that
9998 * has "\zs" at the end and searching backwards. Advance one
9999 * character and try again. */
10000 if (dir == BACKWARD)
10001 decl(&pos);
10002 else
10003 incl(&pos);
10004 }
10005 foundpos = pos;
10006
10007 /* clear the start flag to avoid getting stuck here */
10008 options &= ~SEARCH_START;
10009
10010 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010011 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 {
10013 save_pos = curwin->w_cursor;
10014 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010015 err = FALSE;
10016 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 curwin->w_cursor = save_pos;
10018 if (err)
10019 {
10020 /* Evaluating {skip} caused an error, break here. */
10021 curwin->w_cursor = save_cursor;
10022 retval = -1;
10023 break;
10024 }
10025 if (r)
10026 continue;
10027 }
10028
10029 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10030 {
10031 /* Found end when searching backwards or start when searching
10032 * forward: nested pair. */
10033 ++nest;
10034 pat = pat2; /* nested, don't search for middle */
10035 }
10036 else
10037 {
10038 /* Found end when searching forward or start when searching
10039 * backward: end of (nested) pair; or found middle in outer pair. */
10040 if (--nest == 1)
10041 pat = pat3; /* outer level, search for middle */
10042 }
10043
10044 if (nest == 0)
10045 {
10046 /* Found the match: return matchcount or line number. */
10047 if (flags & SP_RETCOUNT)
10048 ++retval;
10049 else
10050 retval = pos.lnum;
10051 if (flags & SP_SETPCMARK)
10052 setpcmark();
10053 curwin->w_cursor = pos;
10054 if (!(flags & SP_REPEAT))
10055 break;
10056 nest = 1; /* search for next unmatched */
10057 }
10058 }
10059
10060 if (match_pos != NULL)
10061 {
10062 /* Store the match cursor position */
10063 match_pos->lnum = curwin->w_cursor.lnum;
10064 match_pos->col = curwin->w_cursor.col + 1;
10065 }
10066
10067 /* If 'n' flag is used or search failed: restore cursor position. */
10068 if ((flags & SP_NOMOVE) || retval == 0)
10069 curwin->w_cursor = save_cursor;
10070
10071theend:
10072 vim_free(pat2);
10073 vim_free(pat3);
10074 if (p_cpo == empty_option)
10075 p_cpo = save_cpo;
10076 else
10077 /* Darn, evaluating the {skip} expression changed the value. */
10078 free_string_option(save_cpo);
10079
10080 return retval;
10081}
10082
10083/*
10084 * "searchpos()" function
10085 */
10086 static void
10087f_searchpos(typval_T *argvars, typval_T *rettv)
10088{
10089 pos_T match_pos;
10090 int lnum = 0;
10091 int col = 0;
10092 int n;
10093 int flags = 0;
10094
10095 if (rettv_list_alloc(rettv) == FAIL)
10096 return;
10097
10098 n = search_cmn(argvars, &match_pos, &flags);
10099 if (n > 0)
10100 {
10101 lnum = match_pos.lnum;
10102 col = match_pos.col;
10103 }
10104
10105 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10106 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10107 if (flags & SP_SUBPAT)
10108 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10109}
10110
10111 static void
10112f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10113{
10114#ifdef FEAT_CLIENTSERVER
10115 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010116 char_u *server = tv_get_string_chk(&argvars[0]);
10117 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118
10119 rettv->vval.v_number = -1;
10120 if (server == NULL || reply == NULL)
10121 return;
10122 if (check_restricted() || check_secure())
10123 return;
10124# ifdef FEAT_X11
10125 if (check_connection() == FAIL)
10126 return;
10127# endif
10128
10129 if (serverSendReply(server, reply) < 0)
10130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010131 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132 return;
10133 }
10134 rettv->vval.v_number = 0;
10135#else
10136 rettv->vval.v_number = -1;
10137#endif
10138}
10139
10140 static void
10141f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10142{
10143 char_u *r = NULL;
10144
10145#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010146# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 r = serverGetVimNames();
10148# else
10149 make_connection();
10150 if (X_DISPLAY != NULL)
10151 r = serverGetVimNames(X_DISPLAY);
10152# endif
10153#endif
10154 rettv->v_type = VAR_STRING;
10155 rettv->vval.v_string = r;
10156}
10157
10158/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010159 * "setbufline()" function
10160 */
10161 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010162f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010163{
10164 linenr_T lnum;
10165 buf_T *buf;
10166
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010167 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010168 if (buf == NULL)
10169 rettv->vval.v_number = 1; /* FAIL */
10170 else
10171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010172 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010173 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010174 }
10175}
10176
10177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010178 * "setbufvar()" function
10179 */
10180 static void
10181f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10182{
10183 buf_T *buf;
10184 char_u *varname, *bufvarname;
10185 typval_T *varp;
10186 char_u nbuf[NUMBUFLEN];
10187
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010188 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010190 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10191 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010192 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 varp = &argvars[2];
10194
10195 if (buf != NULL && varname != NULL && varp != NULL)
10196 {
10197 if (*varname == '&')
10198 {
10199 long numval;
10200 char_u *strval;
10201 int error = FALSE;
10202 aco_save_T aco;
10203
10204 /* set curbuf to be our buf, temporarily */
10205 aucmd_prepbuf(&aco, buf);
10206
10207 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010208 numval = (long)tv_get_number_chk(varp, &error);
10209 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 if (!error && strval != NULL)
10211 set_option_value(varname, numval, strval, OPT_LOCAL);
10212
10213 /* reset notion of buffer */
10214 aucmd_restbuf(&aco);
10215 }
10216 else
10217 {
10218 buf_T *save_curbuf = curbuf;
10219
Bram Moolenaar964b3742019-05-24 18:54:09 +020010220 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010221 if (bufvarname != NULL)
10222 {
10223 curbuf = buf;
10224 STRCPY(bufvarname, "b:");
10225 STRCPY(bufvarname + 2, varname);
10226 set_var(bufvarname, varp, TRUE);
10227 vim_free(bufvarname);
10228 curbuf = save_curbuf;
10229 }
10230 }
10231 }
10232}
10233
10234 static void
10235f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10236{
10237 dict_T *d;
10238 dictitem_T *di;
10239 char_u *csearch;
10240
10241 if (argvars[0].v_type != VAR_DICT)
10242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010243 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 return;
10245 }
10246
10247 if ((d = argvars[0].vval.v_dict) != NULL)
10248 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010249 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 if (csearch != NULL)
10251 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010252 if (enc_utf8)
10253 {
10254 int pcc[MAX_MCO];
10255 int c = utfc_ptr2char(csearch, pcc);
10256
10257 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10258 }
10259 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 set_last_csearch(PTR2CHAR(csearch),
10261 csearch, MB_PTR2LEN(csearch));
10262 }
10263
10264 di = dict_find(d, (char_u *)"forward", -1);
10265 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010266 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 ? FORWARD : BACKWARD);
10268
10269 di = dict_find(d, (char_u *)"until", -1);
10270 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010271 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 }
10273}
10274
10275/*
10276 * "setcmdpos()" function
10277 */
10278 static void
10279f_setcmdpos(typval_T *argvars, typval_T *rettv)
10280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010281 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282
10283 if (pos >= 0)
10284 rettv->vval.v_number = set_cmdline_pos(pos);
10285}
10286
10287/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010288 * "setenv()" function
10289 */
10290 static void
10291f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10292{
10293 char_u namebuf[NUMBUFLEN];
10294 char_u valbuf[NUMBUFLEN];
10295 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10296
10297 if (argvars[1].v_type == VAR_SPECIAL
10298 && argvars[1].vval.v_number == VVAL_NULL)
10299 vim_unsetenv(name);
10300 else
10301 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10302}
10303
10304/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010305 * "setfperm({fname}, {mode})" function
10306 */
10307 static void
10308f_setfperm(typval_T *argvars, typval_T *rettv)
10309{
10310 char_u *fname;
10311 char_u modebuf[NUMBUFLEN];
10312 char_u *mode_str;
10313 int i;
10314 int mask;
10315 int mode = 0;
10316
10317 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010318 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010319 if (fname == NULL)
10320 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010321 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010322 if (mode_str == NULL)
10323 return;
10324 if (STRLEN(mode_str) != 9)
10325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010326 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010327 return;
10328 }
10329
10330 mask = 1;
10331 for (i = 8; i >= 0; --i)
10332 {
10333 if (mode_str[i] != '-')
10334 mode |= mask;
10335 mask = mask << 1;
10336 }
10337 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10338}
10339
10340/*
10341 * "setline()" function
10342 */
10343 static void
10344f_setline(typval_T *argvars, typval_T *rettv)
10345{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010346 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010347
Bram Moolenaarca851592018-06-06 21:04:07 +020010348 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010349}
10350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351/*
10352 * Used by "setqflist()" and "setloclist()" functions
10353 */
10354 static void
10355set_qf_ll_list(
10356 win_T *wp UNUSED,
10357 typval_T *list_arg UNUSED,
10358 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010359 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 typval_T *rettv)
10361{
10362#ifdef FEAT_QUICKFIX
10363 static char *e_invact = N_("E927: Invalid action: '%s'");
10364 char_u *act;
10365 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010366 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010367#endif
10368
10369 rettv->vval.v_number = -1;
10370
10371#ifdef FEAT_QUICKFIX
10372 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010373 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010374 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010375 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 else
10377 {
10378 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010379 dict_T *d = NULL;
10380 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010381
10382 if (action_arg->v_type == VAR_STRING)
10383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010384 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010385 if (act == NULL)
10386 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010387 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10388 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010389 action = *act;
10390 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010391 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 }
10393 else if (action_arg->v_type == VAR_UNKNOWN)
10394 action = ' ';
10395 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010396 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397
Bram Moolenaard823fa92016-08-12 16:29:27 +020010398 if (action_arg->v_type != VAR_UNKNOWN
10399 && what_arg->v_type != VAR_UNKNOWN)
10400 {
10401 if (what_arg->v_type == VAR_DICT)
10402 d = what_arg->vval.v_dict;
10403 else
10404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010405 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010406 valid_dict = FALSE;
10407 }
10408 }
10409
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010410 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010411 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010412 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10413 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010415 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010416 }
10417#endif
10418}
10419
10420/*
10421 * "setloclist()" function
10422 */
10423 static void
10424f_setloclist(typval_T *argvars, typval_T *rettv)
10425{
10426 win_T *win;
10427
10428 rettv->vval.v_number = -1;
10429
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010430 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010432 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433}
10434
10435/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010436 * "setpos()" function
10437 */
10438 static void
10439f_setpos(typval_T *argvars, typval_T *rettv)
10440{
10441 pos_T pos;
10442 int fnum;
10443 char_u *name;
10444 colnr_T curswant = -1;
10445
10446 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010447 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010448 if (name != NULL)
10449 {
10450 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10451 {
10452 if (--pos.col < 0)
10453 pos.col = 0;
10454 if (name[0] == '.' && name[1] == NUL)
10455 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010456 /* set cursor; "fnum" is ignored */
10457 curwin->w_cursor = pos;
10458 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010459 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010460 curwin->w_curswant = curswant - 1;
10461 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010462 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010463 check_cursor();
10464 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 }
10466 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10467 {
10468 /* set mark */
10469 if (setmark_pos(name[1], &pos, fnum) == OK)
10470 rettv->vval.v_number = 0;
10471 }
10472 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010473 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 }
10475 }
10476}
10477
10478/*
10479 * "setqflist()" function
10480 */
10481 static void
10482f_setqflist(typval_T *argvars, typval_T *rettv)
10483{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010484 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485}
10486
10487/*
10488 * "setreg()" function
10489 */
10490 static void
10491f_setreg(typval_T *argvars, typval_T *rettv)
10492{
10493 int regname;
10494 char_u *strregname;
10495 char_u *stropt;
10496 char_u *strval;
10497 int append;
10498 char_u yank_type;
10499 long block_len;
10500
10501 block_len = -1;
10502 yank_type = MAUTO;
10503 append = FALSE;
10504
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010505 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010506 rettv->vval.v_number = 1; /* FAIL is default */
10507
10508 if (strregname == NULL)
10509 return; /* type error; errmsg already given */
10510 regname = *strregname;
10511 if (regname == 0 || regname == '@')
10512 regname = '"';
10513
10514 if (argvars[2].v_type != VAR_UNKNOWN)
10515 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010516 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517 if (stropt == NULL)
10518 return; /* type error */
10519 for (; *stropt != NUL; ++stropt)
10520 switch (*stropt)
10521 {
10522 case 'a': case 'A': /* append */
10523 append = TRUE;
10524 break;
10525 case 'v': case 'c': /* character-wise selection */
10526 yank_type = MCHAR;
10527 break;
10528 case 'V': case 'l': /* line-wise selection */
10529 yank_type = MLINE;
10530 break;
10531 case 'b': case Ctrl_V: /* block-wise selection */
10532 yank_type = MBLOCK;
10533 if (VIM_ISDIGIT(stropt[1]))
10534 {
10535 ++stropt;
10536 block_len = getdigits(&stropt) - 1;
10537 --stropt;
10538 }
10539 break;
10540 }
10541 }
10542
10543 if (argvars[1].v_type == VAR_LIST)
10544 {
10545 char_u **lstval;
10546 char_u **allocval;
10547 char_u buf[NUMBUFLEN];
10548 char_u **curval;
10549 char_u **curallocval;
10550 list_T *ll = argvars[1].vval.v_list;
10551 listitem_T *li;
10552 int len;
10553
10554 /* If the list is NULL handle like an empty list. */
10555 len = ll == NULL ? 0 : ll->lv_len;
10556
10557 /* First half: use for pointers to result lines; second half: use for
10558 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010559 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010560 if (lstval == NULL)
10561 return;
10562 curval = lstval;
10563 allocval = lstval + len + 2;
10564 curallocval = allocval;
10565
10566 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10567 li = li->li_next)
10568 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010569 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010570 if (strval == NULL)
10571 goto free_lstval;
10572 if (strval == buf)
10573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010574 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010575 * overwrite the string. */
10576 strval = vim_strsave(buf);
10577 if (strval == NULL)
10578 goto free_lstval;
10579 *curallocval++ = strval;
10580 }
10581 *curval++ = strval;
10582 }
10583 *curval++ = NULL;
10584
10585 write_reg_contents_lst(regname, lstval, -1,
10586 append, yank_type, block_len);
10587free_lstval:
10588 while (curallocval > allocval)
10589 vim_free(*--curallocval);
10590 vim_free(lstval);
10591 }
10592 else
10593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010594 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010595 if (strval == NULL)
10596 return;
10597 write_reg_contents_ex(regname, strval, -1,
10598 append, yank_type, block_len);
10599 }
10600 rettv->vval.v_number = 0;
10601}
10602
10603/*
10604 * "settabvar()" function
10605 */
10606 static void
10607f_settabvar(typval_T *argvars, typval_T *rettv)
10608{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 tabpage_T *save_curtab;
10610 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 char_u *varname, *tabvarname;
10612 typval_T *varp;
10613
10614 rettv->vval.v_number = 0;
10615
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010616 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 return;
10618
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010619 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10620 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 varp = &argvars[2];
10622
Bram Moolenaar4033c552017-09-16 20:54:51 +020010623 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010625 save_curtab = curtab;
10626 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010627
Bram Moolenaar964b3742019-05-24 18:54:09 +020010628 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 if (tabvarname != NULL)
10630 {
10631 STRCPY(tabvarname, "t:");
10632 STRCPY(tabvarname + 2, varname);
10633 set_var(tabvarname, varp, TRUE);
10634 vim_free(tabvarname);
10635 }
10636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 /* Restore current tabpage */
10638 if (valid_tabpage(save_curtab))
10639 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 }
10641}
10642
10643/*
10644 * "settabwinvar()" function
10645 */
10646 static void
10647f_settabwinvar(typval_T *argvars, typval_T *rettv)
10648{
10649 setwinvar(argvars, rettv, 1);
10650}
10651
10652/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010653 * "settagstack()" function
10654 */
10655 static void
10656f_settagstack(typval_T *argvars, typval_T *rettv)
10657{
10658 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10659 win_T *wp;
10660 dict_T *d;
10661 int action = 'r';
10662
10663 rettv->vval.v_number = -1;
10664
10665 // first argument: window number or id
10666 wp = find_win_by_nr_or_id(&argvars[0]);
10667 if (wp == NULL)
10668 return;
10669
10670 // second argument: dict with items to set in the tag stack
10671 if (argvars[1].v_type != VAR_DICT)
10672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010673 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010674 return;
10675 }
10676 d = argvars[1].vval.v_dict;
10677 if (d == NULL)
10678 return;
10679
10680 // third argument: action - 'a' for append and 'r' for replace.
10681 // default is to replace the stack.
10682 if (argvars[2].v_type == VAR_UNKNOWN)
10683 action = 'r';
10684 else if (argvars[2].v_type == VAR_STRING)
10685 {
10686 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010687 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010688 if (actstr == NULL)
10689 return;
10690 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10691 action = *actstr;
10692 else
10693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010694 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010695 return;
10696 }
10697 }
10698 else
10699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010700 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010701 return;
10702 }
10703
10704 if (set_tagstack(wp, d, action) == OK)
10705 rettv->vval.v_number = 0;
10706}
10707
10708/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010709 * "setwinvar()" function
10710 */
10711 static void
10712f_setwinvar(typval_T *argvars, typval_T *rettv)
10713{
10714 setwinvar(argvars, rettv, 0);
10715}
10716
10717#ifdef FEAT_CRYPT
10718/*
10719 * "sha256({string})" function
10720 */
10721 static void
10722f_sha256(typval_T *argvars, typval_T *rettv)
10723{
10724 char_u *p;
10725
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010726 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010727 rettv->vval.v_string = vim_strsave(
10728 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10729 rettv->v_type = VAR_STRING;
10730}
10731#endif /* FEAT_CRYPT */
10732
10733/*
10734 * "shellescape({string})" function
10735 */
10736 static void
10737f_shellescape(typval_T *argvars, typval_T *rettv)
10738{
Bram Moolenaar20615522017-06-05 18:46:26 +020010739 int do_special = non_zero_arg(&argvars[1]);
10740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010742 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 rettv->v_type = VAR_STRING;
10744}
10745
10746/*
10747 * shiftwidth() function
10748 */
10749 static void
10750f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10751{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010752 rettv->vval.v_number = 0;
10753
10754 if (argvars[0].v_type != VAR_UNKNOWN)
10755 {
10756 long col;
10757
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010758 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010759 if (col < 0)
10760 return; // type error; errmsg already given
10761#ifdef FEAT_VARTABS
10762 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10763 return;
10764#endif
10765 }
10766
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010767 rettv->vval.v_number = get_sw_value(curbuf);
10768}
10769
10770/*
10771 * "simplify()" function
10772 */
10773 static void
10774f_simplify(typval_T *argvars, typval_T *rettv)
10775{
10776 char_u *p;
10777
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010778 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 rettv->vval.v_string = vim_strsave(p);
10780 simplify_filename(rettv->vval.v_string); /* simplify in place */
10781 rettv->v_type = VAR_STRING;
10782}
10783
10784#ifdef FEAT_FLOAT
10785/*
10786 * "sin()" function
10787 */
10788 static void
10789f_sin(typval_T *argvars, typval_T *rettv)
10790{
10791 float_T f = 0.0;
10792
10793 rettv->v_type = VAR_FLOAT;
10794 if (get_float_arg(argvars, &f) == OK)
10795 rettv->vval.v_float = sin(f);
10796 else
10797 rettv->vval.v_float = 0.0;
10798}
10799
10800/*
10801 * "sinh()" function
10802 */
10803 static void
10804f_sinh(typval_T *argvars, typval_T *rettv)
10805{
10806 float_T f = 0.0;
10807
10808 rettv->v_type = VAR_FLOAT;
10809 if (get_float_arg(argvars, &f) == OK)
10810 rettv->vval.v_float = sinh(f);
10811 else
10812 rettv->vval.v_float = 0.0;
10813}
10814#endif
10815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816/*
10817 * "soundfold({word})" function
10818 */
10819 static void
10820f_soundfold(typval_T *argvars, typval_T *rettv)
10821{
10822 char_u *s;
10823
10824 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010825 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826#ifdef FEAT_SPELL
10827 rettv->vval.v_string = eval_soundfold(s);
10828#else
10829 rettv->vval.v_string = vim_strsave(s);
10830#endif
10831}
10832
10833/*
10834 * "spellbadword()" function
10835 */
10836 static void
10837f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10838{
10839 char_u *word = (char_u *)"";
10840 hlf_T attr = HLF_COUNT;
10841 int len = 0;
10842
10843 if (rettv_list_alloc(rettv) == FAIL)
10844 return;
10845
10846#ifdef FEAT_SPELL
10847 if (argvars[0].v_type == VAR_UNKNOWN)
10848 {
10849 /* Find the start and length of the badly spelled word. */
10850 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10851 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010852 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010853 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010854 curwin->w_set_curswant = TRUE;
10855 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010856 }
10857 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10858 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010859 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 int capcol = -1;
10861
10862 if (str != NULL)
10863 {
10864 /* Check the argument for spelling. */
10865 while (*str != NUL)
10866 {
10867 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10868 if (attr != HLF_COUNT)
10869 {
10870 word = str;
10871 break;
10872 }
10873 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010874 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 }
10876 }
10877 }
10878#endif
10879
10880 list_append_string(rettv->vval.v_list, word, len);
10881 list_append_string(rettv->vval.v_list, (char_u *)(
10882 attr == HLF_SPB ? "bad" :
10883 attr == HLF_SPR ? "rare" :
10884 attr == HLF_SPL ? "local" :
10885 attr == HLF_SPC ? "caps" :
10886 ""), -1);
10887}
10888
10889/*
10890 * "spellsuggest()" function
10891 */
10892 static void
10893f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10894{
10895#ifdef FEAT_SPELL
10896 char_u *str;
10897 int typeerr = FALSE;
10898 int maxcount;
10899 garray_T ga;
10900 int i;
10901 listitem_T *li;
10902 int need_capital = FALSE;
10903#endif
10904
10905 if (rettv_list_alloc(rettv) == FAIL)
10906 return;
10907
10908#ifdef FEAT_SPELL
10909 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010911 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 if (argvars[1].v_type != VAR_UNKNOWN)
10913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010914 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010915 if (maxcount <= 0)
10916 return;
10917 if (argvars[2].v_type != VAR_UNKNOWN)
10918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010919 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010920 if (typeerr)
10921 return;
10922 }
10923 }
10924 else
10925 maxcount = 25;
10926
10927 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10928
10929 for (i = 0; i < ga.ga_len; ++i)
10930 {
10931 str = ((char_u **)ga.ga_data)[i];
10932
10933 li = listitem_alloc();
10934 if (li == NULL)
10935 vim_free(str);
10936 else
10937 {
10938 li->li_tv.v_type = VAR_STRING;
10939 li->li_tv.v_lock = 0;
10940 li->li_tv.vval.v_string = str;
10941 list_append(rettv->vval.v_list, li);
10942 }
10943 }
10944 ga_clear(&ga);
10945 }
10946#endif
10947}
10948
10949 static void
10950f_split(typval_T *argvars, typval_T *rettv)
10951{
10952 char_u *str;
10953 char_u *end;
10954 char_u *pat = NULL;
10955 regmatch_T regmatch;
10956 char_u patbuf[NUMBUFLEN];
10957 char_u *save_cpo;
10958 int match;
10959 colnr_T col = 0;
10960 int keepempty = FALSE;
10961 int typeerr = FALSE;
10962
10963 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10964 save_cpo = p_cpo;
10965 p_cpo = (char_u *)"";
10966
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010967 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (argvars[1].v_type != VAR_UNKNOWN)
10969 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010970 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 if (pat == NULL)
10972 typeerr = TRUE;
10973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 }
10976 if (pat == NULL || *pat == NUL)
10977 pat = (char_u *)"[\\x01- ]\\+";
10978
10979 if (rettv_list_alloc(rettv) == FAIL)
10980 return;
10981 if (typeerr)
10982 return;
10983
10984 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10985 if (regmatch.regprog != NULL)
10986 {
10987 regmatch.rm_ic = FALSE;
10988 while (*str != NUL || keepempty)
10989 {
10990 if (*str == NUL)
10991 match = FALSE; /* empty item at the end */
10992 else
10993 match = vim_regexec_nl(&regmatch, str, col);
10994 if (match)
10995 end = regmatch.startp[0];
10996 else
10997 end = str + STRLEN(str);
10998 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10999 && *str != NUL && match && end < regmatch.endp[0]))
11000 {
11001 if (list_append_string(rettv->vval.v_list, str,
11002 (int)(end - str)) == FAIL)
11003 break;
11004 }
11005 if (!match)
11006 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011007 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 if (regmatch.endp[0] > str)
11009 col = 0;
11010 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011011 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 str = regmatch.endp[0];
11014 }
11015
11016 vim_regfree(regmatch.regprog);
11017 }
11018
11019 p_cpo = save_cpo;
11020}
11021
11022#ifdef FEAT_FLOAT
11023/*
11024 * "sqrt()" function
11025 */
11026 static void
11027f_sqrt(typval_T *argvars, typval_T *rettv)
11028{
11029 float_T f = 0.0;
11030
11031 rettv->v_type = VAR_FLOAT;
11032 if (get_float_arg(argvars, &f) == OK)
11033 rettv->vval.v_float = sqrt(f);
11034 else
11035 rettv->vval.v_float = 0.0;
11036}
11037
11038/*
11039 * "str2float()" function
11040 */
11041 static void
11042f_str2float(typval_T *argvars, typval_T *rettv)
11043{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011044 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011045 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046
Bram Moolenaar08243d22017-01-10 16:12:29 +010011047 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 p = skipwhite(p + 1);
11049 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011050 if (isneg)
11051 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 rettv->v_type = VAR_FLOAT;
11053}
11054#endif
11055
11056/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011057 * "str2list()" function
11058 */
11059 static void
11060f_str2list(typval_T *argvars, typval_T *rettv)
11061{
11062 char_u *p;
11063 int utf8 = FALSE;
11064
11065 if (rettv_list_alloc(rettv) == FAIL)
11066 return;
11067
11068 if (argvars[1].v_type != VAR_UNKNOWN)
11069 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11070
11071 p = tv_get_string(&argvars[0]);
11072
11073 if (has_mbyte || utf8)
11074 {
11075 int (*ptr2len)(char_u *);
11076 int (*ptr2char)(char_u *);
11077
11078 if (utf8 || enc_utf8)
11079 {
11080 ptr2len = utf_ptr2len;
11081 ptr2char = utf_ptr2char;
11082 }
11083 else
11084 {
11085 ptr2len = mb_ptr2len;
11086 ptr2char = mb_ptr2char;
11087 }
11088
11089 for ( ; *p != NUL; p += (*ptr2len)(p))
11090 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11091 }
11092 else
11093 for ( ; *p != NUL; ++p)
11094 list_append_number(rettv->vval.v_list, *p);
11095}
11096
11097/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011098 * "str2nr()" function
11099 */
11100 static void
11101f_str2nr(typval_T *argvars, typval_T *rettv)
11102{
11103 int base = 10;
11104 char_u *p;
11105 varnumber_T n;
11106 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011107 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108
11109 if (argvars[1].v_type != VAR_UNKNOWN)
11110 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011111 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112 if (base != 2 && base != 8 && base != 10 && base != 16)
11113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011114 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 return;
11116 }
11117 }
11118
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011119 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011120 isneg = (*p == '-');
11121 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 p = skipwhite(p + 1);
11123 switch (base)
11124 {
11125 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11126 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11127 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11128 default: what = 0;
11129 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011130 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11131 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011132 if (isneg)
11133 rettv->vval.v_number = -n;
11134 else
11135 rettv->vval.v_number = n;
11136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137}
11138
11139#ifdef HAVE_STRFTIME
11140/*
11141 * "strftime({format}[, {time}])" function
11142 */
11143 static void
11144f_strftime(typval_T *argvars, typval_T *rettv)
11145{
11146 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011147 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148 struct tm *curtime;
11149 time_t seconds;
11150 char_u *p;
11151
11152 rettv->v_type = VAR_STRING;
11153
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011154 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 if (argvars[1].v_type == VAR_UNKNOWN)
11156 seconds = time(NULL);
11157 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011158 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011159 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 /* MSVC returns NULL for an invalid value of seconds. */
11161 if (curtime == NULL)
11162 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11163 else
11164 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 vimconv_T conv;
11166 char_u *enc;
11167
11168 conv.vc_type = CONV_NONE;
11169 enc = enc_locale();
11170 convert_setup(&conv, p_enc, enc);
11171 if (conv.vc_type != CONV_NONE)
11172 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 if (p != NULL)
11174 (void)strftime((char *)result_buf, sizeof(result_buf),
11175 (char *)p, curtime);
11176 else
11177 result_buf[0] = NUL;
11178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011179 if (conv.vc_type != CONV_NONE)
11180 vim_free(p);
11181 convert_setup(&conv, enc, p_enc);
11182 if (conv.vc_type != CONV_NONE)
11183 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11184 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011185 rettv->vval.v_string = vim_strsave(result_buf);
11186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 /* Release conversion descriptors */
11188 convert_setup(&conv, NULL, NULL);
11189 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011190 }
11191}
11192#endif
11193
11194/*
11195 * "strgetchar()" function
11196 */
11197 static void
11198f_strgetchar(typval_T *argvars, typval_T *rettv)
11199{
11200 char_u *str;
11201 int len;
11202 int error = FALSE;
11203 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010011204 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205
11206 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011207 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208 if (str == NULL)
11209 return;
11210 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011211 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011212 if (error)
11213 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011214
Bram Moolenaar13505972019-01-24 15:04:48 +010011215 while (charidx >= 0 && byteidx < len)
11216 {
11217 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 {
Bram Moolenaar13505972019-01-24 15:04:48 +010011219 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11220 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 }
Bram Moolenaar13505972019-01-24 15:04:48 +010011222 --charidx;
11223 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011225}
11226
11227/*
11228 * "stridx()" function
11229 */
11230 static void
11231f_stridx(typval_T *argvars, typval_T *rettv)
11232{
11233 char_u buf[NUMBUFLEN];
11234 char_u *needle;
11235 char_u *haystack;
11236 char_u *save_haystack;
11237 char_u *pos;
11238 int start_idx;
11239
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011240 needle = tv_get_string_chk(&argvars[1]);
11241 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011242 rettv->vval.v_number = -1;
11243 if (needle == NULL || haystack == NULL)
11244 return; /* type error; errmsg already given */
11245
11246 if (argvars[2].v_type != VAR_UNKNOWN)
11247 {
11248 int error = FALSE;
11249
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011250 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011251 if (error || start_idx >= (int)STRLEN(haystack))
11252 return;
11253 if (start_idx >= 0)
11254 haystack += start_idx;
11255 }
11256
11257 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11258 if (pos != NULL)
11259 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11260}
11261
11262/*
11263 * "string()" function
11264 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010011265 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011266f_string(typval_T *argvars, typval_T *rettv)
11267{
11268 char_u *tofree;
11269 char_u numbuf[NUMBUFLEN];
11270
11271 rettv->v_type = VAR_STRING;
11272 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11273 get_copyID());
11274 /* Make a copy if we have a value but it's not in allocated memory. */
11275 if (rettv->vval.v_string != NULL && tofree == NULL)
11276 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11277}
11278
11279/*
11280 * "strlen()" function
11281 */
11282 static void
11283f_strlen(typval_T *argvars, typval_T *rettv)
11284{
11285 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011286 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011287}
11288
11289/*
11290 * "strchars()" function
11291 */
11292 static void
11293f_strchars(typval_T *argvars, typval_T *rettv)
11294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011295 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011296 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011297 varnumber_T len = 0;
11298 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299
11300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011301 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011303 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011304 else
11305 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11307 while (*s != NUL)
11308 {
11309 func_mb_ptr2char_adv(&s);
11310 ++len;
11311 }
11312 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011313 }
11314}
11315
11316/*
11317 * "strdisplaywidth()" function
11318 */
11319 static void
11320f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11321{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011322 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 int col = 0;
11324
11325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011326 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327
11328 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11329}
11330
11331/*
11332 * "strwidth()" function
11333 */
11334 static void
11335f_strwidth(typval_T *argvars, typval_T *rettv)
11336{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011337 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338
Bram Moolenaar13505972019-01-24 15:04:48 +010011339 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340}
11341
11342/*
11343 * "strcharpart()" function
11344 */
11345 static void
11346f_strcharpart(typval_T *argvars, typval_T *rettv)
11347{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011348 char_u *p;
11349 int nchar;
11350 int nbyte = 0;
11351 int charlen;
11352 int len = 0;
11353 int slen;
11354 int error = FALSE;
11355
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011356 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011357 slen = (int)STRLEN(p);
11358
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011359 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 if (!error)
11361 {
11362 if (nchar > 0)
11363 while (nchar > 0 && nbyte < slen)
11364 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011365 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 --nchar;
11367 }
11368 else
11369 nbyte = nchar;
11370 if (argvars[2].v_type != VAR_UNKNOWN)
11371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011372 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011373 while (charlen > 0 && nbyte + len < slen)
11374 {
11375 int off = nbyte + len;
11376
11377 if (off < 0)
11378 len += 1;
11379 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011380 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 --charlen;
11382 }
11383 }
11384 else
11385 len = slen - nbyte; /* default: all bytes that are available. */
11386 }
11387
11388 /*
11389 * Only return the overlap between the specified part and the actual
11390 * string.
11391 */
11392 if (nbyte < 0)
11393 {
11394 len += nbyte;
11395 nbyte = 0;
11396 }
11397 else if (nbyte > slen)
11398 nbyte = slen;
11399 if (len < 0)
11400 len = 0;
11401 else if (nbyte + len > slen)
11402 len = slen - nbyte;
11403
11404 rettv->v_type = VAR_STRING;
11405 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011406}
11407
11408/*
11409 * "strpart()" function
11410 */
11411 static void
11412f_strpart(typval_T *argvars, typval_T *rettv)
11413{
11414 char_u *p;
11415 int n;
11416 int len;
11417 int slen;
11418 int error = FALSE;
11419
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011420 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011421 slen = (int)STRLEN(p);
11422
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011423 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011424 if (error)
11425 len = 0;
11426 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011427 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011428 else
11429 len = slen - n; /* default len: all bytes that are available. */
11430
11431 /*
11432 * Only return the overlap between the specified part and the actual
11433 * string.
11434 */
11435 if (n < 0)
11436 {
11437 len += n;
11438 n = 0;
11439 }
11440 else if (n > slen)
11441 n = slen;
11442 if (len < 0)
11443 len = 0;
11444 else if (n + len > slen)
11445 len = slen - n;
11446
11447 rettv->v_type = VAR_STRING;
11448 rettv->vval.v_string = vim_strnsave(p + n, len);
11449}
11450
11451/*
11452 * "strridx()" function
11453 */
11454 static void
11455f_strridx(typval_T *argvars, typval_T *rettv)
11456{
11457 char_u buf[NUMBUFLEN];
11458 char_u *needle;
11459 char_u *haystack;
11460 char_u *rest;
11461 char_u *lastmatch = NULL;
11462 int haystack_len, end_idx;
11463
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011464 needle = tv_get_string_chk(&argvars[1]);
11465 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466
11467 rettv->vval.v_number = -1;
11468 if (needle == NULL || haystack == NULL)
11469 return; /* type error; errmsg already given */
11470
11471 haystack_len = (int)STRLEN(haystack);
11472 if (argvars[2].v_type != VAR_UNKNOWN)
11473 {
11474 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011475 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011476 if (end_idx < 0)
11477 return; /* can never find a match */
11478 }
11479 else
11480 end_idx = haystack_len;
11481
11482 if (*needle == NUL)
11483 {
11484 /* Empty string matches past the end. */
11485 lastmatch = haystack + end_idx;
11486 }
11487 else
11488 {
11489 for (rest = haystack; *rest != '\0'; ++rest)
11490 {
11491 rest = (char_u *)strstr((char *)rest, (char *)needle);
11492 if (rest == NULL || rest > haystack + end_idx)
11493 break;
11494 lastmatch = rest;
11495 }
11496 }
11497
11498 if (lastmatch == NULL)
11499 rettv->vval.v_number = -1;
11500 else
11501 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11502}
11503
11504/*
11505 * "strtrans()" function
11506 */
11507 static void
11508f_strtrans(typval_T *argvars, typval_T *rettv)
11509{
11510 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011511 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512}
11513
11514/*
11515 * "submatch()" function
11516 */
11517 static void
11518f_submatch(typval_T *argvars, typval_T *rettv)
11519{
11520 int error = FALSE;
11521 int no;
11522 int retList = 0;
11523
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011524 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011525 if (error)
11526 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011527 if (no < 0 || no >= NSUBEXP)
11528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011529 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011530 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011533 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534 if (error)
11535 return;
11536
11537 if (retList == 0)
11538 {
11539 rettv->v_type = VAR_STRING;
11540 rettv->vval.v_string = reg_submatch(no);
11541 }
11542 else
11543 {
11544 rettv->v_type = VAR_LIST;
11545 rettv->vval.v_list = reg_submatch_list(no);
11546 }
11547}
11548
11549/*
11550 * "substitute()" function
11551 */
11552 static void
11553f_substitute(typval_T *argvars, typval_T *rettv)
11554{
11555 char_u patbuf[NUMBUFLEN];
11556 char_u subbuf[NUMBUFLEN];
11557 char_u flagsbuf[NUMBUFLEN];
11558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011559 char_u *str = tv_get_string_chk(&argvars[0]);
11560 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011561 char_u *sub = NULL;
11562 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011563 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011565 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11566 expr = &argvars[2];
11567 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011568 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011571 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11572 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 rettv->vval.v_string = NULL;
11574 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011575 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576}
11577
11578/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011579 * "swapinfo(swap_filename)" function
11580 */
11581 static void
11582f_swapinfo(typval_T *argvars, typval_T *rettv)
11583{
11584 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011585 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011586}
11587
11588/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011589 * "swapname(expr)" function
11590 */
11591 static void
11592f_swapname(typval_T *argvars, typval_T *rettv)
11593{
11594 buf_T *buf;
11595
11596 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011597 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011598 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11599 || buf->b_ml.ml_mfp->mf_fname == NULL)
11600 rettv->vval.v_string = NULL;
11601 else
11602 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11603}
11604
11605/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 * "synID(lnum, col, trans)" function
11607 */
11608 static void
11609f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11610{
11611 int id = 0;
11612#ifdef FEAT_SYN_HL
11613 linenr_T lnum;
11614 colnr_T col;
11615 int trans;
11616 int transerr = FALSE;
11617
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011618 lnum = tv_get_lnum(argvars); /* -1 on type error */
11619 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11620 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621
11622 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11623 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11624 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11625#endif
11626
11627 rettv->vval.v_number = id;
11628}
11629
11630/*
11631 * "synIDattr(id, what [, mode])" function
11632 */
11633 static void
11634f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11635{
11636 char_u *p = NULL;
11637#ifdef FEAT_SYN_HL
11638 int id;
11639 char_u *what;
11640 char_u *mode;
11641 char_u modebuf[NUMBUFLEN];
11642 int modec;
11643
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011644 id = (int)tv_get_number(&argvars[0]);
11645 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011646 if (argvars[2].v_type != VAR_UNKNOWN)
11647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011648 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 modec = TOLOWER_ASC(mode[0]);
11650 if (modec != 't' && modec != 'c' && modec != 'g')
11651 modec = 0; /* replace invalid with current */
11652 }
11653 else
11654 {
11655#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11656 if (USE_24BIT)
11657 modec = 'g';
11658 else
11659#endif
11660 if (t_colors > 1)
11661 modec = 'c';
11662 else
11663 modec = 't';
11664 }
11665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011666 switch (TOLOWER_ASC(what[0]))
11667 {
11668 case 'b':
11669 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11670 p = highlight_color(id, what, modec);
11671 else /* bold */
11672 p = highlight_has_attr(id, HL_BOLD, modec);
11673 break;
11674
11675 case 'f': /* fg[#] or font */
11676 p = highlight_color(id, what, modec);
11677 break;
11678
11679 case 'i':
11680 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11681 p = highlight_has_attr(id, HL_INVERSE, modec);
11682 else /* italic */
11683 p = highlight_has_attr(id, HL_ITALIC, modec);
11684 break;
11685
11686 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011687 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011688 break;
11689
11690 case 'r': /* reverse */
11691 p = highlight_has_attr(id, HL_INVERSE, modec);
11692 break;
11693
11694 case 's':
11695 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11696 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011697 /* strikeout */
11698 else if (TOLOWER_ASC(what[1]) == 't' &&
11699 TOLOWER_ASC(what[2]) == 'r')
11700 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011701 else /* standout */
11702 p = highlight_has_attr(id, HL_STANDOUT, modec);
11703 break;
11704
11705 case 'u':
11706 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11707 /* underline */
11708 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11709 else
11710 /* undercurl */
11711 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11712 break;
11713 }
11714
11715 if (p != NULL)
11716 p = vim_strsave(p);
11717#endif
11718 rettv->v_type = VAR_STRING;
11719 rettv->vval.v_string = p;
11720}
11721
11722/*
11723 * "synIDtrans(id)" function
11724 */
11725 static void
11726f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11727{
11728 int id;
11729
11730#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011731 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732
11733 if (id > 0)
11734 id = syn_get_final_id(id);
11735 else
11736#endif
11737 id = 0;
11738
11739 rettv->vval.v_number = id;
11740}
11741
11742/*
11743 * "synconcealed(lnum, col)" function
11744 */
11745 static void
11746f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11747{
11748#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11749 linenr_T lnum;
11750 colnr_T col;
11751 int syntax_flags = 0;
11752 int cchar;
11753 int matchid = 0;
11754 char_u str[NUMBUFLEN];
11755#endif
11756
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011757 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011758
11759#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011760 lnum = tv_get_lnum(argvars); /* -1 on type error */
11761 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011762
11763 vim_memset(str, NUL, sizeof(str));
11764
11765 if (rettv_list_alloc(rettv) != FAIL)
11766 {
11767 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11768 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11769 && curwin->w_p_cole > 0)
11770 {
11771 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11772 syntax_flags = get_syntax_info(&matchid);
11773
11774 /* get the conceal character */
11775 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11776 {
11777 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011778 if (cchar == NUL && curwin->w_p_cole == 1)
11779 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 if (cchar != NUL)
11781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011782 if (has_mbyte)
11783 (*mb_char2bytes)(cchar, str);
11784 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011785 str[0] = cchar;
11786 }
11787 }
11788 }
11789
11790 list_append_number(rettv->vval.v_list,
11791 (syntax_flags & HL_CONCEAL) != 0);
11792 /* -1 to auto-determine strlen */
11793 list_append_string(rettv->vval.v_list, str, -1);
11794 list_append_number(rettv->vval.v_list, matchid);
11795 }
11796#endif
11797}
11798
11799/*
11800 * "synstack(lnum, col)" function
11801 */
11802 static void
11803f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11804{
11805#ifdef FEAT_SYN_HL
11806 linenr_T lnum;
11807 colnr_T col;
11808 int i;
11809 int id;
11810#endif
11811
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011812 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813
11814#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011815 lnum = tv_get_lnum(argvars); /* -1 on type error */
11816 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817
11818 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11819 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11820 && rettv_list_alloc(rettv) != FAIL)
11821 {
11822 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11823 for (i = 0; ; ++i)
11824 {
11825 id = syn_get_stack_item(i);
11826 if (id < 0)
11827 break;
11828 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11829 break;
11830 }
11831 }
11832#endif
11833}
11834
11835 static void
11836get_cmd_output_as_rettv(
11837 typval_T *argvars,
11838 typval_T *rettv,
11839 int retlist)
11840{
11841 char_u *res = NULL;
11842 char_u *p;
11843 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011844 int err = FALSE;
11845 FILE *fd;
11846 list_T *list = NULL;
11847 int flags = SHELL_SILENT;
11848
11849 rettv->v_type = VAR_STRING;
11850 rettv->vval.v_string = NULL;
11851 if (check_restricted() || check_secure())
11852 goto errret;
11853
11854 if (argvars[1].v_type != VAR_UNKNOWN)
11855 {
11856 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011857 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011858 * command.
11859 */
11860 if ((infile = vim_tempname('i', TRUE)) == NULL)
11861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011862 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011863 goto errret;
11864 }
11865
11866 fd = mch_fopen((char *)infile, WRITEBIN);
11867 if (fd == NULL)
11868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011869 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870 goto errret;
11871 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011872 if (argvars[1].v_type == VAR_NUMBER)
11873 {
11874 linenr_T lnum;
11875 buf_T *buf;
11876
11877 buf = buflist_findnr(argvars[1].vval.v_number);
11878 if (buf == NULL)
11879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011880 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011881 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011882 goto errret;
11883 }
11884
11885 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11886 {
11887 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11888 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11889 {
11890 err = TRUE;
11891 break;
11892 }
11893 if (putc(NL, fd) == EOF)
11894 {
11895 err = TRUE;
11896 break;
11897 }
11898 }
11899 }
11900 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011901 {
11902 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11903 err = TRUE;
11904 }
11905 else
11906 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011907 size_t len;
11908 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011909
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011910 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011911 if (p == NULL)
11912 {
11913 fclose(fd);
11914 goto errret; /* type error; errmsg already given */
11915 }
11916 len = STRLEN(p);
11917 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11918 err = TRUE;
11919 }
11920 if (fclose(fd) != 0)
11921 err = TRUE;
11922 if (err)
11923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011924 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925 goto errret;
11926 }
11927 }
11928
11929 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11930 * echoes typeahead, that messes up the display. */
11931 if (!msg_silent)
11932 flags += SHELL_COOKED;
11933
11934 if (retlist)
11935 {
11936 int len;
11937 listitem_T *li;
11938 char_u *s = NULL;
11939 char_u *start;
11940 char_u *end;
11941 int i;
11942
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011943 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011944 if (res == NULL)
11945 goto errret;
11946
11947 list = list_alloc();
11948 if (list == NULL)
11949 goto errret;
11950
11951 for (i = 0; i < len; ++i)
11952 {
11953 start = res + i;
11954 while (i < len && res[i] != NL)
11955 ++i;
11956 end = res + i;
11957
Bram Moolenaar964b3742019-05-24 18:54:09 +020011958 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 if (s == NULL)
11960 goto errret;
11961
11962 for (p = s; start < end; ++p, ++start)
11963 *p = *start == NUL ? NL : *start;
11964 *p = NUL;
11965
11966 li = listitem_alloc();
11967 if (li == NULL)
11968 {
11969 vim_free(s);
11970 goto errret;
11971 }
11972 li->li_tv.v_type = VAR_STRING;
11973 li->li_tv.v_lock = 0;
11974 li->li_tv.vval.v_string = s;
11975 list_append(list, li);
11976 }
11977
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011978 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011979 list = NULL;
11980 }
11981 else
11982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011983 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011984#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011985 /* translate <CR><NL> into <NL> */
11986 if (res != NULL)
11987 {
11988 char_u *s, *d;
11989
11990 d = res;
11991 for (s = res; *s; ++s)
11992 {
11993 if (s[0] == CAR && s[1] == NL)
11994 ++s;
11995 *d++ = *s;
11996 }
11997 *d = NUL;
11998 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999#endif
12000 rettv->vval.v_string = res;
12001 res = NULL;
12002 }
12003
12004errret:
12005 if (infile != NULL)
12006 {
12007 mch_remove(infile);
12008 vim_free(infile);
12009 }
12010 if (res != NULL)
12011 vim_free(res);
12012 if (list != NULL)
12013 list_free(list);
12014}
12015
12016/*
12017 * "system()" function
12018 */
12019 static void
12020f_system(typval_T *argvars, typval_T *rettv)
12021{
12022 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12023}
12024
12025/*
12026 * "systemlist()" function
12027 */
12028 static void
12029f_systemlist(typval_T *argvars, typval_T *rettv)
12030{
12031 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12032}
12033
12034/*
12035 * "tabpagebuflist()" function
12036 */
12037 static void
12038f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12039{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 tabpage_T *tp;
12041 win_T *wp = NULL;
12042
12043 if (argvars[0].v_type == VAR_UNKNOWN)
12044 wp = firstwin;
12045 else
12046 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012047 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 if (tp != NULL)
12049 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12050 }
12051 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12052 {
12053 for (; wp != NULL; wp = wp->w_next)
12054 if (list_append_number(rettv->vval.v_list,
12055 wp->w_buffer->b_fnum) == FAIL)
12056 break;
12057 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012058}
12059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012060/*
12061 * "tabpagenr()" function
12062 */
12063 static void
12064f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12065{
12066 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012067 char_u *arg;
12068
12069 if (argvars[0].v_type != VAR_UNKNOWN)
12070 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012071 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012072 nr = 0;
12073 if (arg != NULL)
12074 {
12075 if (STRCMP(arg, "$") == 0)
12076 nr = tabpage_index(NULL) - 1;
12077 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012078 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012079 }
12080 }
12081 else
12082 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012083 rettv->vval.v_number = nr;
12084}
12085
12086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012087/*
12088 * Common code for tabpagewinnr() and winnr().
12089 */
12090 static int
12091get_winnr(tabpage_T *tp, typval_T *argvar)
12092{
12093 win_T *twin;
12094 int nr = 1;
12095 win_T *wp;
12096 char_u *arg;
12097
12098 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12099 if (argvar->v_type != VAR_UNKNOWN)
12100 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012101 int invalid_arg = FALSE;
12102
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012103 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012104 if (arg == NULL)
12105 nr = 0; /* type error; errmsg already given */
12106 else if (STRCMP(arg, "$") == 0)
12107 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12108 else if (STRCMP(arg, "#") == 0)
12109 {
12110 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12111 if (twin == NULL)
12112 nr = 0;
12113 }
12114 else
12115 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012116 long count;
12117 char_u *endp;
12118
12119 // Extract the window count (if specified). e.g. winnr('3j')
12120 count = strtol((char *)arg, (char **)&endp, 10);
12121 if (count <= 0)
12122 count = 1; // if count is not specified, default to 1
12123 if (endp != NULL && *endp != '\0')
12124 {
12125 if (STRCMP(endp, "j") == 0)
12126 twin = win_vert_neighbor(tp, twin, FALSE, count);
12127 else if (STRCMP(endp, "k") == 0)
12128 twin = win_vert_neighbor(tp, twin, TRUE, count);
12129 else if (STRCMP(endp, "h") == 0)
12130 twin = win_horz_neighbor(tp, twin, TRUE, count);
12131 else if (STRCMP(endp, "l") == 0)
12132 twin = win_horz_neighbor(tp, twin, FALSE, count);
12133 else
12134 invalid_arg = TRUE;
12135 }
12136 else
12137 invalid_arg = TRUE;
12138 }
12139
12140 if (invalid_arg)
12141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012142 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143 nr = 0;
12144 }
12145 }
12146
12147 if (nr > 0)
12148 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12149 wp != twin; wp = wp->w_next)
12150 {
12151 if (wp == NULL)
12152 {
12153 /* didn't find it in this tabpage */
12154 nr = 0;
12155 break;
12156 }
12157 ++nr;
12158 }
12159 return nr;
12160}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012161
12162/*
12163 * "tabpagewinnr()" function
12164 */
12165 static void
12166f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12167{
12168 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012169 tabpage_T *tp;
12170
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012171 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012172 if (tp == NULL)
12173 nr = 0;
12174 else
12175 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012176 rettv->vval.v_number = nr;
12177}
12178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012179/*
12180 * "tagfiles()" function
12181 */
12182 static void
12183f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12184{
12185 char_u *fname;
12186 tagname_T tn;
12187 int first;
12188
12189 if (rettv_list_alloc(rettv) == FAIL)
12190 return;
12191 fname = alloc(MAXPATHL);
12192 if (fname == NULL)
12193 return;
12194
12195 for (first = TRUE; ; first = FALSE)
12196 if (get_tagfname(&tn, first, fname) == FAIL
12197 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12198 break;
12199 tagname_free(&tn);
12200 vim_free(fname);
12201}
12202
12203/*
12204 * "taglist()" function
12205 */
12206 static void
12207f_taglist(typval_T *argvars, typval_T *rettv)
12208{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012209 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 char_u *tag_pattern;
12211
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012212 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213
12214 rettv->vval.v_number = FALSE;
12215 if (*tag_pattern == NUL)
12216 return;
12217
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012219 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012220 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012221 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012222}
12223
12224/*
12225 * "tempname()" function
12226 */
12227 static void
12228f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12229{
12230 static int x = 'A';
12231
12232 rettv->v_type = VAR_STRING;
12233 rettv->vval.v_string = vim_tempname(x, FALSE);
12234
12235 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12236 * names. Skip 'I' and 'O', they are used for shell redirection. */
12237 do
12238 {
12239 if (x == 'Z')
12240 x = '0';
12241 else if (x == '9')
12242 x = 'A';
12243 else
12244 {
12245#ifdef EBCDIC
12246 if (x == 'I')
12247 x = 'J';
12248 else if (x == 'R')
12249 x = 'S';
12250 else
12251#endif
12252 ++x;
12253 }
12254 } while (x == 'I' || x == 'O');
12255}
12256
12257#ifdef FEAT_FLOAT
12258/*
12259 * "tan()" function
12260 */
12261 static void
12262f_tan(typval_T *argvars, typval_T *rettv)
12263{
12264 float_T f = 0.0;
12265
12266 rettv->v_type = VAR_FLOAT;
12267 if (get_float_arg(argvars, &f) == OK)
12268 rettv->vval.v_float = tan(f);
12269 else
12270 rettv->vval.v_float = 0.0;
12271}
12272
12273/*
12274 * "tanh()" function
12275 */
12276 static void
12277f_tanh(typval_T *argvars, typval_T *rettv)
12278{
12279 float_T f = 0.0;
12280
12281 rettv->v_type = VAR_FLOAT;
12282 if (get_float_arg(argvars, &f) == OK)
12283 rettv->vval.v_float = tanh(f);
12284 else
12285 rettv->vval.v_float = 0.0;
12286}
12287#endif
12288
12289/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 * Get a callback from "arg". It can be a Funcref or a function name.
12291 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012292 * "cb_name" is not allocated.
12293 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012295 callback_T
12296get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012297{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012298 callback_T res;
12299
12300 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012301 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12302 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012303 res.cb_partial = arg->vval.v_partial;
12304 ++res.cb_partial->pt_refcount;
12305 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012307 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012308 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012309 res.cb_partial = NULL;
12310 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12311 {
12312 // Note that we don't make a copy of the string.
12313 res.cb_name = arg->vval.v_string;
12314 func_ref(res.cb_name);
12315 }
12316 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12317 {
12318 res.cb_name = (char_u *)"";
12319 }
12320 else
12321 {
12322 emsg(_("E921: Invalid callback argument"));
12323 res.cb_name = NULL;
12324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012325 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012326 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012327}
12328
12329/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012330 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331 */
12332 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012333put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012334{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012335 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012337 tv->v_type = VAR_PARTIAL;
12338 tv->vval.v_partial = cb->cb_partial;
12339 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012340 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012341 else
12342 {
12343 tv->v_type = VAR_FUNC;
12344 tv->vval.v_string = vim_strsave(cb->cb_name);
12345 func_ref(cb->cb_name);
12346 }
12347}
12348
12349/*
12350 * Make a copy of "src" into "dest", allocating the function name if needed,
12351 * without incrementing the refcount.
12352 */
12353 void
12354set_callback(callback_T *dest, callback_T *src)
12355{
12356 if (src->cb_partial == NULL)
12357 {
12358 // just a function name, make a copy
12359 dest->cb_name = vim_strsave(src->cb_name);
12360 dest->cb_free_name = TRUE;
12361 }
12362 else
12363 {
12364 // cb_name is a pointer into cb_partial
12365 dest->cb_name = src->cb_name;
12366 dest->cb_free_name = FALSE;
12367 }
12368 dest->cb_partial = src->cb_partial;
12369}
12370
12371/*
12372 * Unref/free "callback" returned by get_callback() or set_callback().
12373 */
12374 void
12375free_callback(callback_T *callback)
12376{
12377 if (callback->cb_partial != NULL)
12378 {
12379 partial_unref(callback->cb_partial);
12380 callback->cb_partial = NULL;
12381 }
12382 else if (callback->cb_name != NULL)
12383 func_unref(callback->cb_name);
12384 if (callback->cb_free_name)
12385 {
12386 vim_free(callback->cb_name);
12387 callback->cb_free_name = FALSE;
12388 }
12389 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012390}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012391
12392#ifdef FEAT_TIMERS
12393/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012394 * "timer_info([timer])" function
12395 */
12396 static void
12397f_timer_info(typval_T *argvars, typval_T *rettv)
12398{
12399 timer_T *timer = NULL;
12400
12401 if (rettv_list_alloc(rettv) != OK)
12402 return;
12403 if (argvars[0].v_type != VAR_UNKNOWN)
12404 {
12405 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012406 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012407 else
12408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012409 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012410 if (timer != NULL)
12411 add_timer_info(rettv, timer);
12412 }
12413 }
12414 else
12415 add_timer_info_all(rettv);
12416}
12417
12418/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012419 * "timer_pause(timer, paused)" function
12420 */
12421 static void
12422f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12423{
12424 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012425 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012426
12427 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012428 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012429 else
12430 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012431 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012432 if (timer != NULL)
12433 timer->tr_paused = paused;
12434 }
12435}
12436
12437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012438 * "timer_start(time, callback [, options])" function
12439 */
12440 static void
12441f_timer_start(typval_T *argvars, typval_T *rettv)
12442{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012443 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012444 timer_T *timer;
12445 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012446 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012447 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012448
Bram Moolenaar75537a92016-09-05 22:45:28 +020012449 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 if (check_secure())
12451 return;
12452 if (argvars[2].v_type != VAR_UNKNOWN)
12453 {
12454 if (argvars[2].v_type != VAR_DICT
12455 || (dict = argvars[2].vval.v_dict) == NULL)
12456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012457 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012458 return;
12459 }
12460 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012461 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462 }
12463
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012464 callback = get_callback(&argvars[1]);
12465 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012466 return;
12467
12468 timer = create_timer(msec, repeat);
12469 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012470 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012471 else
12472 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012473 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012474 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475 }
12476}
12477
12478/*
12479 * "timer_stop(timer)" function
12480 */
12481 static void
12482f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12483{
12484 timer_T *timer;
12485
12486 if (argvars[0].v_type != VAR_NUMBER)
12487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012488 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489 return;
12490 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012491 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012492 if (timer != NULL)
12493 stop_timer(timer);
12494}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012495
12496/*
12497 * "timer_stopall()" function
12498 */
12499 static void
12500f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12501{
12502 stop_all_timers();
12503}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504#endif
12505
12506/*
12507 * "tolower(string)" function
12508 */
12509 static void
12510f_tolower(typval_T *argvars, typval_T *rettv)
12511{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012512 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012513 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012514}
12515
12516/*
12517 * "toupper(string)" function
12518 */
12519 static void
12520f_toupper(typval_T *argvars, typval_T *rettv)
12521{
12522 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012523 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012524}
12525
12526/*
12527 * "tr(string, fromstr, tostr)" function
12528 */
12529 static void
12530f_tr(typval_T *argvars, typval_T *rettv)
12531{
12532 char_u *in_str;
12533 char_u *fromstr;
12534 char_u *tostr;
12535 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536 int inlen;
12537 int fromlen;
12538 int tolen;
12539 int idx;
12540 char_u *cpstr;
12541 int cplen;
12542 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 char_u buf[NUMBUFLEN];
12544 char_u buf2[NUMBUFLEN];
12545 garray_T ga;
12546
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012547 in_str = tv_get_string(&argvars[0]);
12548 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12549 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550
12551 /* Default return value: empty string. */
12552 rettv->v_type = VAR_STRING;
12553 rettv->vval.v_string = NULL;
12554 if (fromstr == NULL || tostr == NULL)
12555 return; /* type error; errmsg already given */
12556 ga_init2(&ga, (int)sizeof(char), 80);
12557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012558 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559 /* not multi-byte: fromstr and tostr must be the same length */
12560 if (STRLEN(fromstr) != STRLEN(tostr))
12561 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012563 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564 ga_clear(&ga);
12565 return;
12566 }
12567
12568 /* fromstr and tostr have to contain the same number of chars */
12569 while (*in_str != NUL)
12570 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012571 if (has_mbyte)
12572 {
12573 inlen = (*mb_ptr2len)(in_str);
12574 cpstr = in_str;
12575 cplen = inlen;
12576 idx = 0;
12577 for (p = fromstr; *p != NUL; p += fromlen)
12578 {
12579 fromlen = (*mb_ptr2len)(p);
12580 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12581 {
12582 for (p = tostr; *p != NUL; p += tolen)
12583 {
12584 tolen = (*mb_ptr2len)(p);
12585 if (idx-- == 0)
12586 {
12587 cplen = tolen;
12588 cpstr = p;
12589 break;
12590 }
12591 }
12592 if (*p == NUL) /* tostr is shorter than fromstr */
12593 goto error;
12594 break;
12595 }
12596 ++idx;
12597 }
12598
12599 if (first && cpstr == in_str)
12600 {
12601 /* Check that fromstr and tostr have the same number of
12602 * (multi-byte) characters. Done only once when a character
12603 * of in_str doesn't appear in fromstr. */
12604 first = FALSE;
12605 for (p = tostr; *p != NUL; p += tolen)
12606 {
12607 tolen = (*mb_ptr2len)(p);
12608 --idx;
12609 }
12610 if (idx != 0)
12611 goto error;
12612 }
12613
12614 (void)ga_grow(&ga, cplen);
12615 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12616 ga.ga_len += cplen;
12617
12618 in_str += inlen;
12619 }
12620 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621 {
12622 /* When not using multi-byte chars we can do it faster. */
12623 p = vim_strchr(fromstr, *in_str);
12624 if (p != NULL)
12625 ga_append(&ga, tostr[p - fromstr]);
12626 else
12627 ga_append(&ga, *in_str);
12628 ++in_str;
12629 }
12630 }
12631
12632 /* add a terminating NUL */
12633 (void)ga_grow(&ga, 1);
12634 ga_append(&ga, NUL);
12635
12636 rettv->vval.v_string = ga.ga_data;
12637}
12638
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012639/*
12640 * "trim({expr})" function
12641 */
12642 static void
12643f_trim(typval_T *argvars, typval_T *rettv)
12644{
12645 char_u buf1[NUMBUFLEN];
12646 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012647 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012648 char_u *mask = NULL;
12649 char_u *tail;
12650 char_u *prev;
12651 char_u *p;
12652 int c1;
12653
12654 rettv->v_type = VAR_STRING;
12655 if (head == NULL)
12656 {
12657 rettv->vval.v_string = NULL;
12658 return;
12659 }
12660
12661 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012662 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012663
12664 while (*head != NUL)
12665 {
12666 c1 = PTR2CHAR(head);
12667 if (mask == NULL)
12668 {
12669 if (c1 > ' ' && c1 != 0xa0)
12670 break;
12671 }
12672 else
12673 {
12674 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12675 if (c1 == PTR2CHAR(p))
12676 break;
12677 if (*p == NUL)
12678 break;
12679 }
12680 MB_PTR_ADV(head);
12681 }
12682
12683 for (tail = head + STRLEN(head); tail > head; tail = prev)
12684 {
12685 prev = tail;
12686 MB_PTR_BACK(head, prev);
12687 c1 = PTR2CHAR(prev);
12688 if (mask == NULL)
12689 {
12690 if (c1 > ' ' && c1 != 0xa0)
12691 break;
12692 }
12693 else
12694 {
12695 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12696 if (c1 == PTR2CHAR(p))
12697 break;
12698 if (*p == NUL)
12699 break;
12700 }
12701 }
12702 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12703}
12704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012705#ifdef FEAT_FLOAT
12706/*
12707 * "trunc({float})" function
12708 */
12709 static void
12710f_trunc(typval_T *argvars, typval_T *rettv)
12711{
12712 float_T f = 0.0;
12713
12714 rettv->v_type = VAR_FLOAT;
12715 if (get_float_arg(argvars, &f) == OK)
12716 /* trunc() is not in C90, use floor() or ceil() instead. */
12717 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12718 else
12719 rettv->vval.v_float = 0.0;
12720}
12721#endif
12722
12723/*
12724 * "type(expr)" function
12725 */
12726 static void
12727f_type(typval_T *argvars, typval_T *rettv)
12728{
12729 int n = -1;
12730
12731 switch (argvars[0].v_type)
12732 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012733 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12734 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012735 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012736 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12737 case VAR_LIST: n = VAR_TYPE_LIST; break;
12738 case VAR_DICT: n = VAR_TYPE_DICT; break;
12739 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 case VAR_SPECIAL:
12741 if (argvars[0].vval.v_number == VVAL_FALSE
12742 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012743 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012744 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012745 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012747 case VAR_JOB: n = VAR_TYPE_JOB; break;
12748 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012749 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012751 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 n = -1;
12753 break;
12754 }
12755 rettv->vval.v_number = n;
12756}
12757
12758/*
12759 * "undofile(name)" function
12760 */
12761 static void
12762f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12763{
12764 rettv->v_type = VAR_STRING;
12765#ifdef FEAT_PERSISTENT_UNDO
12766 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012767 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768
12769 if (*fname == NUL)
12770 {
12771 /* If there is no file name there will be no undo file. */
12772 rettv->vval.v_string = NULL;
12773 }
12774 else
12775 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012776 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012777
12778 if (ffname != NULL)
12779 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12780 vim_free(ffname);
12781 }
12782 }
12783#else
12784 rettv->vval.v_string = NULL;
12785#endif
12786}
12787
12788/*
12789 * "undotree()" function
12790 */
12791 static void
12792f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12793{
12794 if (rettv_dict_alloc(rettv) == OK)
12795 {
12796 dict_T *dict = rettv->vval.v_dict;
12797 list_T *list;
12798
Bram Moolenaare0be1672018-07-08 16:50:37 +020012799 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12800 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12801 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12802 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12803 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12804 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012805
12806 list = list_alloc();
12807 if (list != NULL)
12808 {
12809 u_eval_tree(curbuf->b_u_oldhead, list);
12810 dict_add_list(dict, "entries", list);
12811 }
12812 }
12813}
12814
12815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816 * "virtcol(string)" function
12817 */
12818 static void
12819f_virtcol(typval_T *argvars, typval_T *rettv)
12820{
12821 colnr_T vcol = 0;
12822 pos_T *fp;
12823 int fnum = curbuf->b_fnum;
12824
12825 fp = var2fpos(&argvars[0], FALSE, &fnum);
12826 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12827 && fnum == curbuf->b_fnum)
12828 {
12829 getvvcol(curwin, fp, NULL, NULL, &vcol);
12830 ++vcol;
12831 }
12832
12833 rettv->vval.v_number = vcol;
12834}
12835
12836/*
12837 * "visualmode()" function
12838 */
12839 static void
12840f_visualmode(typval_T *argvars, typval_T *rettv)
12841{
12842 char_u str[2];
12843
12844 rettv->v_type = VAR_STRING;
12845 str[0] = curbuf->b_visual_mode_eval;
12846 str[1] = NUL;
12847 rettv->vval.v_string = vim_strsave(str);
12848
12849 /* A non-zero number or non-empty string argument: reset mode. */
12850 if (non_zero_arg(&argvars[0]))
12851 curbuf->b_visual_mode_eval = NUL;
12852}
12853
12854/*
12855 * "wildmenumode()" function
12856 */
12857 static void
12858f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12859{
12860#ifdef FEAT_WILDMENU
12861 if (wild_menu_showing)
12862 rettv->vval.v_number = 1;
12863#endif
12864}
12865
12866/*
12867 * "winbufnr(nr)" function
12868 */
12869 static void
12870f_winbufnr(typval_T *argvars, typval_T *rettv)
12871{
12872 win_T *wp;
12873
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012874 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012875 if (wp == NULL)
12876 rettv->vval.v_number = -1;
12877 else
12878 rettv->vval.v_number = wp->w_buffer->b_fnum;
12879}
12880
12881/*
12882 * "wincol()" function
12883 */
12884 static void
12885f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12886{
12887 validate_cursor();
12888 rettv->vval.v_number = curwin->w_wcol + 1;
12889}
12890
12891/*
12892 * "winheight(nr)" function
12893 */
12894 static void
12895f_winheight(typval_T *argvars, typval_T *rettv)
12896{
12897 win_T *wp;
12898
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012899 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 if (wp == NULL)
12901 rettv->vval.v_number = -1;
12902 else
12903 rettv->vval.v_number = wp->w_height;
12904}
12905
12906/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012907 * "winlayout()" function
12908 */
12909 static void
12910f_winlayout(typval_T *argvars, typval_T *rettv)
12911{
12912 tabpage_T *tp;
12913
12914 if (rettv_list_alloc(rettv) != OK)
12915 return;
12916
12917 if (argvars[0].v_type == VAR_UNKNOWN)
12918 tp = curtab;
12919 else
12920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012921 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012922 if (tp == NULL)
12923 return;
12924 }
12925
12926 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12927}
12928
12929/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012930 * "winline()" function
12931 */
12932 static void
12933f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12934{
12935 validate_cursor();
12936 rettv->vval.v_number = curwin->w_wrow + 1;
12937}
12938
12939/*
12940 * "winnr()" function
12941 */
12942 static void
12943f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12944{
12945 int nr = 1;
12946
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 rettv->vval.v_number = nr;
12949}
12950
12951/*
12952 * "winrestcmd()" function
12953 */
12954 static void
12955f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12956{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012957 win_T *wp;
12958 int winnr = 1;
12959 garray_T ga;
12960 char_u buf[50];
12961
12962 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012963 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964 {
12965 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12966 ga_concat(&ga, buf);
12967 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12968 ga_concat(&ga, buf);
12969 ++winnr;
12970 }
12971 ga_append(&ga, NUL);
12972
12973 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974 rettv->v_type = VAR_STRING;
12975}
12976
12977/*
12978 * "winrestview()" function
12979 */
12980 static void
12981f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12982{
12983 dict_T *dict;
12984
12985 if (argvars[0].v_type != VAR_DICT
12986 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012987 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988 else
12989 {
12990 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012991 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012993 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012994 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012995 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12997 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010012998 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999 curwin->w_set_curswant = FALSE;
13000 }
13001
13002 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013003 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004#ifdef FEAT_DIFF
13005 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013006 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007#endif
13008 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013009 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013011 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012
13013 check_cursor();
13014 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013015 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016 changed_window_setting();
13017
13018 if (curwin->w_topline <= 0)
13019 curwin->w_topline = 1;
13020 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13021 curwin->w_topline = curbuf->b_ml.ml_line_count;
13022#ifdef FEAT_DIFF
13023 check_topfill(curwin, TRUE);
13024#endif
13025 }
13026}
13027
13028/*
13029 * "winsaveview()" function
13030 */
13031 static void
13032f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13033{
13034 dict_T *dict;
13035
13036 if (rettv_dict_alloc(rettv) == FAIL)
13037 return;
13038 dict = rettv->vval.v_dict;
13039
Bram Moolenaare0be1672018-07-08 16:50:37 +020013040 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13041 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013042 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013044 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045
Bram Moolenaare0be1672018-07-08 16:50:37 +020013046 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013048 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013050 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13051 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013052}
13053
13054/*
13055 * "winwidth(nr)" function
13056 */
13057 static void
13058f_winwidth(typval_T *argvars, typval_T *rettv)
13059{
13060 win_T *wp;
13061
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013062 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063 if (wp == NULL)
13064 rettv->vval.v_number = -1;
13065 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013067}
13068
13069/*
13070 * "wordcount()" function
13071 */
13072 static void
13073f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13074{
13075 if (rettv_dict_alloc(rettv) == FAIL)
13076 return;
13077 cursor_pos_info(rettv->vval.v_dict);
13078}
13079
13080/*
13081 * "writefile()" function
13082 */
13083 static void
13084f_writefile(typval_T *argvars, typval_T *rettv)
13085{
13086 int binary = FALSE;
13087 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013088#ifdef HAVE_FSYNC
13089 int do_fsync = p_fs;
13090#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 char_u *fname;
13092 FILE *fd;
13093 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013094 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013095 list_T *list = NULL;
13096 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013097
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013098 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013099 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013100 return;
13101
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013102 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013104 list = argvars[0].vval.v_list;
13105 if (list == NULL)
13106 return;
13107 for (li = list->lv_first; li != NULL; li = li->li_next)
13108 if (tv_get_string_chk(&li->li_tv) == NULL)
13109 return;
13110 }
13111 else if (argvars[0].v_type == VAR_BLOB)
13112 {
13113 blob = argvars[0].vval.v_blob;
13114 if (blob == NULL)
13115 return;
13116 }
13117 else
13118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013119 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120 return;
13121 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122
13123 if (argvars[2].v_type != VAR_UNKNOWN)
13124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013125 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013126
13127 if (arg2 == NULL)
13128 return;
13129 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013130 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013131 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013133#ifdef HAVE_FSYNC
13134 if (vim_strchr(arg2, 's') != NULL)
13135 do_fsync = TRUE;
13136 else if (vim_strchr(arg2, 'S') != NULL)
13137 do_fsync = FALSE;
13138#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139 }
13140
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013141 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013142 if (fname == NULL)
13143 return;
13144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 /* Always open the file in binary mode, library functions have a mind of
13146 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13148 append ? APPENDBIN : WRITEBIN)) == NULL)
13149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013150 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 ret = -1;
13152 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013153 else if (blob)
13154 {
13155 if (write_blob(fd, blob) == FAIL)
13156 ret = -1;
13157#ifdef HAVE_FSYNC
13158 else if (do_fsync)
13159 // Ignore the error, the user wouldn't know what to do about it.
13160 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013161 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013162#endif
13163 fclose(fd);
13164 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165 else
13166 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013167 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013169#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013170 else if (do_fsync)
13171 /* Ignore the error, the user wouldn't know what to do about it.
13172 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013173 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013174#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013175 fclose(fd);
13176 }
13177
13178 rettv->vval.v_number = ret;
13179}
13180
13181/*
13182 * "xor(expr, expr)" function
13183 */
13184 static void
13185f_xor(typval_T *argvars, typval_T *rettv)
13186{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013187 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
13188 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013189}
13190
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191#endif /* FEAT_EVAL */