blob: 3fbeec26cfd5b89f43aa7e99894774e69e352191 [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);
77static void f_clearmatches(typval_T *argvars, typval_T *rettv);
78static void f_col(typval_T *argvars, typval_T *rettv);
79#if defined(FEAT_INS_EXPAND)
80static void f_complete(typval_T *argvars, typval_T *rettv);
81static void f_complete_add(typval_T *argvars, typval_T *rettv);
82static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010083static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020084#endif
85static void f_confirm(typval_T *argvars, typval_T *rettv);
86static void f_copy(typval_T *argvars, typval_T *rettv);
87#ifdef FEAT_FLOAT
88static void f_cos(typval_T *argvars, typval_T *rettv);
89static void f_cosh(typval_T *argvars, typval_T *rettv);
90#endif
91static void f_count(typval_T *argvars, typval_T *rettv);
92static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
93static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010094#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020095static void f_debugbreak(typval_T *argvars, typval_T *rettv);
96#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020097static void f_deepcopy(typval_T *argvars, typval_T *rettv);
98static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +020099static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_did_filetype(typval_T *argvars, typval_T *rettv);
101static void f_diff_filler(typval_T *argvars, typval_T *rettv);
102static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
103static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200104static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_escape(typval_T *argvars, typval_T *rettv);
106static void f_eval(typval_T *argvars, typval_T *rettv);
107static void f_eventhandler(typval_T *argvars, typval_T *rettv);
108static void f_executable(typval_T *argvars, typval_T *rettv);
109static void f_execute(typval_T *argvars, typval_T *rettv);
110static void f_exepath(typval_T *argvars, typval_T *rettv);
111static void f_exists(typval_T *argvars, typval_T *rettv);
112#ifdef FEAT_FLOAT
113static void f_exp(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200116static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200117static void f_extend(typval_T *argvars, typval_T *rettv);
118static void f_feedkeys(typval_T *argvars, typval_T *rettv);
119static void f_filereadable(typval_T *argvars, typval_T *rettv);
120static void f_filewritable(typval_T *argvars, typval_T *rettv);
121static void f_filter(typval_T *argvars, typval_T *rettv);
122static void f_finddir(typval_T *argvars, typval_T *rettv);
123static void f_findfile(typval_T *argvars, typval_T *rettv);
124#ifdef FEAT_FLOAT
125static void f_float2nr(typval_T *argvars, typval_T *rettv);
126static void f_floor(typval_T *argvars, typval_T *rettv);
127static void f_fmod(typval_T *argvars, typval_T *rettv);
128#endif
129static void f_fnameescape(typval_T *argvars, typval_T *rettv);
130static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
131static void f_foldclosed(typval_T *argvars, typval_T *rettv);
132static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
133static void f_foldlevel(typval_T *argvars, typval_T *rettv);
134static void f_foldtext(typval_T *argvars, typval_T *rettv);
135static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
136static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200137static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_function(typval_T *argvars, typval_T *rettv);
139static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
140static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200141static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142static void f_getbufline(typval_T *argvars, typval_T *rettv);
143static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100144static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200145static void f_getchar(typval_T *argvars, typval_T *rettv);
146static void f_getcharmod(typval_T *argvars, typval_T *rettv);
147static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
148static void f_getcmdline(typval_T *argvars, typval_T *rettv);
149#if defined(FEAT_CMDL_COMPL)
150static void f_getcompletion(typval_T *argvars, typval_T *rettv);
151#endif
152static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
153static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
154static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
155static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200156static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_getfontname(typval_T *argvars, typval_T *rettv);
158static void f_getfperm(typval_T *argvars, typval_T *rettv);
159static void f_getfsize(typval_T *argvars, typval_T *rettv);
160static void f_getftime(typval_T *argvars, typval_T *rettv);
161static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100162static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200164static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getpid(typval_T *argvars, typval_T *rettv);
166static void f_getcurpos(typval_T *argvars, typval_T *rettv);
167static void f_getpos(typval_T *argvars, typval_T *rettv);
168static void f_getqflist(typval_T *argvars, typval_T *rettv);
169static void f_getreg(typval_T *argvars, typval_T *rettv);
170static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_gettabvar(typval_T *argvars, typval_T *rettv);
173static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100174static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200175static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100176static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getwinposx(typval_T *argvars, typval_T *rettv);
178static void f_getwinposy(typval_T *argvars, typval_T *rettv);
179static void f_getwinvar(typval_T *argvars, typval_T *rettv);
180static void f_glob(typval_T *argvars, typval_T *rettv);
181static void f_globpath(typval_T *argvars, typval_T *rettv);
182static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
183static void f_has(typval_T *argvars, typval_T *rettv);
184static void f_has_key(typval_T *argvars, typval_T *rettv);
185static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
186static void f_hasmapto(typval_T *argvars, typval_T *rettv);
187static void f_histadd(typval_T *argvars, typval_T *rettv);
188static void f_histdel(typval_T *argvars, typval_T *rettv);
189static void f_histget(typval_T *argvars, typval_T *rettv);
190static void f_histnr(typval_T *argvars, typval_T *rettv);
191static void f_hlID(typval_T *argvars, typval_T *rettv);
192static void f_hlexists(typval_T *argvars, typval_T *rettv);
193static void f_hostname(typval_T *argvars, typval_T *rettv);
194static void f_iconv(typval_T *argvars, typval_T *rettv);
195static void f_indent(typval_T *argvars, typval_T *rettv);
196static void f_index(typval_T *argvars, typval_T *rettv);
197static void f_input(typval_T *argvars, typval_T *rettv);
198static void f_inputdialog(typval_T *argvars, typval_T *rettv);
199static void f_inputlist(typval_T *argvars, typval_T *rettv);
200static void f_inputrestore(typval_T *argvars, typval_T *rettv);
201static void f_inputsave(typval_T *argvars, typval_T *rettv);
202static void f_inputsecret(typval_T *argvars, typval_T *rettv);
203static void f_insert(typval_T *argvars, typval_T *rettv);
204static void f_invert(typval_T *argvars, typval_T *rettv);
205static void f_isdirectory(typval_T *argvars, typval_T *rettv);
206static void f_islocked(typval_T *argvars, typval_T *rettv);
207#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200208static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_isnan(typval_T *argvars, typval_T *rettv);
210#endif
211static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200212static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_keys(typval_T *argvars, typval_T *rettv);
214static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
215static void f_len(typval_T *argvars, typval_T *rettv);
216static void f_libcall(typval_T *argvars, typval_T *rettv);
217static void f_libcallnr(typval_T *argvars, typval_T *rettv);
218static void f_line(typval_T *argvars, typval_T *rettv);
219static void f_line2byte(typval_T *argvars, typval_T *rettv);
220static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200221static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_localtime(typval_T *argvars, typval_T *rettv);
223#ifdef FEAT_FLOAT
224static void f_log(typval_T *argvars, typval_T *rettv);
225static void f_log10(typval_T *argvars, typval_T *rettv);
226#endif
227#ifdef FEAT_LUA
228static void f_luaeval(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_map(typval_T *argvars, typval_T *rettv);
231static void f_maparg(typval_T *argvars, typval_T *rettv);
232static void f_mapcheck(typval_T *argvars, typval_T *rettv);
233static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_matchend(typval_T *argvars, typval_T *rettv);
235static void f_matchlist(typval_T *argvars, typval_T *rettv);
236static void f_matchstr(typval_T *argvars, typval_T *rettv);
237static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
238static void f_max(typval_T *argvars, typval_T *rettv);
239static void f_min(typval_T *argvars, typval_T *rettv);
240#ifdef vim_mkdir
241static void f_mkdir(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_mode(typval_T *argvars, typval_T *rettv);
244#ifdef FEAT_MZSCHEME
245static void f_mzeval(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
248static void f_nr2char(typval_T *argvars, typval_T *rettv);
249static void f_or(typval_T *argvars, typval_T *rettv);
250static void f_pathshorten(typval_T *argvars, typval_T *rettv);
251#ifdef FEAT_PERL
252static void f_perleval(typval_T *argvars, typval_T *rettv);
253#endif
254#ifdef FEAT_FLOAT
255static void f_pow(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
258static void f_printf(typval_T *argvars, typval_T *rettv);
259static void f_pumvisible(typval_T *argvars, typval_T *rettv);
260#ifdef FEAT_PYTHON3
261static void f_py3eval(typval_T *argvars, typval_T *rettv);
262#endif
263#ifdef FEAT_PYTHON
264static void f_pyeval(typval_T *argvars, typval_T *rettv);
265#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100266#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
267static void f_pyxeval(typval_T *argvars, typval_T *rettv);
268#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200269static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200270static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200272static void f_reg_executing(typval_T *argvars, typval_T *rettv);
273static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200274static void f_reltime(typval_T *argvars, typval_T *rettv);
275#ifdef FEAT_FLOAT
276static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_reltimestr(typval_T *argvars, typval_T *rettv);
279static void f_remote_expr(typval_T *argvars, typval_T *rettv);
280static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
281static void f_remote_peek(typval_T *argvars, typval_T *rettv);
282static void f_remote_read(typval_T *argvars, typval_T *rettv);
283static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100284static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200285static void f_remove(typval_T *argvars, typval_T *rettv);
286static void f_rename(typval_T *argvars, typval_T *rettv);
287static void f_repeat(typval_T *argvars, typval_T *rettv);
288static void f_resolve(typval_T *argvars, typval_T *rettv);
289static void f_reverse(typval_T *argvars, typval_T *rettv);
290#ifdef FEAT_FLOAT
291static void f_round(typval_T *argvars, typval_T *rettv);
292#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100293#ifdef FEAT_RUBY
294static void f_rubyeval(typval_T *argvars, typval_T *rettv);
295#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_screenattr(typval_T *argvars, typval_T *rettv);
297static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100298static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_screencol(typval_T *argvars, typval_T *rettv);
300static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100301static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200302static void f_search(typval_T *argvars, typval_T *rettv);
303static void f_searchdecl(typval_T *argvars, typval_T *rettv);
304static void f_searchpair(typval_T *argvars, typval_T *rettv);
305static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
306static void f_searchpos(typval_T *argvars, typval_T *rettv);
307static void f_server2client(typval_T *argvars, typval_T *rettv);
308static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200309static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200310static void f_setbufvar(typval_T *argvars, typval_T *rettv);
311static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
312static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200313static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200314static void f_setfperm(typval_T *argvars, typval_T *rettv);
315static void f_setline(typval_T *argvars, typval_T *rettv);
316static void f_setloclist(typval_T *argvars, typval_T *rettv);
317static void f_setmatches(typval_T *argvars, typval_T *rettv);
318static void f_setpos(typval_T *argvars, typval_T *rettv);
319static void f_setqflist(typval_T *argvars, typval_T *rettv);
320static void f_setreg(typval_T *argvars, typval_T *rettv);
321static void f_settabvar(typval_T *argvars, typval_T *rettv);
322static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100323static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200324static void f_setwinvar(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_CRYPT
326static void f_sha256(typval_T *argvars, typval_T *rettv);
327#endif /* FEAT_CRYPT */
328static void f_shellescape(typval_T *argvars, typval_T *rettv);
329static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
330static void f_simplify(typval_T *argvars, typval_T *rettv);
331#ifdef FEAT_FLOAT
332static void f_sin(typval_T *argvars, typval_T *rettv);
333static void f_sinh(typval_T *argvars, typval_T *rettv);
334#endif
335static void f_sort(typval_T *argvars, typval_T *rettv);
336static void f_soundfold(typval_T *argvars, typval_T *rettv);
337static void f_spellbadword(typval_T *argvars, typval_T *rettv);
338static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
339static void f_split(typval_T *argvars, typval_T *rettv);
340#ifdef FEAT_FLOAT
341static void f_sqrt(typval_T *argvars, typval_T *rettv);
342static void f_str2float(typval_T *argvars, typval_T *rettv);
343#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200344static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200345static void f_str2nr(typval_T *argvars, typval_T *rettv);
346static void f_strchars(typval_T *argvars, typval_T *rettv);
347#ifdef HAVE_STRFTIME
348static void f_strftime(typval_T *argvars, typval_T *rettv);
349#endif
350static void f_strgetchar(typval_T *argvars, typval_T *rettv);
351static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_strlen(typval_T *argvars, typval_T *rettv);
353static void f_strcharpart(typval_T *argvars, typval_T *rettv);
354static void f_strpart(typval_T *argvars, typval_T *rettv);
355static void f_strridx(typval_T *argvars, typval_T *rettv);
356static void f_strtrans(typval_T *argvars, typval_T *rettv);
357static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
358static void f_strwidth(typval_T *argvars, typval_T *rettv);
359static void f_submatch(typval_T *argvars, typval_T *rettv);
360static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200361static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200362static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200363static void f_synID(typval_T *argvars, typval_T *rettv);
364static void f_synIDattr(typval_T *argvars, typval_T *rettv);
365static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
366static void f_synstack(typval_T *argvars, typval_T *rettv);
367static void f_synconcealed(typval_T *argvars, typval_T *rettv);
368static void f_system(typval_T *argvars, typval_T *rettv);
369static void f_systemlist(typval_T *argvars, typval_T *rettv);
370static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
371static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
372static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
373static void f_taglist(typval_T *argvars, typval_T *rettv);
374static void f_tagfiles(typval_T *argvars, typval_T *rettv);
375static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376#ifdef FEAT_FLOAT
377static void f_tan(typval_T *argvars, typval_T *rettv);
378static void f_tanh(typval_T *argvars, typval_T *rettv);
379#endif
380#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200381static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200382static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200383static void f_timer_start(typval_T *argvars, typval_T *rettv);
384static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200385static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200386#endif
387static void f_tolower(typval_T *argvars, typval_T *rettv);
388static void f_toupper(typval_T *argvars, typval_T *rettv);
389static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100390static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391#ifdef FEAT_FLOAT
392static void f_trunc(typval_T *argvars, typval_T *rettv);
393#endif
394static void f_type(typval_T *argvars, typval_T *rettv);
395static void f_undofile(typval_T *argvars, typval_T *rettv);
396static void f_undotree(typval_T *argvars, typval_T *rettv);
397static void f_uniq(typval_T *argvars, typval_T *rettv);
398static void f_values(typval_T *argvars, typval_T *rettv);
399static void f_virtcol(typval_T *argvars, typval_T *rettv);
400static void f_visualmode(typval_T *argvars, typval_T *rettv);
401static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200402static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
404static void f_win_getid(typval_T *argvars, typval_T *rettv);
405static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
406static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
407static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100408static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200409static void f_winbufnr(typval_T *argvars, typval_T *rettv);
410static void f_wincol(typval_T *argvars, typval_T *rettv);
411static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200412static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_winline(typval_T *argvars, typval_T *rettv);
414static void f_winnr(typval_T *argvars, typval_T *rettv);
415static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
416static void f_winrestview(typval_T *argvars, typval_T *rettv);
417static void f_winsaveview(typval_T *argvars, typval_T *rettv);
418static void f_winwidth(typval_T *argvars, typval_T *rettv);
419static void f_writefile(typval_T *argvars, typval_T *rettv);
420static void f_wordcount(typval_T *argvars, typval_T *rettv);
421static void f_xor(typval_T *argvars, typval_T *rettv);
422
423/*
424 * Array with names and number of arguments of all internal functions
425 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
426 */
427static struct fst
428{
429 char *f_name; /* function name */
430 char f_min_argc; /* minimal number of arguments */
431 char f_max_argc; /* maximal number of arguments */
432 void (*f_func)(typval_T *args, typval_T *rvar);
433 /* implementation of function */
434} functions[] =
435{
436#ifdef FEAT_FLOAT
437 {"abs", 1, 1, f_abs},
438 {"acos", 1, 1, f_acos}, /* WJMc */
439#endif
440 {"add", 2, 2, f_add},
441 {"and", 2, 2, f_and},
442 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200443 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200444 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200445 {"argidx", 0, 0, f_argidx},
446 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200447 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200448#ifdef FEAT_FLOAT
449 {"asin", 1, 1, f_asin}, /* WJMc */
450#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100451 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100453 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200455 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200456 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100457 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200458 {"assert_match", 2, 3, f_assert_match},
459 {"assert_notequal", 2, 3, f_assert_notequal},
460 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100461 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462 {"assert_true", 1, 2, f_assert_true},
463#ifdef FEAT_FLOAT
464 {"atan", 1, 1, f_atan},
465 {"atan2", 2, 2, f_atan2},
466#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100467#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200468 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100469 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100470# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100471 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100472# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100473#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200474 {"browse", 4, 4, f_browse},
475 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200476 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477 {"bufexists", 1, 1, f_bufexists},
478 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
479 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
480 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
481 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200482 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200483 {"bufloaded", 1, 1, f_bufloaded},
484 {"bufname", 1, 1, f_bufname},
485 {"bufnr", 1, 2, f_bufnr},
486 {"bufwinid", 1, 1, f_bufwinid},
487 {"bufwinnr", 1, 1, f_bufwinnr},
488 {"byte2line", 1, 1, f_byte2line},
489 {"byteidx", 2, 2, f_byteidx},
490 {"byteidxcomp", 2, 2, f_byteidxcomp},
491 {"call", 2, 3, f_call},
492#ifdef FEAT_FLOAT
493 {"ceil", 1, 1, f_ceil},
494#endif
495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100496 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200498 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
500 {"ch_evalraw", 2, 3, f_ch_evalraw},
501 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
502 {"ch_getjob", 1, 1, f_ch_getjob},
503 {"ch_info", 1, 1, f_ch_info},
504 {"ch_log", 1, 2, f_ch_log},
505 {"ch_logfile", 1, 2, f_ch_logfile},
506 {"ch_open", 1, 2, f_ch_open},
507 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100508 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"ch_readraw", 1, 2, f_ch_readraw},
510 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
511 {"ch_sendraw", 2, 3, f_ch_sendraw},
512 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200513 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514#endif
515 {"changenr", 0, 0, f_changenr},
516 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200517 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100519 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200520 {"col", 1, 1, f_col},
521#if defined(FEAT_INS_EXPAND)
522 {"complete", 2, 2, f_complete},
523 {"complete_add", 1, 1, f_complete_add},
524 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100525 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526#endif
527 {"confirm", 1, 4, f_confirm},
528 {"copy", 1, 1, f_copy},
529#ifdef FEAT_FLOAT
530 {"cos", 1, 1, f_cos},
531 {"cosh", 1, 1, f_cosh},
532#endif
533 {"count", 2, 4, f_count},
534 {"cscope_connection",0,3, f_cscope_connection},
535 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100536#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200537 {"debugbreak", 1, 1, f_debugbreak},
538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"deepcopy", 1, 2, f_deepcopy},
540 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200541 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542 {"did_filetype", 0, 0, f_did_filetype},
543 {"diff_filler", 1, 1, f_diff_filler},
544 {"diff_hlID", 2, 2, f_diff_hlID},
545 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200546 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"escape", 2, 2, f_escape},
548 {"eval", 1, 1, f_eval},
549 {"eventhandler", 0, 0, f_eventhandler},
550 {"executable", 1, 1, f_executable},
551 {"execute", 1, 2, f_execute},
552 {"exepath", 1, 1, f_exepath},
553 {"exists", 1, 1, f_exists},
554#ifdef FEAT_FLOAT
555 {"exp", 1, 1, f_exp},
556#endif
557 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200558 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200559 {"extend", 2, 3, f_extend},
560 {"feedkeys", 1, 2, f_feedkeys},
561 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
562 {"filereadable", 1, 1, f_filereadable},
563 {"filewritable", 1, 1, f_filewritable},
564 {"filter", 2, 2, f_filter},
565 {"finddir", 1, 3, f_finddir},
566 {"findfile", 1, 3, f_findfile},
567#ifdef FEAT_FLOAT
568 {"float2nr", 1, 1, f_float2nr},
569 {"floor", 1, 1, f_floor},
570 {"fmod", 2, 2, f_fmod},
571#endif
572 {"fnameescape", 1, 1, f_fnameescape},
573 {"fnamemodify", 2, 2, f_fnamemodify},
574 {"foldclosed", 1, 1, f_foldclosed},
575 {"foldclosedend", 1, 1, f_foldclosedend},
576 {"foldlevel", 1, 1, f_foldlevel},
577 {"foldtext", 0, 0, f_foldtext},
578 {"foldtextresult", 1, 1, f_foldtextresult},
579 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200580 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581 {"function", 1, 3, f_function},
582 {"garbagecollect", 0, 1, f_garbagecollect},
583 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200584 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200585 {"getbufline", 2, 3, f_getbufline},
586 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100587 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200588 {"getchar", 0, 1, f_getchar},
589 {"getcharmod", 0, 0, f_getcharmod},
590 {"getcharsearch", 0, 0, f_getcharsearch},
591 {"getcmdline", 0, 0, f_getcmdline},
592 {"getcmdpos", 0, 0, f_getcmdpos},
593 {"getcmdtype", 0, 0, f_getcmdtype},
594 {"getcmdwintype", 0, 0, f_getcmdwintype},
595#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200596 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200597#endif
598 {"getcurpos", 0, 0, f_getcurpos},
599 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200600 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"getfontname", 0, 1, f_getfontname},
602 {"getfperm", 1, 1, f_getfperm},
603 {"getfsize", 1, 1, f_getfsize},
604 {"getftime", 1, 1, f_getftime},
605 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100606 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200608 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100609 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"getpid", 0, 0, f_getpid},
611 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200612 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613 {"getreg", 0, 3, f_getreg},
614 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200615 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"gettabvar", 2, 3, f_gettabvar},
617 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100618 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200619 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100620 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"getwinposx", 0, 0, f_getwinposx},
622 {"getwinposy", 0, 0, f_getwinposy},
623 {"getwinvar", 2, 3, f_getwinvar},
624 {"glob", 1, 4, f_glob},
625 {"glob2regpat", 1, 1, f_glob2regpat},
626 {"globpath", 2, 5, f_globpath},
627 {"has", 1, 1, f_has},
628 {"has_key", 2, 2, f_has_key},
629 {"haslocaldir", 0, 2, f_haslocaldir},
630 {"hasmapto", 1, 3, f_hasmapto},
631 {"highlightID", 1, 1, f_hlID}, /* obsolete */
632 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
633 {"histadd", 2, 2, f_histadd},
634 {"histdel", 1, 2, f_histdel},
635 {"histget", 1, 2, f_histget},
636 {"histnr", 1, 1, f_histnr},
637 {"hlID", 1, 1, f_hlID},
638 {"hlexists", 1, 1, f_hlexists},
639 {"hostname", 0, 0, f_hostname},
640 {"iconv", 3, 3, f_iconv},
641 {"indent", 1, 1, f_indent},
642 {"index", 2, 4, f_index},
643 {"input", 1, 3, f_input},
644 {"inputdialog", 1, 3, f_inputdialog},
645 {"inputlist", 1, 1, f_inputlist},
646 {"inputrestore", 0, 0, f_inputrestore},
647 {"inputsave", 0, 0, f_inputsave},
648 {"inputsecret", 1, 2, f_inputsecret},
649 {"insert", 2, 3, f_insert},
650 {"invert", 1, 1, f_invert},
651 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200652#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
653 {"isinf", 1, 1, f_isinf},
654#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200655 {"islocked", 1, 1, f_islocked},
656#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
657 {"isnan", 1, 1, f_isnan},
658#endif
659 {"items", 1, 1, f_items},
660#ifdef FEAT_JOB_CHANNEL
661 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200662 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663 {"job_setoptions", 2, 2, f_job_setoptions},
664 {"job_start", 1, 2, f_job_start},
665 {"job_status", 1, 1, f_job_status},
666 {"job_stop", 1, 2, f_job_stop},
667#endif
668 {"join", 1, 2, f_join},
669 {"js_decode", 1, 1, f_js_decode},
670 {"js_encode", 1, 1, f_js_encode},
671 {"json_decode", 1, 1, f_json_decode},
672 {"json_encode", 1, 1, f_json_encode},
673 {"keys", 1, 1, f_keys},
674 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
675 {"len", 1, 1, f_len},
676 {"libcall", 3, 3, f_libcall},
677 {"libcallnr", 3, 3, f_libcallnr},
678 {"line", 1, 1, f_line},
679 {"line2byte", 1, 1, f_line2byte},
680 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200681 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200682 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200683 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200684 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200685 {"localtime", 0, 0, f_localtime},
686#ifdef FEAT_FLOAT
687 {"log", 1, 1, f_log},
688 {"log10", 1, 1, f_log10},
689#endif
690#ifdef FEAT_LUA
691 {"luaeval", 1, 2, f_luaeval},
692#endif
693 {"map", 2, 2, f_map},
694 {"maparg", 1, 4, f_maparg},
695 {"mapcheck", 1, 3, f_mapcheck},
696 {"match", 2, 4, f_match},
697 {"matchadd", 2, 5, f_matchadd},
698 {"matchaddpos", 2, 5, f_matchaddpos},
699 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100700 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200701 {"matchend", 2, 4, f_matchend},
702 {"matchlist", 2, 4, f_matchlist},
703 {"matchstr", 2, 4, f_matchstr},
704 {"matchstrpos", 2, 4, f_matchstrpos},
705 {"max", 1, 1, f_max},
706 {"min", 1, 1, f_min},
707#ifdef vim_mkdir
708 {"mkdir", 1, 3, f_mkdir},
709#endif
710 {"mode", 0, 1, f_mode},
711#ifdef FEAT_MZSCHEME
712 {"mzeval", 1, 1, f_mzeval},
713#endif
714 {"nextnonblank", 1, 1, f_nextnonblank},
715 {"nr2char", 1, 2, f_nr2char},
716 {"or", 2, 2, f_or},
717 {"pathshorten", 1, 1, f_pathshorten},
718#ifdef FEAT_PERL
719 {"perleval", 1, 1, f_perleval},
720#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200721#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200722 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200723 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200724 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200725 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200726 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200727 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200728 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200729 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200730 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200731 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200732 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200733 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200734 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200735 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200736 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200737 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200738 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200739 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200740#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200741#ifdef FEAT_FLOAT
742 {"pow", 2, 2, f_pow},
743#endif
744 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100745 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200746#ifdef FEAT_JOB_CHANNEL
747 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200748 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200749 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
750#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100751#ifdef FEAT_TEXT_PROP
752 {"prop_add", 3, 3, f_prop_add},
753 {"prop_clear", 1, 3, f_prop_clear},
754 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100755 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100756 {"prop_type_add", 2, 2, f_prop_type_add},
757 {"prop_type_change", 2, 2, f_prop_type_change},
758 {"prop_type_delete", 1, 2, f_prop_type_delete},
759 {"prop_type_get", 1, 2, f_prop_type_get},
760 {"prop_type_list", 0, 1, f_prop_type_list},
761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762 {"pumvisible", 0, 0, f_pumvisible},
763#ifdef FEAT_PYTHON3
764 {"py3eval", 1, 1, f_py3eval},
765#endif
766#ifdef FEAT_PYTHON
767 {"pyeval", 1, 1, f_pyeval},
768#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100769#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
770 {"pyxeval", 1, 1, f_pyxeval},
771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200772 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200773 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200775 {"reg_executing", 0, 0, f_reg_executing},
776 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"reltime", 0, 2, f_reltime},
778#ifdef FEAT_FLOAT
779 {"reltimefloat", 1, 1, f_reltimefloat},
780#endif
781 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100782 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"remote_foreground", 1, 1, f_remote_foreground},
784 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100785 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100787 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"remove", 2, 3, f_remove},
789 {"rename", 2, 2, f_rename},
790 {"repeat", 2, 2, f_repeat},
791 {"resolve", 1, 1, f_resolve},
792 {"reverse", 1, 1, f_reverse},
793#ifdef FEAT_FLOAT
794 {"round", 1, 1, f_round},
795#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100796#ifdef FEAT_RUBY
797 {"rubyeval", 1, 1, f_rubyeval},
798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200799 {"screenattr", 2, 2, f_screenattr},
800 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100801 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200802 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200803 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100805 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200806 {"search", 1, 4, f_search},
807 {"searchdecl", 1, 3, f_searchdecl},
808 {"searchpair", 3, 7, f_searchpair},
809 {"searchpairpos", 3, 7, f_searchpairpos},
810 {"searchpos", 1, 4, f_searchpos},
811 {"server2client", 2, 2, f_server2client},
812 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200813 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200814 {"setbufvar", 3, 3, f_setbufvar},
815 {"setcharsearch", 1, 1, f_setcharsearch},
816 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200817 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setfperm", 2, 2, f_setfperm},
819 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200820 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100821 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200823 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824 {"setreg", 2, 3, f_setreg},
825 {"settabvar", 3, 3, f_settabvar},
826 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100827 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828 {"setwinvar", 3, 3, f_setwinvar},
829#ifdef FEAT_CRYPT
830 {"sha256", 1, 1, f_sha256},
831#endif
832 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100833 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100834#ifdef FEAT_SIGNS
835 {"sign_define", 1, 2, f_sign_define},
836 {"sign_getdefined", 0, 1, f_sign_getdefined},
837 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100838 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100839 {"sign_place", 4, 5, f_sign_place},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200840 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100841 {"sign_undefine", 0, 1, f_sign_undefine},
842 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200843 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100844#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845 {"simplify", 1, 1, f_simplify},
846#ifdef FEAT_FLOAT
847 {"sin", 1, 1, f_sin},
848 {"sinh", 1, 1, f_sinh},
849#endif
850 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200851#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200852 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200853 {"sound_playevent", 1, 2, f_sound_playevent},
854 {"sound_playfile", 1, 2, f_sound_playfile},
855 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200856#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"soundfold", 1, 1, f_soundfold},
858 {"spellbadword", 0, 1, f_spellbadword},
859 {"spellsuggest", 1, 3, f_spellsuggest},
860 {"split", 1, 3, f_split},
861#ifdef FEAT_FLOAT
862 {"sqrt", 1, 1, f_sqrt},
863 {"str2float", 1, 1, f_str2float},
864#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200865 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"str2nr", 1, 2, f_str2nr},
867 {"strcharpart", 2, 3, f_strcharpart},
868 {"strchars", 1, 2, f_strchars},
869 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
870#ifdef HAVE_STRFTIME
871 {"strftime", 1, 2, f_strftime},
872#endif
873 {"strgetchar", 2, 2, f_strgetchar},
874 {"stridx", 2, 3, f_stridx},
875 {"string", 1, 1, f_string},
876 {"strlen", 1, 1, f_strlen},
877 {"strpart", 2, 3, f_strpart},
878 {"strridx", 2, 3, f_strridx},
879 {"strtrans", 1, 1, f_strtrans},
880 {"strwidth", 1, 1, f_strwidth},
881 {"submatch", 1, 2, f_submatch},
882 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200883 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200884 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"synID", 3, 3, f_synID},
886 {"synIDattr", 2, 3, f_synIDattr},
887 {"synIDtrans", 1, 1, f_synIDtrans},
888 {"synconcealed", 2, 2, f_synconcealed},
889 {"synstack", 2, 2, f_synstack},
890 {"system", 1, 2, f_system},
891 {"systemlist", 1, 2, f_systemlist},
892 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
893 {"tabpagenr", 0, 1, f_tabpagenr},
894 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
895 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100896 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897#ifdef FEAT_FLOAT
898 {"tan", 1, 1, f_tan},
899 {"tanh", 1, 1, f_tanh},
900#endif
901 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100903 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
904 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100905 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200906 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200907# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
908 {"term_getansicolors", 1, 1, f_term_getansicolors},
909# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200911 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200912 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200913 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200914 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200916 {"term_getstatus", 1, 1, f_term_getstatus},
917 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200918 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200919 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200920 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200921 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200922# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
923 {"term_setansicolors", 2, 2, f_term_setansicolors},
924# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100925 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100926 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200927 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200929 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200930#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
932 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200933 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200935 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200936 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100937 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100938 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200939#ifdef FEAT_JOB_CHANNEL
940 {"test_null_channel", 0, 0, f_test_null_channel},
941#endif
942 {"test_null_dict", 0, 0, f_test_null_dict},
943#ifdef FEAT_JOB_CHANNEL
944 {"test_null_job", 0, 0, f_test_null_job},
945#endif
946 {"test_null_list", 0, 0, f_test_null_list},
947 {"test_null_partial", 0, 0, f_test_null_partial},
948 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200949 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100950 {"test_override", 2, 2, f_test_override},
951 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200952#ifdef FEAT_GUI
953 {"test_scrollbar", 3, 3, f_test_scrollbar},
954#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200955#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200956 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200957#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200958 {"test_settime", 1, 1, f_test_settime},
959#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200960 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200961 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200962 {"timer_start", 2, 3, f_timer_start},
963 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200964 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965#endif
966 {"tolower", 1, 1, f_tolower},
967 {"toupper", 1, 1, f_toupper},
968 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100969 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200970#ifdef FEAT_FLOAT
971 {"trunc", 1, 1, f_trunc},
972#endif
973 {"type", 1, 1, f_type},
974 {"undofile", 1, 1, f_undofile},
975 {"undotree", 0, 0, f_undotree},
976 {"uniq", 1, 3, f_uniq},
977 {"values", 1, 1, f_values},
978 {"virtcol", 1, 1, f_virtcol},
979 {"visualmode", 0, 1, f_visualmode},
980 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200981 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982 {"win_findbuf", 1, 1, f_win_findbuf},
983 {"win_getid", 0, 2, f_win_getid},
984 {"win_gotoid", 1, 1, f_win_gotoid},
985 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
986 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100987 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200988 {"winbufnr", 1, 1, f_winbufnr},
989 {"wincol", 0, 0, f_wincol},
990 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200991 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200992 {"winline", 0, 0, f_winline},
993 {"winnr", 0, 1, f_winnr},
994 {"winrestcmd", 0, 0, f_winrestcmd},
995 {"winrestview", 1, 1, f_winrestview},
996 {"winsaveview", 0, 0, f_winsaveview},
997 {"winwidth", 1, 1, f_winwidth},
998 {"wordcount", 0, 0, f_wordcount},
999 {"writefile", 2, 3, f_writefile},
1000 {"xor", 2, 2, f_xor},
1001};
1002
1003#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1004
1005/*
1006 * Function given to ExpandGeneric() to obtain the list of internal
1007 * or user defined function names.
1008 */
1009 char_u *
1010get_function_name(expand_T *xp, int idx)
1011{
1012 static int intidx = -1;
1013 char_u *name;
1014
1015 if (idx == 0)
1016 intidx = -1;
1017 if (intidx < 0)
1018 {
1019 name = get_user_func_name(xp, idx);
1020 if (name != NULL)
1021 return name;
1022 }
1023 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1024 {
1025 STRCPY(IObuff, functions[intidx].f_name);
1026 STRCAT(IObuff, "(");
1027 if (functions[intidx].f_max_argc == 0)
1028 STRCAT(IObuff, ")");
1029 return IObuff;
1030 }
1031
1032 return NULL;
1033}
1034
1035/*
1036 * Function given to ExpandGeneric() to obtain the list of internal or
1037 * user defined variable or function names.
1038 */
1039 char_u *
1040get_expr_name(expand_T *xp, int idx)
1041{
1042 static int intidx = -1;
1043 char_u *name;
1044
1045 if (idx == 0)
1046 intidx = -1;
1047 if (intidx < 0)
1048 {
1049 name = get_function_name(xp, idx);
1050 if (name != NULL)
1051 return name;
1052 }
1053 return get_user_var_name(xp, ++intidx);
1054}
1055
1056#endif /* FEAT_CMDL_COMPL */
1057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058/*
1059 * Find internal function in table above.
1060 * Return index, or -1 if not found
1061 */
1062 int
1063find_internal_func(
1064 char_u *name) /* name of the function */
1065{
1066 int first = 0;
1067 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1068 int cmp;
1069 int x;
1070
1071 /*
1072 * Find the function name in the table. Binary search.
1073 */
1074 while (first <= last)
1075 {
1076 x = first + ((unsigned)(last - first) >> 1);
1077 cmp = STRCMP(name, functions[x].f_name);
1078 if (cmp < 0)
1079 last = x - 1;
1080 else if (cmp > 0)
1081 first = x + 1;
1082 else
1083 return x;
1084 }
1085 return -1;
1086}
1087
1088 int
1089call_internal_func(
1090 char_u *name,
1091 int argcount,
1092 typval_T *argvars,
1093 typval_T *rettv)
1094{
1095 int i;
1096
1097 i = find_internal_func(name);
1098 if (i < 0)
1099 return ERROR_UNKNOWN;
1100 if (argcount < functions[i].f_min_argc)
1101 return ERROR_TOOFEW;
1102 if (argcount > functions[i].f_max_argc)
1103 return ERROR_TOOMANY;
1104 argvars[argcount].v_type = VAR_UNKNOWN;
1105 functions[i].f_func(argvars, rettv);
1106 return ERROR_NONE;
1107}
1108
1109/*
1110 * Return TRUE for a non-zero Number and a non-empty String.
1111 */
1112 static int
1113non_zero_arg(typval_T *argvars)
1114{
1115 return ((argvars[0].v_type == VAR_NUMBER
1116 && argvars[0].vval.v_number != 0)
1117 || (argvars[0].v_type == VAR_SPECIAL
1118 && argvars[0].vval.v_number == VVAL_TRUE)
1119 || (argvars[0].v_type == VAR_STRING
1120 && argvars[0].vval.v_string != NULL
1121 && *argvars[0].vval.v_string != NUL));
1122}
1123
1124/*
1125 * Get the lnum from the first argument.
1126 * Also accepts ".", "$", etc., but that only works for the current buffer.
1127 * Returns -1 on error.
1128 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001129 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001130tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001131{
1132 typval_T rettv;
1133 linenr_T lnum;
1134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001135 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136 if (lnum == 0) /* no valid number, try using line() */
1137 {
1138 rettv.v_type = VAR_NUMBER;
1139 f_line(argvars, &rettv);
1140 lnum = (linenr_T)rettv.vval.v_number;
1141 clear_tv(&rettv);
1142 }
1143 return lnum;
1144}
1145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146/*
1147 * Get the lnum from the first argument.
1148 * Also accepts "$", then "buf" is used.
1149 * Returns 0 on error.
1150 */
1151 static linenr_T
1152tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1153{
1154 if (argvars[0].v_type == VAR_STRING
1155 && argvars[0].vval.v_string != NULL
1156 && argvars[0].vval.v_string[0] == '$'
1157 && buf != NULL)
1158 return buf->b_ml.ml_line_count;
1159 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1160}
1161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001162#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001163/*
1164 * Get the float value of "argvars[0]" into "f".
1165 * Returns FAIL when the argument is not a Number or Float.
1166 */
1167 static int
1168get_float_arg(typval_T *argvars, float_T *f)
1169{
1170 if (argvars[0].v_type == VAR_FLOAT)
1171 {
1172 *f = argvars[0].vval.v_float;
1173 return OK;
1174 }
1175 if (argvars[0].v_type == VAR_NUMBER)
1176 {
1177 *f = (float_T)argvars[0].vval.v_number;
1178 return OK;
1179 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001180 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001181 return FAIL;
1182}
1183
1184/*
1185 * "abs(expr)" function
1186 */
1187 static void
1188f_abs(typval_T *argvars, typval_T *rettv)
1189{
1190 if (argvars[0].v_type == VAR_FLOAT)
1191 {
1192 rettv->v_type = VAR_FLOAT;
1193 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1194 }
1195 else
1196 {
1197 varnumber_T n;
1198 int error = FALSE;
1199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201 if (error)
1202 rettv->vval.v_number = -1;
1203 else if (n > 0)
1204 rettv->vval.v_number = n;
1205 else
1206 rettv->vval.v_number = -n;
1207 }
1208}
1209
1210/*
1211 * "acos()" function
1212 */
1213 static void
1214f_acos(typval_T *argvars, typval_T *rettv)
1215{
1216 float_T f = 0.0;
1217
1218 rettv->v_type = VAR_FLOAT;
1219 if (get_float_arg(argvars, &f) == OK)
1220 rettv->vval.v_float = acos(f);
1221 else
1222 rettv->vval.v_float = 0.0;
1223}
1224#endif
1225
1226/*
1227 * "add(list, item)" function
1228 */
1229 static void
1230f_add(typval_T *argvars, typval_T *rettv)
1231{
1232 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001233 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001234
1235 rettv->vval.v_number = 1; /* Default: Failed */
1236 if (argvars[0].v_type == VAR_LIST)
1237 {
1238 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001239 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001240 (char_u *)N_("add() argument"), TRUE)
1241 && list_append_tv(l, &argvars[1]) == OK)
1242 copy_tv(&argvars[0], rettv);
1243 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001244 else if (argvars[0].v_type == VAR_BLOB)
1245 {
1246 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001247 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001248 (char_u *)N_("add() argument"), TRUE))
1249 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001250 int error = FALSE;
1251 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1252
1253 if (!error)
1254 {
1255 ga_append(&b->bv_ga, (int)n);
1256 copy_tv(&argvars[0], rettv);
1257 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001258 }
1259 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001261 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262}
1263
1264/*
1265 * "and(expr, expr)" function
1266 */
1267 static void
1268f_and(typval_T *argvars, typval_T *rettv)
1269{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001270 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1271 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001272}
1273
1274/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001275 * If there is a window for "curbuf", make it the current window.
1276 */
1277 static void
1278find_win_for_curbuf(void)
1279{
1280 wininfo_T *wip;
1281
1282 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1283 {
1284 if (wip->wi_win != NULL)
1285 {
1286 curwin = wip->wi_win;
1287 break;
1288 }
1289 }
1290}
1291
1292/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001293 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001294 */
1295 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001296set_buffer_lines(
1297 buf_T *buf,
1298 linenr_T lnum_arg,
1299 int append,
1300 typval_T *lines,
1301 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001302{
Bram Moolenaarca851592018-06-06 21:04:07 +02001303 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1304 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 list_T *l = NULL;
1306 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001307 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001308 linenr_T append_lnum;
1309 buf_T *curbuf_save = NULL;
1310 win_T *curwin_save = NULL;
1311 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312
Bram Moolenaarca851592018-06-06 21:04:07 +02001313 /* When using the current buffer ml_mfp will be set if needed. Useful when
1314 * setline() is used on startup. For other buffers the buffer must be
1315 * loaded. */
1316 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001318 rettv->vval.v_number = 1; /* FAIL */
1319 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320 }
1321
Bram Moolenaarca851592018-06-06 21:04:07 +02001322 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001324 curbuf_save = curbuf;
1325 curwin_save = curwin;
1326 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001327 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001328 }
1329
1330 if (append)
1331 // appendbufline() uses the line number below which we insert
1332 append_lnum = lnum - 1;
1333 else
1334 // setbufline() uses the line number above which we insert, we only
1335 // append if it's below the last line
1336 append_lnum = curbuf->b_ml.ml_line_count;
1337
1338 if (lines->v_type == VAR_LIST)
1339 {
1340 l = lines->vval.v_list;
1341 li = l->lv_first;
1342 }
1343 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001344 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001345
1346 /* default result is zero == OK */
1347 for (;;)
1348 {
1349 if (l != NULL)
1350 {
1351 /* list argument, get next string */
1352 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001354 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 li = li->li_next;
1356 }
1357
Bram Moolenaarca851592018-06-06 21:04:07 +02001358 rettv->vval.v_number = 1; /* FAIL */
1359 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1360 break;
1361
1362 /* When coming here from Insert mode, sync undo, so that this can be
1363 * undone separately from what was previously inserted. */
1364 if (u_sync_once == 2)
1365 {
1366 u_sync_once = 1; /* notify that u_sync() was called */
1367 u_sync(TRUE);
1368 }
1369
1370 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1371 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001372 // Existing line, replace it.
1373 // Removes any existing text properties.
1374 if (u_savesub(lnum) == OK && ml_replace_len(
1375 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 {
1377 changed_bytes(lnum, 0);
1378 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1379 check_cursor_col();
1380 rettv->vval.v_number = 0; /* OK */
1381 }
1382 }
1383 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1384 {
1385 /* append the line */
1386 ++added;
1387 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1388 rettv->vval.v_number = 0; /* OK */
1389 }
1390
1391 if (l == NULL) /* only one string argument */
1392 break;
1393 ++lnum;
1394 }
1395
1396 if (added > 0)
1397 {
1398 win_T *wp;
1399 tabpage_T *tp;
1400
1401 appended_lines_mark(append_lnum, added);
1402 FOR_ALL_TAB_WINDOWS(tp, wp)
1403 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1404 wp->w_cursor.lnum += added;
1405 check_cursor_col();
1406
Bram Moolenaarf2732452018-06-03 14:47:35 +02001407#ifdef FEAT_JOB_CHANNEL
1408 if (bt_prompt(curbuf) && (State & INSERT))
1409 // show the line with the prompt
1410 update_topline();
1411#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001412 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001413
1414 if (!is_curbuf)
1415 {
1416 curbuf = curbuf_save;
1417 curwin = curwin_save;
1418 }
1419}
1420
1421/*
1422 * "append(lnum, string/list)" function
1423 */
1424 static void
1425f_append(typval_T *argvars, typval_T *rettv)
1426{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001427 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001428
1429 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1430}
1431
1432/*
1433 * "appendbufline(buf, lnum, string/list)" function
1434 */
1435 static void
1436f_appendbufline(typval_T *argvars, typval_T *rettv)
1437{
1438 linenr_T lnum;
1439 buf_T *buf;
1440
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001441 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001442 if (buf == NULL)
1443 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001444 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001445 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001446 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001447 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1448 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001449}
1450
1451/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001452 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001453 */
1454 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001455f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001457 win_T *wp;
1458
1459 if (argvars[0].v_type == VAR_UNKNOWN)
1460 // use the current window
1461 rettv->vval.v_number = ARGCOUNT;
1462 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001463 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001464 // use the global argument list
1465 rettv->vval.v_number = GARGCOUNT;
1466 else
1467 {
1468 // use the argument list of the specified window
1469 wp = find_win_by_nr_or_id(&argvars[0]);
1470 if (wp != NULL)
1471 rettv->vval.v_number = WARGCOUNT(wp);
1472 else
1473 rettv->vval.v_number = -1;
1474 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001475}
1476
1477/*
1478 * "argidx()" function
1479 */
1480 static void
1481f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1482{
1483 rettv->vval.v_number = curwin->w_arg_idx;
1484}
1485
1486/*
1487 * "arglistid()" function
1488 */
1489 static void
1490f_arglistid(typval_T *argvars, typval_T *rettv)
1491{
1492 win_T *wp;
1493
1494 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001495 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001496 if (wp != NULL)
1497 rettv->vval.v_number = wp->w_alist->id;
1498}
1499
1500/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501 * Get the argument list for a given window
1502 */
1503 static void
1504get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1505{
1506 int idx;
1507
1508 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1509 for (idx = 0; idx < argcount; ++idx)
1510 list_append_string(rettv->vval.v_list,
1511 alist_name(&arglist[idx]), -1);
1512}
1513
1514/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001515 * "argv(nr)" function
1516 */
1517 static void
1518f_argv(typval_T *argvars, typval_T *rettv)
1519{
1520 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001521 aentry_T *arglist = NULL;
1522 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523
1524 if (argvars[0].v_type != VAR_UNKNOWN)
1525 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001526 if (argvars[1].v_type == VAR_UNKNOWN)
1527 {
1528 arglist = ARGLIST;
1529 argcount = ARGCOUNT;
1530 }
1531 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001532 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001533 {
1534 arglist = GARGLIST;
1535 argcount = GARGCOUNT;
1536 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 {
1539 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1540
1541 if (wp != NULL)
1542 {
1543 /* Use the argument list of the specified window */
1544 arglist = WARGLIST(wp);
1545 argcount = WARGCOUNT(wp);
1546 }
1547 }
1548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001551 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001552 if (arglist != NULL && idx >= 0 && idx < argcount)
1553 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1554 else if (idx == -1)
1555 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001557 else
1558 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001559}
1560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561#ifdef FEAT_FLOAT
1562/*
1563 * "asin()" function
1564 */
1565 static void
1566f_asin(typval_T *argvars, typval_T *rettv)
1567{
1568 float_T f = 0.0;
1569
1570 rettv->v_type = VAR_FLOAT;
1571 if (get_float_arg(argvars, &f) == OK)
1572 rettv->vval.v_float = asin(f);
1573 else
1574 rettv->vval.v_float = 0.0;
1575}
1576
1577/*
1578 * "atan()" function
1579 */
1580 static void
1581f_atan(typval_T *argvars, typval_T *rettv)
1582{
1583 float_T f = 0.0;
1584
1585 rettv->v_type = VAR_FLOAT;
1586 if (get_float_arg(argvars, &f) == OK)
1587 rettv->vval.v_float = atan(f);
1588 else
1589 rettv->vval.v_float = 0.0;
1590}
1591
1592/*
1593 * "atan2()" function
1594 */
1595 static void
1596f_atan2(typval_T *argvars, typval_T *rettv)
1597{
1598 float_T fx = 0.0, fy = 0.0;
1599
1600 rettv->v_type = VAR_FLOAT;
1601 if (get_float_arg(argvars, &fx) == OK
1602 && get_float_arg(&argvars[1], &fy) == OK)
1603 rettv->vval.v_float = atan2(fx, fy);
1604 else
1605 rettv->vval.v_float = 0.0;
1606}
1607#endif
1608
1609/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001610 * "balloon_show()" function
1611 */
1612#ifdef FEAT_BEVAL
1613 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001614f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1615{
1616 rettv->v_type = VAR_STRING;
1617 if (balloonEval != NULL)
1618 {
1619 if (balloonEval->msg == NULL)
1620 rettv->vval.v_string = NULL;
1621 else
1622 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1623 }
1624}
1625
1626 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001627f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1628{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001629 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001630 {
1631 if (argvars[0].v_type == VAR_LIST
1632# ifdef FEAT_GUI
1633 && !gui.in_use
1634# endif
1635 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001636 {
1637 list_T *l = argvars[0].vval.v_list;
1638
1639 // empty list removes the balloon
1640 post_balloon(balloonEval, NULL,
1641 l == NULL || l->lv_len == 0 ? NULL : l);
1642 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001643 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001644 {
1645 char_u *mesg = tv_get_string_chk(&argvars[0]);
1646
1647 if (mesg != NULL)
1648 // empty string removes the balloon
1649 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1650 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001651 }
1652}
1653
Bram Moolenaar669a8282017-11-19 20:13:05 +01001654# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001655 static void
1656f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1657{
1658 if (rettv_list_alloc(rettv) == OK)
1659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001660 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001661
1662 if (msg != NULL)
1663 {
1664 pumitem_T *array;
1665 int size = split_message(msg, &array);
1666 int i;
1667
1668 /* Skip the first and last item, they are always empty. */
1669 for (i = 1; i < size - 1; ++i)
1670 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001671 while (size > 0)
1672 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001673 vim_free(array);
1674 }
1675 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001676}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001677# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001678#endif
1679
1680/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681 * "browse(save, title, initdir, default)" function
1682 */
1683 static void
1684f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1685{
1686#ifdef FEAT_BROWSE
1687 int save;
1688 char_u *title;
1689 char_u *initdir;
1690 char_u *defname;
1691 char_u buf[NUMBUFLEN];
1692 char_u buf2[NUMBUFLEN];
1693 int error = FALSE;
1694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001695 save = (int)tv_get_number_chk(&argvars[0], &error);
1696 title = tv_get_string_chk(&argvars[1]);
1697 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1698 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699
1700 if (error || title == NULL || initdir == NULL || defname == NULL)
1701 rettv->vval.v_string = NULL;
1702 else
1703 rettv->vval.v_string =
1704 do_browse(save ? BROWSE_SAVE : 0,
1705 title, defname, NULL, initdir, NULL, curbuf);
1706#else
1707 rettv->vval.v_string = NULL;
1708#endif
1709 rettv->v_type = VAR_STRING;
1710}
1711
1712/*
1713 * "browsedir(title, initdir)" function
1714 */
1715 static void
1716f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1717{
1718#ifdef FEAT_BROWSE
1719 char_u *title;
1720 char_u *initdir;
1721 char_u buf[NUMBUFLEN];
1722
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001723 title = tv_get_string_chk(&argvars[0]);
1724 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725
1726 if (title == NULL || initdir == NULL)
1727 rettv->vval.v_string = NULL;
1728 else
1729 rettv->vval.v_string = do_browse(BROWSE_DIR,
1730 title, NULL, NULL, initdir, NULL, curbuf);
1731#else
1732 rettv->vval.v_string = NULL;
1733#endif
1734 rettv->v_type = VAR_STRING;
1735}
1736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737/*
1738 * Find a buffer by number or exact name.
1739 */
1740 static buf_T *
1741find_buffer(typval_T *avar)
1742{
1743 buf_T *buf = NULL;
1744
1745 if (avar->v_type == VAR_NUMBER)
1746 buf = buflist_findnr((int)avar->vval.v_number);
1747 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1748 {
1749 buf = buflist_findname_exp(avar->vval.v_string);
1750 if (buf == NULL)
1751 {
1752 /* No full path name match, try a match with a URL or a "nofile"
1753 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001754 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001755 if (buf->b_fname != NULL
1756 && (path_with_url(buf->b_fname)
1757#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001758 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001759#endif
1760 )
1761 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1762 break;
1763 }
1764 }
1765 return buf;
1766}
1767
1768/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001769 * "bufadd(expr)" function
1770 */
1771 static void
1772f_bufadd(typval_T *argvars, typval_T *rettv)
1773{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001774 char_u *name = tv_get_string(&argvars[0]);
1775
1776 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001777}
1778
1779/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780 * "bufexists(expr)" function
1781 */
1782 static void
1783f_bufexists(typval_T *argvars, typval_T *rettv)
1784{
1785 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1786}
1787
1788/*
1789 * "buflisted(expr)" function
1790 */
1791 static void
1792f_buflisted(typval_T *argvars, typval_T *rettv)
1793{
1794 buf_T *buf;
1795
1796 buf = find_buffer(&argvars[0]);
1797 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1798}
1799
1800/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001801 * "bufload(expr)" function
1802 */
1803 static void
1804f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1805{
1806 buf_T *buf = get_buf_arg(&argvars[0]);
1807
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001808 if (buf != NULL)
1809 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001810}
1811
1812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001813 * "bufloaded(expr)" function
1814 */
1815 static void
1816f_bufloaded(typval_T *argvars, typval_T *rettv)
1817{
1818 buf_T *buf;
1819
1820 buf = find_buffer(&argvars[0]);
1821 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1822}
1823
1824 buf_T *
1825buflist_find_by_name(char_u *name, int curtab_only)
1826{
1827 int save_magic;
1828 char_u *save_cpo;
1829 buf_T *buf;
1830
1831 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1832 save_magic = p_magic;
1833 p_magic = TRUE;
1834 save_cpo = p_cpo;
1835 p_cpo = (char_u *)"";
1836
1837 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1838 TRUE, FALSE, curtab_only));
1839
1840 p_magic = save_magic;
1841 p_cpo = save_cpo;
1842 return buf;
1843}
1844
1845/*
1846 * Get buffer by number or pattern.
1847 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001848 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001849tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001850{
1851 char_u *name = tv->vval.v_string;
1852 buf_T *buf;
1853
1854 if (tv->v_type == VAR_NUMBER)
1855 return buflist_findnr((int)tv->vval.v_number);
1856 if (tv->v_type != VAR_STRING)
1857 return NULL;
1858 if (name == NULL || *name == NUL)
1859 return curbuf;
1860 if (name[0] == '$' && name[1] == NUL)
1861 return lastbuf;
1862
1863 buf = buflist_find_by_name(name, curtab_only);
1864
1865 /* If not found, try expanding the name, like done for bufexists(). */
1866 if (buf == NULL)
1867 buf = find_buffer(tv);
1868
1869 return buf;
1870}
1871
1872/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001873 * Get the buffer from "arg" and give an error and return NULL if it is not
1874 * valid.
1875 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001876 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001877get_buf_arg(typval_T *arg)
1878{
1879 buf_T *buf;
1880
1881 ++emsg_off;
1882 buf = tv_get_buf(arg, FALSE);
1883 --emsg_off;
1884 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001886 return buf;
1887}
1888
1889/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890 * "bufname(expr)" function
1891 */
1892 static void
1893f_bufname(typval_T *argvars, typval_T *rettv)
1894{
1895 buf_T *buf;
1896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001897 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001899 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 rettv->v_type = VAR_STRING;
1901 if (buf != NULL && buf->b_fname != NULL)
1902 rettv->vval.v_string = vim_strsave(buf->b_fname);
1903 else
1904 rettv->vval.v_string = NULL;
1905 --emsg_off;
1906}
1907
1908/*
1909 * "bufnr(expr)" function
1910 */
1911 static void
1912f_bufnr(typval_T *argvars, typval_T *rettv)
1913{
1914 buf_T *buf;
1915 int error = FALSE;
1916 char_u *name;
1917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001918 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001920 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 --emsg_off;
1922
1923 /* If the buffer isn't found and the second argument is not zero create a
1924 * new buffer. */
1925 if (buf == NULL
1926 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001927 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001929 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 && !error)
1931 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1932
1933 if (buf != NULL)
1934 rettv->vval.v_number = buf->b_fnum;
1935 else
1936 rettv->vval.v_number = -1;
1937}
1938
1939 static void
1940buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1941{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001942 win_T *wp;
1943 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001944 buf_T *buf;
1945
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001946 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001948 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001949 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950 {
1951 ++winnr;
1952 if (wp->w_buffer == buf)
1953 break;
1954 }
1955 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001956 --emsg_off;
1957}
1958
1959/*
1960 * "bufwinid(nr)" function
1961 */
1962 static void
1963f_bufwinid(typval_T *argvars, typval_T *rettv)
1964{
1965 buf_win_common(argvars, rettv, FALSE);
1966}
1967
1968/*
1969 * "bufwinnr(nr)" function
1970 */
1971 static void
1972f_bufwinnr(typval_T *argvars, typval_T *rettv)
1973{
1974 buf_win_common(argvars, rettv, TRUE);
1975}
1976
1977/*
1978 * "byte2line(byte)" function
1979 */
1980 static void
1981f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1982{
1983#ifndef FEAT_BYTEOFF
1984 rettv->vval.v_number = -1;
1985#else
1986 long boff = 0;
1987
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001988 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 if (boff < 0)
1990 rettv->vval.v_number = -1;
1991 else
1992 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1993 (linenr_T)0, &boff);
1994#endif
1995}
1996
1997 static void
1998byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1999{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002001 char_u *str;
2002 varnumber_T idx;
2003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002004 str = tv_get_string_chk(&argvars[0]);
2005 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002006 rettv->vval.v_number = -1;
2007 if (str == NULL || idx < 0)
2008 return;
2009
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002010 t = str;
2011 for ( ; idx > 0; idx--)
2012 {
2013 if (*t == NUL) /* EOL reached */
2014 return;
2015 if (enc_utf8 && comp)
2016 t += utf_ptr2len(t);
2017 else
2018 t += (*mb_ptr2len)(t);
2019 }
2020 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021}
2022
2023/*
2024 * "byteidx()" function
2025 */
2026 static void
2027f_byteidx(typval_T *argvars, typval_T *rettv)
2028{
2029 byteidx(argvars, rettv, FALSE);
2030}
2031
2032/*
2033 * "byteidxcomp()" function
2034 */
2035 static void
2036f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2037{
2038 byteidx(argvars, rettv, TRUE);
2039}
2040
2041/*
2042 * "call(func, arglist [, dict])" function
2043 */
2044 static void
2045f_call(typval_T *argvars, typval_T *rettv)
2046{
2047 char_u *func;
2048 partial_T *partial = NULL;
2049 dict_T *selfdict = NULL;
2050
2051 if (argvars[1].v_type != VAR_LIST)
2052 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 return;
2055 }
2056 if (argvars[1].vval.v_list == NULL)
2057 return;
2058
2059 if (argvars[0].v_type == VAR_FUNC)
2060 func = argvars[0].vval.v_string;
2061 else if (argvars[0].v_type == VAR_PARTIAL)
2062 {
2063 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002064 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 }
2066 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002067 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002068 if (*func == NUL)
2069 return; /* type error or empty name */
2070
2071 if (argvars[2].v_type != VAR_UNKNOWN)
2072 {
2073 if (argvars[2].v_type != VAR_DICT)
2074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002076 return;
2077 }
2078 selfdict = argvars[2].vval.v_dict;
2079 }
2080
2081 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2082}
2083
2084#ifdef FEAT_FLOAT
2085/*
2086 * "ceil({float})" function
2087 */
2088 static void
2089f_ceil(typval_T *argvars, typval_T *rettv)
2090{
2091 float_T f = 0.0;
2092
2093 rettv->v_type = VAR_FLOAT;
2094 if (get_float_arg(argvars, &f) == OK)
2095 rettv->vval.v_float = ceil(f);
2096 else
2097 rettv->vval.v_float = 0.0;
2098}
2099#endif
2100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101/*
2102 * "changenr()" function
2103 */
2104 static void
2105f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2106{
2107 rettv->vval.v_number = curbuf->b_u_seq_cur;
2108}
2109
2110/*
2111 * "char2nr(string)" function
2112 */
2113 static void
2114f_char2nr(typval_T *argvars, typval_T *rettv)
2115{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002116 if (has_mbyte)
2117 {
2118 int utf8 = 0;
2119
2120 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002121 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002122
2123 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002124 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002126 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002127 }
2128 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002129 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130}
2131
2132/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002133 * "chdir(dir)" function
2134 */
2135 static void
2136f_chdir(typval_T *argvars, typval_T *rettv)
2137{
2138 char_u *cwd;
2139 cdscope_T scope = CDSCOPE_GLOBAL;
2140
2141 rettv->v_type = VAR_STRING;
2142 rettv->vval.v_string = NULL;
2143
2144 if (argvars[0].v_type != VAR_STRING)
2145 return;
2146
2147 // Return the current directory
2148 cwd = alloc(MAXPATHL);
2149 if (cwd != NULL)
2150 {
2151 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2152 {
2153#ifdef BACKSLASH_IN_FILENAME
2154 slash_adjust(cwd);
2155#endif
2156 rettv->vval.v_string = vim_strsave(cwd);
2157 }
2158 vim_free(cwd);
2159 }
2160
2161 if (curwin->w_localdir != NULL)
2162 scope = CDSCOPE_WINDOW;
2163 else if (curtab->tp_localdir != NULL)
2164 scope = CDSCOPE_TABPAGE;
2165
2166 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2167 // Directory change failed
2168 VIM_CLEAR(rettv->vval.v_string);
2169}
2170
2171/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172 * "cindent(lnum)" function
2173 */
2174 static void
2175f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2176{
2177#ifdef FEAT_CINDENT
2178 pos_T pos;
2179 linenr_T lnum;
2180
2181 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002182 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2184 {
2185 curwin->w_cursor.lnum = lnum;
2186 rettv->vval.v_number = get_c_indent();
2187 curwin->w_cursor = pos;
2188 }
2189 else
2190#endif
2191 rettv->vval.v_number = -1;
2192}
2193
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002194 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002195get_optional_window(typval_T *argvars, int idx)
2196{
2197 win_T *win = curwin;
2198
2199 if (argvars[idx].v_type != VAR_UNKNOWN)
2200 {
2201 win = find_win_by_nr_or_id(&argvars[idx]);
2202 if (win == NULL)
2203 {
2204 emsg(_(e_invalwindow));
2205 return NULL;
2206 }
2207 }
2208 return win;
2209}
2210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211/*
2212 * "clearmatches()" function
2213 */
2214 static void
2215f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2216{
2217#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002218 win_T *win = get_optional_window(argvars, 0);
2219
2220 if (win != NULL)
2221 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222#endif
2223}
2224
2225/*
2226 * "col(string)" function
2227 */
2228 static void
2229f_col(typval_T *argvars, typval_T *rettv)
2230{
2231 colnr_T col = 0;
2232 pos_T *fp;
2233 int fnum = curbuf->b_fnum;
2234
2235 fp = var2fpos(&argvars[0], FALSE, &fnum);
2236 if (fp != NULL && fnum == curbuf->b_fnum)
2237 {
2238 if (fp->col == MAXCOL)
2239 {
2240 /* '> can be MAXCOL, get the length of the line then */
2241 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2242 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2243 else
2244 col = MAXCOL;
2245 }
2246 else
2247 {
2248 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002249 /* col(".") when the cursor is on the NUL at the end of the line
2250 * because of "coladd" can be seen as an extra column. */
2251 if (virtual_active() && fp == &curwin->w_cursor)
2252 {
2253 char_u *p = ml_get_cursor();
2254
2255 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2256 curwin->w_virtcol - curwin->w_cursor.coladd))
2257 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 int l;
2259
2260 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2261 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002262 }
2263 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 }
2265 }
2266 rettv->vval.v_number = col;
2267}
2268
2269#if defined(FEAT_INS_EXPAND)
2270/*
2271 * "complete()" function
2272 */
2273 static void
2274f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2275{
2276 int startcol;
2277
2278 if ((State & INSERT) == 0)
2279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002280 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 return;
2282 }
2283
2284 /* Check for undo allowed here, because if something was already inserted
2285 * the line was already saved for undo and this check isn't done. */
2286 if (!undo_allowed())
2287 return;
2288
2289 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002291 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002292 return;
2293 }
2294
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002295 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 if (startcol <= 0)
2297 return;
2298
2299 set_completion(startcol - 1, argvars[1].vval.v_list);
2300}
2301
2302/*
2303 * "complete_add()" function
2304 */
2305 static void
2306f_complete_add(typval_T *argvars, typval_T *rettv)
2307{
2308 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2309}
2310
2311/*
2312 * "complete_check()" function
2313 */
2314 static void
2315f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2316{
2317 int saved = RedrawingDisabled;
2318
2319 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002320 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002321 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002322 RedrawingDisabled = saved;
2323}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002324
2325/*
2326 * "complete_info()" function
2327 */
2328 static void
2329f_complete_info(typval_T *argvars, typval_T *rettv)
2330{
2331 list_T *what_list = NULL;
2332
2333 if (rettv_dict_alloc(rettv) != OK)
2334 return;
2335
2336 if (argvars[0].v_type != VAR_UNKNOWN)
2337 {
2338 if (argvars[0].v_type != VAR_LIST)
2339 {
2340 emsg(_(e_listreq));
2341 return;
2342 }
2343 what_list = argvars[0].vval.v_list;
2344 }
2345 get_complete_info(what_list, rettv->vval.v_dict);
2346}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002347#endif
2348
2349/*
2350 * "confirm(message, buttons[, default [, type]])" function
2351 */
2352 static void
2353f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2354{
2355#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2356 char_u *message;
2357 char_u *buttons = NULL;
2358 char_u buf[NUMBUFLEN];
2359 char_u buf2[NUMBUFLEN];
2360 int def = 1;
2361 int type = VIM_GENERIC;
2362 char_u *typestr;
2363 int error = FALSE;
2364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002365 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002366 if (message == NULL)
2367 error = TRUE;
2368 if (argvars[1].v_type != VAR_UNKNOWN)
2369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002370 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (buttons == NULL)
2372 error = TRUE;
2373 if (argvars[2].v_type != VAR_UNKNOWN)
2374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002375 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 if (argvars[3].v_type != VAR_UNKNOWN)
2377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002378 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 if (typestr == NULL)
2380 error = TRUE;
2381 else
2382 {
2383 switch (TOUPPER_ASC(*typestr))
2384 {
2385 case 'E': type = VIM_ERROR; break;
2386 case 'Q': type = VIM_QUESTION; break;
2387 case 'I': type = VIM_INFO; break;
2388 case 'W': type = VIM_WARNING; break;
2389 case 'G': type = VIM_GENERIC; break;
2390 }
2391 }
2392 }
2393 }
2394 }
2395
2396 if (buttons == NULL || *buttons == NUL)
2397 buttons = (char_u *)_("&Ok");
2398
2399 if (!error)
2400 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2401 def, NULL, FALSE);
2402#endif
2403}
2404
2405/*
2406 * "copy()" function
2407 */
2408 static void
2409f_copy(typval_T *argvars, typval_T *rettv)
2410{
2411 item_copy(&argvars[0], rettv, FALSE, 0);
2412}
2413
2414#ifdef FEAT_FLOAT
2415/*
2416 * "cos()" function
2417 */
2418 static void
2419f_cos(typval_T *argvars, typval_T *rettv)
2420{
2421 float_T f = 0.0;
2422
2423 rettv->v_type = VAR_FLOAT;
2424 if (get_float_arg(argvars, &f) == OK)
2425 rettv->vval.v_float = cos(f);
2426 else
2427 rettv->vval.v_float = 0.0;
2428}
2429
2430/*
2431 * "cosh()" function
2432 */
2433 static void
2434f_cosh(typval_T *argvars, typval_T *rettv)
2435{
2436 float_T f = 0.0;
2437
2438 rettv->v_type = VAR_FLOAT;
2439 if (get_float_arg(argvars, &f) == OK)
2440 rettv->vval.v_float = cosh(f);
2441 else
2442 rettv->vval.v_float = 0.0;
2443}
2444#endif
2445
2446/*
2447 * "count()" function
2448 */
2449 static void
2450f_count(typval_T *argvars, typval_T *rettv)
2451{
2452 long n = 0;
2453 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002454 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455
Bram Moolenaar9966b212017-07-28 16:46:57 +02002456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002457 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002458
2459 if (argvars[0].v_type == VAR_STRING)
2460 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002461 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002462 char_u *p = argvars[0].vval.v_string;
2463 char_u *next;
2464
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002465 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002466 {
2467 if (ic)
2468 {
2469 size_t len = STRLEN(expr);
2470
2471 while (*p != NUL)
2472 {
2473 if (MB_STRNICMP(p, expr, len) == 0)
2474 {
2475 ++n;
2476 p += len;
2477 }
2478 else
2479 MB_PTR_ADV(p);
2480 }
2481 }
2482 else
2483 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2484 != NULL)
2485 {
2486 ++n;
2487 p = next + STRLEN(expr);
2488 }
2489 }
2490
2491 }
2492 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493 {
2494 listitem_T *li;
2495 list_T *l;
2496 long idx;
2497
2498 if ((l = argvars[0].vval.v_list) != NULL)
2499 {
2500 li = l->lv_first;
2501 if (argvars[2].v_type != VAR_UNKNOWN)
2502 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002503 if (argvars[3].v_type != VAR_UNKNOWN)
2504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002505 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002506 if (!error)
2507 {
2508 li = list_find(l, idx);
2509 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002510 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511 }
2512 }
2513 if (error)
2514 li = NULL;
2515 }
2516
2517 for ( ; li != NULL; li = li->li_next)
2518 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2519 ++n;
2520 }
2521 }
2522 else if (argvars[0].v_type == VAR_DICT)
2523 {
2524 int todo;
2525 dict_T *d;
2526 hashitem_T *hi;
2527
2528 if ((d = argvars[0].vval.v_dict) != NULL)
2529 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 if (argvars[2].v_type != VAR_UNKNOWN)
2531 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 }
2535
2536 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2537 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2538 {
2539 if (!HASHITEM_EMPTY(hi))
2540 {
2541 --todo;
2542 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2543 ++n;
2544 }
2545 }
2546 }
2547 }
2548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002549 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 rettv->vval.v_number = n;
2551}
2552
2553/*
2554 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2555 *
2556 * Checks the existence of a cscope connection.
2557 */
2558 static void
2559f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2560{
2561#ifdef FEAT_CSCOPE
2562 int num = 0;
2563 char_u *dbpath = NULL;
2564 char_u *prepend = NULL;
2565 char_u buf[NUMBUFLEN];
2566
2567 if (argvars[0].v_type != VAR_UNKNOWN
2568 && argvars[1].v_type != VAR_UNKNOWN)
2569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002570 num = (int)tv_get_number(&argvars[0]);
2571 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002573 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 }
2575
2576 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2577#endif
2578}
2579
2580/*
2581 * "cursor(lnum, col)" function, or
2582 * "cursor(list)"
2583 *
2584 * Moves the cursor to the specified line and column.
2585 * Returns 0 when the position could be set, -1 otherwise.
2586 */
2587 static void
2588f_cursor(typval_T *argvars, typval_T *rettv)
2589{
2590 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 int set_curswant = TRUE;
2593
2594 rettv->vval.v_number = -1;
2595 if (argvars[1].v_type == VAR_UNKNOWN)
2596 {
2597 pos_T pos;
2598 colnr_T curswant = -1;
2599
2600 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 return;
2604 }
2605 line = pos.lnum;
2606 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608 if (curswant >= 0)
2609 {
2610 curwin->w_curswant = curswant - 1;
2611 set_curswant = FALSE;
2612 }
2613 }
2614 else
2615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002616 line = tv_get_lnum(argvars);
2617 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002619 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002621 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002622 return; /* type error; errmsg already given */
2623 if (line > 0)
2624 curwin->w_cursor.lnum = line;
2625 if (col > 0)
2626 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628
2629 /* Make sure the cursor is in a valid position. */
2630 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 /* Correct cursor for multi-byte character. */
2632 if (has_mbyte)
2633 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634
2635 curwin->w_set_curswant = set_curswant;
2636 rettv->vval.v_number = 0;
2637}
2638
Bram Moolenaar4f974752019-02-17 17:44:42 +01002639#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002640/*
2641 * "debugbreak()" function
2642 */
2643 static void
2644f_debugbreak(typval_T *argvars, typval_T *rettv)
2645{
2646 int pid;
2647
2648 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002649 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002650 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002652 else
2653 {
2654 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2655
2656 if (hProcess != NULL)
2657 {
2658 DebugBreakProcess(hProcess);
2659 CloseHandle(hProcess);
2660 rettv->vval.v_number = OK;
2661 }
2662 }
2663}
2664#endif
2665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666/*
2667 * "deepcopy()" function
2668 */
2669 static void
2670f_deepcopy(typval_T *argvars, typval_T *rettv)
2671{
2672 int noref = 0;
2673 int copyID;
2674
2675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 else
2680 {
2681 copyID = get_copyID();
2682 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2683 }
2684}
2685
2686/*
2687 * "delete()" function
2688 */
2689 static void
2690f_delete(typval_T *argvars, typval_T *rettv)
2691{
2692 char_u nbuf[NUMBUFLEN];
2693 char_u *name;
2694 char_u *flags;
2695
2696 rettv->vval.v_number = -1;
2697 if (check_restricted() || check_secure())
2698 return;
2699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002700 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 if (name == NULL || *name == NUL)
2702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002703 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704 return;
2705 }
2706
2707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002708 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 else
2710 flags = (char_u *)"";
2711
2712 if (*flags == NUL)
2713 /* delete a file */
2714 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2715 else if (STRCMP(flags, "d") == 0)
2716 /* delete an empty directory */
2717 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2718 else if (STRCMP(flags, "rf") == 0)
2719 /* delete a directory recursively */
2720 rettv->vval.v_number = delete_recursive(name);
2721 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002722 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723}
2724
2725/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002726 * "deletebufline()" function
2727 */
2728 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002729f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002730{
2731 buf_T *buf;
2732 linenr_T first, last;
2733 linenr_T lnum;
2734 long count;
2735 int is_curbuf;
2736 buf_T *curbuf_save = NULL;
2737 win_T *curwin_save = NULL;
2738 tabpage_T *tp;
2739 win_T *wp;
2740
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002741 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002742 if (buf == NULL)
2743 {
2744 rettv->vval.v_number = 1; /* FAIL */
2745 return;
2746 }
2747 is_curbuf = buf == curbuf;
2748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002749 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002750 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002751 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002752 else
2753 last = first;
2754
2755 if (buf->b_ml.ml_mfp == NULL || first < 1
2756 || first > buf->b_ml.ml_line_count || last < first)
2757 {
2758 rettv->vval.v_number = 1; /* FAIL */
2759 return;
2760 }
2761
2762 if (!is_curbuf)
2763 {
2764 curbuf_save = curbuf;
2765 curwin_save = curwin;
2766 curbuf = buf;
2767 find_win_for_curbuf();
2768 }
2769 if (last > curbuf->b_ml.ml_line_count)
2770 last = curbuf->b_ml.ml_line_count;
2771 count = last - first + 1;
2772
2773 // When coming here from Insert mode, sync undo, so that this can be
2774 // undone separately from what was previously inserted.
2775 if (u_sync_once == 2)
2776 {
2777 u_sync_once = 1; // notify that u_sync() was called
2778 u_sync(TRUE);
2779 }
2780
2781 if (u_save(first - 1, last + 1) == FAIL)
2782 {
2783 rettv->vval.v_number = 1; /* FAIL */
2784 return;
2785 }
2786
2787 for (lnum = first; lnum <= last; ++lnum)
2788 ml_delete(first, TRUE);
2789
2790 FOR_ALL_TAB_WINDOWS(tp, wp)
2791 if (wp->w_buffer == buf)
2792 {
2793 if (wp->w_cursor.lnum > last)
2794 wp->w_cursor.lnum -= count;
2795 else if (wp->w_cursor.lnum> first)
2796 wp->w_cursor.lnum = first;
2797 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2798 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2799 }
2800 check_cursor_col();
2801 deleted_lines_mark(first, count);
2802
2803 if (!is_curbuf)
2804 {
2805 curbuf = curbuf_save;
2806 curwin = curwin_save;
2807 }
2808}
2809
2810/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 * "did_filetype()" function
2812 */
2813 static void
2814f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2815{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817}
2818
2819/*
2820 * "diff_filler()" function
2821 */
2822 static void
2823f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2824{
2825#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002826 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827#endif
2828}
2829
2830/*
2831 * "diff_hlID()" function
2832 */
2833 static void
2834f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2835{
2836#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002837 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002839 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 static int fnum = 0;
2841 static int change_start = 0;
2842 static int change_end = 0;
2843 static hlf_T hlID = (hlf_T)0;
2844 int filler_lines;
2845 int col;
2846
2847 if (lnum < 0) /* ignore type error in {lnum} arg */
2848 lnum = 0;
2849 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002850 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 || fnum != curbuf->b_fnum)
2852 {
2853 /* New line, buffer, change: need to get the values. */
2854 filler_lines = diff_check(curwin, lnum);
2855 if (filler_lines < 0)
2856 {
2857 if (filler_lines == -1)
2858 {
2859 change_start = MAXCOL;
2860 change_end = -1;
2861 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2862 hlID = HLF_ADD; /* added line */
2863 else
2864 hlID = HLF_CHD; /* changed line */
2865 }
2866 else
2867 hlID = HLF_ADD; /* added line */
2868 }
2869 else
2870 hlID = (hlf_T)0;
2871 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002872 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002873 fnum = curbuf->b_fnum;
2874 }
2875
2876 if (hlID == HLF_CHD || hlID == HLF_TXD)
2877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002878 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 if (col >= change_start && col <= change_end)
2880 hlID = HLF_TXD; /* changed text */
2881 else
2882 hlID = HLF_CHD; /* changed line */
2883 }
2884 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2885#endif
2886}
2887
2888/*
2889 * "empty({expr})" function
2890 */
2891 static void
2892f_empty(typval_T *argvars, typval_T *rettv)
2893{
2894 int n = FALSE;
2895
2896 switch (argvars[0].v_type)
2897 {
2898 case VAR_STRING:
2899 case VAR_FUNC:
2900 n = argvars[0].vval.v_string == NULL
2901 || *argvars[0].vval.v_string == NUL;
2902 break;
2903 case VAR_PARTIAL:
2904 n = FALSE;
2905 break;
2906 case VAR_NUMBER:
2907 n = argvars[0].vval.v_number == 0;
2908 break;
2909 case VAR_FLOAT:
2910#ifdef FEAT_FLOAT
2911 n = argvars[0].vval.v_float == 0.0;
2912 break;
2913#endif
2914 case VAR_LIST:
2915 n = argvars[0].vval.v_list == NULL
2916 || argvars[0].vval.v_list->lv_first == NULL;
2917 break;
2918 case VAR_DICT:
2919 n = argvars[0].vval.v_dict == NULL
2920 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2921 break;
2922 case VAR_SPECIAL:
2923 n = argvars[0].vval.v_number != VVAL_TRUE;
2924 break;
2925
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002926 case VAR_BLOB:
2927 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002928 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2929 break;
2930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 case VAR_JOB:
2932#ifdef FEAT_JOB_CHANNEL
2933 n = argvars[0].vval.v_job == NULL
2934 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2935 break;
2936#endif
2937 case VAR_CHANNEL:
2938#ifdef FEAT_JOB_CHANNEL
2939 n = argvars[0].vval.v_channel == NULL
2940 || !channel_is_open(argvars[0].vval.v_channel);
2941 break;
2942#endif
2943 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002944 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 n = TRUE;
2946 break;
2947 }
2948
2949 rettv->vval.v_number = n;
2950}
2951
2952/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002953 * "environ()" function
2954 */
2955 static void
2956f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2957{
2958#if !defined(AMIGA)
2959 int i = 0;
2960 char_u *entry, *value;
2961# ifdef MSWIN
2962 extern wchar_t **_wenviron;
2963# else
2964 extern char **environ;
2965# endif
2966
2967 if (rettv_dict_alloc(rettv) != OK)
2968 return;
2969
2970# ifdef MSWIN
2971 if (*_wenviron == NULL)
2972 return;
2973# else
2974 if (*environ == NULL)
2975 return;
2976# endif
2977
2978 for (i = 0; ; ++i)
2979 {
2980# ifdef MSWIN
2981 short_u *p;
2982
2983 if ((p = (short_u *)_wenviron[i]) == NULL)
2984 return;
2985 entry = utf16_to_enc(p, NULL);
2986# else
2987 if ((entry = (char_u *)environ[i]) == NULL)
2988 return;
2989 entry = vim_strsave(entry);
2990# endif
2991 if (entry == NULL) // out of memory
2992 return;
2993 if ((value = vim_strchr(entry, '=')) == NULL)
2994 {
2995 vim_free(entry);
2996 continue;
2997 }
2998 *value++ = NUL;
2999 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3000 vim_free(entry);
3001 }
3002#endif
3003}
3004
3005/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 * "escape({string}, {chars})" function
3007 */
3008 static void
3009f_escape(typval_T *argvars, typval_T *rettv)
3010{
3011 char_u buf[NUMBUFLEN];
3012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003013 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3014 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 rettv->v_type = VAR_STRING;
3016}
3017
3018/*
3019 * "eval()" function
3020 */
3021 static void
3022f_eval(typval_T *argvars, typval_T *rettv)
3023{
3024 char_u *s, *p;
3025
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003026 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 if (s != NULL)
3028 s = skipwhite(s);
3029
3030 p = s;
3031 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3032 {
3033 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003034 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 need_clr_eos = FALSE;
3036 rettv->v_type = VAR_NUMBER;
3037 rettv->vval.v_number = 0;
3038 }
3039 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003040 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041}
3042
3043/*
3044 * "eventhandler()" function
3045 */
3046 static void
3047f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3048{
3049 rettv->vval.v_number = vgetc_busy;
3050}
3051
3052/*
3053 * "executable()" function
3054 */
3055 static void
3056f_executable(typval_T *argvars, typval_T *rettv)
3057{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003058 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059
3060 /* Check in $PATH and also check directly if there is a directory name. */
3061 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3062 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3063}
3064
3065static garray_T redir_execute_ga;
3066
3067/*
3068 * Append "value[value_len]" to the execute() output.
3069 */
3070 void
3071execute_redir_str(char_u *value, int value_len)
3072{
3073 int len;
3074
3075 if (value_len == -1)
3076 len = (int)STRLEN(value); /* Append the entire string */
3077 else
3078 len = value_len; /* Append only "value_len" characters */
3079 if (ga_grow(&redir_execute_ga, len) == OK)
3080 {
3081 mch_memmove((char *)redir_execute_ga.ga_data
3082 + redir_execute_ga.ga_len, value, len);
3083 redir_execute_ga.ga_len += len;
3084 }
3085}
3086
3087/*
3088 * Get next line from a list.
3089 * Called by do_cmdline() to get the next line.
3090 * Returns allocated string, or NULL for end of function.
3091 */
3092
3093 static char_u *
3094get_list_line(
3095 int c UNUSED,
3096 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003097 int indent UNUSED,
3098 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099{
3100 listitem_T **p = (listitem_T **)cookie;
3101 listitem_T *item = *p;
3102 char_u buf[NUMBUFLEN];
3103 char_u *s;
3104
3105 if (item == NULL)
3106 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003107 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 *p = item->li_next;
3109 return s == NULL ? NULL : vim_strsave(s);
3110}
3111
3112/*
3113 * "execute()" function
3114 */
3115 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003116execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117{
3118 char_u *cmd = NULL;
3119 list_T *list = NULL;
3120 int save_msg_silent = msg_silent;
3121 int save_emsg_silent = emsg_silent;
3122 int save_emsg_noredir = emsg_noredir;
3123 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003124 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003126 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003127 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128
3129 rettv->vval.v_string = NULL;
3130 rettv->v_type = VAR_STRING;
3131
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003132 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003134 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 if (list == NULL || list->lv_first == NULL)
3136 /* empty list, no commands, empty output */
3137 return;
3138 ++list->lv_refcount;
3139 }
3140 else
3141 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003142 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 if (cmd == NULL)
3144 return;
3145 }
3146
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003147 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148 {
3149 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003150 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003151
3152 if (s == NULL)
3153 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003154 if (*s == NUL)
3155 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 if (STRNCMP(s, "silent", 6) == 0)
3157 ++msg_silent;
3158 if (STRCMP(s, "silent!") == 0)
3159 {
3160 emsg_silent = TRUE;
3161 emsg_noredir = TRUE;
3162 }
3163 }
3164 else
3165 ++msg_silent;
3166
3167 if (redir_execute)
3168 save_ga = redir_execute_ga;
3169 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3170 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003171 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003172 if (!echo_output)
3173 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174
3175 if (cmd != NULL)
3176 do_cmdline_cmd(cmd);
3177 else
3178 {
3179 listitem_T *item = list->lv_first;
3180
3181 do_cmdline(NULL, get_list_line, (void *)&item,
3182 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3183 --list->lv_refcount;
3184 }
3185
Bram Moolenaard297f352017-01-29 20:31:21 +01003186 /* Need to append a NUL to the result. */
3187 if (ga_grow(&redir_execute_ga, 1) == OK)
3188 {
3189 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3190 rettv->vval.v_string = redir_execute_ga.ga_data;
3191 }
3192 else
3193 {
3194 ga_clear(&redir_execute_ga);
3195 rettv->vval.v_string = NULL;
3196 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 msg_silent = save_msg_silent;
3198 emsg_silent = save_emsg_silent;
3199 emsg_noredir = save_emsg_noredir;
3200
3201 redir_execute = save_redir_execute;
3202 if (redir_execute)
3203 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003204 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003206 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003207 if (echo_output)
3208 // When not working silently: put it in column zero. A following
3209 // "echon" will overwrite the message, unavoidably.
3210 msg_col = 0;
3211 else
3212 // When working silently: Put it back where it was, since nothing
3213 // should have been written.
3214 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215}
3216
3217/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003218 * "execute()" function
3219 */
3220 static void
3221f_execute(typval_T *argvars, typval_T *rettv)
3222{
3223 execute_common(argvars, rettv, 0);
3224}
3225
3226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003227 * "exepath()" function
3228 */
3229 static void
3230f_exepath(typval_T *argvars, typval_T *rettv)
3231{
3232 char_u *p = NULL;
3233
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003234 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 rettv->v_type = VAR_STRING;
3236 rettv->vval.v_string = p;
3237}
3238
3239/*
3240 * "exists()" function
3241 */
3242 static void
3243f_exists(typval_T *argvars, typval_T *rettv)
3244{
3245 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003248 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 if (*p == '$') /* environment variable */
3250 {
3251 /* first try "normal" environment variables (fast) */
3252 if (mch_getenv(p + 1) != NULL)
3253 n = TRUE;
3254 else
3255 {
3256 /* try expanding things like $VIM and ${HOME} */
3257 p = expand_env_save(p);
3258 if (p != NULL && *p != '$')
3259 n = TRUE;
3260 vim_free(p);
3261 }
3262 }
3263 else if (*p == '&' || *p == '+') /* option */
3264 {
3265 n = (get_option_tv(&p, NULL, TRUE) == OK);
3266 if (*skipwhite(p) != NUL)
3267 n = FALSE; /* trailing garbage */
3268 }
3269 else if (*p == '*') /* internal or user defined function */
3270 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003271 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 }
3273 else if (*p == ':')
3274 {
3275 n = cmd_exists(p + 1);
3276 }
3277 else if (*p == '#')
3278 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 if (p[1] == '#')
3280 n = autocmd_supported(p + 2);
3281 else
3282 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 }
3284 else /* internal variable */
3285 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003286 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 }
3288
3289 rettv->vval.v_number = n;
3290}
3291
3292#ifdef FEAT_FLOAT
3293/*
3294 * "exp()" function
3295 */
3296 static void
3297f_exp(typval_T *argvars, typval_T *rettv)
3298{
3299 float_T f = 0.0;
3300
3301 rettv->v_type = VAR_FLOAT;
3302 if (get_float_arg(argvars, &f) == OK)
3303 rettv->vval.v_float = exp(f);
3304 else
3305 rettv->vval.v_float = 0.0;
3306}
3307#endif
3308
3309/*
3310 * "expand()" function
3311 */
3312 static void
3313f_expand(typval_T *argvars, typval_T *rettv)
3314{
3315 char_u *s;
3316 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003317 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3319 expand_T xpc;
3320 int error = FALSE;
3321 char_u *result;
3322
3323 rettv->v_type = VAR_STRING;
3324 if (argvars[1].v_type != VAR_UNKNOWN
3325 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003326 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003327 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003328 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003330 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331 if (*s == '%' || *s == '#' || *s == '<')
3332 {
3333 ++emsg_off;
3334 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3335 --emsg_off;
3336 if (rettv->v_type == VAR_LIST)
3337 {
3338 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3339 list_append_string(rettv->vval.v_list, result, -1);
3340 else
3341 vim_free(result);
3342 }
3343 else
3344 rettv->vval.v_string = result;
3345 }
3346 else
3347 {
3348 /* When the optional second argument is non-zero, don't remove matches
3349 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3350 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003351 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 options |= WILD_KEEP_ALL;
3353 if (!error)
3354 {
3355 ExpandInit(&xpc);
3356 xpc.xp_context = EXPAND_FILES;
3357 if (p_wic)
3358 options += WILD_ICASE;
3359 if (rettv->v_type == VAR_STRING)
3360 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3361 options, WILD_ALL);
3362 else if (rettv_list_alloc(rettv) != FAIL)
3363 {
3364 int i;
3365
3366 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3367 for (i = 0; i < xpc.xp_numfiles; i++)
3368 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3369 ExpandCleanup(&xpc);
3370 }
3371 }
3372 else
3373 rettv->vval.v_string = NULL;
3374 }
3375}
3376
3377/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003378 * "expandcmd()" function
3379 * Expand all the special characters in a command string.
3380 */
3381 static void
3382f_expandcmd(typval_T *argvars, typval_T *rettv)
3383{
3384 exarg_T eap;
3385 char_u *cmdstr;
3386 char *errormsg = NULL;
3387
3388 rettv->v_type = VAR_STRING;
3389 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3390
3391 memset(&eap, 0, sizeof(eap));
3392 eap.cmd = cmdstr;
3393 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003394 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003395 eap.usefilter = FALSE;
3396 eap.nextcmd = NULL;
3397 eap.cmdidx = CMD_USER;
3398
3399 expand_filename(&eap, &cmdstr, &errormsg);
3400 if (errormsg != NULL && *errormsg != NUL)
3401 emsg(errormsg);
3402
3403 rettv->vval.v_string = cmdstr;
3404}
3405
3406/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407 * "extend(list, list [, idx])" function
3408 * "extend(dict, dict [, action])" function
3409 */
3410 static void
3411f_extend(typval_T *argvars, typval_T *rettv)
3412{
3413 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3414
3415 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3416 {
3417 list_T *l1, *l2;
3418 listitem_T *item;
3419 long before;
3420 int error = FALSE;
3421
3422 l1 = argvars[0].vval.v_list;
3423 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003424 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 && l2 != NULL)
3426 {
3427 if (argvars[2].v_type != VAR_UNKNOWN)
3428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003429 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 if (error)
3431 return; /* type error; errmsg already given */
3432
3433 if (before == l1->lv_len)
3434 item = NULL;
3435 else
3436 {
3437 item = list_find(l1, before);
3438 if (item == NULL)
3439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003440 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 return;
3442 }
3443 }
3444 }
3445 else
3446 item = NULL;
3447 list_extend(l1, l2, item);
3448
3449 copy_tv(&argvars[0], rettv);
3450 }
3451 }
3452 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3453 {
3454 dict_T *d1, *d2;
3455 char_u *action;
3456 int i;
3457
3458 d1 = argvars[0].vval.v_dict;
3459 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003460 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461 && d2 != NULL)
3462 {
3463 /* Check the third argument. */
3464 if (argvars[2].v_type != VAR_UNKNOWN)
3465 {
3466 static char *(av[]) = {"keep", "force", "error"};
3467
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003468 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003469 if (action == NULL)
3470 return; /* type error; errmsg already given */
3471 for (i = 0; i < 3; ++i)
3472 if (STRCMP(action, av[i]) == 0)
3473 break;
3474 if (i == 3)
3475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477 return;
3478 }
3479 }
3480 else
3481 action = (char_u *)"force";
3482
3483 dict_extend(d1, d2, action);
3484
3485 copy_tv(&argvars[0], rettv);
3486 }
3487 }
3488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003489 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490}
3491
3492/*
3493 * "feedkeys()" function
3494 */
3495 static void
3496f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3497{
3498 int remap = TRUE;
3499 int insert = FALSE;
3500 char_u *keys, *flags;
3501 char_u nbuf[NUMBUFLEN];
3502 int typed = FALSE;
3503 int execute = FALSE;
3504 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003505 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 char_u *keys_esc;
3507
3508 /* This is not allowed in the sandbox. If the commands would still be
3509 * executed in the sandbox it would be OK, but it probably happens later,
3510 * when "sandbox" is no longer set. */
3511 if (check_secure())
3512 return;
3513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003514 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515
3516 if (argvars[1].v_type != VAR_UNKNOWN)
3517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003518 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 for ( ; *flags != NUL; ++flags)
3520 {
3521 switch (*flags)
3522 {
3523 case 'n': remap = FALSE; break;
3524 case 'm': remap = TRUE; break;
3525 case 't': typed = TRUE; break;
3526 case 'i': insert = TRUE; break;
3527 case 'x': execute = TRUE; break;
3528 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003529 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 }
3531 }
3532 }
3533
3534 if (*keys != NUL || execute)
3535 {
3536 /* Need to escape K_SPECIAL and CSI before putting the string in the
3537 * typeahead buffer. */
3538 keys_esc = vim_strsave_escape_csi(keys);
3539 if (keys_esc != NULL)
3540 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003541 if (lowlevel)
3542 {
3543#ifdef USE_INPUT_BUF
3544 add_to_input_buf(keys, (int)STRLEN(keys));
3545#else
3546 emsg(_("E980: lowlevel input not supported"));
3547#endif
3548 }
3549 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003550 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003551 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003553 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003554#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003555 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003556#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003557 )
3558 typebuf_was_filled = TRUE;
3559 }
3560 vim_free(keys_esc);
3561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 if (execute)
3563 {
3564 int save_msg_scroll = msg_scroll;
3565
3566 /* Avoid a 1 second delay when the keys start Insert mode. */
3567 msg_scroll = FALSE;
3568
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003569 if (!dangerous)
3570 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003571 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003572 if (!dangerous)
3573 --ex_normal_busy;
3574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 msg_scroll |= save_msg_scroll;
3576 }
3577 }
3578 }
3579}
3580
3581/*
3582 * "filereadable()" function
3583 */
3584 static void
3585f_filereadable(typval_T *argvars, typval_T *rettv)
3586{
3587 int fd;
3588 char_u *p;
3589 int n;
3590
3591#ifndef O_NONBLOCK
3592# define O_NONBLOCK 0
3593#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003594 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3596 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3597 {
3598 n = TRUE;
3599 close(fd);
3600 }
3601 else
3602 n = FALSE;
3603
3604 rettv->vval.v_number = n;
3605}
3606
3607/*
3608 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3609 * rights to write into.
3610 */
3611 static void
3612f_filewritable(typval_T *argvars, typval_T *rettv)
3613{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003614 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615}
3616
3617 static void
3618findfilendir(
3619 typval_T *argvars UNUSED,
3620 typval_T *rettv,
3621 int find_what UNUSED)
3622{
3623#ifdef FEAT_SEARCHPATH
3624 char_u *fname;
3625 char_u *fresult = NULL;
3626 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3627 char_u *p;
3628 char_u pathbuf[NUMBUFLEN];
3629 int count = 1;
3630 int first = TRUE;
3631 int error = FALSE;
3632#endif
3633
3634 rettv->vval.v_string = NULL;
3635 rettv->v_type = VAR_STRING;
3636
3637#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003638 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639
3640 if (argvars[1].v_type != VAR_UNKNOWN)
3641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003642 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 if (p == NULL)
3644 error = TRUE;
3645 else
3646 {
3647 if (*p != NUL)
3648 path = p;
3649
3650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003651 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 }
3653 }
3654
3655 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3656 error = TRUE;
3657
3658 if (*fname != NUL && !error)
3659 {
3660 do
3661 {
3662 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3663 vim_free(fresult);
3664 fresult = find_file_in_path_option(first ? fname : NULL,
3665 first ? (int)STRLEN(fname) : 0,
3666 0, first, path,
3667 find_what,
3668 curbuf->b_ffname,
3669 find_what == FINDFILE_DIR
3670 ? (char_u *)"" : curbuf->b_p_sua);
3671 first = FALSE;
3672
3673 if (fresult != NULL && rettv->v_type == VAR_LIST)
3674 list_append_string(rettv->vval.v_list, fresult, -1);
3675
3676 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3677 }
3678
3679 if (rettv->v_type == VAR_STRING)
3680 rettv->vval.v_string = fresult;
3681#endif
3682}
3683
3684/*
3685 * "filter()" function
3686 */
3687 static void
3688f_filter(typval_T *argvars, typval_T *rettv)
3689{
3690 filter_map(argvars, rettv, FALSE);
3691}
3692
3693/*
3694 * "finddir({fname}[, {path}[, {count}]])" function
3695 */
3696 static void
3697f_finddir(typval_T *argvars, typval_T *rettv)
3698{
3699 findfilendir(argvars, rettv, FINDFILE_DIR);
3700}
3701
3702/*
3703 * "findfile({fname}[, {path}[, {count}]])" function
3704 */
3705 static void
3706f_findfile(typval_T *argvars, typval_T *rettv)
3707{
3708 findfilendir(argvars, rettv, FINDFILE_FILE);
3709}
3710
3711#ifdef FEAT_FLOAT
3712/*
3713 * "float2nr({float})" function
3714 */
3715 static void
3716f_float2nr(typval_T *argvars, typval_T *rettv)
3717{
3718 float_T f = 0.0;
3719
3720 if (get_float_arg(argvars, &f) == OK)
3721 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003722 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003723 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003724 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003725 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 else
3727 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 }
3729}
3730
3731/*
3732 * "floor({float})" function
3733 */
3734 static void
3735f_floor(typval_T *argvars, typval_T *rettv)
3736{
3737 float_T f = 0.0;
3738
3739 rettv->v_type = VAR_FLOAT;
3740 if (get_float_arg(argvars, &f) == OK)
3741 rettv->vval.v_float = floor(f);
3742 else
3743 rettv->vval.v_float = 0.0;
3744}
3745
3746/*
3747 * "fmod()" function
3748 */
3749 static void
3750f_fmod(typval_T *argvars, typval_T *rettv)
3751{
3752 float_T fx = 0.0, fy = 0.0;
3753
3754 rettv->v_type = VAR_FLOAT;
3755 if (get_float_arg(argvars, &fx) == OK
3756 && get_float_arg(&argvars[1], &fy) == OK)
3757 rettv->vval.v_float = fmod(fx, fy);
3758 else
3759 rettv->vval.v_float = 0.0;
3760}
3761#endif
3762
3763/*
3764 * "fnameescape({string})" function
3765 */
3766 static void
3767f_fnameescape(typval_T *argvars, typval_T *rettv)
3768{
3769 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003770 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003771 rettv->v_type = VAR_STRING;
3772}
3773
3774/*
3775 * "fnamemodify({fname}, {mods})" function
3776 */
3777 static void
3778f_fnamemodify(typval_T *argvars, typval_T *rettv)
3779{
3780 char_u *fname;
3781 char_u *mods;
3782 int usedlen = 0;
3783 int len;
3784 char_u *fbuf = NULL;
3785 char_u buf[NUMBUFLEN];
3786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 fname = tv_get_string_chk(&argvars[0]);
3788 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003789 if (fname == NULL || mods == NULL)
3790 fname = NULL;
3791 else
3792 {
3793 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003794 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003795 }
3796
3797 rettv->v_type = VAR_STRING;
3798 if (fname == NULL)
3799 rettv->vval.v_string = NULL;
3800 else
3801 rettv->vval.v_string = vim_strnsave(fname, len);
3802 vim_free(fbuf);
3803}
3804
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805/*
3806 * "foldclosed()" function
3807 */
3808 static void
3809foldclosed_both(
3810 typval_T *argvars UNUSED,
3811 typval_T *rettv,
3812 int end UNUSED)
3813{
3814#ifdef FEAT_FOLDING
3815 linenr_T lnum;
3816 linenr_T first, last;
3817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003818 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3820 {
3821 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3822 {
3823 if (end)
3824 rettv->vval.v_number = (varnumber_T)last;
3825 else
3826 rettv->vval.v_number = (varnumber_T)first;
3827 return;
3828 }
3829 }
3830#endif
3831 rettv->vval.v_number = -1;
3832}
3833
3834/*
3835 * "foldclosed()" function
3836 */
3837 static void
3838f_foldclosed(typval_T *argvars, typval_T *rettv)
3839{
3840 foldclosed_both(argvars, rettv, FALSE);
3841}
3842
3843/*
3844 * "foldclosedend()" function
3845 */
3846 static void
3847f_foldclosedend(typval_T *argvars, typval_T *rettv)
3848{
3849 foldclosed_both(argvars, rettv, TRUE);
3850}
3851
3852/*
3853 * "foldlevel()" function
3854 */
3855 static void
3856f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3857{
3858#ifdef FEAT_FOLDING
3859 linenr_T lnum;
3860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003861 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003862 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3863 rettv->vval.v_number = foldLevel(lnum);
3864#endif
3865}
3866
3867/*
3868 * "foldtext()" function
3869 */
3870 static void
3871f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3872{
3873#ifdef FEAT_FOLDING
3874 linenr_T foldstart;
3875 linenr_T foldend;
3876 char_u *dashes;
3877 linenr_T lnum;
3878 char_u *s;
3879 char_u *r;
3880 int len;
3881 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003882 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883#endif
3884
3885 rettv->v_type = VAR_STRING;
3886 rettv->vval.v_string = NULL;
3887#ifdef FEAT_FOLDING
3888 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3889 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3890 dashes = get_vim_var_str(VV_FOLDDASHES);
3891 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3892 && dashes != NULL)
3893 {
3894 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003895 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 if (!linewhite(lnum))
3897 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898
3899 /* Find interesting text in this line. */
3900 s = skipwhite(ml_get(lnum));
3901 /* skip C comment-start */
3902 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3903 {
3904 s = skipwhite(s + 2);
3905 if (*skipwhite(s) == NUL
3906 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3907 {
3908 s = skipwhite(ml_get(lnum + 1));
3909 if (*s == '*')
3910 s = skipwhite(s + 1);
3911 }
3912 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003913 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003914 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003915 r = alloc(STRLEN(txt)
3916 + STRLEN(dashes) // for %s
3917 + 20 // for %3ld
3918 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 if (r != NULL)
3920 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003921 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 len = (int)STRLEN(r);
3923 STRCAT(r, s);
3924 /* remove 'foldmarker' and 'commentstring' */
3925 foldtext_cleanup(r + len);
3926 rettv->vval.v_string = r;
3927 }
3928 }
3929#endif
3930}
3931
3932/*
3933 * "foldtextresult(lnum)" function
3934 */
3935 static void
3936f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3937{
3938#ifdef FEAT_FOLDING
3939 linenr_T lnum;
3940 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003941 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 foldinfo_T foldinfo;
3943 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003944 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945#endif
3946
3947 rettv->v_type = VAR_STRING;
3948 rettv->vval.v_string = NULL;
3949#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003950 if (entered)
3951 return; /* reject recursive use */
3952 entered = TRUE;
3953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003954 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003955 /* treat illegal types and illegal string values for {lnum} the same */
3956 if (lnum < 0)
3957 lnum = 0;
3958 fold_count = foldedCount(curwin, lnum, &foldinfo);
3959 if (fold_count > 0)
3960 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003961 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3962 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963 if (text == buf)
3964 text = vim_strsave(text);
3965 rettv->vval.v_string = text;
3966 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003967
3968 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969#endif
3970}
3971
3972/*
3973 * "foreground()" function
3974 */
3975 static void
3976f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3977{
3978#ifdef FEAT_GUI
3979 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003980 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003982 return;
3983 }
3984#endif
3985#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003986 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003987#endif
3988}
3989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003990 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003991common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003992{
3993 char_u *s;
3994 char_u *name;
3995 int use_string = FALSE;
3996 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003997 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998
3999 if (argvars[0].v_type == VAR_FUNC)
4000 {
4001 /* function(MyFunc, [arg], dict) */
4002 s = argvars[0].vval.v_string;
4003 }
4004 else if (argvars[0].v_type == VAR_PARTIAL
4005 && argvars[0].vval.v_partial != NULL)
4006 {
4007 /* function(dict.MyFunc, [arg]) */
4008 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010 }
4011 else
4012 {
4013 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004014 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 use_string = TRUE;
4016 }
4017
Bram Moolenaar843b8842016-08-21 14:36:15 +02004018 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 {
4020 name = s;
4021 trans_name = trans_function_name(&name, FALSE,
4022 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4023 if (*name != NUL)
4024 s = NULL;
4025 }
4026
Bram Moolenaar843b8842016-08-21 14:36:15 +02004027 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4028 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004029 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004031 else if (trans_name != NULL && (is_funcref
4032 ? find_func(trans_name) == NULL
4033 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004034 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004035 else
4036 {
4037 int dict_idx = 0;
4038 int arg_idx = 0;
4039 list_T *list = NULL;
4040
4041 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4042 {
4043 char sid_buf[25];
4044 int off = *s == 's' ? 2 : 5;
4045
4046 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4047 * also be called from another script. Using trans_function_name()
4048 * would also work, but some plugins depend on the name being
4049 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004050 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004051 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 if (name != NULL)
4053 {
4054 STRCPY(name, sid_buf);
4055 STRCAT(name, s + off);
4056 }
4057 }
4058 else
4059 name = vim_strsave(s);
4060
4061 if (argvars[1].v_type != VAR_UNKNOWN)
4062 {
4063 if (argvars[2].v_type != VAR_UNKNOWN)
4064 {
4065 /* function(name, [args], dict) */
4066 arg_idx = 1;
4067 dict_idx = 2;
4068 }
4069 else if (argvars[1].v_type == VAR_DICT)
4070 /* function(name, dict) */
4071 dict_idx = 1;
4072 else
4073 /* function(name, [args]) */
4074 arg_idx = 1;
4075 if (dict_idx > 0)
4076 {
4077 if (argvars[dict_idx].v_type != VAR_DICT)
4078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004079 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004081 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004082 }
4083 if (argvars[dict_idx].vval.v_dict == NULL)
4084 dict_idx = 0;
4085 }
4086 if (arg_idx > 0)
4087 {
4088 if (argvars[arg_idx].v_type != VAR_LIST)
4089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004090 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004092 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 }
4094 list = argvars[arg_idx].vval.v_list;
4095 if (list == NULL || list->lv_len == 0)
4096 arg_idx = 0;
4097 }
4098 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004099 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004101 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004102
4103 /* result is a VAR_PARTIAL */
4104 if (pt == NULL)
4105 vim_free(name);
4106 else
4107 {
4108 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4109 {
4110 listitem_T *li;
4111 int i = 0;
4112 int arg_len = 0;
4113 int lv_len = 0;
4114
4115 if (arg_pt != NULL)
4116 arg_len = arg_pt->pt_argc;
4117 if (list != NULL)
4118 lv_len = list->lv_len;
4119 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004120 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 if (pt->pt_argv == NULL)
4122 {
4123 vim_free(pt);
4124 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004125 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004127 for (i = 0; i < arg_len; i++)
4128 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4129 if (lv_len > 0)
4130 for (li = list->lv_first; li != NULL;
4131 li = li->li_next)
4132 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004133 }
4134
4135 /* For "function(dict.func, [], dict)" and "func" is a partial
4136 * use "dict". That is backwards compatible. */
4137 if (dict_idx > 0)
4138 {
4139 /* The dict is bound explicitly, pt_auto is FALSE. */
4140 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4141 ++pt->pt_dict->dv_refcount;
4142 }
4143 else if (arg_pt != NULL)
4144 {
4145 /* If the dict was bound automatically the result is also
4146 * bound automatically. */
4147 pt->pt_dict = arg_pt->pt_dict;
4148 pt->pt_auto = arg_pt->pt_auto;
4149 if (pt->pt_dict != NULL)
4150 ++pt->pt_dict->dv_refcount;
4151 }
4152
4153 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4155 {
4156 pt->pt_func = arg_pt->pt_func;
4157 func_ptr_ref(pt->pt_func);
4158 vim_free(name);
4159 }
4160 else if (is_funcref)
4161 {
4162 pt->pt_func = find_func(trans_name);
4163 func_ptr_ref(pt->pt_func);
4164 vim_free(name);
4165 }
4166 else
4167 {
4168 pt->pt_name = name;
4169 func_ref(name);
4170 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004171 }
4172 rettv->v_type = VAR_PARTIAL;
4173 rettv->vval.v_partial = pt;
4174 }
4175 else
4176 {
4177 /* result is a VAR_FUNC */
4178 rettv->v_type = VAR_FUNC;
4179 rettv->vval.v_string = name;
4180 func_ref(name);
4181 }
4182 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004183theend:
4184 vim_free(trans_name);
4185}
4186
4187/*
4188 * "funcref()" function
4189 */
4190 static void
4191f_funcref(typval_T *argvars, typval_T *rettv)
4192{
4193 common_function(argvars, rettv, TRUE);
4194}
4195
4196/*
4197 * "function()" function
4198 */
4199 static void
4200f_function(typval_T *argvars, typval_T *rettv)
4201{
4202 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004203}
4204
4205/*
4206 * "garbagecollect()" function
4207 */
4208 static void
4209f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4210{
4211 /* This is postponed until we are back at the toplevel, because we may be
4212 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4213 want_garbage_collect = TRUE;
4214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004215 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216 garbage_collect_at_exit = TRUE;
4217}
4218
4219/*
4220 * "get()" function
4221 */
4222 static void
4223f_get(typval_T *argvars, typval_T *rettv)
4224{
4225 listitem_T *li;
4226 list_T *l;
4227 dictitem_T *di;
4228 dict_T *d;
4229 typval_T *tv = NULL;
4230
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004231 if (argvars[0].v_type == VAR_BLOB)
4232 {
4233 int error = FALSE;
4234 int idx = tv_get_number_chk(&argvars[1], &error);
4235
4236 if (!error)
4237 {
4238 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004239 if (idx < 0)
4240 idx = blob_len(argvars[0].vval.v_blob) + idx;
4241 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4242 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004243 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004244 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004245 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004246 tv = rettv;
4247 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004248 }
4249 }
4250 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251 {
4252 if ((l = argvars[0].vval.v_list) != NULL)
4253 {
4254 int error = FALSE;
4255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004256 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 if (!error && li != NULL)
4258 tv = &li->li_tv;
4259 }
4260 }
4261 else if (argvars[0].v_type == VAR_DICT)
4262 {
4263 if ((d = argvars[0].vval.v_dict) != NULL)
4264 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004265 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004266 if (di != NULL)
4267 tv = &di->di_tv;
4268 }
4269 }
4270 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4271 {
4272 partial_T *pt;
4273 partial_T fref_pt;
4274
4275 if (argvars[0].v_type == VAR_PARTIAL)
4276 pt = argvars[0].vval.v_partial;
4277 else
4278 {
4279 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4280 fref_pt.pt_name = argvars[0].vval.v_string;
4281 pt = &fref_pt;
4282 }
4283
4284 if (pt != NULL)
4285 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004286 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004287 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288
4289 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4290 {
4291 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004292 n = partial_name(pt);
4293 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294 rettv->vval.v_string = NULL;
4295 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004296 {
4297 rettv->vval.v_string = vim_strsave(n);
4298 if (rettv->v_type == VAR_FUNC)
4299 func_ref(rettv->vval.v_string);
4300 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 }
4302 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004303 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004304 else if (STRCMP(what, "args") == 0)
4305 {
4306 rettv->v_type = VAR_LIST;
4307 if (rettv_list_alloc(rettv) == OK)
4308 {
4309 int i;
4310
4311 for (i = 0; i < pt->pt_argc; ++i)
4312 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4313 }
4314 }
4315 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004316 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 return;
4318 }
4319 }
4320 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004321 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322
4323 if (tv == NULL)
4324 {
4325 if (argvars[2].v_type != VAR_UNKNOWN)
4326 copy_tv(&argvars[2], rettv);
4327 }
4328 else
4329 copy_tv(tv, rettv);
4330}
4331
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004332/*
4333 * Returns buffer options, variables and other attributes in a dictionary.
4334 */
4335 static dict_T *
4336get_buffer_info(buf_T *buf)
4337{
4338 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004339 tabpage_T *tp;
4340 win_T *wp;
4341 list_T *windows;
4342
4343 dict = dict_alloc();
4344 if (dict == NULL)
4345 return NULL;
4346
Bram Moolenaare0be1672018-07-08 16:50:37 +02004347 dict_add_number(dict, "bufnr", buf->b_fnum);
4348 dict_add_string(dict, "name", buf->b_ffname);
4349 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4350 : buflist_findlnum(buf));
4351 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4352 dict_add_number(dict, "listed", buf->b_p_bl);
4353 dict_add_number(dict, "changed", bufIsChanged(buf));
4354 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4355 dict_add_number(dict, "hidden",
4356 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004357
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004358 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004359 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004360
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004361 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004362 windows = list_alloc();
4363 if (windows != NULL)
4364 {
4365 FOR_ALL_TAB_WINDOWS(tp, wp)
4366 if (wp->w_buffer == buf)
4367 list_append_number(windows, (varnumber_T)wp->w_id);
4368 dict_add_list(dict, "windows", windows);
4369 }
4370
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004371#ifdef FEAT_TEXT_PROP
4372 // List of popup windows displaying this buffer
4373 windows = list_alloc();
4374 if (windows != NULL)
4375 {
4376 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4377 if (wp->w_buffer == buf)
4378 list_append_number(windows, (varnumber_T)wp->w_id);
4379 FOR_ALL_TABPAGES(tp)
4380 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4381 if (wp->w_buffer == buf)
4382 list_append_number(windows, (varnumber_T)wp->w_id);
4383
4384 dict_add_list(dict, "popups", windows);
4385 }
4386#endif
4387
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004388#ifdef FEAT_SIGNS
4389 if (buf->b_signlist != NULL)
4390 {
4391 /* List of signs placed in this buffer */
4392 list_T *signs = list_alloc();
4393 if (signs != NULL)
4394 {
4395 get_buffer_signs(buf, signs);
4396 dict_add_list(dict, "signs", signs);
4397 }
4398 }
4399#endif
4400
4401 return dict;
4402}
4403
4404/*
4405 * "getbufinfo()" function
4406 */
4407 static void
4408f_getbufinfo(typval_T *argvars, typval_T *rettv)
4409{
4410 buf_T *buf = NULL;
4411 buf_T *argbuf = NULL;
4412 dict_T *d;
4413 int filtered = FALSE;
4414 int sel_buflisted = FALSE;
4415 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004416 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004417
4418 if (rettv_list_alloc(rettv) != OK)
4419 return;
4420
4421 /* List of all the buffers or selected buffers */
4422 if (argvars[0].v_type == VAR_DICT)
4423 {
4424 dict_T *sel_d = argvars[0].vval.v_dict;
4425
4426 if (sel_d != NULL)
4427 {
4428 dictitem_T *di;
4429
4430 filtered = TRUE;
4431
4432 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004433 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004434 sel_buflisted = TRUE;
4435
4436 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004437 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004438 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004439
4440 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004441 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004442 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004443 }
4444 }
4445 else if (argvars[0].v_type != VAR_UNKNOWN)
4446 {
4447 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004448 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004449 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004450 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004451 --emsg_off;
4452 if (argbuf == NULL)
4453 return;
4454 }
4455
4456 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004457 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004458 {
4459 if (argbuf != NULL && argbuf != buf)
4460 continue;
4461 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004462 || (sel_buflisted && !buf->b_p_bl)
4463 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004464 continue;
4465
4466 d = get_buffer_info(buf);
4467 if (d != NULL)
4468 list_append_dict(rettv->vval.v_list, d);
4469 if (argbuf != NULL)
4470 return;
4471 }
4472}
4473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474/*
4475 * Get line or list of lines from buffer "buf" into "rettv".
4476 * Return a range (from start to end) of lines in rettv from the specified
4477 * buffer.
4478 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4479 */
4480 static void
4481get_buffer_lines(
4482 buf_T *buf,
4483 linenr_T start,
4484 linenr_T end,
4485 int retlist,
4486 typval_T *rettv)
4487{
4488 char_u *p;
4489
4490 rettv->v_type = VAR_STRING;
4491 rettv->vval.v_string = NULL;
4492 if (retlist && rettv_list_alloc(rettv) == FAIL)
4493 return;
4494
4495 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4496 return;
4497
4498 if (!retlist)
4499 {
4500 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4501 p = ml_get_buf(buf, start, FALSE);
4502 else
4503 p = (char_u *)"";
4504 rettv->vval.v_string = vim_strsave(p);
4505 }
4506 else
4507 {
4508 if (end < start)
4509 return;
4510
4511 if (start < 1)
4512 start = 1;
4513 if (end > buf->b_ml.ml_line_count)
4514 end = buf->b_ml.ml_line_count;
4515 while (start <= end)
4516 if (list_append_string(rettv->vval.v_list,
4517 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4518 break;
4519 }
4520}
4521
4522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523 * "getbufline()" function
4524 */
4525 static void
4526f_getbufline(typval_T *argvars, typval_T *rettv)
4527{
4528 linenr_T lnum;
4529 linenr_T end;
4530 buf_T *buf;
4531
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004532 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004533 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004534 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535 --emsg_off;
4536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004537 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 if (argvars[2].v_type == VAR_UNKNOWN)
4539 end = lnum;
4540 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004541 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542
4543 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4544}
4545
4546/*
4547 * "getbufvar()" function
4548 */
4549 static void
4550f_getbufvar(typval_T *argvars, typval_T *rettv)
4551{
4552 buf_T *buf;
4553 buf_T *save_curbuf;
4554 char_u *varname;
4555 dictitem_T *v;
4556 int done = FALSE;
4557
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004558 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4559 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004561 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562
4563 rettv->v_type = VAR_STRING;
4564 rettv->vval.v_string = NULL;
4565
4566 if (buf != NULL && varname != NULL)
4567 {
4568 /* set curbuf to be our buf, temporarily */
4569 save_curbuf = curbuf;
4570 curbuf = buf;
4571
Bram Moolenaar30567352016-08-27 21:25:44 +02004572 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004574 if (varname[1] == NUL)
4575 {
4576 /* get all buffer-local options in a dict */
4577 dict_T *opts = get_winbuf_options(TRUE);
4578
4579 if (opts != NULL)
4580 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004581 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004582 done = TRUE;
4583 }
4584 }
4585 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4586 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587 done = TRUE;
4588 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 else
4590 {
4591 /* Look up the variable. */
4592 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4593 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4594 'b', varname, FALSE);
4595 if (v != NULL)
4596 {
4597 copy_tv(&v->di_tv, rettv);
4598 done = TRUE;
4599 }
4600 }
4601
4602 /* restore previous notion of curbuf */
4603 curbuf = save_curbuf;
4604 }
4605
4606 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4607 /* use the default value */
4608 copy_tv(&argvars[2], rettv);
4609
4610 --emsg_off;
4611}
4612
4613/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004614 * "getchangelist()" function
4615 */
4616 static void
4617f_getchangelist(typval_T *argvars, typval_T *rettv)
4618{
4619#ifdef FEAT_JUMPLIST
4620 buf_T *buf;
4621 int i;
4622 list_T *l;
4623 dict_T *d;
4624#endif
4625
4626 if (rettv_list_alloc(rettv) != OK)
4627 return;
4628
4629#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004630 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004631 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004632 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004633 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004634 if (buf == NULL)
4635 return;
4636
4637 l = list_alloc();
4638 if (l == NULL)
4639 return;
4640
4641 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4642 return;
4643 /*
4644 * The current window change list index tracks only the position in the
4645 * current buffer change list. For other buffers, use the change list
4646 * length as the current index.
4647 */
4648 list_append_number(rettv->vval.v_list,
4649 (varnumber_T)((buf == curwin->w_buffer)
4650 ? curwin->w_changelistidx : buf->b_changelistlen));
4651
4652 for (i = 0; i < buf->b_changelistlen; ++i)
4653 {
4654 if (buf->b_changelist[i].lnum == 0)
4655 continue;
4656 if ((d = dict_alloc()) == NULL)
4657 return;
4658 if (list_append_dict(l, d) == FAIL)
4659 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004660 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4661 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004662 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004663 }
4664#endif
4665}
4666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004667 * "getchar()" function
4668 */
4669 static void
4670f_getchar(typval_T *argvars, typval_T *rettv)
4671{
4672 varnumber_T n;
4673 int error = FALSE;
4674
Bram Moolenaar84d93902018-09-11 20:10:20 +02004675#ifdef MESSAGE_QUEUE
4676 // vpeekc() used to check for messages, but that caused problems, invoking
4677 // a callback where it was not expected. Some plugins use getchar(1) in a
4678 // loop to await a message, therefore make sure we check for messages here.
4679 parse_queued_messages();
4680#endif
4681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682 /* Position the cursor. Needed after a message that ends in a space. */
4683 windgoto(msg_row, msg_col);
4684
4685 ++no_mapping;
4686 ++allow_keys;
4687 for (;;)
4688 {
4689 if (argvars[0].v_type == VAR_UNKNOWN)
4690 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004691 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004692 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 /* getchar(1): only check if char avail */
4694 n = vpeekc_any();
4695 else if (error || vpeekc_any() == NUL)
4696 /* illegal argument or getchar(0) and no char avail: return zero */
4697 n = 0;
4698 else
4699 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004700 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701
4702 if (n == K_IGNORE)
4703 continue;
4704 break;
4705 }
4706 --no_mapping;
4707 --allow_keys;
4708
4709 set_vim_var_nr(VV_MOUSE_WIN, 0);
4710 set_vim_var_nr(VV_MOUSE_WINID, 0);
4711 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4712 set_vim_var_nr(VV_MOUSE_COL, 0);
4713
4714 rettv->vval.v_number = n;
4715 if (IS_SPECIAL(n) || mod_mask != 0)
4716 {
4717 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4718 int i = 0;
4719
4720 /* Turn a special key into three bytes, plus modifier. */
4721 if (mod_mask != 0)
4722 {
4723 temp[i++] = K_SPECIAL;
4724 temp[i++] = KS_MODIFIER;
4725 temp[i++] = mod_mask;
4726 }
4727 if (IS_SPECIAL(n))
4728 {
4729 temp[i++] = K_SPECIAL;
4730 temp[i++] = K_SECOND(n);
4731 temp[i++] = K_THIRD(n);
4732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004733 else if (has_mbyte)
4734 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004735 else
4736 temp[i++] = n;
4737 temp[i++] = NUL;
4738 rettv->v_type = VAR_STRING;
4739 rettv->vval.v_string = vim_strsave(temp);
4740
4741#ifdef FEAT_MOUSE
4742 if (is_mouse_key(n))
4743 {
4744 int row = mouse_row;
4745 int col = mouse_col;
4746 win_T *win;
4747 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 int winnr = 1;
4750
4751 if (row >= 0 && col >= 0)
4752 {
4753 /* Find the window at the mouse coordinates and compute the
4754 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004755 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004756 if (win == NULL)
4757 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004758 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004759# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004760 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004761 winnr = 0;
4762 else
4763# endif
4764 for (wp = firstwin; wp != win && wp != NULL;
4765 wp = wp->w_next)
4766 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004767 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4768 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4769 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4770 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4771 }
4772 }
4773#endif
4774 }
4775}
4776
4777/*
4778 * "getcharmod()" function
4779 */
4780 static void
4781f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4782{
4783 rettv->vval.v_number = mod_mask;
4784}
4785
4786/*
4787 * "getcharsearch()" function
4788 */
4789 static void
4790f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4791{
4792 if (rettv_dict_alloc(rettv) != FAIL)
4793 {
4794 dict_T *dict = rettv->vval.v_dict;
4795
Bram Moolenaare0be1672018-07-08 16:50:37 +02004796 dict_add_string(dict, "char", last_csearch());
4797 dict_add_number(dict, "forward", last_csearch_forward());
4798 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004799 }
4800}
4801
4802/*
4803 * "getcmdline()" function
4804 */
4805 static void
4806f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4807{
4808 rettv->v_type = VAR_STRING;
4809 rettv->vval.v_string = get_cmdline_str();
4810}
4811
4812/*
4813 * "getcmdpos()" function
4814 */
4815 static void
4816f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4817{
4818 rettv->vval.v_number = get_cmdline_pos() + 1;
4819}
4820
4821/*
4822 * "getcmdtype()" function
4823 */
4824 static void
4825f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4826{
4827 rettv->v_type = VAR_STRING;
4828 rettv->vval.v_string = alloc(2);
4829 if (rettv->vval.v_string != NULL)
4830 {
4831 rettv->vval.v_string[0] = get_cmdline_type();
4832 rettv->vval.v_string[1] = NUL;
4833 }
4834}
4835
4836/*
4837 * "getcmdwintype()" function
4838 */
4839 static void
4840f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4841{
4842 rettv->v_type = VAR_STRING;
4843 rettv->vval.v_string = NULL;
4844#ifdef FEAT_CMDWIN
4845 rettv->vval.v_string = alloc(2);
4846 if (rettv->vval.v_string != NULL)
4847 {
4848 rettv->vval.v_string[0] = cmdwin_type;
4849 rettv->vval.v_string[1] = NUL;
4850 }
4851#endif
4852}
4853
4854#if defined(FEAT_CMDL_COMPL)
4855/*
4856 * "getcompletion()" function
4857 */
4858 static void
4859f_getcompletion(typval_T *argvars, typval_T *rettv)
4860{
4861 char_u *pat;
4862 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004863 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004864 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4865 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004866
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004868 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004869
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 if (p_wic)
4871 options |= WILD_ICASE;
4872
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004873 /* For filtered results, 'wildignore' is used */
4874 if (!filtered)
4875 options |= WILD_KEEP_ALL;
4876
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004878 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004880 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 if (xpc.xp_context == EXPAND_NOTHING)
4882 {
4883 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004884 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004886 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 return;
4888 }
4889
4890# if defined(FEAT_MENU)
4891 if (xpc.xp_context == EXPAND_MENUS)
4892 {
4893 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4894 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4895 }
4896# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004897#ifdef FEAT_CSCOPE
4898 if (xpc.xp_context == EXPAND_CSCOPE)
4899 {
4900 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4901 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4902 }
4903#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004904#ifdef FEAT_SIGNS
4905 if (xpc.xp_context == EXPAND_SIGN)
4906 {
4907 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4908 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4909 }
4910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911
4912 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4913 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4914 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004915 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004916
4917 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4918
4919 for (i = 0; i < xpc.xp_numfiles; i++)
4920 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4921 }
4922 vim_free(pat);
4923 ExpandCleanup(&xpc);
4924}
4925#endif
4926
4927/*
4928 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004929 *
4930 * Return the current working directory of a window in a tab page.
4931 * First optional argument 'winnr' is the window number or -1 and the second
4932 * optional argument 'tabnr' is the tab page number.
4933 *
4934 * If no arguments are supplied, then return the directory of the current
4935 * window.
4936 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4937 * the specified window.
4938 * If 'winnr' is 0 then return the directory of the current window.
4939 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4940 * directory of the specified tab page. Otherwise return the directory of the
4941 * specified window in the specified tab page.
4942 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 */
4944 static void
4945f_getcwd(typval_T *argvars, typval_T *rettv)
4946{
4947 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004948 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004949 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004950 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951
4952 rettv->v_type = VAR_STRING;
4953 rettv->vval.v_string = NULL;
4954
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004955 if (argvars[0].v_type == VAR_NUMBER
4956 && argvars[0].vval.v_number == -1
4957 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004958 global = TRUE;
4959 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004960 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004961
4962 if (wp != NULL && wp->w_localdir != NULL)
4963 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004964 else if (tp != NULL && tp->tp_localdir != NULL)
4965 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4966 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004968 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 rettv->vval.v_string = vim_strsave(globaldir);
4970 else
4971 {
4972 cwd = alloc(MAXPATHL);
4973 if (cwd != NULL)
4974 {
4975 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4976 rettv->vval.v_string = vim_strsave(cwd);
4977 vim_free(cwd);
4978 }
4979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004981#ifdef BACKSLASH_IN_FILENAME
4982 if (rettv->vval.v_string != NULL)
4983 slash_adjust(rettv->vval.v_string);
4984#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004985}
4986
4987/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004988 * "getenv()" function
4989 */
4990 static void
4991f_getenv(typval_T *argvars, typval_T *rettv)
4992{
4993 int mustfree = FALSE;
4994 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4995
4996 if (p == NULL)
4997 {
4998 rettv->v_type = VAR_SPECIAL;
4999 rettv->vval.v_number = VVAL_NULL;
5000 return;
5001 }
5002 if (!mustfree)
5003 p = vim_strsave(p);
5004 rettv->vval.v_string = p;
5005 rettv->v_type = VAR_STRING;
5006}
5007
5008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 * "getfontname()" function
5010 */
5011 static void
5012f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5013{
5014 rettv->v_type = VAR_STRING;
5015 rettv->vval.v_string = NULL;
5016#ifdef FEAT_GUI
5017 if (gui.in_use)
5018 {
5019 GuiFont font;
5020 char_u *name = NULL;
5021
5022 if (argvars[0].v_type == VAR_UNKNOWN)
5023 {
5024 /* Get the "Normal" font. Either the name saved by
5025 * hl_set_font_name() or from the font ID. */
5026 font = gui.norm_font;
5027 name = hl_get_font_name();
5028 }
5029 else
5030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005031 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5033 return;
5034 font = gui_mch_get_font(name, FALSE);
5035 if (font == NOFONT)
5036 return; /* Invalid font name, return empty string. */
5037 }
5038 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5039 if (argvars[0].v_type != VAR_UNKNOWN)
5040 gui_mch_free_font(font);
5041 }
5042#endif
5043}
5044
5045/*
5046 * "getfperm({fname})" function
5047 */
5048 static void
5049f_getfperm(typval_T *argvars, typval_T *rettv)
5050{
5051 char_u *fname;
5052 stat_T st;
5053 char_u *perm = NULL;
5054 char_u flags[] = "rwx";
5055 int i;
5056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005057 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005058
5059 rettv->v_type = VAR_STRING;
5060 if (mch_stat((char *)fname, &st) >= 0)
5061 {
5062 perm = vim_strsave((char_u *)"---------");
5063 if (perm != NULL)
5064 {
5065 for (i = 0; i < 9; i++)
5066 {
5067 if (st.st_mode & (1 << (8 - i)))
5068 perm[i] = flags[i % 3];
5069 }
5070 }
5071 }
5072 rettv->vval.v_string = perm;
5073}
5074
5075/*
5076 * "getfsize({fname})" function
5077 */
5078 static void
5079f_getfsize(typval_T *argvars, typval_T *rettv)
5080{
5081 char_u *fname;
5082 stat_T st;
5083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005084 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085
5086 rettv->v_type = VAR_NUMBER;
5087
5088 if (mch_stat((char *)fname, &st) >= 0)
5089 {
5090 if (mch_isdir(fname))
5091 rettv->vval.v_number = 0;
5092 else
5093 {
5094 rettv->vval.v_number = (varnumber_T)st.st_size;
5095
5096 /* non-perfect check for overflow */
5097 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5098 rettv->vval.v_number = -2;
5099 }
5100 }
5101 else
5102 rettv->vval.v_number = -1;
5103}
5104
5105/*
5106 * "getftime({fname})" function
5107 */
5108 static void
5109f_getftime(typval_T *argvars, typval_T *rettv)
5110{
5111 char_u *fname;
5112 stat_T st;
5113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005114 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005115
5116 if (mch_stat((char *)fname, &st) >= 0)
5117 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5118 else
5119 rettv->vval.v_number = -1;
5120}
5121
5122/*
5123 * "getftype({fname})" function
5124 */
5125 static void
5126f_getftype(typval_T *argvars, typval_T *rettv)
5127{
5128 char_u *fname;
5129 stat_T st;
5130 char_u *type = NULL;
5131 char *t;
5132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005133 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134
5135 rettv->v_type = VAR_STRING;
5136 if (mch_lstat((char *)fname, &st) >= 0)
5137 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005138 if (S_ISREG(st.st_mode))
5139 t = "file";
5140 else if (S_ISDIR(st.st_mode))
5141 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005142 else if (S_ISLNK(st.st_mode))
5143 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 else if (S_ISBLK(st.st_mode))
5145 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 else if (S_ISCHR(st.st_mode))
5147 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 else if (S_ISFIFO(st.st_mode))
5149 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005151 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152 else
5153 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 type = vim_strsave((char_u *)t);
5155 }
5156 rettv->vval.v_string = type;
5157}
5158
5159/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005160 * "getjumplist()" function
5161 */
5162 static void
5163f_getjumplist(typval_T *argvars, typval_T *rettv)
5164{
5165#ifdef FEAT_JUMPLIST
5166 win_T *wp;
5167 int i;
5168 list_T *l;
5169 dict_T *d;
5170#endif
5171
5172 if (rettv_list_alloc(rettv) != OK)
5173 return;
5174
5175#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005176 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005177 if (wp == NULL)
5178 return;
5179
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005180 cleanup_jumplist(wp, TRUE);
5181
Bram Moolenaar4f505882018-02-10 21:06:32 +01005182 l = list_alloc();
5183 if (l == NULL)
5184 return;
5185
5186 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5187 return;
5188 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5189
5190 for (i = 0; i < wp->w_jumplistlen; ++i)
5191 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005192 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5193 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005194 if ((d = dict_alloc()) == NULL)
5195 return;
5196 if (list_append_dict(l, d) == FAIL)
5197 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005198 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5199 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005200 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005201 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005202 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005203 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005204 }
5205#endif
5206}
5207
5208/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005209 * "getline(lnum, [end])" function
5210 */
5211 static void
5212f_getline(typval_T *argvars, typval_T *rettv)
5213{
5214 linenr_T lnum;
5215 linenr_T end;
5216 int retlist;
5217
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005218 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219 if (argvars[1].v_type == VAR_UNKNOWN)
5220 {
5221 end = 0;
5222 retlist = FALSE;
5223 }
5224 else
5225 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005226 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005227 retlist = TRUE;
5228 }
5229
5230 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5231}
5232
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005233#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005234 static void
5235get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5236{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237 if (what_arg->v_type == VAR_UNKNOWN)
5238 {
5239 if (rettv_list_alloc(rettv) == OK)
5240 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005241 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005242 }
5243 else
5244 {
5245 if (rettv_dict_alloc(rettv) == OK)
5246 if (is_qf || (wp != NULL))
5247 {
5248 if (what_arg->v_type == VAR_DICT)
5249 {
5250 dict_T *d = what_arg->vval.v_dict;
5251
5252 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005253 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005254 }
5255 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005256 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005257 }
5258 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005259}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005260#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261
5262/*
5263 * "getloclist()" function
5264 */
5265 static void
5266f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5267{
5268#ifdef FEAT_QUICKFIX
5269 win_T *wp;
5270
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005271 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005272 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5273#endif
5274}
5275
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 * "getpid()" function
5278 */
5279 static void
5280f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5281{
5282 rettv->vval.v_number = mch_get_pid();
5283}
5284
5285 static void
5286getpos_both(
5287 typval_T *argvars,
5288 typval_T *rettv,
5289 int getcurpos)
5290{
5291 pos_T *fp;
5292 list_T *l;
5293 int fnum = -1;
5294
5295 if (rettv_list_alloc(rettv) == OK)
5296 {
5297 l = rettv->vval.v_list;
5298 if (getcurpos)
5299 fp = &curwin->w_cursor;
5300 else
5301 fp = var2fpos(&argvars[0], TRUE, &fnum);
5302 if (fnum != -1)
5303 list_append_number(l, (varnumber_T)fnum);
5304 else
5305 list_append_number(l, (varnumber_T)0);
5306 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5307 : (varnumber_T)0);
5308 list_append_number(l, (fp != NULL)
5309 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5310 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005311 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 (varnumber_T)0);
5313 if (getcurpos)
5314 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005315 int save_set_curswant = curwin->w_set_curswant;
5316 colnr_T save_curswant = curwin->w_curswant;
5317 colnr_T save_virtcol = curwin->w_virtcol;
5318
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 update_curswant();
5320 list_append_number(l, curwin->w_curswant == MAXCOL ?
5321 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005322
5323 // Do not change "curswant", as it is unexpected that a get
5324 // function has a side effect.
5325 if (save_set_curswant)
5326 {
5327 curwin->w_set_curswant = save_set_curswant;
5328 curwin->w_curswant = save_curswant;
5329 curwin->w_virtcol = save_virtcol;
5330 curwin->w_valid &= ~VALID_VIRTCOL;
5331 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332 }
5333 }
5334 else
5335 rettv->vval.v_number = FALSE;
5336}
5337
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338/*
5339 * "getcurpos()" function
5340 */
5341 static void
5342f_getcurpos(typval_T *argvars, typval_T *rettv)
5343{
5344 getpos_both(argvars, rettv, TRUE);
5345}
5346
5347/*
5348 * "getpos(string)" function
5349 */
5350 static void
5351f_getpos(typval_T *argvars, typval_T *rettv)
5352{
5353 getpos_both(argvars, rettv, FALSE);
5354}
5355
5356/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005357 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 */
5359 static void
5360f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5361{
5362#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005363 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364#endif
5365}
5366
5367/*
5368 * "getreg()" function
5369 */
5370 static void
5371f_getreg(typval_T *argvars, typval_T *rettv)
5372{
5373 char_u *strregname;
5374 int regname;
5375 int arg2 = FALSE;
5376 int return_list = FALSE;
5377 int error = FALSE;
5378
5379 if (argvars[0].v_type != VAR_UNKNOWN)
5380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005381 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 error = strregname == NULL;
5383 if (argvars[1].v_type != VAR_UNKNOWN)
5384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005385 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005387 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388 }
5389 }
5390 else
5391 strregname = get_vim_var_str(VV_REG);
5392
5393 if (error)
5394 return;
5395
5396 regname = (strregname == NULL ? '"' : *strregname);
5397 if (regname == 0)
5398 regname = '"';
5399
5400 if (return_list)
5401 {
5402 rettv->v_type = VAR_LIST;
5403 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5404 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5405 if (rettv->vval.v_list == NULL)
5406 (void)rettv_list_alloc(rettv);
5407 else
5408 ++rettv->vval.v_list->lv_refcount;
5409 }
5410 else
5411 {
5412 rettv->v_type = VAR_STRING;
5413 rettv->vval.v_string = get_reg_contents(regname,
5414 arg2 ? GREG_EXPR_SRC : 0);
5415 }
5416}
5417
5418/*
5419 * "getregtype()" function
5420 */
5421 static void
5422f_getregtype(typval_T *argvars, typval_T *rettv)
5423{
5424 char_u *strregname;
5425 int regname;
5426 char_u buf[NUMBUFLEN + 2];
5427 long reglen = 0;
5428
5429 if (argvars[0].v_type != VAR_UNKNOWN)
5430 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005431 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005432 if (strregname == NULL) /* type error; errmsg already given */
5433 {
5434 rettv->v_type = VAR_STRING;
5435 rettv->vval.v_string = NULL;
5436 return;
5437 }
5438 }
5439 else
5440 /* Default to v:register */
5441 strregname = get_vim_var_str(VV_REG);
5442
5443 regname = (strregname == NULL ? '"' : *strregname);
5444 if (regname == 0)
5445 regname = '"';
5446
5447 buf[0] = NUL;
5448 buf[1] = NUL;
5449 switch (get_reg_type(regname, &reglen))
5450 {
5451 case MLINE: buf[0] = 'V'; break;
5452 case MCHAR: buf[0] = 'v'; break;
5453 case MBLOCK:
5454 buf[0] = Ctrl_V;
5455 sprintf((char *)buf + 1, "%ld", reglen + 1);
5456 break;
5457 }
5458 rettv->v_type = VAR_STRING;
5459 rettv->vval.v_string = vim_strsave(buf);
5460}
5461
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005462/*
5463 * Returns information (variables, options, etc.) about a tab page
5464 * as a dictionary.
5465 */
5466 static dict_T *
5467get_tabpage_info(tabpage_T *tp, int tp_idx)
5468{
5469 win_T *wp;
5470 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005471 list_T *l;
5472
5473 dict = dict_alloc();
5474 if (dict == NULL)
5475 return NULL;
5476
Bram Moolenaare0be1672018-07-08 16:50:37 +02005477 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005478
5479 l = list_alloc();
5480 if (l != NULL)
5481 {
5482 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005483 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005484 list_append_number(l, (varnumber_T)wp->w_id);
5485 dict_add_list(dict, "windows", l);
5486 }
5487
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005488 /* Make a reference to tabpage variables */
5489 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005490
5491 return dict;
5492}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005493
5494/*
5495 * "gettabinfo()" function
5496 */
5497 static void
5498f_gettabinfo(typval_T *argvars, typval_T *rettv)
5499{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005500 tabpage_T *tp, *tparg = NULL;
5501 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005502 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005503
5504 if (rettv_list_alloc(rettv) != OK)
5505 return;
5506
5507 if (argvars[0].v_type != VAR_UNKNOWN)
5508 {
5509 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005510 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005511 if (tparg == NULL)
5512 return;
5513 }
5514
5515 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005516 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005517 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005518 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005519 if (tparg != NULL && tp != tparg)
5520 continue;
5521 d = get_tabpage_info(tp, tpnr);
5522 if (d != NULL)
5523 list_append_dict(rettv->vval.v_list, d);
5524 if (tparg != NULL)
5525 return;
5526 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005527}
5528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529/*
5530 * "gettabvar()" function
5531 */
5532 static void
5533f_gettabvar(typval_T *argvars, typval_T *rettv)
5534{
5535 win_T *oldcurwin;
5536 tabpage_T *tp, *oldtabpage;
5537 dictitem_T *v;
5538 char_u *varname;
5539 int done = FALSE;
5540
5541 rettv->v_type = VAR_STRING;
5542 rettv->vval.v_string = NULL;
5543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005544 varname = tv_get_string_chk(&argvars[1]);
5545 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 if (tp != NULL && varname != NULL)
5547 {
5548 /* Set tp to be our tabpage, temporarily. Also set the window to the
5549 * first window in the tabpage, otherwise the window is not valid. */
5550 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005551 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5552 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 {
5554 /* look up the variable */
5555 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5556 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5557 if (v != NULL)
5558 {
5559 copy_tv(&v->di_tv, rettv);
5560 done = TRUE;
5561 }
5562 }
5563
5564 /* restore previous notion of curwin */
5565 restore_win(oldcurwin, oldtabpage, TRUE);
5566 }
5567
5568 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5569 copy_tv(&argvars[2], rettv);
5570}
5571
5572/*
5573 * "gettabwinvar()" function
5574 */
5575 static void
5576f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5577{
5578 getwinvar(argvars, rettv, 1);
5579}
5580
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005581/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005582 * "gettagstack()" function
5583 */
5584 static void
5585f_gettagstack(typval_T *argvars, typval_T *rettv)
5586{
5587 win_T *wp = curwin; // default is current window
5588
5589 if (rettv_dict_alloc(rettv) != OK)
5590 return;
5591
5592 if (argvars[0].v_type != VAR_UNKNOWN)
5593 {
5594 wp = find_win_by_nr_or_id(&argvars[0]);
5595 if (wp == NULL)
5596 return;
5597 }
5598
5599 get_tagstack(wp, rettv->vval.v_dict);
5600}
5601
5602/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005603 * Returns information about a window as a dictionary.
5604 */
5605 static dict_T *
5606get_win_info(win_T *wp, short tpnr, short winnr)
5607{
5608 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005609
5610 dict = dict_alloc();
5611 if (dict == NULL)
5612 return NULL;
5613
Bram Moolenaare0be1672018-07-08 16:50:37 +02005614 dict_add_number(dict, "tabnr", tpnr);
5615 dict_add_number(dict, "winnr", winnr);
5616 dict_add_number(dict, "winid", wp->w_id);
5617 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005618 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005619 dict_add_number(dict, "topline", wp->w_topline);
5620 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005621#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005622 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005623#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005624 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005625 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005626 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005627
Bram Moolenaar69905d12017-08-13 18:14:47 +02005628#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005629 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005630#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005631#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005632 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5633 dict_add_number(dict, "loclist",
5634 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005635#endif
5636
Bram Moolenaar30567352016-08-27 21:25:44 +02005637 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005638 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005639
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640 return dict;
5641}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005642
5643/*
5644 * "getwininfo()" function
5645 */
5646 static void
5647f_getwininfo(typval_T *argvars, typval_T *rettv)
5648{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649 tabpage_T *tp;
5650 win_T *wp = NULL, *wparg = NULL;
5651 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005652 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005653
5654 if (rettv_list_alloc(rettv) != OK)
5655 return;
5656
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005657 if (argvars[0].v_type != VAR_UNKNOWN)
5658 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005659 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660 if (wparg == NULL)
5661 return;
5662 }
5663
5664 /* Collect information about either all the windows across all the tab
5665 * pages or one particular window.
5666 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005667 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005668 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005669 tabnr++;
5670 winnr = 0;
5671 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005672 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005673 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005674 if (wparg != NULL && wp != wparg)
5675 continue;
5676 d = get_win_info(wp, tabnr, winnr);
5677 if (d != NULL)
5678 list_append_dict(rettv->vval.v_list, d);
5679 if (wparg != NULL)
5680 /* found information about a specific window */
5681 return;
5682 }
5683 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005684}
5685
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005686/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005687 * "win_execute()" function
5688 */
5689 static void
5690f_win_execute(typval_T *argvars, typval_T *rettv)
5691{
5692 int id = (int)tv_get_number(argvars);
5693 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005694 win_T *save_curwin;
5695 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005696
5697 if (wp != NULL)
5698 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005699 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5700 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005701 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005702 check_cursor();
5703 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005704 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005705 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005706 }
5707}
5708
5709/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710 * "win_findbuf()" function
5711 */
5712 static void
5713f_win_findbuf(typval_T *argvars, typval_T *rettv)
5714{
5715 if (rettv_list_alloc(rettv) != FAIL)
5716 win_findbuf(argvars, rettv->vval.v_list);
5717}
5718
5719/*
5720 * "win_getid()" function
5721 */
5722 static void
5723f_win_getid(typval_T *argvars, typval_T *rettv)
5724{
5725 rettv->vval.v_number = win_getid(argvars);
5726}
5727
5728/*
5729 * "win_gotoid()" function
5730 */
5731 static void
5732f_win_gotoid(typval_T *argvars, typval_T *rettv)
5733{
5734 rettv->vval.v_number = win_gotoid(argvars);
5735}
5736
5737/*
5738 * "win_id2tabwin()" function
5739 */
5740 static void
5741f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5742{
5743 if (rettv_list_alloc(rettv) != FAIL)
5744 win_id2tabwin(argvars, rettv->vval.v_list);
5745}
5746
5747/*
5748 * "win_id2win()" function
5749 */
5750 static void
5751f_win_id2win(typval_T *argvars, typval_T *rettv)
5752{
5753 rettv->vval.v_number = win_id2win(argvars);
5754}
5755
5756/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005757 * "win_screenpos()" function
5758 */
5759 static void
5760f_win_screenpos(typval_T *argvars, typval_T *rettv)
5761{
5762 win_T *wp;
5763
5764 if (rettv_list_alloc(rettv) == FAIL)
5765 return;
5766
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005767 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005768 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5769 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5770}
5771
5772/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005773 * "getwinpos({timeout})" function
5774 */
5775 static void
5776f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5777{
5778 int x = -1;
5779 int y = -1;
5780
5781 if (rettv_list_alloc(rettv) == FAIL)
5782 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005783#if defined(FEAT_GUI) \
5784 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5785 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005786 {
5787 varnumber_T timeout = 100;
5788
5789 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005790 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005791
5792 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005793 }
5794#endif
5795 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5796 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5797}
5798
5799
5800/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005801 * "getwinposx()" function
5802 */
5803 static void
5804f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5805{
5806 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005807#if defined(FEAT_GUI) \
5808 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5809 || defined(MSWIN)
5810
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005811 {
5812 int x, y;
5813
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005814 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005815 rettv->vval.v_number = x;
5816 }
5817#endif
5818}
5819
5820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 * "getwinposy()" function
5822 */
5823 static void
5824f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5825{
5826 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005827#if defined(FEAT_GUI) \
5828 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5829 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 {
5831 int x, y;
5832
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005833 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005834 rettv->vval.v_number = y;
5835 }
5836#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005837}
5838
5839/*
5840 * "getwinvar()" function
5841 */
5842 static void
5843f_getwinvar(typval_T *argvars, typval_T *rettv)
5844{
5845 getwinvar(argvars, rettv, 0);
5846}
5847
5848/*
5849 * "glob()" function
5850 */
5851 static void
5852f_glob(typval_T *argvars, typval_T *rettv)
5853{
5854 int options = WILD_SILENT|WILD_USE_NL;
5855 expand_T xpc;
5856 int error = FALSE;
5857
5858 /* When the optional second argument is non-zero, don't remove matches
5859 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5860 rettv->v_type = VAR_STRING;
5861 if (argvars[1].v_type != VAR_UNKNOWN)
5862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 options |= WILD_KEEP_ALL;
5865 if (argvars[2].v_type != VAR_UNKNOWN)
5866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005867 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005868 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005870 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871 options |= WILD_ALLLINKS;
5872 }
5873 }
5874 if (!error)
5875 {
5876 ExpandInit(&xpc);
5877 xpc.xp_context = EXPAND_FILES;
5878 if (p_wic)
5879 options += WILD_ICASE;
5880 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 NULL, options, WILD_ALL);
5883 else if (rettv_list_alloc(rettv) != FAIL)
5884 {
5885 int i;
5886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005887 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 NULL, options, WILD_ALL_KEEP);
5889 for (i = 0; i < xpc.xp_numfiles; i++)
5890 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5891
5892 ExpandCleanup(&xpc);
5893 }
5894 }
5895 else
5896 rettv->vval.v_string = NULL;
5897}
5898
5899/*
5900 * "globpath()" function
5901 */
5902 static void
5903f_globpath(typval_T *argvars, typval_T *rettv)
5904{
5905 int flags = 0;
5906 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 int error = FALSE;
5909 garray_T ga;
5910 int i;
5911
5912 /* When the optional second argument is non-zero, don't remove matches
5913 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5914 rettv->v_type = VAR_STRING;
5915 if (argvars[2].v_type != VAR_UNKNOWN)
5916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005917 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 flags |= WILD_KEEP_ALL;
5919 if (argvars[3].v_type != VAR_UNKNOWN)
5920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005921 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005922 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005924 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925 flags |= WILD_ALLLINKS;
5926 }
5927 }
5928 if (file != NULL && !error)
5929 {
5930 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005931 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 if (rettv->v_type == VAR_STRING)
5933 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5934 else if (rettv_list_alloc(rettv) != FAIL)
5935 for (i = 0; i < ga.ga_len; ++i)
5936 list_append_string(rettv->vval.v_list,
5937 ((char_u **)(ga.ga_data))[i], -1);
5938 ga_clear_strings(&ga);
5939 }
5940 else
5941 rettv->vval.v_string = NULL;
5942}
5943
5944/*
5945 * "glob2regpat()" function
5946 */
5947 static void
5948f_glob2regpat(typval_T *argvars, typval_T *rettv)
5949{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005950 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005951
5952 rettv->v_type = VAR_STRING;
5953 rettv->vval.v_string = (pat == NULL)
5954 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5955}
5956
5957/* for VIM_VERSION_ defines */
5958#include "version.h"
5959
5960/*
5961 * "has()" function
5962 */
5963 static void
5964f_has(typval_T *argvars, typval_T *rettv)
5965{
5966 int i;
5967 char_u *name;
5968 int n = FALSE;
5969 static char *(has_list[]) =
5970 {
5971#ifdef AMIGA
5972 "amiga",
5973# ifdef FEAT_ARP
5974 "arp",
5975# endif
5976#endif
5977#ifdef __BEOS__
5978 "beos",
5979#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005980#if defined(BSD) && !defined(MACOS_X)
5981 "bsd",
5982#endif
5983#ifdef hpux
5984 "hpux",
5985#endif
5986#ifdef __linux__
5987 "linux",
5988#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005989#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005990 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5991 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005992# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005993 "macunix", /* Mac OS X, with the darwin feature */
5994 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005995# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996#endif
5997#ifdef __QNX__
5998 "qnx",
5999#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006000#ifdef SUN_SYSTEM
6001 "sun",
6002#else
6003 "moon",
6004#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005#ifdef UNIX
6006 "unix",
6007#endif
6008#ifdef VMS
6009 "vms",
6010#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006011#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 "win32",
6013#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006014#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 "win32unix",
6016#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006017#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 "win64",
6019#endif
6020#ifdef EBCDIC
6021 "ebcdic",
6022#endif
6023#ifndef CASE_INSENSITIVE_FILENAME
6024 "fname_case",
6025#endif
6026#ifdef HAVE_ACL
6027 "acl",
6028#endif
6029#ifdef FEAT_ARABIC
6030 "arabic",
6031#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006033#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006034 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006035#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006036#ifdef FEAT_AUTOSERVERNAME
6037 "autoservername",
6038#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006039#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006041# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 "balloon_multiline",
6043# endif
6044#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006045#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006046 "balloon_eval_term",
6047#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6049 "builtin_terms",
6050# ifdef ALL_BUILTIN_TCAPS
6051 "all_builtin_terms",
6052# endif
6053#endif
6054#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006055 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 || defined(FEAT_GUI_MOTIF))
6057 "browsefilter",
6058#endif
6059#ifdef FEAT_BYTEOFF
6060 "byte_offset",
6061#endif
6062#ifdef FEAT_JOB_CHANNEL
6063 "channel",
6064#endif
6065#ifdef FEAT_CINDENT
6066 "cindent",
6067#endif
6068#ifdef FEAT_CLIENTSERVER
6069 "clientserver",
6070#endif
6071#ifdef FEAT_CLIPBOARD
6072 "clipboard",
6073#endif
6074#ifdef FEAT_CMDL_COMPL
6075 "cmdline_compl",
6076#endif
6077#ifdef FEAT_CMDHIST
6078 "cmdline_hist",
6079#endif
6080#ifdef FEAT_COMMENTS
6081 "comments",
6082#endif
6083#ifdef FEAT_CONCEAL
6084 "conceal",
6085#endif
6086#ifdef FEAT_CRYPT
6087 "cryptv",
6088 "crypt-blowfish",
6089 "crypt-blowfish2",
6090#endif
6091#ifdef FEAT_CSCOPE
6092 "cscope",
6093#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095#ifdef CURSOR_SHAPE
6096 "cursorshape",
6097#endif
6098#ifdef DEBUG
6099 "debug",
6100#endif
6101#ifdef FEAT_CON_DIALOG
6102 "dialog_con",
6103#endif
6104#ifdef FEAT_GUI_DIALOG
6105 "dialog_gui",
6106#endif
6107#ifdef FEAT_DIFF
6108 "diff",
6109#endif
6110#ifdef FEAT_DIGRAPHS
6111 "digraphs",
6112#endif
6113#ifdef FEAT_DIRECTX
6114 "directx",
6115#endif
6116#ifdef FEAT_DND
6117 "dnd",
6118#endif
6119#ifdef FEAT_EMACS_TAGS
6120 "emacs_tags",
6121#endif
6122 "eval", /* always present, of course! */
6123 "ex_extra", /* graduated feature */
6124#ifdef FEAT_SEARCH_EXTRA
6125 "extra_search",
6126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127#ifdef FEAT_SEARCHPATH
6128 "file_in_path",
6129#endif
6130#ifdef FEAT_FILTERPIPE
6131 "filterpipe",
6132#endif
6133#ifdef FEAT_FIND_ID
6134 "find_in_path",
6135#endif
6136#ifdef FEAT_FLOAT
6137 "float",
6138#endif
6139#ifdef FEAT_FOLDING
6140 "folding",
6141#endif
6142#ifdef FEAT_FOOTER
6143 "footer",
6144#endif
6145#if !defined(USE_SYSTEM) && defined(UNIX)
6146 "fork",
6147#endif
6148#ifdef FEAT_GETTEXT
6149 "gettext",
6150#endif
6151#ifdef FEAT_GUI
6152 "gui",
6153#endif
6154#ifdef FEAT_GUI_ATHENA
6155# ifdef FEAT_GUI_NEXTAW
6156 "gui_neXtaw",
6157# else
6158 "gui_athena",
6159# endif
6160#endif
6161#ifdef FEAT_GUI_GTK
6162 "gui_gtk",
6163# ifdef USE_GTK3
6164 "gui_gtk3",
6165# else
6166 "gui_gtk2",
6167# endif
6168#endif
6169#ifdef FEAT_GUI_GNOME
6170 "gui_gnome",
6171#endif
6172#ifdef FEAT_GUI_MAC
6173 "gui_mac",
6174#endif
6175#ifdef FEAT_GUI_MOTIF
6176 "gui_motif",
6177#endif
6178#ifdef FEAT_GUI_PHOTON
6179 "gui_photon",
6180#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006181#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182 "gui_win32",
6183#endif
6184#ifdef FEAT_HANGULIN
6185 "hangul_input",
6186#endif
6187#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6188 "iconv",
6189#endif
6190#ifdef FEAT_INS_EXPAND
6191 "insert_expand",
6192#endif
6193#ifdef FEAT_JOB_CHANNEL
6194 "job",
6195#endif
6196#ifdef FEAT_JUMPLIST
6197 "jumplist",
6198#endif
6199#ifdef FEAT_KEYMAP
6200 "keymap",
6201#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006202 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203#ifdef FEAT_LANGMAP
6204 "langmap",
6205#endif
6206#ifdef FEAT_LIBCALL
6207 "libcall",
6208#endif
6209#ifdef FEAT_LINEBREAK
6210 "linebreak",
6211#endif
6212#ifdef FEAT_LISP
6213 "lispindent",
6214#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216#ifdef FEAT_LOCALMAP
6217 "localmap",
6218#endif
6219#ifdef FEAT_LUA
6220# ifndef DYNAMIC_LUA
6221 "lua",
6222# endif
6223#endif
6224#ifdef FEAT_MENU
6225 "menu",
6226#endif
6227#ifdef FEAT_SESSION
6228 "mksession",
6229#endif
6230#ifdef FEAT_MODIFY_FNAME
6231 "modify_fname",
6232#endif
6233#ifdef FEAT_MOUSE
6234 "mouse",
6235#endif
6236#ifdef FEAT_MOUSESHAPE
6237 "mouseshape",
6238#endif
6239#if defined(UNIX) || defined(VMS)
6240# ifdef FEAT_MOUSE_DEC
6241 "mouse_dec",
6242# endif
6243# ifdef FEAT_MOUSE_GPM
6244 "mouse_gpm",
6245# endif
6246# ifdef FEAT_MOUSE_JSB
6247 "mouse_jsbterm",
6248# endif
6249# ifdef FEAT_MOUSE_NET
6250 "mouse_netterm",
6251# endif
6252# ifdef FEAT_MOUSE_PTERM
6253 "mouse_pterm",
6254# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006255# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "mouse_sgr",
6257# endif
6258# ifdef FEAT_SYSMOUSE
6259 "mouse_sysmouse",
6260# endif
6261# ifdef FEAT_MOUSE_URXVT
6262 "mouse_urxvt",
6263# endif
6264# ifdef FEAT_MOUSE_XTERM
6265 "mouse_xterm",
6266# endif
6267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269#ifdef FEAT_MBYTE_IME
6270 "multi_byte_ime",
6271#endif
6272#ifdef FEAT_MULTI_LANG
6273 "multi_lang",
6274#endif
6275#ifdef FEAT_MZSCHEME
6276#ifndef DYNAMIC_MZSCHEME
6277 "mzscheme",
6278#endif
6279#endif
6280#ifdef FEAT_NUM64
6281 "num64",
6282#endif
6283#ifdef FEAT_OLE
6284 "ole",
6285#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006286#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006288#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289#ifdef FEAT_PATH_EXTRA
6290 "path_extra",
6291#endif
6292#ifdef FEAT_PERL
6293#ifndef DYNAMIC_PERL
6294 "perl",
6295#endif
6296#endif
6297#ifdef FEAT_PERSISTENT_UNDO
6298 "persistent_undo",
6299#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006300#if defined(FEAT_PYTHON)
6301 "python_compiled",
6302# if defined(DYNAMIC_PYTHON)
6303 "python_dynamic",
6304# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006306 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006307# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006309#if defined(FEAT_PYTHON3)
6310 "python3_compiled",
6311# if defined(DYNAMIC_PYTHON3)
6312 "python3_dynamic",
6313# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006315 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006316# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317#endif
6318#ifdef FEAT_POSTSCRIPT
6319 "postscript",
6320#endif
6321#ifdef FEAT_PRINTER
6322 "printer",
6323#endif
6324#ifdef FEAT_PROFILE
6325 "profile",
6326#endif
6327#ifdef FEAT_RELTIME
6328 "reltime",
6329#endif
6330#ifdef FEAT_QUICKFIX
6331 "quickfix",
6332#endif
6333#ifdef FEAT_RIGHTLEFT
6334 "rightleft",
6335#endif
6336#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6337 "ruby",
6338#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340#ifdef FEAT_CMDL_INFO
6341 "showcmd",
6342 "cmdline_info",
6343#endif
6344#ifdef FEAT_SIGNS
6345 "signs",
6346#endif
6347#ifdef FEAT_SMARTINDENT
6348 "smartindent",
6349#endif
6350#ifdef STARTUPTIME
6351 "startuptime",
6352#endif
6353#ifdef FEAT_STL_OPT
6354 "statusline",
6355#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356#ifdef FEAT_NETBEANS_INTG
6357 "netbeans_intg",
6358#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006359#ifdef FEAT_SOUND
6360 "sound",
6361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006362#ifdef FEAT_SPELL
6363 "spell",
6364#endif
6365#ifdef FEAT_SYN_HL
6366 "syntax",
6367#endif
6368#if defined(USE_SYSTEM) || !defined(UNIX)
6369 "system",
6370#endif
6371#ifdef FEAT_TAG_BINS
6372 "tag_binary",
6373#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374#ifdef FEAT_TCL
6375# ifndef DYNAMIC_TCL
6376 "tcl",
6377# endif
6378#endif
6379#ifdef FEAT_TERMGUICOLORS
6380 "termguicolors",
6381#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006382#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006383 "terminal",
6384#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385#ifdef TERMINFO
6386 "terminfo",
6387#endif
6388#ifdef FEAT_TERMRESPONSE
6389 "termresponse",
6390#endif
6391#ifdef FEAT_TEXTOBJ
6392 "textobjects",
6393#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006394#ifdef FEAT_TEXT_PROP
6395 "textprop",
6396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397#ifdef HAVE_TGETENT
6398 "tgetent",
6399#endif
6400#ifdef FEAT_TIMERS
6401 "timers",
6402#endif
6403#ifdef FEAT_TITLE
6404 "title",
6405#endif
6406#ifdef FEAT_TOOLBAR
6407 "toolbar",
6408#endif
6409#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6410 "unnamedplus",
6411#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412 "user-commands", /* was accidentally included in 5.4 */
6413 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006414#ifdef FEAT_VARTABS
6415 "vartabs",
6416#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006417 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418#ifdef FEAT_VIMINFO
6419 "viminfo",
6420#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006421 "vimscript-1",
6422 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006423 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006426 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006428#ifdef FEAT_VTP
6429 "vtp",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#ifdef FEAT_WILDIGN
6432 "wildignore",
6433#endif
6434#ifdef FEAT_WILDMENU
6435 "wildmenu",
6436#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438#ifdef FEAT_WAK
6439 "winaltkeys",
6440#endif
6441#ifdef FEAT_WRITEBACKUP
6442 "writebackup",
6443#endif
6444#ifdef FEAT_XIM
6445 "xim",
6446#endif
6447#ifdef FEAT_XFONTSET
6448 "xfontset",
6449#endif
6450#ifdef FEAT_XPM_W32
6451 "xpm",
6452 "xpm_w32", /* for backward compatibility */
6453#else
6454# if defined(HAVE_XPM)
6455 "xpm",
6456# endif
6457#endif
6458#ifdef USE_XSMP
6459 "xsmp",
6460#endif
6461#ifdef USE_XSMP_INTERACT
6462 "xsmp_interact",
6463#endif
6464#ifdef FEAT_XCLIPBOARD
6465 "xterm_clipboard",
6466#endif
6467#ifdef FEAT_XTERM_SAVE
6468 "xterm_save",
6469#endif
6470#if defined(UNIX) && defined(FEAT_X11)
6471 "X11",
6472#endif
6473 NULL
6474 };
6475
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006476 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 for (i = 0; has_list[i] != NULL; ++i)
6478 if (STRICMP(name, has_list[i]) == 0)
6479 {
6480 n = TRUE;
6481 break;
6482 }
6483
6484 if (n == FALSE)
6485 {
6486 if (STRNICMP(name, "patch", 5) == 0)
6487 {
6488 if (name[5] == '-'
6489 && STRLEN(name) >= 11
6490 && vim_isdigit(name[6])
6491 && vim_isdigit(name[8])
6492 && vim_isdigit(name[10]))
6493 {
6494 int major = atoi((char *)name + 6);
6495 int minor = atoi((char *)name + 8);
6496
6497 /* Expect "patch-9.9.01234". */
6498 n = (major < VIM_VERSION_MAJOR
6499 || (major == VIM_VERSION_MAJOR
6500 && (minor < VIM_VERSION_MINOR
6501 || (minor == VIM_VERSION_MINOR
6502 && has_patch(atoi((char *)name + 10))))));
6503 }
6504 else
6505 n = has_patch(atoi((char *)name + 5));
6506 }
6507 else if (STRICMP(name, "vim_starting") == 0)
6508 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006509 else if (STRICMP(name, "ttyin") == 0)
6510 n = mch_input_isatty();
6511 else if (STRICMP(name, "ttyout") == 0)
6512 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 else if (STRICMP(name, "multi_byte_encoding") == 0)
6514 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006515#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 else if (STRICMP(name, "balloon_multiline") == 0)
6517 n = multiline_balloon_available();
6518#endif
6519#ifdef DYNAMIC_TCL
6520 else if (STRICMP(name, "tcl") == 0)
6521 n = tcl_enabled(FALSE);
6522#endif
6523#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6524 else if (STRICMP(name, "iconv") == 0)
6525 n = iconv_enabled(FALSE);
6526#endif
6527#ifdef DYNAMIC_LUA
6528 else if (STRICMP(name, "lua") == 0)
6529 n = lua_enabled(FALSE);
6530#endif
6531#ifdef DYNAMIC_MZSCHEME
6532 else if (STRICMP(name, "mzscheme") == 0)
6533 n = mzscheme_enabled(FALSE);
6534#endif
6535#ifdef DYNAMIC_RUBY
6536 else if (STRICMP(name, "ruby") == 0)
6537 n = ruby_enabled(FALSE);
6538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef DYNAMIC_PYTHON
6540 else if (STRICMP(name, "python") == 0)
6541 n = python_enabled(FALSE);
6542#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543#ifdef DYNAMIC_PYTHON3
6544 else if (STRICMP(name, "python3") == 0)
6545 n = python3_enabled(FALSE);
6546#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006547#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6548 else if (STRICMP(name, "pythonx") == 0)
6549 {
6550# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6551 if (p_pyx == 0)
6552 n = python3_enabled(FALSE) || python_enabled(FALSE);
6553 else if (p_pyx == 3)
6554 n = python3_enabled(FALSE);
6555 else if (p_pyx == 2)
6556 n = python_enabled(FALSE);
6557# elif defined(DYNAMIC_PYTHON)
6558 n = python_enabled(FALSE);
6559# elif defined(DYNAMIC_PYTHON3)
6560 n = python3_enabled(FALSE);
6561# endif
6562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563#endif
6564#ifdef DYNAMIC_PERL
6565 else if (STRICMP(name, "perl") == 0)
6566 n = perl_enabled(FALSE);
6567#endif
6568#ifdef FEAT_GUI
6569 else if (STRICMP(name, "gui_running") == 0)
6570 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571# ifdef FEAT_BROWSE
6572 else if (STRICMP(name, "browse") == 0)
6573 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6574# endif
6575#endif
6576#ifdef FEAT_SYN_HL
6577 else if (STRICMP(name, "syntax_items") == 0)
6578 n = syntax_present(curwin);
6579#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006580#ifdef FEAT_VTP
6581 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006582 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#endif
6584#ifdef FEAT_NETBEANS_INTG
6585 else if (STRICMP(name, "netbeans_enabled") == 0)
6586 n = netbeans_active();
6587#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006588#ifdef FEAT_MOUSE_GPM
6589 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6590 n = gpm_enabled();
6591#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006592#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006593 else if (STRICMP(name, "terminal") == 0)
6594 n = terminal_enabled();
6595#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006596#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006597 else if (STRICMP(name, "conpty") == 0)
6598 n = use_conpty();
6599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 }
6601
6602 rettv->vval.v_number = n;
6603}
6604
6605/*
6606 * "has_key()" function
6607 */
6608 static void
6609f_has_key(typval_T *argvars, typval_T *rettv)
6610{
6611 if (argvars[0].v_type != VAR_DICT)
6612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006613 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 return;
6615 }
6616 if (argvars[0].vval.v_dict == NULL)
6617 return;
6618
6619 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006620 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621}
6622
6623/*
6624 * "haslocaldir()" function
6625 */
6626 static void
6627f_haslocaldir(typval_T *argvars, typval_T *rettv)
6628{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006629 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 win_T *wp = NULL;
6631
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006632 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6633
6634 // Check for window-local and tab-local directories
6635 if (wp != NULL && wp->w_localdir != NULL)
6636 rettv->vval.v_number = 1;
6637 else if (tp != NULL && tp->tp_localdir != NULL)
6638 rettv->vval.v_number = 2;
6639 else
6640 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641}
6642
6643/*
6644 * "hasmapto()" function
6645 */
6646 static void
6647f_hasmapto(typval_T *argvars, typval_T *rettv)
6648{
6649 char_u *name;
6650 char_u *mode;
6651 char_u buf[NUMBUFLEN];
6652 int abbr = FALSE;
6653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006654 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 if (argvars[1].v_type == VAR_UNKNOWN)
6656 mode = (char_u *)"nvo";
6657 else
6658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006659 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006661 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 }
6663
6664 if (map_to_exists(name, mode, abbr))
6665 rettv->vval.v_number = TRUE;
6666 else
6667 rettv->vval.v_number = FALSE;
6668}
6669
6670/*
6671 * "histadd()" function
6672 */
6673 static void
6674f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6675{
6676#ifdef FEAT_CMDHIST
6677 int histype;
6678 char_u *str;
6679 char_u buf[NUMBUFLEN];
6680#endif
6681
6682 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006683 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684 return;
6685#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006686 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687 histype = str != NULL ? get_histtype(str) : -1;
6688 if (histype >= 0)
6689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006690 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 if (*str != NUL)
6692 {
6693 init_history();
6694 add_to_history(histype, str, FALSE, NUL);
6695 rettv->vval.v_number = TRUE;
6696 return;
6697 }
6698 }
6699#endif
6700}
6701
6702/*
6703 * "histdel()" function
6704 */
6705 static void
6706f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6707{
6708#ifdef FEAT_CMDHIST
6709 int n;
6710 char_u buf[NUMBUFLEN];
6711 char_u *str;
6712
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006713 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 if (str == NULL)
6715 n = 0;
6716 else if (argvars[1].v_type == VAR_UNKNOWN)
6717 /* only one argument: clear entire history */
6718 n = clr_history(get_histtype(str));
6719 else if (argvars[1].v_type == VAR_NUMBER)
6720 /* index given: remove that entry */
6721 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006722 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723 else
6724 /* string given: remove all matching entries */
6725 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006726 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 rettv->vval.v_number = n;
6728#endif
6729}
6730
6731/*
6732 * "histget()" function
6733 */
6734 static void
6735f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6736{
6737#ifdef FEAT_CMDHIST
6738 int type;
6739 int idx;
6740 char_u *str;
6741
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006742 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 if (str == NULL)
6744 rettv->vval.v_string = NULL;
6745 else
6746 {
6747 type = get_histtype(str);
6748 if (argvars[1].v_type == VAR_UNKNOWN)
6749 idx = get_history_idx(type);
6750 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006751 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752 /* -1 on type error */
6753 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6754 }
6755#else
6756 rettv->vval.v_string = NULL;
6757#endif
6758 rettv->v_type = VAR_STRING;
6759}
6760
6761/*
6762 * "histnr()" function
6763 */
6764 static void
6765f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6766{
6767 int i;
6768
6769#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006770 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771
6772 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6773 if (i >= HIST_CMD && i < HIST_COUNT)
6774 i = get_history_idx(i);
6775 else
6776#endif
6777 i = -1;
6778 rettv->vval.v_number = i;
6779}
6780
6781/*
6782 * "highlightID(name)" function
6783 */
6784 static void
6785f_hlID(typval_T *argvars, typval_T *rettv)
6786{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006787 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788}
6789
6790/*
6791 * "highlight_exists()" function
6792 */
6793 static void
6794f_hlexists(typval_T *argvars, typval_T *rettv)
6795{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006796 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797}
6798
6799/*
6800 * "hostname()" function
6801 */
6802 static void
6803f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6804{
6805 char_u hostname[256];
6806
6807 mch_get_host_name(hostname, 256);
6808 rettv->v_type = VAR_STRING;
6809 rettv->vval.v_string = vim_strsave(hostname);
6810}
6811
6812/*
6813 * iconv() function
6814 */
6815 static void
6816f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6817{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 char_u buf1[NUMBUFLEN];
6819 char_u buf2[NUMBUFLEN];
6820 char_u *from, *to, *str;
6821 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822
6823 rettv->v_type = VAR_STRING;
6824 rettv->vval.v_string = NULL;
6825
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006826 str = tv_get_string(&argvars[0]);
6827 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6828 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006829 vimconv.vc_type = CONV_NONE;
6830 convert_setup(&vimconv, from, to);
6831
6832 /* If the encodings are equal, no conversion needed. */
6833 if (vimconv.vc_type == CONV_NONE)
6834 rettv->vval.v_string = vim_strsave(str);
6835 else
6836 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6837
6838 convert_setup(&vimconv, NULL, NULL);
6839 vim_free(from);
6840 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841}
6842
6843/*
6844 * "indent()" function
6845 */
6846 static void
6847f_indent(typval_T *argvars, typval_T *rettv)
6848{
6849 linenr_T lnum;
6850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006851 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6853 rettv->vval.v_number = get_indent_lnum(lnum);
6854 else
6855 rettv->vval.v_number = -1;
6856}
6857
6858/*
6859 * "index()" function
6860 */
6861 static void
6862f_index(typval_T *argvars, typval_T *rettv)
6863{
6864 list_T *l;
6865 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006866 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 long idx = 0;
6868 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006869 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870
6871 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006872 if (argvars[0].v_type == VAR_BLOB)
6873 {
6874 typval_T tv;
6875 int start = 0;
6876
6877 if (argvars[2].v_type != VAR_UNKNOWN)
6878 {
6879 start = tv_get_number_chk(&argvars[2], &error);
6880 if (error)
6881 return;
6882 }
6883 b = argvars[0].vval.v_blob;
6884 if (b == NULL)
6885 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006886 if (start < 0)
6887 {
6888 start = blob_len(b) + start;
6889 if (start < 0)
6890 start = 0;
6891 }
6892
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006893 for (idx = start; idx < blob_len(b); ++idx)
6894 {
6895 tv.v_type = VAR_NUMBER;
6896 tv.vval.v_number = blob_get(b, idx);
6897 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6898 {
6899 rettv->vval.v_number = idx;
6900 return;
6901 }
6902 }
6903 return;
6904 }
6905 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006907 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 return;
6909 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 l = argvars[0].vval.v_list;
6912 if (l != NULL)
6913 {
6914 item = l->lv_first;
6915 if (argvars[2].v_type != VAR_UNKNOWN)
6916 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 /* Start at specified item. Use the cached index that list_find()
6918 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 idx = l->lv_idx;
6921 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 if (error)
6924 item = NULL;
6925 }
6926
6927 for ( ; item != NULL; item = item->li_next, ++idx)
6928 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6929 {
6930 rettv->vval.v_number = idx;
6931 break;
6932 }
6933 }
6934}
6935
6936static int inputsecret_flag = 0;
6937
6938/*
6939 * "input()" function
6940 * Also handles inputsecret() when inputsecret is set.
6941 */
6942 static void
6943f_input(typval_T *argvars, typval_T *rettv)
6944{
6945 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6946}
6947
6948/*
6949 * "inputdialog()" function
6950 */
6951 static void
6952f_inputdialog(typval_T *argvars, typval_T *rettv)
6953{
6954#if defined(FEAT_GUI_TEXTDIALOG)
6955 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6956 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6957 {
6958 char_u *message;
6959 char_u buf[NUMBUFLEN];
6960 char_u *defstr = (char_u *)"";
6961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006962 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006964 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6966 else
6967 IObuff[0] = NUL;
6968 if (message != NULL && defstr != NULL
6969 && do_dialog(VIM_QUESTION, NULL, message,
6970 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6971 rettv->vval.v_string = vim_strsave(IObuff);
6972 else
6973 {
6974 if (message != NULL && defstr != NULL
6975 && argvars[1].v_type != VAR_UNKNOWN
6976 && argvars[2].v_type != VAR_UNKNOWN)
6977 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006978 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 else
6980 rettv->vval.v_string = NULL;
6981 }
6982 rettv->v_type = VAR_STRING;
6983 }
6984 else
6985#endif
6986 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6987}
6988
6989/*
6990 * "inputlist()" function
6991 */
6992 static void
6993f_inputlist(typval_T *argvars, typval_T *rettv)
6994{
6995 listitem_T *li;
6996 int selected;
6997 int mouse_used;
6998
6999#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007000 /* While starting up, there is no place to enter text. When running tests
7001 * with --not-a-term we assume feedkeys() will be used. */
7002 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003 return;
7004#endif
7005 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007007 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 return;
7009 }
7010
7011 msg_start();
7012 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7013 lines_left = Rows; /* avoid more prompt */
7014 msg_scroll = TRUE;
7015 msg_clr_eos();
7016
7017 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7018 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007019 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 msg_putchar('\n');
7021 }
7022
7023 /* Ask for choice. */
7024 selected = prompt_for_number(&mouse_used);
7025 if (mouse_used)
7026 selected -= lines_left;
7027
7028 rettv->vval.v_number = selected;
7029}
7030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7032
7033/*
7034 * "inputrestore()" function
7035 */
7036 static void
7037f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7038{
7039 if (ga_userinput.ga_len > 0)
7040 {
7041 --ga_userinput.ga_len;
7042 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7043 + ga_userinput.ga_len);
7044 /* default return is zero == OK */
7045 }
7046 else if (p_verbose > 1)
7047 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007048 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 rettv->vval.v_number = 1; /* Failed */
7050 }
7051}
7052
7053/*
7054 * "inputsave()" function
7055 */
7056 static void
7057f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7058{
7059 /* Add an entry to the stack of typeahead storage. */
7060 if (ga_grow(&ga_userinput, 1) == OK)
7061 {
7062 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7063 + ga_userinput.ga_len);
7064 ++ga_userinput.ga_len;
7065 /* default return is zero == OK */
7066 }
7067 else
7068 rettv->vval.v_number = 1; /* Failed */
7069}
7070
7071/*
7072 * "inputsecret()" function
7073 */
7074 static void
7075f_inputsecret(typval_T *argvars, typval_T *rettv)
7076{
7077 ++cmdline_star;
7078 ++inputsecret_flag;
7079 f_input(argvars, rettv);
7080 --cmdline_star;
7081 --inputsecret_flag;
7082}
7083
7084/*
7085 * "insert()" function
7086 */
7087 static void
7088f_insert(typval_T *argvars, typval_T *rettv)
7089{
7090 long before = 0;
7091 listitem_T *item;
7092 list_T *l;
7093 int error = FALSE;
7094
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007095 if (argvars[0].v_type == VAR_BLOB)
7096 {
7097 int val, len;
7098 char_u *p;
7099
7100 len = blob_len(argvars[0].vval.v_blob);
7101 if (argvars[2].v_type != VAR_UNKNOWN)
7102 {
7103 before = (long)tv_get_number_chk(&argvars[2], &error);
7104 if (error)
7105 return; // type error; errmsg already given
7106 if (before < 0 || before > len)
7107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007108 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007109 return;
7110 }
7111 }
7112 val = tv_get_number_chk(&argvars[1], &error);
7113 if (error)
7114 return;
7115 if (val < 0 || val > 255)
7116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007117 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007118 return;
7119 }
7120
7121 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7122 return;
7123 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7124 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7125 *(p + before) = val;
7126 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7127
7128 copy_tv(&argvars[0], rettv);
7129 }
7130 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007131 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007132 else if ((l = argvars[0].vval.v_list) != NULL
7133 && !var_check_lock(l->lv_lock,
7134 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 {
7136 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007137 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 if (error)
7139 return; /* type error; errmsg already given */
7140
7141 if (before == l->lv_len)
7142 item = NULL;
7143 else
7144 {
7145 item = list_find(l, before);
7146 if (item == NULL)
7147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007148 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 l = NULL;
7150 }
7151 }
7152 if (l != NULL)
7153 {
7154 list_insert_tv(l, &argvars[1], item);
7155 copy_tv(&argvars[0], rettv);
7156 }
7157 }
7158}
7159
7160/*
7161 * "invert(expr)" function
7162 */
7163 static void
7164f_invert(typval_T *argvars, typval_T *rettv)
7165{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007166 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167}
7168
7169/*
7170 * "isdirectory()" function
7171 */
7172 static void
7173f_isdirectory(typval_T *argvars, typval_T *rettv)
7174{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007175 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007176}
7177
7178/*
7179 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7180 * or it refers to a List or Dictionary that is locked.
7181 */
7182 static int
7183tv_islocked(typval_T *tv)
7184{
7185 return (tv->v_lock & VAR_LOCKED)
7186 || (tv->v_type == VAR_LIST
7187 && tv->vval.v_list != NULL
7188 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7189 || (tv->v_type == VAR_DICT
7190 && tv->vval.v_dict != NULL
7191 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7192}
7193
7194/*
7195 * "islocked()" function
7196 */
7197 static void
7198f_islocked(typval_T *argvars, typval_T *rettv)
7199{
7200 lval_T lv;
7201 char_u *end;
7202 dictitem_T *di;
7203
7204 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007205 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007206 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 if (end != NULL && lv.ll_name != NULL)
7208 {
7209 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007210 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 else
7212 {
7213 if (lv.ll_tv == NULL)
7214 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007215 di = find_var(lv.ll_name, NULL, TRUE);
7216 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007218 /* Consider a variable locked when:
7219 * 1. the variable itself is locked
7220 * 2. the value of the variable is locked.
7221 * 3. the List or Dict value is locked.
7222 */
7223 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7224 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225 }
7226 }
7227 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007228 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007230 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 else if (lv.ll_list != NULL)
7232 /* List item. */
7233 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7234 else
7235 /* Dictionary item. */
7236 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7237 }
7238 }
7239
7240 clear_lval(&lv);
7241}
7242
7243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7244/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007245 * "isinf()" function
7246 */
7247 static void
7248f_isinf(typval_T *argvars, typval_T *rettv)
7249{
7250 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7251 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7252}
7253
7254/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255 * "isnan()" function
7256 */
7257 static void
7258f_isnan(typval_T *argvars, typval_T *rettv)
7259{
7260 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7261 && isnan(argvars[0].vval.v_float);
7262}
7263#endif
7264
7265/*
7266 * "items(dict)" function
7267 */
7268 static void
7269f_items(typval_T *argvars, typval_T *rettv)
7270{
7271 dict_list(argvars, rettv, 2);
7272}
7273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274/*
7275 * "join()" function
7276 */
7277 static void
7278f_join(typval_T *argvars, typval_T *rettv)
7279{
7280 garray_T ga;
7281 char_u *sep;
7282
7283 if (argvars[0].v_type != VAR_LIST)
7284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007285 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 return;
7287 }
7288 if (argvars[0].vval.v_list == NULL)
7289 return;
7290 if (argvars[1].v_type == VAR_UNKNOWN)
7291 sep = (char_u *)" ";
7292 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007293 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294
7295 rettv->v_type = VAR_STRING;
7296
7297 if (sep != NULL)
7298 {
7299 ga_init2(&ga, (int)sizeof(char), 80);
7300 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7301 ga_append(&ga, NUL);
7302 rettv->vval.v_string = (char_u *)ga.ga_data;
7303 }
7304 else
7305 rettv->vval.v_string = NULL;
7306}
7307
7308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 * "keys()" function
7310 */
7311 static void
7312f_keys(typval_T *argvars, typval_T *rettv)
7313{
7314 dict_list(argvars, rettv, 0);
7315}
7316
7317/*
7318 * "last_buffer_nr()" function.
7319 */
7320 static void
7321f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7322{
7323 int n = 0;
7324 buf_T *buf;
7325
Bram Moolenaar29323592016-07-24 22:04:11 +02007326 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 if (n < buf->b_fnum)
7328 n = buf->b_fnum;
7329
7330 rettv->vval.v_number = n;
7331}
7332
7333/*
7334 * "len()" function
7335 */
7336 static void
7337f_len(typval_T *argvars, typval_T *rettv)
7338{
7339 switch (argvars[0].v_type)
7340 {
7341 case VAR_STRING:
7342 case VAR_NUMBER:
7343 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007344 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007346 case VAR_BLOB:
7347 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7348 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 case VAR_LIST:
7350 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7351 break;
7352 case VAR_DICT:
7353 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7354 break;
7355 case VAR_UNKNOWN:
7356 case VAR_SPECIAL:
7357 case VAR_FLOAT:
7358 case VAR_FUNC:
7359 case VAR_PARTIAL:
7360 case VAR_JOB:
7361 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 break;
7364 }
7365}
7366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007368libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369{
7370#ifdef FEAT_LIBCALL
7371 char_u *string_in;
7372 char_u **string_result;
7373 int nr_result;
7374#endif
7375
7376 rettv->v_type = type;
7377 if (type != VAR_NUMBER)
7378 rettv->vval.v_string = NULL;
7379
7380 if (check_restricted() || check_secure())
7381 return;
7382
7383#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007384 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7386 {
7387 string_in = NULL;
7388 if (argvars[2].v_type == VAR_STRING)
7389 string_in = argvars[2].vval.v_string;
7390 if (type == VAR_NUMBER)
7391 string_result = NULL;
7392 else
7393 string_result = &rettv->vval.v_string;
7394 if (mch_libcall(argvars[0].vval.v_string,
7395 argvars[1].vval.v_string,
7396 string_in,
7397 argvars[2].vval.v_number,
7398 string_result,
7399 &nr_result) == OK
7400 && type == VAR_NUMBER)
7401 rettv->vval.v_number = nr_result;
7402 }
7403#endif
7404}
7405
7406/*
7407 * "libcall()" function
7408 */
7409 static void
7410f_libcall(typval_T *argvars, typval_T *rettv)
7411{
7412 libcall_common(argvars, rettv, VAR_STRING);
7413}
7414
7415/*
7416 * "libcallnr()" function
7417 */
7418 static void
7419f_libcallnr(typval_T *argvars, typval_T *rettv)
7420{
7421 libcall_common(argvars, rettv, VAR_NUMBER);
7422}
7423
7424/*
7425 * "line(string)" function
7426 */
7427 static void
7428f_line(typval_T *argvars, typval_T *rettv)
7429{
7430 linenr_T lnum = 0;
7431 pos_T *fp;
7432 int fnum;
7433
7434 fp = var2fpos(&argvars[0], TRUE, &fnum);
7435 if (fp != NULL)
7436 lnum = fp->lnum;
7437 rettv->vval.v_number = lnum;
7438}
7439
7440/*
7441 * "line2byte(lnum)" function
7442 */
7443 static void
7444f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7445{
7446#ifndef FEAT_BYTEOFF
7447 rettv->vval.v_number = -1;
7448#else
7449 linenr_T lnum;
7450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007451 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7453 rettv->vval.v_number = -1;
7454 else
7455 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7456 if (rettv->vval.v_number >= 0)
7457 ++rettv->vval.v_number;
7458#endif
7459}
7460
7461/*
7462 * "lispindent(lnum)" function
7463 */
7464 static void
7465f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7466{
7467#ifdef FEAT_LISP
7468 pos_T pos;
7469 linenr_T lnum;
7470
7471 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007472 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7474 {
7475 curwin->w_cursor.lnum = lnum;
7476 rettv->vval.v_number = get_lisp_indent();
7477 curwin->w_cursor = pos;
7478 }
7479 else
7480#endif
7481 rettv->vval.v_number = -1;
7482}
7483
7484/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007485 * "list2str()" function
7486 */
7487 static void
7488f_list2str(typval_T *argvars, typval_T *rettv)
7489{
7490 list_T *l;
7491 listitem_T *li;
7492 garray_T ga;
7493 int utf8 = FALSE;
7494
7495 rettv->v_type = VAR_STRING;
7496 rettv->vval.v_string = NULL;
7497 if (argvars[0].v_type != VAR_LIST)
7498 {
7499 emsg(_(e_invarg));
7500 return;
7501 }
7502
7503 l = argvars[0].vval.v_list;
7504 if (l == NULL)
7505 return; // empty list results in empty string
7506
7507 if (argvars[1].v_type != VAR_UNKNOWN)
7508 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7509
7510 ga_init2(&ga, 1, 80);
7511 if (has_mbyte || utf8)
7512 {
7513 char_u buf[MB_MAXBYTES + 1];
7514 int (*char2bytes)(int, char_u *);
7515
7516 if (utf8 || enc_utf8)
7517 char2bytes = utf_char2bytes;
7518 else
7519 char2bytes = mb_char2bytes;
7520
7521 for (li = l->lv_first; li != NULL; li = li->li_next)
7522 {
7523 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7524 ga_concat(&ga, buf);
7525 }
7526 ga_append(&ga, NUL);
7527 }
7528 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7529 {
7530 for (li = l->lv_first; li != NULL; li = li->li_next)
7531 ga_append(&ga, tv_get_number(&li->li_tv));
7532 ga_append(&ga, NUL);
7533 }
7534
7535 rettv->v_type = VAR_STRING;
7536 rettv->vval.v_string = ga.ga_data;
7537}
7538
7539/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007540 * "localtime()" function
7541 */
7542 static void
7543f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7544{
7545 rettv->vval.v_number = (varnumber_T)time(NULL);
7546}
7547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 static void
7549get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7550{
7551 char_u *keys;
7552 char_u *which;
7553 char_u buf[NUMBUFLEN];
7554 char_u *keys_buf = NULL;
7555 char_u *rhs;
7556 int mode;
7557 int abbr = FALSE;
7558 int get_dict = FALSE;
7559 mapblock_T *mp;
7560 int buffer_local;
7561
7562 /* return empty string for failure */
7563 rettv->v_type = VAR_STRING;
7564 rettv->vval.v_string = NULL;
7565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567 if (*keys == NUL)
7568 return;
7569
7570 if (argvars[1].v_type != VAR_UNKNOWN)
7571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007572 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 if (argvars[2].v_type != VAR_UNKNOWN)
7574 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007575 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 }
7579 }
7580 else
7581 which = (char_u *)"";
7582 if (which == NULL)
7583 return;
7584
7585 mode = get_map_mode(&which, 0);
7586
7587 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7588 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7589 vim_free(keys_buf);
7590
7591 if (!get_dict)
7592 {
7593 /* Return a string. */
7594 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007595 {
7596 if (*rhs == NUL)
7597 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7598 else
7599 rettv->vval.v_string = str2special_save(rhs, FALSE);
7600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601
7602 }
7603 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7604 {
7605 /* Return a dictionary. */
7606 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7607 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7608 dict_T *dict = rettv->vval.v_dict;
7609
Bram Moolenaare0be1672018-07-08 16:50:37 +02007610 dict_add_string(dict, "lhs", lhs);
7611 dict_add_string(dict, "rhs", mp->m_orig_str);
7612 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7613 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7614 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007615 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7616 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007617 dict_add_number(dict, "buffer", (long)buffer_local);
7618 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7619 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620
7621 vim_free(lhs);
7622 vim_free(mapmode);
7623 }
7624}
7625
7626#ifdef FEAT_FLOAT
7627/*
7628 * "log()" function
7629 */
7630 static void
7631f_log(typval_T *argvars, typval_T *rettv)
7632{
7633 float_T f = 0.0;
7634
7635 rettv->v_type = VAR_FLOAT;
7636 if (get_float_arg(argvars, &f) == OK)
7637 rettv->vval.v_float = log(f);
7638 else
7639 rettv->vval.v_float = 0.0;
7640}
7641
7642/*
7643 * "log10()" function
7644 */
7645 static void
7646f_log10(typval_T *argvars, typval_T *rettv)
7647{
7648 float_T f = 0.0;
7649
7650 rettv->v_type = VAR_FLOAT;
7651 if (get_float_arg(argvars, &f) == OK)
7652 rettv->vval.v_float = log10(f);
7653 else
7654 rettv->vval.v_float = 0.0;
7655}
7656#endif
7657
7658#ifdef FEAT_LUA
7659/*
7660 * "luaeval()" function
7661 */
7662 static void
7663f_luaeval(typval_T *argvars, typval_T *rettv)
7664{
7665 char_u *str;
7666 char_u buf[NUMBUFLEN];
7667
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007668 if (check_restricted() || check_secure())
7669 return;
7670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007671 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 do_luaeval(str, argvars + 1, rettv);
7673}
7674#endif
7675
7676/*
7677 * "map()" function
7678 */
7679 static void
7680f_map(typval_T *argvars, typval_T *rettv)
7681{
7682 filter_map(argvars, rettv, TRUE);
7683}
7684
7685/*
7686 * "maparg()" function
7687 */
7688 static void
7689f_maparg(typval_T *argvars, typval_T *rettv)
7690{
7691 get_maparg(argvars, rettv, TRUE);
7692}
7693
7694/*
7695 * "mapcheck()" function
7696 */
7697 static void
7698f_mapcheck(typval_T *argvars, typval_T *rettv)
7699{
7700 get_maparg(argvars, rettv, FALSE);
7701}
7702
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007703typedef enum
7704{
7705 MATCH_END, /* matchend() */
7706 MATCH_MATCH, /* match() */
7707 MATCH_STR, /* matchstr() */
7708 MATCH_LIST, /* matchlist() */
7709 MATCH_POS /* matchstrpos() */
7710} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711
7712 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007713find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714{
7715 char_u *str = NULL;
7716 long len = 0;
7717 char_u *expr = NULL;
7718 char_u *pat;
7719 regmatch_T regmatch;
7720 char_u patbuf[NUMBUFLEN];
7721 char_u strbuf[NUMBUFLEN];
7722 char_u *save_cpo;
7723 long start = 0;
7724 long nth = 1;
7725 colnr_T startcol = 0;
7726 int match = 0;
7727 list_T *l = NULL;
7728 listitem_T *li = NULL;
7729 long idx = 0;
7730 char_u *tofree = NULL;
7731
7732 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7733 save_cpo = p_cpo;
7734 p_cpo = (char_u *)"";
7735
7736 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007737 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007739 /* type MATCH_LIST: return empty list when there are no matches.
7740 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 if (rettv_list_alloc(rettv) == FAIL)
7742 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007743 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 && (list_append_string(rettv->vval.v_list,
7745 (char_u *)"", 0) == FAIL
7746 || list_append_number(rettv->vval.v_list,
7747 (varnumber_T)-1) == FAIL
7748 || list_append_number(rettv->vval.v_list,
7749 (varnumber_T)-1) == FAIL
7750 || list_append_number(rettv->vval.v_list,
7751 (varnumber_T)-1) == FAIL))
7752 {
7753 list_free(rettv->vval.v_list);
7754 rettv->vval.v_list = NULL;
7755 goto theend;
7756 }
7757 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007758 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 {
7760 rettv->v_type = VAR_STRING;
7761 rettv->vval.v_string = NULL;
7762 }
7763
7764 if (argvars[0].v_type == VAR_LIST)
7765 {
7766 if ((l = argvars[0].vval.v_list) == NULL)
7767 goto theend;
7768 li = l->lv_first;
7769 }
7770 else
7771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007772 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773 len = (long)STRLEN(str);
7774 }
7775
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 if (pat == NULL)
7778 goto theend;
7779
7780 if (argvars[2].v_type != VAR_UNKNOWN)
7781 {
7782 int error = FALSE;
7783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007784 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 if (error)
7786 goto theend;
7787 if (l != NULL)
7788 {
7789 li = list_find(l, start);
7790 if (li == NULL)
7791 goto theend;
7792 idx = l->lv_idx; /* use the cached index */
7793 }
7794 else
7795 {
7796 if (start < 0)
7797 start = 0;
7798 if (start > len)
7799 goto theend;
7800 /* When "count" argument is there ignore matches before "start",
7801 * otherwise skip part of the string. Differs when pattern is "^"
7802 * or "\<". */
7803 if (argvars[3].v_type != VAR_UNKNOWN)
7804 startcol = start;
7805 else
7806 {
7807 str += start;
7808 len -= start;
7809 }
7810 }
7811
7812 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007813 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (error)
7815 goto theend;
7816 }
7817
7818 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7819 if (regmatch.regprog != NULL)
7820 {
7821 regmatch.rm_ic = p_ic;
7822
7823 for (;;)
7824 {
7825 if (l != NULL)
7826 {
7827 if (li == NULL)
7828 {
7829 match = FALSE;
7830 break;
7831 }
7832 vim_free(tofree);
7833 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7834 if (str == NULL)
7835 break;
7836 }
7837
7838 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7839
7840 if (match && --nth <= 0)
7841 break;
7842 if (l == NULL && !match)
7843 break;
7844
7845 /* Advance to just after the match. */
7846 if (l != NULL)
7847 {
7848 li = li->li_next;
7849 ++idx;
7850 }
7851 else
7852 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 startcol = (colnr_T)(regmatch.startp[0]
7854 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 if (startcol > (colnr_T)len
7856 || str + startcol <= regmatch.startp[0])
7857 {
7858 match = FALSE;
7859 break;
7860 }
7861 }
7862 }
7863
7864 if (match)
7865 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007866 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 {
7868 listitem_T *li1 = rettv->vval.v_list->lv_first;
7869 listitem_T *li2 = li1->li_next;
7870 listitem_T *li3 = li2->li_next;
7871 listitem_T *li4 = li3->li_next;
7872
7873 vim_free(li1->li_tv.vval.v_string);
7874 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7875 (int)(regmatch.endp[0] - regmatch.startp[0]));
7876 li3->li_tv.vval.v_number =
7877 (varnumber_T)(regmatch.startp[0] - expr);
7878 li4->li_tv.vval.v_number =
7879 (varnumber_T)(regmatch.endp[0] - expr);
7880 if (l != NULL)
7881 li2->li_tv.vval.v_number = (varnumber_T)idx;
7882 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007883 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 {
7885 int i;
7886
7887 /* return list with matched string and submatches */
7888 for (i = 0; i < NSUBEXP; ++i)
7889 {
7890 if (regmatch.endp[i] == NULL)
7891 {
7892 if (list_append_string(rettv->vval.v_list,
7893 (char_u *)"", 0) == FAIL)
7894 break;
7895 }
7896 else if (list_append_string(rettv->vval.v_list,
7897 regmatch.startp[i],
7898 (int)(regmatch.endp[i] - regmatch.startp[i]))
7899 == FAIL)
7900 break;
7901 }
7902 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007903 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 {
7905 /* return matched string */
7906 if (l != NULL)
7907 copy_tv(&li->li_tv, rettv);
7908 else
7909 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7910 (int)(regmatch.endp[0] - regmatch.startp[0]));
7911 }
7912 else if (l != NULL)
7913 rettv->vval.v_number = idx;
7914 else
7915 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007916 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917 rettv->vval.v_number =
7918 (varnumber_T)(regmatch.startp[0] - str);
7919 else
7920 rettv->vval.v_number =
7921 (varnumber_T)(regmatch.endp[0] - str);
7922 rettv->vval.v_number += (varnumber_T)(str - expr);
7923 }
7924 }
7925 vim_regfree(regmatch.regprog);
7926 }
7927
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007928theend:
7929 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 /* matchstrpos() without a list: drop the second item. */
7931 listitem_remove(rettv->vval.v_list,
7932 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 vim_free(tofree);
7934 p_cpo = save_cpo;
7935}
7936
7937/*
7938 * "match()" function
7939 */
7940 static void
7941f_match(typval_T *argvars, typval_T *rettv)
7942{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007943 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944}
7945
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946/*
7947 * "matchend()" function
7948 */
7949 static void
7950f_matchend(typval_T *argvars, typval_T *rettv)
7951{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007952 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953}
7954
7955/*
7956 * "matchlist()" function
7957 */
7958 static void
7959f_matchlist(typval_T *argvars, typval_T *rettv)
7960{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007961 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962}
7963
7964/*
7965 * "matchstr()" function
7966 */
7967 static void
7968f_matchstr(typval_T *argvars, typval_T *rettv)
7969{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007970 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971}
7972
7973/*
7974 * "matchstrpos()" function
7975 */
7976 static void
7977f_matchstrpos(typval_T *argvars, typval_T *rettv)
7978{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007979 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980}
7981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 static void
7983max_min(typval_T *argvars, typval_T *rettv, int domax)
7984{
7985 varnumber_T n = 0;
7986 varnumber_T i;
7987 int error = FALSE;
7988
7989 if (argvars[0].v_type == VAR_LIST)
7990 {
7991 list_T *l;
7992 listitem_T *li;
7993
7994 l = argvars[0].vval.v_list;
7995 if (l != NULL)
7996 {
7997 li = l->lv_first;
7998 if (li != NULL)
7999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008000 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 for (;;)
8002 {
8003 li = li->li_next;
8004 if (li == NULL)
8005 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008006 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 if (domax ? i > n : i < n)
8008 n = i;
8009 }
8010 }
8011 }
8012 }
8013 else if (argvars[0].v_type == VAR_DICT)
8014 {
8015 dict_T *d;
8016 int first = TRUE;
8017 hashitem_T *hi;
8018 int todo;
8019
8020 d = argvars[0].vval.v_dict;
8021 if (d != NULL)
8022 {
8023 todo = (int)d->dv_hashtab.ht_used;
8024 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8025 {
8026 if (!HASHITEM_EMPTY(hi))
8027 {
8028 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008029 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 if (first)
8031 {
8032 n = i;
8033 first = FALSE;
8034 }
8035 else if (domax ? i > n : i < n)
8036 n = i;
8037 }
8038 }
8039 }
8040 }
8041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008042 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 rettv->vval.v_number = error ? 0 : n;
8044}
8045
8046/*
8047 * "max()" function
8048 */
8049 static void
8050f_max(typval_T *argvars, typval_T *rettv)
8051{
8052 max_min(argvars, rettv, TRUE);
8053}
8054
8055/*
8056 * "min()" function
8057 */
8058 static void
8059f_min(typval_T *argvars, typval_T *rettv)
8060{
8061 max_min(argvars, rettv, FALSE);
8062}
8063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064/*
8065 * Create the directory in which "dir" is located, and higher levels when
8066 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008067 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 */
8069 static int
8070mkdir_recurse(char_u *dir, int prot)
8071{
8072 char_u *p;
8073 char_u *updir;
8074 int r = FAIL;
8075
8076 /* Get end of directory name in "dir".
8077 * We're done when it's "/" or "c:/". */
8078 p = gettail_sep(dir);
8079 if (p <= get_past_head(dir))
8080 return OK;
8081
8082 /* If the directory exists we're done. Otherwise: create it.*/
8083 updir = vim_strnsave(dir, (int)(p - dir));
8084 if (updir == NULL)
8085 return FAIL;
8086 if (mch_isdir(updir))
8087 r = OK;
8088 else if (mkdir_recurse(updir, prot) == OK)
8089 r = vim_mkdir_emsg(updir, prot);
8090 vim_free(updir);
8091 return r;
8092}
8093
8094#ifdef vim_mkdir
8095/*
8096 * "mkdir()" function
8097 */
8098 static void
8099f_mkdir(typval_T *argvars, typval_T *rettv)
8100{
8101 char_u *dir;
8102 char_u buf[NUMBUFLEN];
8103 int prot = 0755;
8104
8105 rettv->vval.v_number = FAIL;
8106 if (check_restricted() || check_secure())
8107 return;
8108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008109 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008111 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008113 if (*gettail(dir) == NUL)
8114 /* remove trailing slashes */
8115 *gettail_sep(dir) = NUL;
8116
8117 if (argvars[1].v_type != VAR_UNKNOWN)
8118 {
8119 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008121 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008122 if (prot == -1)
8123 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008125 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008126 {
8127 if (mch_isdir(dir))
8128 {
8129 /* With the "p" flag it's OK if the dir already exists. */
8130 rettv->vval.v_number = OK;
8131 return;
8132 }
8133 mkdir_recurse(dir, prot);
8134 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008136 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137}
8138#endif
8139
8140/*
8141 * "mode()" function
8142 */
8143 static void
8144f_mode(typval_T *argvars, typval_T *rettv)
8145{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008146 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147
Bram Moolenaar612cc382018-07-29 15:34:26 +02008148 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149
8150 if (time_for_testing == 93784)
8151 {
8152 /* Testing the two-character code. */
8153 buf[0] = 'x';
8154 buf[1] = '!';
8155 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008156#ifdef FEAT_TERMINAL
8157 else if (term_use_loop())
8158 buf[0] = 't';
8159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 else if (VIsual_active)
8161 {
8162 if (VIsual_select)
8163 buf[0] = VIsual_mode + 's' - 'v';
8164 else
8165 buf[0] = VIsual_mode;
8166 }
8167 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8168 || State == CONFIRM)
8169 {
8170 buf[0] = 'r';
8171 if (State == ASKMORE)
8172 buf[1] = 'm';
8173 else if (State == CONFIRM)
8174 buf[1] = '?';
8175 }
8176 else if (State == EXTERNCMD)
8177 buf[0] = '!';
8178 else if (State & INSERT)
8179 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 if (State & VREPLACE_FLAG)
8181 {
8182 buf[0] = 'R';
8183 buf[1] = 'v';
8184 }
8185 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008186 {
8187 if (State & REPLACE_FLAG)
8188 buf[0] = 'R';
8189 else
8190 buf[0] = 'i';
8191#ifdef FEAT_INS_EXPAND
8192 if (ins_compl_active())
8193 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008194 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008195 buf[1] = 'x';
8196#endif
8197 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008199 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 {
8201 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008202 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008204 else if (exmode_active == EXMODE_NORMAL)
8205 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 }
8207 else
8208 {
8209 buf[0] = 'n';
8210 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008211 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008213 // to be able to detect force-linewise/blockwise/characterwise operations
8214 buf[2] = motion_force;
8215 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008216 else if (restart_edit == 'I' || restart_edit == 'R'
8217 || restart_edit == 'V')
8218 {
8219 buf[1] = 'i';
8220 buf[2] = restart_edit;
8221 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 }
8223
8224 /* Clear out the minor mode when the argument is not a non-zero number or
8225 * non-empty string. */
8226 if (!non_zero_arg(&argvars[0]))
8227 buf[1] = NUL;
8228
8229 rettv->vval.v_string = vim_strsave(buf);
8230 rettv->v_type = VAR_STRING;
8231}
8232
8233#if defined(FEAT_MZSCHEME) || defined(PROTO)
8234/*
8235 * "mzeval()" function
8236 */
8237 static void
8238f_mzeval(typval_T *argvars, typval_T *rettv)
8239{
8240 char_u *str;
8241 char_u buf[NUMBUFLEN];
8242
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008243 if (check_restricted() || check_secure())
8244 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008245 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 do_mzeval(str, rettv);
8247}
8248
8249 void
8250mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8251{
8252 typval_T argvars[3];
8253
8254 argvars[0].v_type = VAR_STRING;
8255 argvars[0].vval.v_string = name;
8256 copy_tv(args, &argvars[1]);
8257 argvars[2].v_type = VAR_UNKNOWN;
8258 f_call(argvars, rettv);
8259 clear_tv(&argvars[1]);
8260}
8261#endif
8262
8263/*
8264 * "nextnonblank()" function
8265 */
8266 static void
8267f_nextnonblank(typval_T *argvars, typval_T *rettv)
8268{
8269 linenr_T lnum;
8270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008271 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 {
8273 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8274 {
8275 lnum = 0;
8276 break;
8277 }
8278 if (*skipwhite(ml_get(lnum)) != NUL)
8279 break;
8280 }
8281 rettv->vval.v_number = lnum;
8282}
8283
8284/*
8285 * "nr2char()" function
8286 */
8287 static void
8288f_nr2char(typval_T *argvars, typval_T *rettv)
8289{
8290 char_u buf[NUMBUFLEN];
8291
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 if (has_mbyte)
8293 {
8294 int utf8 = 0;
8295
8296 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008297 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008299 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008301 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 }
8303 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008305 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 buf[1] = NUL;
8307 }
8308 rettv->v_type = VAR_STRING;
8309 rettv->vval.v_string = vim_strsave(buf);
8310}
8311
8312/*
8313 * "or(expr, expr)" function
8314 */
8315 static void
8316f_or(typval_T *argvars, typval_T *rettv)
8317{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008318 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8319 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320}
8321
8322/*
8323 * "pathshorten()" function
8324 */
8325 static void
8326f_pathshorten(typval_T *argvars, typval_T *rettv)
8327{
8328 char_u *p;
8329
8330 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008331 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 if (p == NULL)
8333 rettv->vval.v_string = NULL;
8334 else
8335 {
8336 p = vim_strsave(p);
8337 rettv->vval.v_string = p;
8338 if (p != NULL)
8339 shorten_dir(p);
8340 }
8341}
8342
8343#ifdef FEAT_PERL
8344/*
8345 * "perleval()" function
8346 */
8347 static void
8348f_perleval(typval_T *argvars, typval_T *rettv)
8349{
8350 char_u *str;
8351 char_u buf[NUMBUFLEN];
8352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008353 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 do_perleval(str, rettv);
8355}
8356#endif
8357
8358#ifdef FEAT_FLOAT
8359/*
8360 * "pow()" function
8361 */
8362 static void
8363f_pow(typval_T *argvars, typval_T *rettv)
8364{
8365 float_T fx = 0.0, fy = 0.0;
8366
8367 rettv->v_type = VAR_FLOAT;
8368 if (get_float_arg(argvars, &fx) == OK
8369 && get_float_arg(&argvars[1], &fy) == OK)
8370 rettv->vval.v_float = pow(fx, fy);
8371 else
8372 rettv->vval.v_float = 0.0;
8373}
8374#endif
8375
8376/*
8377 * "prevnonblank()" function
8378 */
8379 static void
8380f_prevnonblank(typval_T *argvars, typval_T *rettv)
8381{
8382 linenr_T lnum;
8383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008384 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8386 lnum = 0;
8387 else
8388 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8389 --lnum;
8390 rettv->vval.v_number = lnum;
8391}
8392
8393/* This dummy va_list is here because:
8394 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8395 * - locally in the function results in a "used before set" warning
8396 * - using va_start() to initialize it gives "function with fixed args" error */
8397static va_list ap;
8398
8399/*
8400 * "printf()" function
8401 */
8402 static void
8403f_printf(typval_T *argvars, typval_T *rettv)
8404{
8405 char_u buf[NUMBUFLEN];
8406 int len;
8407 char_u *s;
8408 int saved_did_emsg = did_emsg;
8409 char *fmt;
8410
8411 rettv->v_type = VAR_STRING;
8412 rettv->vval.v_string = NULL;
8413
8414 /* Get the required length, allocate the buffer and do it for real. */
8415 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008417 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 if (!did_emsg)
8419 {
8420 s = alloc(len + 1);
8421 if (s != NULL)
8422 {
8423 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008424 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8425 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 }
8427 }
8428 did_emsg |= saved_did_emsg;
8429}
8430
8431/*
8432 * "pumvisible()" function
8433 */
8434 static void
8435f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8436{
8437#ifdef FEAT_INS_EXPAND
8438 if (pum_visible())
8439 rettv->vval.v_number = 1;
8440#endif
8441}
8442
8443#ifdef FEAT_PYTHON3
8444/*
8445 * "py3eval()" function
8446 */
8447 static void
8448f_py3eval(typval_T *argvars, typval_T *rettv)
8449{
8450 char_u *str;
8451 char_u buf[NUMBUFLEN];
8452
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008453 if (check_restricted() || check_secure())
8454 return;
8455
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008456 if (p_pyx == 0)
8457 p_pyx = 3;
8458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008459 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 do_py3eval(str, rettv);
8461}
8462#endif
8463
8464#ifdef FEAT_PYTHON
8465/*
8466 * "pyeval()" function
8467 */
8468 static void
8469f_pyeval(typval_T *argvars, typval_T *rettv)
8470{
8471 char_u *str;
8472 char_u buf[NUMBUFLEN];
8473
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008474 if (check_restricted() || check_secure())
8475 return;
8476
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008477 if (p_pyx == 0)
8478 p_pyx = 2;
8479
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008480 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 do_pyeval(str, rettv);
8482}
8483#endif
8484
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008485#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8486/*
8487 * "pyxeval()" function
8488 */
8489 static void
8490f_pyxeval(typval_T *argvars, typval_T *rettv)
8491{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008492 if (check_restricted() || check_secure())
8493 return;
8494
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008495# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8496 init_pyxversion();
8497 if (p_pyx == 2)
8498 f_pyeval(argvars, rettv);
8499 else
8500 f_py3eval(argvars, rettv);
8501# elif defined(FEAT_PYTHON)
8502 f_pyeval(argvars, rettv);
8503# elif defined(FEAT_PYTHON3)
8504 f_py3eval(argvars, rettv);
8505# endif
8506}
8507#endif
8508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509/*
8510 * "range()" function
8511 */
8512 static void
8513f_range(typval_T *argvars, typval_T *rettv)
8514{
8515 varnumber_T start;
8516 varnumber_T end;
8517 varnumber_T stride = 1;
8518 varnumber_T i;
8519 int error = FALSE;
8520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008521 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 if (argvars[1].v_type == VAR_UNKNOWN)
8523 {
8524 end = start - 1;
8525 start = 0;
8526 }
8527 else
8528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008529 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 }
8533
8534 if (error)
8535 return; /* type error; errmsg already given */
8536 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008537 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 else
8541 {
8542 if (rettv_list_alloc(rettv) == OK)
8543 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8544 if (list_append_number(rettv->vval.v_list,
8545 (varnumber_T)i) == FAIL)
8546 break;
8547 }
8548}
8549
8550/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008551 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008552 */
8553 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008554readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008555{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008556 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008557 typval_T save_val;
8558 typval_T rettv;
8559 typval_T argv[2];
8560 int retval = 0;
8561 int error = FALSE;
8562
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008563 if (expr->v_type == VAR_UNKNOWN)
8564 return 1;
8565
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008566 prepare_vimvar(VV_VAL, &save_val);
8567 set_vim_var_string(VV_VAL, name, -1);
8568 argv[0].v_type = VAR_STRING;
8569 argv[0].vval.v_string = name;
8570
8571 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8572 goto theend;
8573
8574 retval = tv_get_number_chk(&rettv, &error);
8575 if (error)
8576 retval = -1;
8577 clear_tv(&rettv);
8578
8579theend:
8580 set_vim_var_string(VV_VAL, NULL, 0);
8581 restore_vimvar(VV_VAL, &save_val);
8582 return retval;
8583}
8584
8585/*
8586 * "readdir()" function
8587 */
8588 static void
8589f_readdir(typval_T *argvars, typval_T *rettv)
8590{
8591 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008592 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008593 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008594 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008595 garray_T ga;
8596 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008597
8598 if (rettv_list_alloc(rettv) == FAIL)
8599 return;
8600 path = tv_get_string(&argvars[0]);
8601 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008602
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008603 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8604 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008605 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008606 for (i = 0; i < ga.ga_len; i++)
8607 {
8608 p = ((char_u **)ga.ga_data)[i];
8609 list_append_string(rettv->vval.v_list, p, -1);
8610 }
8611 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008612 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008613}
8614
8615/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 * "readfile()" function
8617 */
8618 static void
8619f_readfile(typval_T *argvars, typval_T *rettv)
8620{
8621 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008622 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 int failed = FALSE;
8624 char_u *fname;
8625 FILE *fd;
8626 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8627 int io_size = sizeof(buf);
8628 int readlen; /* size of last fread() */
8629 char_u *prev = NULL; /* previously read bytes, if any */
8630 long prevlen = 0; /* length of data in prev */
8631 long prevsize = 0; /* size of prev buffer */
8632 long maxline = MAXLNUM;
8633 long cnt = 0;
8634 char_u *p; /* position in buf */
8635 char_u *start; /* start of current line */
8636
8637 if (argvars[1].v_type != VAR_UNKNOWN)
8638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008639 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008641 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8642 blob = TRUE;
8643
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008645 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 }
8647
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008648 if (blob)
8649 {
8650 if (rettv_blob_alloc(rettv) == FAIL)
8651 return;
8652 }
8653 else
8654 {
8655 if (rettv_list_alloc(rettv) == FAIL)
8656 return;
8657 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658
8659 /* Always open the file in binary mode, library functions have a mind of
8660 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008664 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665 return;
8666 }
8667
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008668 if (blob)
8669 {
8670 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008672 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008673 blob_free(rettv->vval.v_blob);
8674 }
8675 fclose(fd);
8676 return;
8677 }
8678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 while (cnt < maxline || maxline < 0)
8680 {
8681 readlen = (int)fread(buf, 1, io_size, fd);
8682
8683 /* This for loop processes what was read, but is also entered at end
8684 * of file so that either:
8685 * - an incomplete line gets written
8686 * - a "binary" file gets an empty line at the end if it ends in a
8687 * newline. */
8688 for (p = buf, start = buf;
8689 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8690 ++p)
8691 {
8692 if (*p == '\n' || readlen <= 0)
8693 {
8694 listitem_T *li;
8695 char_u *s = NULL;
8696 long_u len = p - start;
8697
8698 /* Finished a line. Remove CRs before NL. */
8699 if (readlen > 0 && !binary)
8700 {
8701 while (len > 0 && start[len - 1] == '\r')
8702 --len;
8703 /* removal may cross back to the "prev" string */
8704 if (len == 0)
8705 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8706 --prevlen;
8707 }
8708 if (prevlen == 0)
8709 s = vim_strnsave(start, (int)len);
8710 else
8711 {
8712 /* Change "prev" buffer to be the right size. This way
8713 * the bytes are only copied once, and very long lines are
8714 * allocated only once. */
8715 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8716 {
8717 mch_memmove(s + prevlen, start, len);
8718 s[prevlen + len] = NUL;
8719 prev = NULL; /* the list will own the string */
8720 prevlen = prevsize = 0;
8721 }
8722 }
8723 if (s == NULL)
8724 {
8725 do_outofmem_msg((long_u) prevlen + len + 1);
8726 failed = TRUE;
8727 break;
8728 }
8729
8730 if ((li = listitem_alloc()) == NULL)
8731 {
8732 vim_free(s);
8733 failed = TRUE;
8734 break;
8735 }
8736 li->li_tv.v_type = VAR_STRING;
8737 li->li_tv.v_lock = 0;
8738 li->li_tv.vval.v_string = s;
8739 list_append(rettv->vval.v_list, li);
8740
8741 start = p + 1; /* step over newline */
8742 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8743 break;
8744 }
8745 else if (*p == NUL)
8746 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8748 * when finding the BF and check the previous two bytes. */
8749 else if (*p == 0xbf && enc_utf8 && !binary)
8750 {
8751 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8752 * + 1, these may be in the "prev" string. */
8753 char_u back1 = p >= buf + 1 ? p[-1]
8754 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8755 char_u back2 = p >= buf + 2 ? p[-2]
8756 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8757 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8758
8759 if (back2 == 0xef && back1 == 0xbb)
8760 {
8761 char_u *dest = p - 2;
8762
8763 /* Usually a BOM is at the beginning of a file, and so at
8764 * the beginning of a line; then we can just step over it.
8765 */
8766 if (start == dest)
8767 start = p + 1;
8768 else
8769 {
8770 /* have to shuffle buf to close gap */
8771 int adjust_prevlen = 0;
8772
8773 if (dest < buf)
8774 {
8775 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8776 dest = buf;
8777 }
8778 if (readlen > p - buf + 1)
8779 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8780 readlen -= 3 - adjust_prevlen;
8781 prevlen -= adjust_prevlen;
8782 p = dest - 1;
8783 }
8784 }
8785 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 } /* for */
8787
8788 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8789 break;
8790 if (start < p)
8791 {
8792 /* There's part of a line in buf, store it in "prev". */
8793 if (p - start + prevlen >= prevsize)
8794 {
8795 /* need bigger "prev" buffer */
8796 char_u *newprev;
8797
8798 /* A common use case is ordinary text files and "prev" gets a
8799 * fragment of a line, so the first allocation is made
8800 * small, to avoid repeatedly 'allocing' large and
8801 * 'reallocing' small. */
8802 if (prevsize == 0)
8803 prevsize = (long)(p - start);
8804 else
8805 {
8806 long grow50pc = (prevsize * 3) / 2;
8807 long growmin = (long)((p - start) * 2 + prevlen);
8808 prevsize = grow50pc > growmin ? grow50pc : growmin;
8809 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008810 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 if (newprev == NULL)
8812 {
8813 do_outofmem_msg((long_u)prevsize);
8814 failed = TRUE;
8815 break;
8816 }
8817 prev = newprev;
8818 }
8819 /* Add the line part to end of "prev". */
8820 mch_memmove(prev + prevlen, start, p - start);
8821 prevlen += (long)(p - start);
8822 }
8823 } /* while */
8824
8825 /*
8826 * For a negative line count use only the lines at the end of the file,
8827 * free the rest.
8828 */
8829 if (!failed && maxline < 0)
8830 while (cnt > -maxline)
8831 {
8832 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8833 --cnt;
8834 }
8835
8836 if (failed)
8837 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008838 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008840 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 }
8842
8843 vim_free(prev);
8844 fclose(fd);
8845}
8846
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008847 static void
8848return_register(int regname, typval_T *rettv)
8849{
8850 char_u buf[2] = {0, 0};
8851
8852 buf[0] = (char_u)regname;
8853 rettv->v_type = VAR_STRING;
8854 rettv->vval.v_string = vim_strsave(buf);
8855}
8856
8857/*
8858 * "reg_executing()" function
8859 */
8860 static void
8861f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8862{
8863 return_register(reg_executing, rettv);
8864}
8865
8866/*
8867 * "reg_recording()" function
8868 */
8869 static void
8870f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8871{
8872 return_register(reg_recording, rettv);
8873}
8874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876/*
8877 * Convert a List to proftime_T.
8878 * Return FAIL when there is something wrong.
8879 */
8880 static int
8881list2proftime(typval_T *arg, proftime_T *tm)
8882{
8883 long n1, n2;
8884 int error = FALSE;
8885
8886 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8887 || arg->vval.v_list->lv_len != 2)
8888 return FAIL;
8889 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8890 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008891# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 tm->HighPart = n1;
8893 tm->LowPart = n2;
8894# else
8895 tm->tv_sec = n1;
8896 tm->tv_usec = n2;
8897# endif
8898 return error ? FAIL : OK;
8899}
8900#endif /* FEAT_RELTIME */
8901
8902/*
8903 * "reltime()" function
8904 */
8905 static void
8906f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8907{
8908#ifdef FEAT_RELTIME
8909 proftime_T res;
8910 proftime_T start;
8911
8912 if (argvars[0].v_type == VAR_UNKNOWN)
8913 {
8914 /* No arguments: get current time. */
8915 profile_start(&res);
8916 }
8917 else if (argvars[1].v_type == VAR_UNKNOWN)
8918 {
8919 if (list2proftime(&argvars[0], &res) == FAIL)
8920 return;
8921 profile_end(&res);
8922 }
8923 else
8924 {
8925 /* Two arguments: compute the difference. */
8926 if (list2proftime(&argvars[0], &start) == FAIL
8927 || list2proftime(&argvars[1], &res) == FAIL)
8928 return;
8929 profile_sub(&res, &start);
8930 }
8931
8932 if (rettv_list_alloc(rettv) == OK)
8933 {
8934 long n1, n2;
8935
Bram Moolenaar4f974752019-02-17 17:44:42 +01008936# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 n1 = res.HighPart;
8938 n2 = res.LowPart;
8939# else
8940 n1 = res.tv_sec;
8941 n2 = res.tv_usec;
8942# endif
8943 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8944 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8945 }
8946#endif
8947}
8948
8949#ifdef FEAT_FLOAT
8950/*
8951 * "reltimefloat()" function
8952 */
8953 static void
8954f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8955{
8956# ifdef FEAT_RELTIME
8957 proftime_T tm;
8958# endif
8959
8960 rettv->v_type = VAR_FLOAT;
8961 rettv->vval.v_float = 0;
8962# ifdef FEAT_RELTIME
8963 if (list2proftime(&argvars[0], &tm) == OK)
8964 rettv->vval.v_float = profile_float(&tm);
8965# endif
8966}
8967#endif
8968
8969/*
8970 * "reltimestr()" function
8971 */
8972 static void
8973f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8974{
8975#ifdef FEAT_RELTIME
8976 proftime_T tm;
8977#endif
8978
8979 rettv->v_type = VAR_STRING;
8980 rettv->vval.v_string = NULL;
8981#ifdef FEAT_RELTIME
8982 if (list2proftime(&argvars[0], &tm) == OK)
8983 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8984#endif
8985}
8986
8987#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 static void
8989make_connection(void)
8990{
8991 if (X_DISPLAY == NULL
8992# ifdef FEAT_GUI
8993 && !gui.in_use
8994# endif
8995 )
8996 {
8997 x_force_connect = TRUE;
8998 setup_term_clip();
8999 x_force_connect = FALSE;
9000 }
9001}
9002
9003 static int
9004check_connection(void)
9005{
9006 make_connection();
9007 if (X_DISPLAY == NULL)
9008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009009 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 return FAIL;
9011 }
9012 return OK;
9013}
9014#endif
9015
9016#ifdef FEAT_CLIENTSERVER
9017 static void
9018remote_common(typval_T *argvars, typval_T *rettv, int expr)
9019{
9020 char_u *server_name;
9021 char_u *keys;
9022 char_u *r = NULL;
9023 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009024 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009025# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 HWND w;
9027# else
9028 Window w;
9029# endif
9030
9031 if (check_restricted() || check_secure())
9032 return;
9033
9034# ifdef FEAT_X11
9035 if (check_connection() == FAIL)
9036 return;
9037# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009038 if (argvars[2].v_type != VAR_UNKNOWN
9039 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009040 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009042 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 if (server_name == NULL)
9044 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009045 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009046# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009047 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009049 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9050 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051# endif
9052 {
9053 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009055 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009056 vim_free(r);
9057 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009059 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 return;
9061 }
9062
9063 rettv->vval.v_string = r;
9064
9065 if (argvars[2].v_type != VAR_UNKNOWN)
9066 {
9067 dictitem_T v;
9068 char_u str[30];
9069 char_u *idvar;
9070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009071 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009072 if (idvar != NULL && *idvar != NUL)
9073 {
9074 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9075 v.di_tv.v_type = VAR_STRING;
9076 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009078 vim_free(v.di_tv.vval.v_string);
9079 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 }
9081}
9082#endif
9083
9084/*
9085 * "remote_expr()" function
9086 */
9087 static void
9088f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9089{
9090 rettv->v_type = VAR_STRING;
9091 rettv->vval.v_string = NULL;
9092#ifdef FEAT_CLIENTSERVER
9093 remote_common(argvars, rettv, TRUE);
9094#endif
9095}
9096
9097/*
9098 * "remote_foreground()" function
9099 */
9100 static void
9101f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9102{
9103#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009104# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 /* On Win32 it's done in this application. */
9106 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009107 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108
9109 if (server_name != NULL)
9110 serverForeground(server_name);
9111 }
9112# else
9113 /* Send a foreground() expression to the server. */
9114 argvars[1].v_type = VAR_STRING;
9115 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9116 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009117 rettv->v_type = VAR_STRING;
9118 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 remote_common(argvars, rettv, TRUE);
9120 vim_free(argvars[1].vval.v_string);
9121# endif
9122#endif
9123}
9124
9125 static void
9126f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9127{
9128#ifdef FEAT_CLIENTSERVER
9129 dictitem_T v;
9130 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009131# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132 long_u n = 0;
9133# endif
9134 char_u *serverid;
9135
9136 if (check_restricted() || check_secure())
9137 {
9138 rettv->vval.v_number = -1;
9139 return;
9140 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009141 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 if (serverid == NULL)
9143 {
9144 rettv->vval.v_number = -1;
9145 return; /* type error; errmsg already given */
9146 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009147# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9149 if (n == 0)
9150 rettv->vval.v_number = -1;
9151 else
9152 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009153 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 rettv->vval.v_number = (s != NULL);
9155 }
9156# else
9157 if (check_connection() == FAIL)
9158 return;
9159
9160 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9161 serverStrToWin(serverid), &s);
9162# endif
9163
9164 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9165 {
9166 char_u *retvar;
9167
9168 v.di_tv.v_type = VAR_STRING;
9169 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009170 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 if (retvar != NULL)
9172 set_var(retvar, &v.di_tv, FALSE);
9173 vim_free(v.di_tv.vval.v_string);
9174 }
9175#else
9176 rettv->vval.v_number = -1;
9177#endif
9178}
9179
9180 static void
9181f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9182{
9183 char_u *r = NULL;
9184
9185#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009186 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187
9188 if (serverid != NULL && !check_restricted() && !check_secure())
9189 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009190 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009191# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009192 /* The server's HWND is encoded in the 'id' parameter */
9193 long_u n = 0;
9194# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009195
9196 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009197 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009198
Bram Moolenaar4f974752019-02-17 17:44:42 +01009199# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9201 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009202 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203 if (r == NULL)
9204# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009205 if (check_connection() == FAIL
9206 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9207 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009209 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210 }
9211#endif
9212 rettv->v_type = VAR_STRING;
9213 rettv->vval.v_string = r;
9214}
9215
9216/*
9217 * "remote_send()" function
9218 */
9219 static void
9220f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9221{
9222 rettv->v_type = VAR_STRING;
9223 rettv->vval.v_string = NULL;
9224#ifdef FEAT_CLIENTSERVER
9225 remote_common(argvars, rettv, FALSE);
9226#endif
9227}
9228
9229/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009230 * "remote_startserver()" function
9231 */
9232 static void
9233f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9234{
9235#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009236 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009237
9238 if (server == NULL)
9239 return; /* type error; errmsg already given */
9240 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009241 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009242 else
9243 {
9244# ifdef FEAT_X11
9245 if (check_connection() == OK)
9246 serverRegisterName(X_DISPLAY, server);
9247# else
9248 serverSetName(server);
9249# endif
9250 }
9251#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009252 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009253#endif
9254}
9255
9256/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257 * "remove()" function
9258 */
9259 static void
9260f_remove(typval_T *argvars, typval_T *rettv)
9261{
9262 list_T *l;
9263 listitem_T *item, *item2;
9264 listitem_T *li;
9265 long idx;
9266 long end;
9267 char_u *key;
9268 dict_T *d;
9269 dictitem_T *di;
9270 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009271 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272
9273 if (argvars[0].v_type == VAR_DICT)
9274 {
9275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009276 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009278 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009279 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009280 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281 if (key != NULL)
9282 {
9283 di = dict_find(d, key, -1);
9284 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9287 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9288 {
9289 *rettv = di->di_tv;
9290 init_tv(&di->di_tv);
9291 dictitem_remove(d, di);
9292 }
9293 }
9294 }
9295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009296 else if (argvars[0].v_type == VAR_BLOB)
9297 {
9298 idx = (long)tv_get_number_chk(&argvars[1], &error);
9299 if (!error)
9300 {
9301 blob_T *b = argvars[0].vval.v_blob;
9302 int len = blob_len(b);
9303 char_u *p;
9304
9305 if (idx < 0)
9306 // count from the end
9307 idx = len + idx;
9308 if (idx < 0 || idx >= len)
9309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009310 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009311 return;
9312 }
9313 if (argvars[2].v_type == VAR_UNKNOWN)
9314 {
9315 // Remove one item, return its value.
9316 p = (char_u *)b->bv_ga.ga_data;
9317 rettv->vval.v_number = (varnumber_T) *(p + idx);
9318 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9319 --b->bv_ga.ga_len;
9320 }
9321 else
9322 {
9323 blob_T *blob;
9324
9325 // Remove range of items, return list with values.
9326 end = (long)tv_get_number_chk(&argvars[2], &error);
9327 if (error)
9328 return;
9329 if (end < 0)
9330 // count from the end
9331 end = len + end;
9332 if (end >= len || idx > end)
9333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009334 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009335 return;
9336 }
9337 blob = blob_alloc();
9338 if (blob == NULL)
9339 return;
9340 blob->bv_ga.ga_len = end - idx + 1;
9341 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9342 {
9343 vim_free(blob);
9344 return;
9345 }
9346 p = (char_u *)b->bv_ga.ga_data;
9347 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9348 (size_t)(end - idx + 1));
9349 ++blob->bv_refcount;
9350 rettv->v_type = VAR_BLOB;
9351 rettv->vval.v_blob = blob;
9352
9353 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9354 b->bv_ga.ga_len -= end - idx + 1;
9355 }
9356 }
9357 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009359 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009361 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009365 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009367 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 else
9369 {
9370 if (argvars[2].v_type == VAR_UNKNOWN)
9371 {
9372 /* Remove one item, return its value. */
9373 vimlist_remove(l, item, item);
9374 *rettv = item->li_tv;
9375 vim_free(item);
9376 }
9377 else
9378 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009379 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009380 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009382 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009384 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 else
9386 {
9387 int cnt = 0;
9388
9389 for (li = item; li != NULL; li = li->li_next)
9390 {
9391 ++cnt;
9392 if (li == item2)
9393 break;
9394 }
9395 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009396 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397 else
9398 {
9399 vimlist_remove(l, item, item2);
9400 if (rettv_list_alloc(rettv) == OK)
9401 {
9402 l = rettv->vval.v_list;
9403 l->lv_first = item;
9404 l->lv_last = item2;
9405 item->li_prev = NULL;
9406 item2->li_next = NULL;
9407 l->lv_len = cnt;
9408 }
9409 }
9410 }
9411 }
9412 }
9413 }
9414}
9415
9416/*
9417 * "rename({from}, {to})" function
9418 */
9419 static void
9420f_rename(typval_T *argvars, typval_T *rettv)
9421{
9422 char_u buf[NUMBUFLEN];
9423
9424 if (check_restricted() || check_secure())
9425 rettv->vval.v_number = -1;
9426 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009427 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9428 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429}
9430
9431/*
9432 * "repeat()" function
9433 */
9434 static void
9435f_repeat(typval_T *argvars, typval_T *rettv)
9436{
9437 char_u *p;
9438 int n;
9439 int slen;
9440 int len;
9441 char_u *r;
9442 int i;
9443
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009444 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445 if (argvars[0].v_type == VAR_LIST)
9446 {
9447 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9448 while (n-- > 0)
9449 if (list_extend(rettv->vval.v_list,
9450 argvars[0].vval.v_list, NULL) == FAIL)
9451 break;
9452 }
9453 else
9454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009455 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456 rettv->v_type = VAR_STRING;
9457 rettv->vval.v_string = NULL;
9458
9459 slen = (int)STRLEN(p);
9460 len = slen * n;
9461 if (len <= 0)
9462 return;
9463
9464 r = alloc(len + 1);
9465 if (r != NULL)
9466 {
9467 for (i = 0; i < n; i++)
9468 mch_memmove(r + i * slen, p, (size_t)slen);
9469 r[len] = NUL;
9470 }
9471
9472 rettv->vval.v_string = r;
9473 }
9474}
9475
9476/*
9477 * "resolve()" function
9478 */
9479 static void
9480f_resolve(typval_T *argvars, typval_T *rettv)
9481{
9482 char_u *p;
9483#ifdef HAVE_READLINK
9484 char_u *buf = NULL;
9485#endif
9486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009487 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488#ifdef FEAT_SHORTCUT
9489 {
9490 char_u *v = NULL;
9491
Bram Moolenaardce1e892019-02-10 23:18:53 +01009492 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 if (v != NULL)
9494 rettv->vval.v_string = v;
9495 else
9496 rettv->vval.v_string = vim_strsave(p);
9497 }
9498#else
9499# ifdef HAVE_READLINK
9500 {
9501 char_u *cpy;
9502 int len;
9503 char_u *remain = NULL;
9504 char_u *q;
9505 int is_relative_to_current = FALSE;
9506 int has_trailing_pathsep = FALSE;
9507 int limit = 100;
9508
9509 p = vim_strsave(p);
9510
9511 if (p[0] == '.' && (vim_ispathsep(p[1])
9512 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9513 is_relative_to_current = TRUE;
9514
9515 len = STRLEN(p);
9516 if (len > 0 && after_pathsep(p, p + len))
9517 {
9518 has_trailing_pathsep = TRUE;
9519 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9520 }
9521
9522 q = getnextcomp(p);
9523 if (*q != NUL)
9524 {
9525 /* Separate the first path component in "p", and keep the
9526 * remainder (beginning with the path separator). */
9527 remain = vim_strsave(q - 1);
9528 q[-1] = NUL;
9529 }
9530
9531 buf = alloc(MAXPATHL + 1);
9532 if (buf == NULL)
9533 goto fail;
9534
9535 for (;;)
9536 {
9537 for (;;)
9538 {
9539 len = readlink((char *)p, (char *)buf, MAXPATHL);
9540 if (len <= 0)
9541 break;
9542 buf[len] = NUL;
9543
9544 if (limit-- == 0)
9545 {
9546 vim_free(p);
9547 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009548 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 rettv->vval.v_string = NULL;
9550 goto fail;
9551 }
9552
9553 /* Ensure that the result will have a trailing path separator
9554 * if the argument has one. */
9555 if (remain == NULL && has_trailing_pathsep)
9556 add_pathsep(buf);
9557
9558 /* Separate the first path component in the link value and
9559 * concatenate the remainders. */
9560 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9561 if (*q != NUL)
9562 {
9563 if (remain == NULL)
9564 remain = vim_strsave(q - 1);
9565 else
9566 {
9567 cpy = concat_str(q - 1, remain);
9568 if (cpy != NULL)
9569 {
9570 vim_free(remain);
9571 remain = cpy;
9572 }
9573 }
9574 q[-1] = NUL;
9575 }
9576
9577 q = gettail(p);
9578 if (q > p && *q == NUL)
9579 {
9580 /* Ignore trailing path separator. */
9581 q[-1] = NUL;
9582 q = gettail(p);
9583 }
9584 if (q > p && !mch_isFullName(buf))
9585 {
9586 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009587 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 if (cpy != NULL)
9589 {
9590 STRCPY(cpy, p);
9591 STRCPY(gettail(cpy), buf);
9592 vim_free(p);
9593 p = cpy;
9594 }
9595 }
9596 else
9597 {
9598 vim_free(p);
9599 p = vim_strsave(buf);
9600 }
9601 }
9602
9603 if (remain == NULL)
9604 break;
9605
9606 /* Append the first path component of "remain" to "p". */
9607 q = getnextcomp(remain + 1);
9608 len = q - remain - (*q != NUL);
9609 cpy = vim_strnsave(p, STRLEN(p) + len);
9610 if (cpy != NULL)
9611 {
9612 STRNCAT(cpy, remain, len);
9613 vim_free(p);
9614 p = cpy;
9615 }
9616 /* Shorten "remain". */
9617 if (*q != NUL)
9618 STRMOVE(remain, q - 1);
9619 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009620 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 }
9622
9623 /* If the result is a relative path name, make it explicitly relative to
9624 * the current directory if and only if the argument had this form. */
9625 if (!vim_ispathsep(*p))
9626 {
9627 if (is_relative_to_current
9628 && *p != NUL
9629 && !(p[0] == '.'
9630 && (p[1] == NUL
9631 || vim_ispathsep(p[1])
9632 || (p[1] == '.'
9633 && (p[2] == NUL
9634 || vim_ispathsep(p[2]))))))
9635 {
9636 /* Prepend "./". */
9637 cpy = concat_str((char_u *)"./", p);
9638 if (cpy != NULL)
9639 {
9640 vim_free(p);
9641 p = cpy;
9642 }
9643 }
9644 else if (!is_relative_to_current)
9645 {
9646 /* Strip leading "./". */
9647 q = p;
9648 while (q[0] == '.' && vim_ispathsep(q[1]))
9649 q += 2;
9650 if (q > p)
9651 STRMOVE(p, p + 2);
9652 }
9653 }
9654
9655 /* Ensure that the result will have no trailing path separator
9656 * if the argument had none. But keep "/" or "//". */
9657 if (!has_trailing_pathsep)
9658 {
9659 q = p + STRLEN(p);
9660 if (after_pathsep(p, q))
9661 *gettail_sep(p) = NUL;
9662 }
9663
9664 rettv->vval.v_string = p;
9665 }
9666# else
9667 rettv->vval.v_string = vim_strsave(p);
9668# endif
9669#endif
9670
9671 simplify_filename(rettv->vval.v_string);
9672
9673#ifdef HAVE_READLINK
9674fail:
9675 vim_free(buf);
9676#endif
9677 rettv->v_type = VAR_STRING;
9678}
9679
9680/*
9681 * "reverse({list})" function
9682 */
9683 static void
9684f_reverse(typval_T *argvars, typval_T *rettv)
9685{
9686 list_T *l;
9687 listitem_T *li, *ni;
9688
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009689 if (argvars[0].v_type == VAR_BLOB)
9690 {
9691 blob_T *b = argvars[0].vval.v_blob;
9692 int i, len = blob_len(b);
9693
9694 for (i = 0; i < len / 2; i++)
9695 {
9696 int tmp = blob_get(b, i);
9697
9698 blob_set(b, i, blob_get(b, len - i - 1));
9699 blob_set(b, len - i - 1, tmp);
9700 }
9701 rettv_blob_set(rettv, b);
9702 return;
9703 }
9704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009706 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009708 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 (char_u *)N_("reverse() argument"), TRUE))
9710 {
9711 li = l->lv_last;
9712 l->lv_first = l->lv_last = NULL;
9713 l->lv_len = 0;
9714 while (li != NULL)
9715 {
9716 ni = li->li_prev;
9717 list_append(l, li);
9718 li = ni;
9719 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009720 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 l->lv_idx = l->lv_len - l->lv_idx - 1;
9722 }
9723}
9724
9725#define SP_NOMOVE 0x01 /* don't move cursor */
9726#define SP_REPEAT 0x02 /* repeat to find outer pair */
9727#define SP_RETCOUNT 0x04 /* return matchcount */
9728#define SP_SETPCMARK 0x08 /* set previous context mark */
9729#define SP_START 0x10 /* accept match at start position */
9730#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9731#define SP_END 0x40 /* leave cursor at end of match */
9732#define SP_COLUMN 0x80 /* start at cursor column */
9733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734/*
9735 * Get flags for a search function.
9736 * Possibly sets "p_ws".
9737 * Returns BACKWARD, FORWARD or zero (for an error).
9738 */
9739 static int
9740get_search_arg(typval_T *varp, int *flagsp)
9741{
9742 int dir = FORWARD;
9743 char_u *flags;
9744 char_u nbuf[NUMBUFLEN];
9745 int mask;
9746
9747 if (varp->v_type != VAR_UNKNOWN)
9748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009749 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 if (flags == NULL)
9751 return 0; /* type error; errmsg already given */
9752 while (*flags != NUL)
9753 {
9754 switch (*flags)
9755 {
9756 case 'b': dir = BACKWARD; break;
9757 case 'w': p_ws = TRUE; break;
9758 case 'W': p_ws = FALSE; break;
9759 default: mask = 0;
9760 if (flagsp != NULL)
9761 switch (*flags)
9762 {
9763 case 'c': mask = SP_START; break;
9764 case 'e': mask = SP_END; break;
9765 case 'm': mask = SP_RETCOUNT; break;
9766 case 'n': mask = SP_NOMOVE; break;
9767 case 'p': mask = SP_SUBPAT; break;
9768 case 'r': mask = SP_REPEAT; break;
9769 case 's': mask = SP_SETPCMARK; break;
9770 case 'z': mask = SP_COLUMN; break;
9771 }
9772 if (mask == 0)
9773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009774 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775 dir = 0;
9776 }
9777 else
9778 *flagsp |= mask;
9779 }
9780 if (dir == 0)
9781 break;
9782 ++flags;
9783 }
9784 }
9785 return dir;
9786}
9787
9788/*
9789 * Shared by search() and searchpos() functions.
9790 */
9791 static int
9792search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9793{
9794 int flags;
9795 char_u *pat;
9796 pos_T pos;
9797 pos_T save_cursor;
9798 int save_p_ws = p_ws;
9799 int dir;
9800 int retval = 0; /* default: FAIL */
9801 long lnum_stop = 0;
9802 proftime_T tm;
9803#ifdef FEAT_RELTIME
9804 long time_limit = 0;
9805#endif
9806 int options = SEARCH_KEEP;
9807 int subpatnum;
9808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009809 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9811 if (dir == 0)
9812 goto theend;
9813 flags = *flagsp;
9814 if (flags & SP_START)
9815 options |= SEARCH_START;
9816 if (flags & SP_END)
9817 options |= SEARCH_END;
9818 if (flags & SP_COLUMN)
9819 options |= SEARCH_COL;
9820
9821 /* Optional arguments: line number to stop searching and timeout. */
9822 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009824 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009825 if (lnum_stop < 0)
9826 goto theend;
9827#ifdef FEAT_RELTIME
9828 if (argvars[3].v_type != VAR_UNKNOWN)
9829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009830 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 if (time_limit < 0)
9832 goto theend;
9833 }
9834#endif
9835 }
9836
9837#ifdef FEAT_RELTIME
9838 /* Set the time limit, if there is one. */
9839 profile_setlimit(time_limit, &tm);
9840#endif
9841
9842 /*
9843 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9844 * Check to make sure only those flags are set.
9845 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9846 * flags cannot be set. Check for that condition also.
9847 */
9848 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9849 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009851 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 goto theend;
9853 }
9854
9855 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009856 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009857 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 if (subpatnum != FAIL)
9859 {
9860 if (flags & SP_SUBPAT)
9861 retval = subpatnum;
9862 else
9863 retval = pos.lnum;
9864 if (flags & SP_SETPCMARK)
9865 setpcmark();
9866 curwin->w_cursor = pos;
9867 if (match_pos != NULL)
9868 {
9869 /* Store the match cursor position */
9870 match_pos->lnum = pos.lnum;
9871 match_pos->col = pos.col + 1;
9872 }
9873 /* "/$" will put the cursor after the end of the line, may need to
9874 * correct that here */
9875 check_cursor();
9876 }
9877
9878 /* If 'n' flag is used: restore cursor position. */
9879 if (flags & SP_NOMOVE)
9880 curwin->w_cursor = save_cursor;
9881 else
9882 curwin->w_set_curswant = TRUE;
9883theend:
9884 p_ws = save_p_ws;
9885
9886 return retval;
9887}
9888
9889#ifdef FEAT_FLOAT
9890
9891/*
9892 * round() is not in C90, use ceil() or floor() instead.
9893 */
9894 float_T
9895vim_round(float_T f)
9896{
9897 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9898}
9899
9900/*
9901 * "round({float})" function
9902 */
9903 static void
9904f_round(typval_T *argvars, typval_T *rettv)
9905{
9906 float_T f = 0.0;
9907
9908 rettv->v_type = VAR_FLOAT;
9909 if (get_float_arg(argvars, &f) == OK)
9910 rettv->vval.v_float = vim_round(f);
9911 else
9912 rettv->vval.v_float = 0.0;
9913}
9914#endif
9915
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009916#ifdef FEAT_RUBY
9917/*
9918 * "rubyeval()" function
9919 */
9920 static void
9921f_rubyeval(typval_T *argvars, typval_T *rettv)
9922{
9923 char_u *str;
9924 char_u buf[NUMBUFLEN];
9925
9926 str = tv_get_string_buf(&argvars[0], buf);
9927 do_rubyeval(str, rettv);
9928}
9929#endif
9930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931/*
9932 * "screenattr()" function
9933 */
9934 static void
9935f_screenattr(typval_T *argvars, typval_T *rettv)
9936{
9937 int row;
9938 int col;
9939 int c;
9940
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009941 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9942 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 if (row < 0 || row >= screen_Rows
9944 || col < 0 || col >= screen_Columns)
9945 c = -1;
9946 else
9947 c = ScreenAttrs[LineOffset[row] + col];
9948 rettv->vval.v_number = c;
9949}
9950
9951/*
9952 * "screenchar()" function
9953 */
9954 static void
9955f_screenchar(typval_T *argvars, typval_T *rettv)
9956{
9957 int row;
9958 int col;
9959 int off;
9960 int c;
9961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009962 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9963 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009964 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 c = -1;
9966 else
9967 {
9968 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009969 if (enc_utf8 && ScreenLinesUC[off] != 0)
9970 c = ScreenLinesUC[off];
9971 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 c = ScreenLines[off];
9973 }
9974 rettv->vval.v_number = c;
9975}
9976
9977/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009978 * "screenchars()" function
9979 */
9980 static void
9981f_screenchars(typval_T *argvars, typval_T *rettv)
9982{
9983 int row;
9984 int col;
9985 int off;
9986 int c;
9987 int i;
9988
9989 if (rettv_list_alloc(rettv) == FAIL)
9990 return;
9991 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9992 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9993 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9994 return;
9995
9996 off = LineOffset[row] + col;
9997 if (enc_utf8 && ScreenLinesUC[off] != 0)
9998 c = ScreenLinesUC[off];
9999 else
10000 c = ScreenLines[off];
10001 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10002
10003 if (enc_utf8)
10004
10005 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10006 list_append_number(rettv->vval.v_list,
10007 (varnumber_T)ScreenLinesC[i][off]);
10008}
10009
10010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 * "screencol()" function
10012 *
10013 * First column is 1 to be consistent with virtcol().
10014 */
10015 static void
10016f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10017{
10018 rettv->vval.v_number = screen_screencol() + 1;
10019}
10020
10021/*
10022 * "screenrow()" function
10023 */
10024 static void
10025f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10026{
10027 rettv->vval.v_number = screen_screenrow() + 1;
10028}
10029
10030/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010031 * "screenstring()" function
10032 */
10033 static void
10034f_screenstring(typval_T *argvars, typval_T *rettv)
10035{
10036 int row;
10037 int col;
10038 int off;
10039 int c;
10040 int i;
10041 char_u buf[MB_MAXBYTES + 1];
10042 int buflen = 0;
10043
10044 rettv->vval.v_string = NULL;
10045 rettv->v_type = VAR_STRING;
10046
10047 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10048 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10049 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10050 return;
10051
10052 off = LineOffset[row] + col;
10053 if (enc_utf8 && ScreenLinesUC[off] != 0)
10054 c = ScreenLinesUC[off];
10055 else
10056 c = ScreenLines[off];
10057 buflen += mb_char2bytes(c, buf);
10058
10059 if (enc_utf8)
10060 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10061 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10062
10063 buf[buflen] = NUL;
10064 rettv->vval.v_string = vim_strsave(buf);
10065}
10066
10067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010068 * "search()" function
10069 */
10070 static void
10071f_search(typval_T *argvars, typval_T *rettv)
10072{
10073 int flags = 0;
10074
10075 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10076}
10077
10078/*
10079 * "searchdecl()" function
10080 */
10081 static void
10082f_searchdecl(typval_T *argvars, typval_T *rettv)
10083{
10084 int locally = 1;
10085 int thisblock = 0;
10086 int error = FALSE;
10087 char_u *name;
10088
10089 rettv->vval.v_number = 1; /* default: FAIL */
10090
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010091 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092 if (argvars[1].v_type != VAR_UNKNOWN)
10093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010094 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010095 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010096 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010097 }
10098 if (!error && name != NULL)
10099 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10100 locally, thisblock, SEARCH_KEEP) == FAIL;
10101}
10102
10103/*
10104 * Used by searchpair() and searchpairpos()
10105 */
10106 static int
10107searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10108{
10109 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010110 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 int save_p_ws = p_ws;
10112 int dir;
10113 int flags = 0;
10114 char_u nbuf1[NUMBUFLEN];
10115 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010116 int retval = 0; /* default: FAIL */
10117 long lnum_stop = 0;
10118 long time_limit = 0;
10119
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010120 /* Get the three pattern arguments: start, middle, end. Will result in an
10121 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010122 spat = tv_get_string_chk(&argvars[0]);
10123 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10124 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010125 if (spat == NULL || mpat == NULL || epat == NULL)
10126 goto theend; /* type error */
10127
10128 /* Handle the optional fourth argument: flags */
10129 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10130 if (dir == 0)
10131 goto theend;
10132
10133 /* Don't accept SP_END or SP_SUBPAT.
10134 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10135 */
10136 if ((flags & (SP_END | SP_SUBPAT)) != 0
10137 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010139 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 goto theend;
10141 }
10142
10143 /* Using 'r' implies 'W', otherwise it doesn't work. */
10144 if (flags & SP_REPEAT)
10145 p_ws = FALSE;
10146
10147 /* Optional fifth argument: skip expression */
10148 if (argvars[3].v_type == VAR_UNKNOWN
10149 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010150 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 else
10152 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010153 skip = &argvars[4];
10154 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10155 && skip->v_type != VAR_STRING)
10156 {
10157 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010158 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010159 goto theend;
10160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 if (argvars[5].v_type != VAR_UNKNOWN)
10162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010163 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010166 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010168 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169#ifdef FEAT_RELTIME
10170 if (argvars[6].v_type != VAR_UNKNOWN)
10171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010172 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010175 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010177 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010178 }
10179#endif
10180 }
10181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182
10183 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10184 match_pos, lnum_stop, time_limit);
10185
10186theend:
10187 p_ws = save_p_ws;
10188
10189 return retval;
10190}
10191
10192/*
10193 * "searchpair()" function
10194 */
10195 static void
10196f_searchpair(typval_T *argvars, typval_T *rettv)
10197{
10198 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10199}
10200
10201/*
10202 * "searchpairpos()" function
10203 */
10204 static void
10205f_searchpairpos(typval_T *argvars, typval_T *rettv)
10206{
10207 pos_T match_pos;
10208 int lnum = 0;
10209 int col = 0;
10210
10211 if (rettv_list_alloc(rettv) == FAIL)
10212 return;
10213
10214 if (searchpair_cmn(argvars, &match_pos) > 0)
10215 {
10216 lnum = match_pos.lnum;
10217 col = match_pos.col;
10218 }
10219
10220 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10221 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10222}
10223
10224/*
10225 * Search for a start/middle/end thing.
10226 * Used by searchpair(), see its documentation for the details.
10227 * Returns 0 or -1 for no match,
10228 */
10229 long
10230do_searchpair(
10231 char_u *spat, /* start pattern */
10232 char_u *mpat, /* middle pattern */
10233 char_u *epat, /* end pattern */
10234 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010235 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236 int flags, /* SP_SETPCMARK and other SP_ values */
10237 pos_T *match_pos,
10238 linenr_T lnum_stop, /* stop at this line if not zero */
10239 long time_limit UNUSED) /* stop after this many msec */
10240{
10241 char_u *save_cpo;
10242 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10243 long retval = 0;
10244 pos_T pos;
10245 pos_T firstpos;
10246 pos_T foundpos;
10247 pos_T save_cursor;
10248 pos_T save_pos;
10249 int n;
10250 int r;
10251 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010252 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 int err;
10254 int options = SEARCH_KEEP;
10255 proftime_T tm;
10256
10257 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10258 save_cpo = p_cpo;
10259 p_cpo = empty_option;
10260
10261#ifdef FEAT_RELTIME
10262 /* Set the time limit, if there is one. */
10263 profile_setlimit(time_limit, &tm);
10264#endif
10265
10266 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10267 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010268 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10269 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 if (pat2 == NULL || pat3 == NULL)
10271 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010272 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273 if (*mpat == NUL)
10274 STRCPY(pat3, pat2);
10275 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010276 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 spat, epat, mpat);
10278 if (flags & SP_START)
10279 options |= SEARCH_START;
10280
Bram Moolenaar48570482017-10-30 21:48:41 +010010281 if (skip != NULL)
10282 {
10283 /* Empty string means to not use the skip expression. */
10284 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10285 use_skip = skip->vval.v_string != NULL
10286 && *skip->vval.v_string != NUL;
10287 }
10288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 save_cursor = curwin->w_cursor;
10290 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010291 CLEAR_POS(&firstpos);
10292 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010293 pat = pat3;
10294 for (;;)
10295 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010296 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010297 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010298 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 /* didn't find it or found the first match again: FAIL */
10300 break;
10301
10302 if (firstpos.lnum == 0)
10303 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010304 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010305 {
10306 /* Found the same position again. Can happen with a pattern that
10307 * has "\zs" at the end and searching backwards. Advance one
10308 * character and try again. */
10309 if (dir == BACKWARD)
10310 decl(&pos);
10311 else
10312 incl(&pos);
10313 }
10314 foundpos = pos;
10315
10316 /* clear the start flag to avoid getting stuck here */
10317 options &= ~SEARCH_START;
10318
10319 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010320 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321 {
10322 save_pos = curwin->w_cursor;
10323 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010324 err = FALSE;
10325 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010326 curwin->w_cursor = save_pos;
10327 if (err)
10328 {
10329 /* Evaluating {skip} caused an error, break here. */
10330 curwin->w_cursor = save_cursor;
10331 retval = -1;
10332 break;
10333 }
10334 if (r)
10335 continue;
10336 }
10337
10338 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10339 {
10340 /* Found end when searching backwards or start when searching
10341 * forward: nested pair. */
10342 ++nest;
10343 pat = pat2; /* nested, don't search for middle */
10344 }
10345 else
10346 {
10347 /* Found end when searching forward or start when searching
10348 * backward: end of (nested) pair; or found middle in outer pair. */
10349 if (--nest == 1)
10350 pat = pat3; /* outer level, search for middle */
10351 }
10352
10353 if (nest == 0)
10354 {
10355 /* Found the match: return matchcount or line number. */
10356 if (flags & SP_RETCOUNT)
10357 ++retval;
10358 else
10359 retval = pos.lnum;
10360 if (flags & SP_SETPCMARK)
10361 setpcmark();
10362 curwin->w_cursor = pos;
10363 if (!(flags & SP_REPEAT))
10364 break;
10365 nest = 1; /* search for next unmatched */
10366 }
10367 }
10368
10369 if (match_pos != NULL)
10370 {
10371 /* Store the match cursor position */
10372 match_pos->lnum = curwin->w_cursor.lnum;
10373 match_pos->col = curwin->w_cursor.col + 1;
10374 }
10375
10376 /* If 'n' flag is used or search failed: restore cursor position. */
10377 if ((flags & SP_NOMOVE) || retval == 0)
10378 curwin->w_cursor = save_cursor;
10379
10380theend:
10381 vim_free(pat2);
10382 vim_free(pat3);
10383 if (p_cpo == empty_option)
10384 p_cpo = save_cpo;
10385 else
10386 /* Darn, evaluating the {skip} expression changed the value. */
10387 free_string_option(save_cpo);
10388
10389 return retval;
10390}
10391
10392/*
10393 * "searchpos()" function
10394 */
10395 static void
10396f_searchpos(typval_T *argvars, typval_T *rettv)
10397{
10398 pos_T match_pos;
10399 int lnum = 0;
10400 int col = 0;
10401 int n;
10402 int flags = 0;
10403
10404 if (rettv_list_alloc(rettv) == FAIL)
10405 return;
10406
10407 n = search_cmn(argvars, &match_pos, &flags);
10408 if (n > 0)
10409 {
10410 lnum = match_pos.lnum;
10411 col = match_pos.col;
10412 }
10413
10414 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10415 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10416 if (flags & SP_SUBPAT)
10417 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10418}
10419
10420 static void
10421f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10422{
10423#ifdef FEAT_CLIENTSERVER
10424 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010425 char_u *server = tv_get_string_chk(&argvars[0]);
10426 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010427
10428 rettv->vval.v_number = -1;
10429 if (server == NULL || reply == NULL)
10430 return;
10431 if (check_restricted() || check_secure())
10432 return;
10433# ifdef FEAT_X11
10434 if (check_connection() == FAIL)
10435 return;
10436# endif
10437
10438 if (serverSendReply(server, reply) < 0)
10439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010440 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010441 return;
10442 }
10443 rettv->vval.v_number = 0;
10444#else
10445 rettv->vval.v_number = -1;
10446#endif
10447}
10448
10449 static void
10450f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10451{
10452 char_u *r = NULL;
10453
10454#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010455# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010456 r = serverGetVimNames();
10457# else
10458 make_connection();
10459 if (X_DISPLAY != NULL)
10460 r = serverGetVimNames(X_DISPLAY);
10461# endif
10462#endif
10463 rettv->v_type = VAR_STRING;
10464 rettv->vval.v_string = r;
10465}
10466
10467/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010468 * "setbufline()" function
10469 */
10470 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010471f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010472{
10473 linenr_T lnum;
10474 buf_T *buf;
10475
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010476 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010477 if (buf == NULL)
10478 rettv->vval.v_number = 1; /* FAIL */
10479 else
10480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010481 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010482 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010483 }
10484}
10485
10486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010487 * "setbufvar()" function
10488 */
10489 static void
10490f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10491{
10492 buf_T *buf;
10493 char_u *varname, *bufvarname;
10494 typval_T *varp;
10495 char_u nbuf[NUMBUFLEN];
10496
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010497 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010498 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010499 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10500 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010501 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010502 varp = &argvars[2];
10503
10504 if (buf != NULL && varname != NULL && varp != NULL)
10505 {
10506 if (*varname == '&')
10507 {
10508 long numval;
10509 char_u *strval;
10510 int error = FALSE;
10511 aco_save_T aco;
10512
10513 /* set curbuf to be our buf, temporarily */
10514 aucmd_prepbuf(&aco, buf);
10515
10516 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010517 numval = (long)tv_get_number_chk(varp, &error);
10518 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010519 if (!error && strval != NULL)
10520 set_option_value(varname, numval, strval, OPT_LOCAL);
10521
10522 /* reset notion of buffer */
10523 aucmd_restbuf(&aco);
10524 }
10525 else
10526 {
10527 buf_T *save_curbuf = curbuf;
10528
Bram Moolenaar964b3742019-05-24 18:54:09 +020010529 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 if (bufvarname != NULL)
10531 {
10532 curbuf = buf;
10533 STRCPY(bufvarname, "b:");
10534 STRCPY(bufvarname + 2, varname);
10535 set_var(bufvarname, varp, TRUE);
10536 vim_free(bufvarname);
10537 curbuf = save_curbuf;
10538 }
10539 }
10540 }
10541}
10542
10543 static void
10544f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10545{
10546 dict_T *d;
10547 dictitem_T *di;
10548 char_u *csearch;
10549
10550 if (argvars[0].v_type != VAR_DICT)
10551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010552 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 return;
10554 }
10555
10556 if ((d = argvars[0].vval.v_dict) != NULL)
10557 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010558 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 if (csearch != NULL)
10560 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010561 if (enc_utf8)
10562 {
10563 int pcc[MAX_MCO];
10564 int c = utfc_ptr2char(csearch, pcc);
10565
10566 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10567 }
10568 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 set_last_csearch(PTR2CHAR(csearch),
10570 csearch, MB_PTR2LEN(csearch));
10571 }
10572
10573 di = dict_find(d, (char_u *)"forward", -1);
10574 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010575 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 ? FORWARD : BACKWARD);
10577
10578 di = dict_find(d, (char_u *)"until", -1);
10579 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010580 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 }
10582}
10583
10584/*
10585 * "setcmdpos()" function
10586 */
10587 static void
10588f_setcmdpos(typval_T *argvars, typval_T *rettv)
10589{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010590 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010591
10592 if (pos >= 0)
10593 rettv->vval.v_number = set_cmdline_pos(pos);
10594}
10595
10596/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020010597 * "setenv()" function
10598 */
10599 static void
10600f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
10601{
10602 char_u namebuf[NUMBUFLEN];
10603 char_u valbuf[NUMBUFLEN];
10604 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
10605
10606 if (argvars[1].v_type == VAR_SPECIAL
10607 && argvars[1].vval.v_number == VVAL_NULL)
10608 vim_unsetenv(name);
10609 else
10610 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10611}
10612
10613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 * "setfperm({fname}, {mode})" function
10615 */
10616 static void
10617f_setfperm(typval_T *argvars, typval_T *rettv)
10618{
10619 char_u *fname;
10620 char_u modebuf[NUMBUFLEN];
10621 char_u *mode_str;
10622 int i;
10623 int mask;
10624 int mode = 0;
10625
10626 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010627 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 if (fname == NULL)
10629 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010630 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 if (mode_str == NULL)
10632 return;
10633 if (STRLEN(mode_str) != 9)
10634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010635 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636 return;
10637 }
10638
10639 mask = 1;
10640 for (i = 8; i >= 0; --i)
10641 {
10642 if (mode_str[i] != '-')
10643 mode |= mask;
10644 mask = mask << 1;
10645 }
10646 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10647}
10648
10649/*
10650 * "setline()" function
10651 */
10652 static void
10653f_setline(typval_T *argvars, typval_T *rettv)
10654{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010655 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656
Bram Moolenaarca851592018-06-06 21:04:07 +020010657 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658}
10659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660/*
10661 * Used by "setqflist()" and "setloclist()" functions
10662 */
10663 static void
10664set_qf_ll_list(
10665 win_T *wp UNUSED,
10666 typval_T *list_arg UNUSED,
10667 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010668 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669 typval_T *rettv)
10670{
10671#ifdef FEAT_QUICKFIX
10672 static char *e_invact = N_("E927: Invalid action: '%s'");
10673 char_u *act;
10674 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010675 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676#endif
10677
10678 rettv->vval.v_number = -1;
10679
10680#ifdef FEAT_QUICKFIX
10681 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010682 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010683 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010684 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685 else
10686 {
10687 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010688 dict_T *d = NULL;
10689 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690
10691 if (action_arg->v_type == VAR_STRING)
10692 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010693 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010694 if (act == NULL)
10695 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010696 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10697 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698 action = *act;
10699 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010700 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010701 }
10702 else if (action_arg->v_type == VAR_UNKNOWN)
10703 action = ' ';
10704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010705 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706
Bram Moolenaard823fa92016-08-12 16:29:27 +020010707 if (action_arg->v_type != VAR_UNKNOWN
10708 && what_arg->v_type != VAR_UNKNOWN)
10709 {
10710 if (what_arg->v_type == VAR_DICT)
10711 d = what_arg->vval.v_dict;
10712 else
10713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010714 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010715 valid_dict = FALSE;
10716 }
10717 }
10718
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010719 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010720 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010721 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10722 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010723 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010724 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 }
10726#endif
10727}
10728
10729/*
10730 * "setloclist()" function
10731 */
10732 static void
10733f_setloclist(typval_T *argvars, typval_T *rettv)
10734{
10735 win_T *win;
10736
10737 rettv->vval.v_number = -1;
10738
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010739 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010741 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742}
10743
10744/*
10745 * "setmatches()" function
10746 */
10747 static void
10748f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10749{
10750#ifdef FEAT_SEARCH_EXTRA
10751 list_T *l;
10752 listitem_T *li;
10753 dict_T *d;
10754 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010010755 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010756
10757 rettv->vval.v_number = -1;
10758 if (argvars[0].v_type != VAR_LIST)
10759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010760 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761 return;
10762 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010010763 if (win == NULL)
10764 return;
10765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 if ((l = argvars[0].vval.v_list) != NULL)
10767 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 /* To some extent make sure that we are dealing with a list from
10769 * "getmatches()". */
10770 li = l->lv_first;
10771 while (li != NULL)
10772 {
10773 if (li->li_tv.v_type != VAR_DICT
10774 || (d = li->li_tv.vval.v_dict) == NULL)
10775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010776 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 return;
10778 }
10779 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10780 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10781 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10782 && dict_find(d, (char_u *)"priority", -1) != NULL
10783 && dict_find(d, (char_u *)"id", -1) != NULL))
10784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010785 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786 return;
10787 }
10788 li = li->li_next;
10789 }
10790
Bram Moolenaaraff74912019-03-30 18:11:49 +010010791 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 li = l->lv_first;
10793 while (li != NULL)
10794 {
10795 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020010796 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 dictitem_T *di;
10798 char_u *group;
10799 int priority;
10800 int id;
10801 char_u *conceal;
10802
10803 d = li->li_tv.vval.v_dict;
10804 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10805 {
10806 if (s == NULL)
10807 {
10808 s = list_alloc();
10809 if (s == NULL)
10810 return;
10811 }
10812
10813 /* match from matchaddpos() */
10814 for (i = 1; i < 9; i++)
10815 {
10816 sprintf((char *)buf, (char *)"pos%d", i);
10817 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10818 {
10819 if (di->di_tv.v_type != VAR_LIST)
10820 return;
10821
10822 list_append_tv(s, &di->di_tv);
10823 s->lv_refcount++;
10824 }
10825 else
10826 break;
10827 }
10828 }
10829
Bram Moolenaar8f667172018-12-14 15:38:31 +010010830 group = dict_get_string(d, (char_u *)"group", TRUE);
10831 priority = (int)dict_get_number(d, (char_u *)"priority");
10832 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010833 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010010834 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 : NULL;
10836 if (i == 0)
10837 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010010838 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010010839 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840 priority, id, NULL, conceal);
10841 }
10842 else
10843 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010010844 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 list_unref(s);
10846 s = NULL;
10847 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010848 vim_free(group);
10849 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850
10851 li = li->li_next;
10852 }
10853 rettv->vval.v_number = 0;
10854 }
10855#endif
10856}
10857
10858/*
10859 * "setpos()" function
10860 */
10861 static void
10862f_setpos(typval_T *argvars, typval_T *rettv)
10863{
10864 pos_T pos;
10865 int fnum;
10866 char_u *name;
10867 colnr_T curswant = -1;
10868
10869 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010870 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010871 if (name != NULL)
10872 {
10873 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10874 {
10875 if (--pos.col < 0)
10876 pos.col = 0;
10877 if (name[0] == '.' && name[1] == NUL)
10878 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010879 /* set cursor; "fnum" is ignored */
10880 curwin->w_cursor = pos;
10881 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010882 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010883 curwin->w_curswant = curswant - 1;
10884 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010886 check_cursor();
10887 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 }
10889 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10890 {
10891 /* set mark */
10892 if (setmark_pos(name[1], &pos, fnum) == OK)
10893 rettv->vval.v_number = 0;
10894 }
10895 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010896 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 }
10898 }
10899}
10900
10901/*
10902 * "setqflist()" function
10903 */
10904 static void
10905f_setqflist(typval_T *argvars, typval_T *rettv)
10906{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010907 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908}
10909
10910/*
10911 * "setreg()" function
10912 */
10913 static void
10914f_setreg(typval_T *argvars, typval_T *rettv)
10915{
10916 int regname;
10917 char_u *strregname;
10918 char_u *stropt;
10919 char_u *strval;
10920 int append;
10921 char_u yank_type;
10922 long block_len;
10923
10924 block_len = -1;
10925 yank_type = MAUTO;
10926 append = FALSE;
10927
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010928 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 rettv->vval.v_number = 1; /* FAIL is default */
10930
10931 if (strregname == NULL)
10932 return; /* type error; errmsg already given */
10933 regname = *strregname;
10934 if (regname == 0 || regname == '@')
10935 regname = '"';
10936
10937 if (argvars[2].v_type != VAR_UNKNOWN)
10938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010939 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 if (stropt == NULL)
10941 return; /* type error */
10942 for (; *stropt != NUL; ++stropt)
10943 switch (*stropt)
10944 {
10945 case 'a': case 'A': /* append */
10946 append = TRUE;
10947 break;
10948 case 'v': case 'c': /* character-wise selection */
10949 yank_type = MCHAR;
10950 break;
10951 case 'V': case 'l': /* line-wise selection */
10952 yank_type = MLINE;
10953 break;
10954 case 'b': case Ctrl_V: /* block-wise selection */
10955 yank_type = MBLOCK;
10956 if (VIM_ISDIGIT(stropt[1]))
10957 {
10958 ++stropt;
10959 block_len = getdigits(&stropt) - 1;
10960 --stropt;
10961 }
10962 break;
10963 }
10964 }
10965
10966 if (argvars[1].v_type == VAR_LIST)
10967 {
10968 char_u **lstval;
10969 char_u **allocval;
10970 char_u buf[NUMBUFLEN];
10971 char_u **curval;
10972 char_u **curallocval;
10973 list_T *ll = argvars[1].vval.v_list;
10974 listitem_T *li;
10975 int len;
10976
10977 /* If the list is NULL handle like an empty list. */
10978 len = ll == NULL ? 0 : ll->lv_len;
10979
10980 /* First half: use for pointers to result lines; second half: use for
10981 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010982 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 if (lstval == NULL)
10984 return;
10985 curval = lstval;
10986 allocval = lstval + len + 2;
10987 curallocval = allocval;
10988
10989 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10990 li = li->li_next)
10991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010992 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010993 if (strval == NULL)
10994 goto free_lstval;
10995 if (strval == buf)
10996 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010997 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010998 * overwrite the string. */
10999 strval = vim_strsave(buf);
11000 if (strval == NULL)
11001 goto free_lstval;
11002 *curallocval++ = strval;
11003 }
11004 *curval++ = strval;
11005 }
11006 *curval++ = NULL;
11007
11008 write_reg_contents_lst(regname, lstval, -1,
11009 append, yank_type, block_len);
11010free_lstval:
11011 while (curallocval > allocval)
11012 vim_free(*--curallocval);
11013 vim_free(lstval);
11014 }
11015 else
11016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011017 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011018 if (strval == NULL)
11019 return;
11020 write_reg_contents_ex(regname, strval, -1,
11021 append, yank_type, block_len);
11022 }
11023 rettv->vval.v_number = 0;
11024}
11025
11026/*
11027 * "settabvar()" function
11028 */
11029 static void
11030f_settabvar(typval_T *argvars, typval_T *rettv)
11031{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 tabpage_T *save_curtab;
11033 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 char_u *varname, *tabvarname;
11035 typval_T *varp;
11036
11037 rettv->vval.v_number = 0;
11038
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011039 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 return;
11041
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011042 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11043 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 varp = &argvars[2];
11045
Bram Moolenaar4033c552017-09-16 20:54:51 +020011046 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 save_curtab = curtab;
11049 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050
Bram Moolenaar964b3742019-05-24 18:54:09 +020011051 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 if (tabvarname != NULL)
11053 {
11054 STRCPY(tabvarname, "t:");
11055 STRCPY(tabvarname + 2, varname);
11056 set_var(tabvarname, varp, TRUE);
11057 vim_free(tabvarname);
11058 }
11059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 /* Restore current tabpage */
11061 if (valid_tabpage(save_curtab))
11062 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 }
11064}
11065
11066/*
11067 * "settabwinvar()" function
11068 */
11069 static void
11070f_settabwinvar(typval_T *argvars, typval_T *rettv)
11071{
11072 setwinvar(argvars, rettv, 1);
11073}
11074
11075/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011076 * "settagstack()" function
11077 */
11078 static void
11079f_settagstack(typval_T *argvars, typval_T *rettv)
11080{
11081 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11082 win_T *wp;
11083 dict_T *d;
11084 int action = 'r';
11085
11086 rettv->vval.v_number = -1;
11087
11088 // first argument: window number or id
11089 wp = find_win_by_nr_or_id(&argvars[0]);
11090 if (wp == NULL)
11091 return;
11092
11093 // second argument: dict with items to set in the tag stack
11094 if (argvars[1].v_type != VAR_DICT)
11095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011096 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011097 return;
11098 }
11099 d = argvars[1].vval.v_dict;
11100 if (d == NULL)
11101 return;
11102
11103 // third argument: action - 'a' for append and 'r' for replace.
11104 // default is to replace the stack.
11105 if (argvars[2].v_type == VAR_UNKNOWN)
11106 action = 'r';
11107 else if (argvars[2].v_type == VAR_STRING)
11108 {
11109 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011110 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011111 if (actstr == NULL)
11112 return;
11113 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11114 action = *actstr;
11115 else
11116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011117 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011118 return;
11119 }
11120 }
11121 else
11122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011123 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011124 return;
11125 }
11126
11127 if (set_tagstack(wp, d, action) == OK)
11128 rettv->vval.v_number = 0;
11129}
11130
11131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132 * "setwinvar()" function
11133 */
11134 static void
11135f_setwinvar(typval_T *argvars, typval_T *rettv)
11136{
11137 setwinvar(argvars, rettv, 0);
11138}
11139
11140#ifdef FEAT_CRYPT
11141/*
11142 * "sha256({string})" function
11143 */
11144 static void
11145f_sha256(typval_T *argvars, typval_T *rettv)
11146{
11147 char_u *p;
11148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011149 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 rettv->vval.v_string = vim_strsave(
11151 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11152 rettv->v_type = VAR_STRING;
11153}
11154#endif /* FEAT_CRYPT */
11155
11156/*
11157 * "shellescape({string})" function
11158 */
11159 static void
11160f_shellescape(typval_T *argvars, typval_T *rettv)
11161{
Bram Moolenaar20615522017-06-05 18:46:26 +020011162 int do_special = non_zero_arg(&argvars[1]);
11163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011165 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 rettv->v_type = VAR_STRING;
11167}
11168
11169/*
11170 * shiftwidth() function
11171 */
11172 static void
11173f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11174{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011175 rettv->vval.v_number = 0;
11176
11177 if (argvars[0].v_type != VAR_UNKNOWN)
11178 {
11179 long col;
11180
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011181 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011182 if (col < 0)
11183 return; // type error; errmsg already given
11184#ifdef FEAT_VARTABS
11185 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11186 return;
11187#endif
11188 }
11189
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011190 rettv->vval.v_number = get_sw_value(curbuf);
11191}
11192
11193/*
11194 * "simplify()" function
11195 */
11196 static void
11197f_simplify(typval_T *argvars, typval_T *rettv)
11198{
11199 char_u *p;
11200
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011201 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202 rettv->vval.v_string = vim_strsave(p);
11203 simplify_filename(rettv->vval.v_string); /* simplify in place */
11204 rettv->v_type = VAR_STRING;
11205}
11206
11207#ifdef FEAT_FLOAT
11208/*
11209 * "sin()" function
11210 */
11211 static void
11212f_sin(typval_T *argvars, typval_T *rettv)
11213{
11214 float_T f = 0.0;
11215
11216 rettv->v_type = VAR_FLOAT;
11217 if (get_float_arg(argvars, &f) == OK)
11218 rettv->vval.v_float = sin(f);
11219 else
11220 rettv->vval.v_float = 0.0;
11221}
11222
11223/*
11224 * "sinh()" function
11225 */
11226 static void
11227f_sinh(typval_T *argvars, typval_T *rettv)
11228{
11229 float_T f = 0.0;
11230
11231 rettv->v_type = VAR_FLOAT;
11232 if (get_float_arg(argvars, &f) == OK)
11233 rettv->vval.v_float = sinh(f);
11234 else
11235 rettv->vval.v_float = 0.0;
11236}
11237#endif
11238
Bram Moolenaareae1b912019-05-09 15:12:55 +020011239static int item_compare(const void *s1, const void *s2);
11240static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241
11242/* struct used in the array that's given to qsort() */
11243typedef struct
11244{
11245 listitem_T *item;
11246 int idx;
11247} sortItem_T;
11248
11249/* struct storing information about current sort */
11250typedef struct
11251{
11252 int item_compare_ic;
11253 int item_compare_numeric;
11254 int item_compare_numbers;
11255#ifdef FEAT_FLOAT
11256 int item_compare_float;
11257#endif
11258 char_u *item_compare_func;
11259 partial_T *item_compare_partial;
11260 dict_T *item_compare_selfdict;
11261 int item_compare_func_err;
11262 int item_compare_keep_zero;
11263} sortinfo_T;
11264static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011265#define ITEM_COMPARE_FAIL 999
11266
11267/*
11268 * Compare functions for f_sort() and f_uniq() below.
11269 */
11270 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011271item_compare(const void *s1, const void *s2)
11272{
11273 sortItem_T *si1, *si2;
11274 typval_T *tv1, *tv2;
11275 char_u *p1, *p2;
11276 char_u *tofree1 = NULL, *tofree2 = NULL;
11277 int res;
11278 char_u numbuf1[NUMBUFLEN];
11279 char_u numbuf2[NUMBUFLEN];
11280
11281 si1 = (sortItem_T *)s1;
11282 si2 = (sortItem_T *)s2;
11283 tv1 = &si1->item->li_tv;
11284 tv2 = &si2->item->li_tv;
11285
11286 if (sortinfo->item_compare_numbers)
11287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011288 varnumber_T v1 = tv_get_number(tv1);
11289 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011290
11291 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11292 }
11293
11294#ifdef FEAT_FLOAT
11295 if (sortinfo->item_compare_float)
11296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011297 float_T v1 = tv_get_float(tv1);
11298 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299
11300 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11301 }
11302#endif
11303
11304 /* tv2string() puts quotes around a string and allocates memory. Don't do
11305 * that for string variables. Use a single quote when comparing with a
11306 * non-string to do what the docs promise. */
11307 if (tv1->v_type == VAR_STRING)
11308 {
11309 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11310 p1 = (char_u *)"'";
11311 else
11312 p1 = tv1->vval.v_string;
11313 }
11314 else
11315 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11316 if (tv2->v_type == VAR_STRING)
11317 {
11318 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11319 p2 = (char_u *)"'";
11320 else
11321 p2 = tv2->vval.v_string;
11322 }
11323 else
11324 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11325 if (p1 == NULL)
11326 p1 = (char_u *)"";
11327 if (p2 == NULL)
11328 p2 = (char_u *)"";
11329 if (!sortinfo->item_compare_numeric)
11330 {
11331 if (sortinfo->item_compare_ic)
11332 res = STRICMP(p1, p2);
11333 else
11334 res = STRCMP(p1, p2);
11335 }
11336 else
11337 {
11338 double n1, n2;
11339 n1 = strtod((char *)p1, (char **)&p1);
11340 n2 = strtod((char *)p2, (char **)&p2);
11341 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11342 }
11343
11344 /* When the result would be zero, compare the item indexes. Makes the
11345 * sort stable. */
11346 if (res == 0 && !sortinfo->item_compare_keep_zero)
11347 res = si1->idx > si2->idx ? 1 : -1;
11348
11349 vim_free(tofree1);
11350 vim_free(tofree2);
11351 return res;
11352}
11353
11354 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355item_compare2(const void *s1, const void *s2)
11356{
11357 sortItem_T *si1, *si2;
11358 int res;
11359 typval_T rettv;
11360 typval_T argv[3];
11361 int dummy;
11362 char_u *func_name;
11363 partial_T *partial = sortinfo->item_compare_partial;
11364
11365 /* shortcut after failure in previous call; compare all items equal */
11366 if (sortinfo->item_compare_func_err)
11367 return 0;
11368
11369 si1 = (sortItem_T *)s1;
11370 si2 = (sortItem_T *)s2;
11371
11372 if (partial == NULL)
11373 func_name = sortinfo->item_compare_func;
11374 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011375 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011376
11377 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11378 * in the copy without changing the original list items. */
11379 copy_tv(&si1->item->li_tv, &argv[0]);
11380 copy_tv(&si2->item->li_tv, &argv[1]);
11381
11382 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011383 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 partial, sortinfo->item_compare_selfdict);
11385 clear_tv(&argv[0]);
11386 clear_tv(&argv[1]);
11387
11388 if (res == FAIL)
11389 res = ITEM_COMPARE_FAIL;
11390 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011391 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392 if (sortinfo->item_compare_func_err)
11393 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11394 clear_tv(&rettv);
11395
11396 /* When the result would be zero, compare the pointers themselves. Makes
11397 * the sort stable. */
11398 if (res == 0 && !sortinfo->item_compare_keep_zero)
11399 res = si1->idx > si2->idx ? 1 : -1;
11400
11401 return res;
11402}
11403
11404/*
11405 * "sort({list})" function
11406 */
11407 static void
11408do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11409{
11410 list_T *l;
11411 listitem_T *li;
11412 sortItem_T *ptrs;
11413 sortinfo_T *old_sortinfo;
11414 sortinfo_T info;
11415 long len;
11416 long i;
11417
11418 /* Pointer to current info struct used in compare function. Save and
11419 * restore the current one for nested calls. */
11420 old_sortinfo = sortinfo;
11421 sortinfo = &info;
11422
11423 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011424 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011425 else
11426 {
11427 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011428 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011429 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11430 TRUE))
11431 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011432 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433
11434 len = list_len(l);
11435 if (len <= 1)
11436 goto theend; /* short list sorts pretty quickly */
11437
11438 info.item_compare_ic = FALSE;
11439 info.item_compare_numeric = FALSE;
11440 info.item_compare_numbers = FALSE;
11441#ifdef FEAT_FLOAT
11442 info.item_compare_float = FALSE;
11443#endif
11444 info.item_compare_func = NULL;
11445 info.item_compare_partial = NULL;
11446 info.item_compare_selfdict = NULL;
11447 if (argvars[1].v_type != VAR_UNKNOWN)
11448 {
11449 /* optional second argument: {func} */
11450 if (argvars[1].v_type == VAR_FUNC)
11451 info.item_compare_func = argvars[1].vval.v_string;
11452 else if (argvars[1].v_type == VAR_PARTIAL)
11453 info.item_compare_partial = argvars[1].vval.v_partial;
11454 else
11455 {
11456 int error = FALSE;
11457
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011458 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011459 if (error)
11460 goto theend; /* type error; errmsg already given */
11461 if (i == 1)
11462 info.item_compare_ic = TRUE;
11463 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011464 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465 else if (i != 0)
11466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011467 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 goto theend;
11469 }
11470 if (info.item_compare_func != NULL)
11471 {
11472 if (*info.item_compare_func == NUL)
11473 {
11474 /* empty string means default sort */
11475 info.item_compare_func = NULL;
11476 }
11477 else if (STRCMP(info.item_compare_func, "n") == 0)
11478 {
11479 info.item_compare_func = NULL;
11480 info.item_compare_numeric = TRUE;
11481 }
11482 else if (STRCMP(info.item_compare_func, "N") == 0)
11483 {
11484 info.item_compare_func = NULL;
11485 info.item_compare_numbers = TRUE;
11486 }
11487#ifdef FEAT_FLOAT
11488 else if (STRCMP(info.item_compare_func, "f") == 0)
11489 {
11490 info.item_compare_func = NULL;
11491 info.item_compare_float = TRUE;
11492 }
11493#endif
11494 else if (STRCMP(info.item_compare_func, "i") == 0)
11495 {
11496 info.item_compare_func = NULL;
11497 info.item_compare_ic = TRUE;
11498 }
11499 }
11500 }
11501
11502 if (argvars[2].v_type != VAR_UNKNOWN)
11503 {
11504 /* optional third argument: {dict} */
11505 if (argvars[2].v_type != VAR_DICT)
11506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011507 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508 goto theend;
11509 }
11510 info.item_compare_selfdict = argvars[2].vval.v_dict;
11511 }
11512 }
11513
11514 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011515 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 if (ptrs == NULL)
11517 goto theend;
11518
11519 i = 0;
11520 if (sort)
11521 {
11522 /* sort(): ptrs will be the list to sort */
11523 for (li = l->lv_first; li != NULL; li = li->li_next)
11524 {
11525 ptrs[i].item = li;
11526 ptrs[i].idx = i;
11527 ++i;
11528 }
11529
11530 info.item_compare_func_err = FALSE;
11531 info.item_compare_keep_zero = FALSE;
11532 /* test the compare function */
11533 if ((info.item_compare_func != NULL
11534 || info.item_compare_partial != NULL)
11535 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11536 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011537 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538 else
11539 {
11540 /* Sort the array with item pointers. */
11541 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11542 info.item_compare_func == NULL
11543 && info.item_compare_partial == NULL
11544 ? item_compare : item_compare2);
11545
11546 if (!info.item_compare_func_err)
11547 {
11548 /* Clear the List and append the items in sorted order. */
11549 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11550 l->lv_len = 0;
11551 for (i = 0; i < len; ++i)
11552 list_append(l, ptrs[i].item);
11553 }
11554 }
11555 }
11556 else
11557 {
11558 int (*item_compare_func_ptr)(const void *, const void *);
11559
11560 /* f_uniq(): ptrs will be a stack of items to remove */
11561 info.item_compare_func_err = FALSE;
11562 info.item_compare_keep_zero = TRUE;
11563 item_compare_func_ptr = info.item_compare_func != NULL
11564 || info.item_compare_partial != NULL
11565 ? item_compare2 : item_compare;
11566
11567 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11568 li = li->li_next)
11569 {
11570 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11571 == 0)
11572 ptrs[i++].item = li;
11573 if (info.item_compare_func_err)
11574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011575 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 break;
11577 }
11578 }
11579
11580 if (!info.item_compare_func_err)
11581 {
11582 while (--i >= 0)
11583 {
11584 li = ptrs[i].item->li_next;
11585 ptrs[i].item->li_next = li->li_next;
11586 if (li->li_next != NULL)
11587 li->li_next->li_prev = ptrs[i].item;
11588 else
11589 l->lv_last = ptrs[i].item;
11590 list_fix_watch(l, li);
11591 listitem_free(li);
11592 l->lv_len--;
11593 }
11594 }
11595 }
11596
11597 vim_free(ptrs);
11598 }
11599theend:
11600 sortinfo = old_sortinfo;
11601}
11602
11603/*
11604 * "sort({list})" function
11605 */
11606 static void
11607f_sort(typval_T *argvars, typval_T *rettv)
11608{
11609 do_sort_uniq(argvars, rettv, TRUE);
11610}
11611
11612/*
11613 * "uniq({list})" function
11614 */
11615 static void
11616f_uniq(typval_T *argvars, typval_T *rettv)
11617{
11618 do_sort_uniq(argvars, rettv, FALSE);
11619}
11620
11621/*
11622 * "soundfold({word})" function
11623 */
11624 static void
11625f_soundfold(typval_T *argvars, typval_T *rettv)
11626{
11627 char_u *s;
11628
11629 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011630 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011631#ifdef FEAT_SPELL
11632 rettv->vval.v_string = eval_soundfold(s);
11633#else
11634 rettv->vval.v_string = vim_strsave(s);
11635#endif
11636}
11637
11638/*
11639 * "spellbadword()" function
11640 */
11641 static void
11642f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11643{
11644 char_u *word = (char_u *)"";
11645 hlf_T attr = HLF_COUNT;
11646 int len = 0;
11647
11648 if (rettv_list_alloc(rettv) == FAIL)
11649 return;
11650
11651#ifdef FEAT_SPELL
11652 if (argvars[0].v_type == VAR_UNKNOWN)
11653 {
11654 /* Find the start and length of the badly spelled word. */
11655 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11656 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011657 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011659 curwin->w_set_curswant = TRUE;
11660 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011661 }
11662 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011664 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 int capcol = -1;
11666
11667 if (str != NULL)
11668 {
11669 /* Check the argument for spelling. */
11670 while (*str != NUL)
11671 {
11672 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11673 if (attr != HLF_COUNT)
11674 {
11675 word = str;
11676 break;
11677 }
11678 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011679 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 }
11681 }
11682 }
11683#endif
11684
11685 list_append_string(rettv->vval.v_list, word, len);
11686 list_append_string(rettv->vval.v_list, (char_u *)(
11687 attr == HLF_SPB ? "bad" :
11688 attr == HLF_SPR ? "rare" :
11689 attr == HLF_SPL ? "local" :
11690 attr == HLF_SPC ? "caps" :
11691 ""), -1);
11692}
11693
11694/*
11695 * "spellsuggest()" function
11696 */
11697 static void
11698f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11699{
11700#ifdef FEAT_SPELL
11701 char_u *str;
11702 int typeerr = FALSE;
11703 int maxcount;
11704 garray_T ga;
11705 int i;
11706 listitem_T *li;
11707 int need_capital = FALSE;
11708#endif
11709
11710 if (rettv_list_alloc(rettv) == FAIL)
11711 return;
11712
11713#ifdef FEAT_SPELL
11714 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011716 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 if (argvars[1].v_type != VAR_UNKNOWN)
11718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011719 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720 if (maxcount <= 0)
11721 return;
11722 if (argvars[2].v_type != VAR_UNKNOWN)
11723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011724 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 if (typeerr)
11726 return;
11727 }
11728 }
11729 else
11730 maxcount = 25;
11731
11732 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11733
11734 for (i = 0; i < ga.ga_len; ++i)
11735 {
11736 str = ((char_u **)ga.ga_data)[i];
11737
11738 li = listitem_alloc();
11739 if (li == NULL)
11740 vim_free(str);
11741 else
11742 {
11743 li->li_tv.v_type = VAR_STRING;
11744 li->li_tv.v_lock = 0;
11745 li->li_tv.vval.v_string = str;
11746 list_append(rettv->vval.v_list, li);
11747 }
11748 }
11749 ga_clear(&ga);
11750 }
11751#endif
11752}
11753
11754 static void
11755f_split(typval_T *argvars, typval_T *rettv)
11756{
11757 char_u *str;
11758 char_u *end;
11759 char_u *pat = NULL;
11760 regmatch_T regmatch;
11761 char_u patbuf[NUMBUFLEN];
11762 char_u *save_cpo;
11763 int match;
11764 colnr_T col = 0;
11765 int keepempty = FALSE;
11766 int typeerr = FALSE;
11767
11768 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11769 save_cpo = p_cpo;
11770 p_cpo = (char_u *)"";
11771
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011772 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011773 if (argvars[1].v_type != VAR_UNKNOWN)
11774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011775 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011776 if (pat == NULL)
11777 typeerr = TRUE;
11778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011779 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 }
11781 if (pat == NULL || *pat == NUL)
11782 pat = (char_u *)"[\\x01- ]\\+";
11783
11784 if (rettv_list_alloc(rettv) == FAIL)
11785 return;
11786 if (typeerr)
11787 return;
11788
11789 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11790 if (regmatch.regprog != NULL)
11791 {
11792 regmatch.rm_ic = FALSE;
11793 while (*str != NUL || keepempty)
11794 {
11795 if (*str == NUL)
11796 match = FALSE; /* empty item at the end */
11797 else
11798 match = vim_regexec_nl(&regmatch, str, col);
11799 if (match)
11800 end = regmatch.startp[0];
11801 else
11802 end = str + STRLEN(str);
11803 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11804 && *str != NUL && match && end < regmatch.endp[0]))
11805 {
11806 if (list_append_string(rettv->vval.v_list, str,
11807 (int)(end - str)) == FAIL)
11808 break;
11809 }
11810 if (!match)
11811 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010011812 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813 if (regmatch.endp[0] > str)
11814 col = 0;
11815 else
Bram Moolenaar13505972019-01-24 15:04:48 +010011816 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011818 str = regmatch.endp[0];
11819 }
11820
11821 vim_regfree(regmatch.regprog);
11822 }
11823
11824 p_cpo = save_cpo;
11825}
11826
11827#ifdef FEAT_FLOAT
11828/*
11829 * "sqrt()" function
11830 */
11831 static void
11832f_sqrt(typval_T *argvars, typval_T *rettv)
11833{
11834 float_T f = 0.0;
11835
11836 rettv->v_type = VAR_FLOAT;
11837 if (get_float_arg(argvars, &f) == OK)
11838 rettv->vval.v_float = sqrt(f);
11839 else
11840 rettv->vval.v_float = 0.0;
11841}
11842
11843/*
11844 * "str2float()" function
11845 */
11846 static void
11847f_str2float(typval_T *argvars, typval_T *rettv)
11848{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011849 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011850 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011851
Bram Moolenaar08243d22017-01-10 16:12:29 +010011852 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 p = skipwhite(p + 1);
11854 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011855 if (isneg)
11856 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857 rettv->v_type = VAR_FLOAT;
11858}
11859#endif
11860
11861/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020011862 * "str2list()" function
11863 */
11864 static void
11865f_str2list(typval_T *argvars, typval_T *rettv)
11866{
11867 char_u *p;
11868 int utf8 = FALSE;
11869
11870 if (rettv_list_alloc(rettv) == FAIL)
11871 return;
11872
11873 if (argvars[1].v_type != VAR_UNKNOWN)
11874 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
11875
11876 p = tv_get_string(&argvars[0]);
11877
11878 if (has_mbyte || utf8)
11879 {
11880 int (*ptr2len)(char_u *);
11881 int (*ptr2char)(char_u *);
11882
11883 if (utf8 || enc_utf8)
11884 {
11885 ptr2len = utf_ptr2len;
11886 ptr2char = utf_ptr2char;
11887 }
11888 else
11889 {
11890 ptr2len = mb_ptr2len;
11891 ptr2char = mb_ptr2char;
11892 }
11893
11894 for ( ; *p != NUL; p += (*ptr2len)(p))
11895 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
11896 }
11897 else
11898 for ( ; *p != NUL; ++p)
11899 list_append_number(rettv->vval.v_list, *p);
11900}
11901
11902/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903 * "str2nr()" function
11904 */
11905 static void
11906f_str2nr(typval_T *argvars, typval_T *rettv)
11907{
11908 int base = 10;
11909 char_u *p;
11910 varnumber_T n;
11911 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011912 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011913
11914 if (argvars[1].v_type != VAR_UNKNOWN)
11915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011916 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 if (base != 2 && base != 8 && base != 10 && base != 16)
11918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011919 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 return;
11921 }
11922 }
11923
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011924 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011925 isneg = (*p == '-');
11926 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011927 p = skipwhite(p + 1);
11928 switch (base)
11929 {
11930 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11931 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11932 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11933 default: what = 0;
11934 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020011935 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
11936 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010011937 if (isneg)
11938 rettv->vval.v_number = -n;
11939 else
11940 rettv->vval.v_number = n;
11941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011942}
11943
11944#ifdef HAVE_STRFTIME
11945/*
11946 * "strftime({format}[, {time}])" function
11947 */
11948 static void
11949f_strftime(typval_T *argvars, typval_T *rettv)
11950{
11951 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020011952 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011953 struct tm *curtime;
11954 time_t seconds;
11955 char_u *p;
11956
11957 rettv->v_type = VAR_STRING;
11958
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011959 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960 if (argvars[1].v_type == VAR_UNKNOWN)
11961 seconds = time(NULL);
11962 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011963 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020011964 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011965 /* MSVC returns NULL for an invalid value of seconds. */
11966 if (curtime == NULL)
11967 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11968 else
11969 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011970 vimconv_T conv;
11971 char_u *enc;
11972
11973 conv.vc_type = CONV_NONE;
11974 enc = enc_locale();
11975 convert_setup(&conv, p_enc, enc);
11976 if (conv.vc_type != CONV_NONE)
11977 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011978 if (p != NULL)
11979 (void)strftime((char *)result_buf, sizeof(result_buf),
11980 (char *)p, curtime);
11981 else
11982 result_buf[0] = NUL;
11983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011984 if (conv.vc_type != CONV_NONE)
11985 vim_free(p);
11986 convert_setup(&conv, enc, p_enc);
11987 if (conv.vc_type != CONV_NONE)
11988 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11989 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011990 rettv->vval.v_string = vim_strsave(result_buf);
11991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011992 /* Release conversion descriptors */
11993 convert_setup(&conv, NULL, NULL);
11994 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011995 }
11996}
11997#endif
11998
11999/*
12000 * "strgetchar()" function
12001 */
12002 static void
12003f_strgetchar(typval_T *argvars, typval_T *rettv)
12004{
12005 char_u *str;
12006 int len;
12007 int error = FALSE;
12008 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012009 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012010
12011 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012012 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012013 if (str == NULL)
12014 return;
12015 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012016 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012017 if (error)
12018 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012019
Bram Moolenaar13505972019-01-24 15:04:48 +010012020 while (charidx >= 0 && byteidx < len)
12021 {
12022 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012023 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012024 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12025 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012026 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012027 --charidx;
12028 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012030}
12031
12032/*
12033 * "stridx()" function
12034 */
12035 static void
12036f_stridx(typval_T *argvars, typval_T *rettv)
12037{
12038 char_u buf[NUMBUFLEN];
12039 char_u *needle;
12040 char_u *haystack;
12041 char_u *save_haystack;
12042 char_u *pos;
12043 int start_idx;
12044
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012045 needle = tv_get_string_chk(&argvars[1]);
12046 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047 rettv->vval.v_number = -1;
12048 if (needle == NULL || haystack == NULL)
12049 return; /* type error; errmsg already given */
12050
12051 if (argvars[2].v_type != VAR_UNKNOWN)
12052 {
12053 int error = FALSE;
12054
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012055 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012056 if (error || start_idx >= (int)STRLEN(haystack))
12057 return;
12058 if (start_idx >= 0)
12059 haystack += start_idx;
12060 }
12061
12062 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12063 if (pos != NULL)
12064 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12065}
12066
12067/*
12068 * "string()" function
12069 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012070 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012071f_string(typval_T *argvars, typval_T *rettv)
12072{
12073 char_u *tofree;
12074 char_u numbuf[NUMBUFLEN];
12075
12076 rettv->v_type = VAR_STRING;
12077 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12078 get_copyID());
12079 /* Make a copy if we have a value but it's not in allocated memory. */
12080 if (rettv->vval.v_string != NULL && tofree == NULL)
12081 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12082}
12083
12084/*
12085 * "strlen()" function
12086 */
12087 static void
12088f_strlen(typval_T *argvars, typval_T *rettv)
12089{
12090 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012091 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012092}
12093
12094/*
12095 * "strchars()" function
12096 */
12097 static void
12098f_strchars(typval_T *argvars, typval_T *rettv)
12099{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012100 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012101 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012102 varnumber_T len = 0;
12103 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012104
12105 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012106 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012107 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012108 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012109 else
12110 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012111 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12112 while (*s != NUL)
12113 {
12114 func_mb_ptr2char_adv(&s);
12115 ++len;
12116 }
12117 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012118 }
12119}
12120
12121/*
12122 * "strdisplaywidth()" function
12123 */
12124 static void
12125f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12126{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012127 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012128 int col = 0;
12129
12130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012131 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012132
12133 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12134}
12135
12136/*
12137 * "strwidth()" function
12138 */
12139 static void
12140f_strwidth(typval_T *argvars, typval_T *rettv)
12141{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012142 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143
Bram Moolenaar13505972019-01-24 15:04:48 +010012144 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012145}
12146
12147/*
12148 * "strcharpart()" function
12149 */
12150 static void
12151f_strcharpart(typval_T *argvars, typval_T *rettv)
12152{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 char_u *p;
12154 int nchar;
12155 int nbyte = 0;
12156 int charlen;
12157 int len = 0;
12158 int slen;
12159 int error = FALSE;
12160
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012161 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 slen = (int)STRLEN(p);
12163
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012164 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012165 if (!error)
12166 {
12167 if (nchar > 0)
12168 while (nchar > 0 && nbyte < slen)
12169 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012170 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012171 --nchar;
12172 }
12173 else
12174 nbyte = nchar;
12175 if (argvars[2].v_type != VAR_UNKNOWN)
12176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012178 while (charlen > 0 && nbyte + len < slen)
12179 {
12180 int off = nbyte + len;
12181
12182 if (off < 0)
12183 len += 1;
12184 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012185 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012186 --charlen;
12187 }
12188 }
12189 else
12190 len = slen - nbyte; /* default: all bytes that are available. */
12191 }
12192
12193 /*
12194 * Only return the overlap between the specified part and the actual
12195 * string.
12196 */
12197 if (nbyte < 0)
12198 {
12199 len += nbyte;
12200 nbyte = 0;
12201 }
12202 else if (nbyte > slen)
12203 nbyte = slen;
12204 if (len < 0)
12205 len = 0;
12206 else if (nbyte + len > slen)
12207 len = slen - nbyte;
12208
12209 rettv->v_type = VAR_STRING;
12210 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012211}
12212
12213/*
12214 * "strpart()" function
12215 */
12216 static void
12217f_strpart(typval_T *argvars, typval_T *rettv)
12218{
12219 char_u *p;
12220 int n;
12221 int len;
12222 int slen;
12223 int error = FALSE;
12224
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012225 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 slen = (int)STRLEN(p);
12227
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229 if (error)
12230 len = 0;
12231 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012232 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012233 else
12234 len = slen - n; /* default len: all bytes that are available. */
12235
12236 /*
12237 * Only return the overlap between the specified part and the actual
12238 * string.
12239 */
12240 if (n < 0)
12241 {
12242 len += n;
12243 n = 0;
12244 }
12245 else if (n > slen)
12246 n = slen;
12247 if (len < 0)
12248 len = 0;
12249 else if (n + len > slen)
12250 len = slen - n;
12251
12252 rettv->v_type = VAR_STRING;
12253 rettv->vval.v_string = vim_strnsave(p + n, len);
12254}
12255
12256/*
12257 * "strridx()" function
12258 */
12259 static void
12260f_strridx(typval_T *argvars, typval_T *rettv)
12261{
12262 char_u buf[NUMBUFLEN];
12263 char_u *needle;
12264 char_u *haystack;
12265 char_u *rest;
12266 char_u *lastmatch = NULL;
12267 int haystack_len, end_idx;
12268
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012269 needle = tv_get_string_chk(&argvars[1]);
12270 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012271
12272 rettv->vval.v_number = -1;
12273 if (needle == NULL || haystack == NULL)
12274 return; /* type error; errmsg already given */
12275
12276 haystack_len = (int)STRLEN(haystack);
12277 if (argvars[2].v_type != VAR_UNKNOWN)
12278 {
12279 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012280 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012281 if (end_idx < 0)
12282 return; /* can never find a match */
12283 }
12284 else
12285 end_idx = haystack_len;
12286
12287 if (*needle == NUL)
12288 {
12289 /* Empty string matches past the end. */
12290 lastmatch = haystack + end_idx;
12291 }
12292 else
12293 {
12294 for (rest = haystack; *rest != '\0'; ++rest)
12295 {
12296 rest = (char_u *)strstr((char *)rest, (char *)needle);
12297 if (rest == NULL || rest > haystack + end_idx)
12298 break;
12299 lastmatch = rest;
12300 }
12301 }
12302
12303 if (lastmatch == NULL)
12304 rettv->vval.v_number = -1;
12305 else
12306 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12307}
12308
12309/*
12310 * "strtrans()" function
12311 */
12312 static void
12313f_strtrans(typval_T *argvars, typval_T *rettv)
12314{
12315 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012316 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012317}
12318
12319/*
12320 * "submatch()" function
12321 */
12322 static void
12323f_submatch(typval_T *argvars, typval_T *rettv)
12324{
12325 int error = FALSE;
12326 int no;
12327 int retList = 0;
12328
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012329 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330 if (error)
12331 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012332 if (no < 0 || no >= NSUBEXP)
12333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012334 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012335 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012336 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012338 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012339 if (error)
12340 return;
12341
12342 if (retList == 0)
12343 {
12344 rettv->v_type = VAR_STRING;
12345 rettv->vval.v_string = reg_submatch(no);
12346 }
12347 else
12348 {
12349 rettv->v_type = VAR_LIST;
12350 rettv->vval.v_list = reg_submatch_list(no);
12351 }
12352}
12353
12354/*
12355 * "substitute()" function
12356 */
12357 static void
12358f_substitute(typval_T *argvars, typval_T *rettv)
12359{
12360 char_u patbuf[NUMBUFLEN];
12361 char_u subbuf[NUMBUFLEN];
12362 char_u flagsbuf[NUMBUFLEN];
12363
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012364 char_u *str = tv_get_string_chk(&argvars[0]);
12365 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012366 char_u *sub = NULL;
12367 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012368 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012369
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012370 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12371 expr = &argvars[2];
12372 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012373 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012375 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012376 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12377 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012378 rettv->vval.v_string = NULL;
12379 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012380 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381}
12382
12383/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012384 * "swapinfo(swap_filename)" function
12385 */
12386 static void
12387f_swapinfo(typval_T *argvars, typval_T *rettv)
12388{
12389 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012390 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012391}
12392
12393/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012394 * "swapname(expr)" function
12395 */
12396 static void
12397f_swapname(typval_T *argvars, typval_T *rettv)
12398{
12399 buf_T *buf;
12400
12401 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012402 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012403 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12404 || buf->b_ml.ml_mfp->mf_fname == NULL)
12405 rettv->vval.v_string = NULL;
12406 else
12407 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12408}
12409
12410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411 * "synID(lnum, col, trans)" function
12412 */
12413 static void
12414f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12415{
12416 int id = 0;
12417#ifdef FEAT_SYN_HL
12418 linenr_T lnum;
12419 colnr_T col;
12420 int trans;
12421 int transerr = FALSE;
12422
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012423 lnum = tv_get_lnum(argvars); /* -1 on type error */
12424 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12425 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426
12427 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12428 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12429 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12430#endif
12431
12432 rettv->vval.v_number = id;
12433}
12434
12435/*
12436 * "synIDattr(id, what [, mode])" function
12437 */
12438 static void
12439f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12440{
12441 char_u *p = NULL;
12442#ifdef FEAT_SYN_HL
12443 int id;
12444 char_u *what;
12445 char_u *mode;
12446 char_u modebuf[NUMBUFLEN];
12447 int modec;
12448
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 id = (int)tv_get_number(&argvars[0]);
12450 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451 if (argvars[2].v_type != VAR_UNKNOWN)
12452 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012453 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 modec = TOLOWER_ASC(mode[0]);
12455 if (modec != 't' && modec != 'c' && modec != 'g')
12456 modec = 0; /* replace invalid with current */
12457 }
12458 else
12459 {
12460#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12461 if (USE_24BIT)
12462 modec = 'g';
12463 else
12464#endif
12465 if (t_colors > 1)
12466 modec = 'c';
12467 else
12468 modec = 't';
12469 }
12470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012471 switch (TOLOWER_ASC(what[0]))
12472 {
12473 case 'b':
12474 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12475 p = highlight_color(id, what, modec);
12476 else /* bold */
12477 p = highlight_has_attr(id, HL_BOLD, modec);
12478 break;
12479
12480 case 'f': /* fg[#] or font */
12481 p = highlight_color(id, what, modec);
12482 break;
12483
12484 case 'i':
12485 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12486 p = highlight_has_attr(id, HL_INVERSE, modec);
12487 else /* italic */
12488 p = highlight_has_attr(id, HL_ITALIC, modec);
12489 break;
12490
12491 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012492 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493 break;
12494
12495 case 'r': /* reverse */
12496 p = highlight_has_attr(id, HL_INVERSE, modec);
12497 break;
12498
12499 case 's':
12500 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12501 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012502 /* strikeout */
12503 else if (TOLOWER_ASC(what[1]) == 't' &&
12504 TOLOWER_ASC(what[2]) == 'r')
12505 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 else /* standout */
12507 p = highlight_has_attr(id, HL_STANDOUT, modec);
12508 break;
12509
12510 case 'u':
12511 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12512 /* underline */
12513 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12514 else
12515 /* undercurl */
12516 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12517 break;
12518 }
12519
12520 if (p != NULL)
12521 p = vim_strsave(p);
12522#endif
12523 rettv->v_type = VAR_STRING;
12524 rettv->vval.v_string = p;
12525}
12526
12527/*
12528 * "synIDtrans(id)" function
12529 */
12530 static void
12531f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12532{
12533 int id;
12534
12535#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012536 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 if (id > 0)
12539 id = syn_get_final_id(id);
12540 else
12541#endif
12542 id = 0;
12543
12544 rettv->vval.v_number = id;
12545}
12546
12547/*
12548 * "synconcealed(lnum, col)" function
12549 */
12550 static void
12551f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12552{
12553#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12554 linenr_T lnum;
12555 colnr_T col;
12556 int syntax_flags = 0;
12557 int cchar;
12558 int matchid = 0;
12559 char_u str[NUMBUFLEN];
12560#endif
12561
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012562 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563
12564#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012565 lnum = tv_get_lnum(argvars); /* -1 on type error */
12566 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012567
12568 vim_memset(str, NUL, sizeof(str));
12569
12570 if (rettv_list_alloc(rettv) != FAIL)
12571 {
12572 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12573 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12574 && curwin->w_p_cole > 0)
12575 {
12576 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12577 syntax_flags = get_syntax_info(&matchid);
12578
12579 /* get the conceal character */
12580 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12581 {
12582 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012583 if (cchar == NUL && curwin->w_p_cole == 1)
12584 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 if (cchar != NUL)
12586 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 if (has_mbyte)
12588 (*mb_char2bytes)(cchar, str);
12589 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590 str[0] = cchar;
12591 }
12592 }
12593 }
12594
12595 list_append_number(rettv->vval.v_list,
12596 (syntax_flags & HL_CONCEAL) != 0);
12597 /* -1 to auto-determine strlen */
12598 list_append_string(rettv->vval.v_list, str, -1);
12599 list_append_number(rettv->vval.v_list, matchid);
12600 }
12601#endif
12602}
12603
12604/*
12605 * "synstack(lnum, col)" function
12606 */
12607 static void
12608f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12609{
12610#ifdef FEAT_SYN_HL
12611 linenr_T lnum;
12612 colnr_T col;
12613 int i;
12614 int id;
12615#endif
12616
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012617 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618
12619#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012620 lnum = tv_get_lnum(argvars); /* -1 on type error */
12621 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622
12623 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12624 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12625 && rettv_list_alloc(rettv) != FAIL)
12626 {
12627 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12628 for (i = 0; ; ++i)
12629 {
12630 id = syn_get_stack_item(i);
12631 if (id < 0)
12632 break;
12633 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12634 break;
12635 }
12636 }
12637#endif
12638}
12639
12640 static void
12641get_cmd_output_as_rettv(
12642 typval_T *argvars,
12643 typval_T *rettv,
12644 int retlist)
12645{
12646 char_u *res = NULL;
12647 char_u *p;
12648 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 int err = FALSE;
12650 FILE *fd;
12651 list_T *list = NULL;
12652 int flags = SHELL_SILENT;
12653
12654 rettv->v_type = VAR_STRING;
12655 rettv->vval.v_string = NULL;
12656 if (check_restricted() || check_secure())
12657 goto errret;
12658
12659 if (argvars[1].v_type != VAR_UNKNOWN)
12660 {
12661 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012662 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 * command.
12664 */
12665 if ((infile = vim_tempname('i', TRUE)) == NULL)
12666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012667 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668 goto errret;
12669 }
12670
12671 fd = mch_fopen((char *)infile, WRITEBIN);
12672 if (fd == NULL)
12673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012674 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012675 goto errret;
12676 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012677 if (argvars[1].v_type == VAR_NUMBER)
12678 {
12679 linenr_T lnum;
12680 buf_T *buf;
12681
12682 buf = buflist_findnr(argvars[1].vval.v_number);
12683 if (buf == NULL)
12684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012685 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012686 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012687 goto errret;
12688 }
12689
12690 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12691 {
12692 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12693 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12694 {
12695 err = TRUE;
12696 break;
12697 }
12698 if (putc(NL, fd) == EOF)
12699 {
12700 err = TRUE;
12701 break;
12702 }
12703 }
12704 }
12705 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706 {
12707 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12708 err = TRUE;
12709 }
12710 else
12711 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012712 size_t len;
12713 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012714
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012715 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012716 if (p == NULL)
12717 {
12718 fclose(fd);
12719 goto errret; /* type error; errmsg already given */
12720 }
12721 len = STRLEN(p);
12722 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12723 err = TRUE;
12724 }
12725 if (fclose(fd) != 0)
12726 err = TRUE;
12727 if (err)
12728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012729 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730 goto errret;
12731 }
12732 }
12733
12734 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12735 * echoes typeahead, that messes up the display. */
12736 if (!msg_silent)
12737 flags += SHELL_COOKED;
12738
12739 if (retlist)
12740 {
12741 int len;
12742 listitem_T *li;
12743 char_u *s = NULL;
12744 char_u *start;
12745 char_u *end;
12746 int i;
12747
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012748 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749 if (res == NULL)
12750 goto errret;
12751
12752 list = list_alloc();
12753 if (list == NULL)
12754 goto errret;
12755
12756 for (i = 0; i < len; ++i)
12757 {
12758 start = res + i;
12759 while (i < len && res[i] != NL)
12760 ++i;
12761 end = res + i;
12762
Bram Moolenaar964b3742019-05-24 18:54:09 +020012763 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012764 if (s == NULL)
12765 goto errret;
12766
12767 for (p = s; start < end; ++p, ++start)
12768 *p = *start == NUL ? NL : *start;
12769 *p = NUL;
12770
12771 li = listitem_alloc();
12772 if (li == NULL)
12773 {
12774 vim_free(s);
12775 goto errret;
12776 }
12777 li->li_tv.v_type = VAR_STRING;
12778 li->li_tv.v_lock = 0;
12779 li->li_tv.vval.v_string = s;
12780 list_append(list, li);
12781 }
12782
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012783 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784 list = NULL;
12785 }
12786 else
12787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012788 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010012789#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 /* translate <CR><NL> into <NL> */
12791 if (res != NULL)
12792 {
12793 char_u *s, *d;
12794
12795 d = res;
12796 for (s = res; *s; ++s)
12797 {
12798 if (s[0] == CAR && s[1] == NL)
12799 ++s;
12800 *d++ = *s;
12801 }
12802 *d = NUL;
12803 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804#endif
12805 rettv->vval.v_string = res;
12806 res = NULL;
12807 }
12808
12809errret:
12810 if (infile != NULL)
12811 {
12812 mch_remove(infile);
12813 vim_free(infile);
12814 }
12815 if (res != NULL)
12816 vim_free(res);
12817 if (list != NULL)
12818 list_free(list);
12819}
12820
12821/*
12822 * "system()" function
12823 */
12824 static void
12825f_system(typval_T *argvars, typval_T *rettv)
12826{
12827 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12828}
12829
12830/*
12831 * "systemlist()" function
12832 */
12833 static void
12834f_systemlist(typval_T *argvars, typval_T *rettv)
12835{
12836 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12837}
12838
12839/*
12840 * "tabpagebuflist()" function
12841 */
12842 static void
12843f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12844{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012845 tabpage_T *tp;
12846 win_T *wp = NULL;
12847
12848 if (argvars[0].v_type == VAR_UNKNOWN)
12849 wp = firstwin;
12850 else
12851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012852 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853 if (tp != NULL)
12854 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12855 }
12856 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12857 {
12858 for (; wp != NULL; wp = wp->w_next)
12859 if (list_append_number(rettv->vval.v_list,
12860 wp->w_buffer->b_fnum) == FAIL)
12861 break;
12862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012863}
12864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865/*
12866 * "tabpagenr()" function
12867 */
12868 static void
12869f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12870{
12871 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012872 char_u *arg;
12873
12874 if (argvars[0].v_type != VAR_UNKNOWN)
12875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 nr = 0;
12878 if (arg != NULL)
12879 {
12880 if (STRCMP(arg, "$") == 0)
12881 nr = tabpage_index(NULL) - 1;
12882 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012883 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 }
12885 }
12886 else
12887 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888 rettv->vval.v_number = nr;
12889}
12890
12891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892/*
12893 * Common code for tabpagewinnr() and winnr().
12894 */
12895 static int
12896get_winnr(tabpage_T *tp, typval_T *argvar)
12897{
12898 win_T *twin;
12899 int nr = 1;
12900 win_T *wp;
12901 char_u *arg;
12902
12903 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12904 if (argvar->v_type != VAR_UNKNOWN)
12905 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012906 int invalid_arg = FALSE;
12907
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012908 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012909 if (arg == NULL)
12910 nr = 0; /* type error; errmsg already given */
12911 else if (STRCMP(arg, "$") == 0)
12912 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12913 else if (STRCMP(arg, "#") == 0)
12914 {
12915 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12916 if (twin == NULL)
12917 nr = 0;
12918 }
12919 else
12920 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020012921 long count;
12922 char_u *endp;
12923
12924 // Extract the window count (if specified). e.g. winnr('3j')
12925 count = strtol((char *)arg, (char **)&endp, 10);
12926 if (count <= 0)
12927 count = 1; // if count is not specified, default to 1
12928 if (endp != NULL && *endp != '\0')
12929 {
12930 if (STRCMP(endp, "j") == 0)
12931 twin = win_vert_neighbor(tp, twin, FALSE, count);
12932 else if (STRCMP(endp, "k") == 0)
12933 twin = win_vert_neighbor(tp, twin, TRUE, count);
12934 else if (STRCMP(endp, "h") == 0)
12935 twin = win_horz_neighbor(tp, twin, TRUE, count);
12936 else if (STRCMP(endp, "l") == 0)
12937 twin = win_horz_neighbor(tp, twin, FALSE, count);
12938 else
12939 invalid_arg = TRUE;
12940 }
12941 else
12942 invalid_arg = TRUE;
12943 }
12944
12945 if (invalid_arg)
12946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012947 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 nr = 0;
12949 }
12950 }
12951
12952 if (nr > 0)
12953 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12954 wp != twin; wp = wp->w_next)
12955 {
12956 if (wp == NULL)
12957 {
12958 /* didn't find it in this tabpage */
12959 nr = 0;
12960 break;
12961 }
12962 ++nr;
12963 }
12964 return nr;
12965}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012966
12967/*
12968 * "tabpagewinnr()" function
12969 */
12970 static void
12971f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12972{
12973 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974 tabpage_T *tp;
12975
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012976 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 if (tp == NULL)
12978 nr = 0;
12979 else
12980 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012981 rettv->vval.v_number = nr;
12982}
12983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012984/*
12985 * "tagfiles()" function
12986 */
12987 static void
12988f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12989{
12990 char_u *fname;
12991 tagname_T tn;
12992 int first;
12993
12994 if (rettv_list_alloc(rettv) == FAIL)
12995 return;
12996 fname = alloc(MAXPATHL);
12997 if (fname == NULL)
12998 return;
12999
13000 for (first = TRUE; ; first = FALSE)
13001 if (get_tagfname(&tn, first, fname) == FAIL
13002 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13003 break;
13004 tagname_free(&tn);
13005 vim_free(fname);
13006}
13007
13008/*
13009 * "taglist()" function
13010 */
13011 static void
13012f_taglist(typval_T *argvars, typval_T *rettv)
13013{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013014 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 char_u *tag_pattern;
13016
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013017 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018
13019 rettv->vval.v_number = FALSE;
13020 if (*tag_pattern == NUL)
13021 return;
13022
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013024 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013026 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027}
13028
13029/*
13030 * "tempname()" function
13031 */
13032 static void
13033f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13034{
13035 static int x = 'A';
13036
13037 rettv->v_type = VAR_STRING;
13038 rettv->vval.v_string = vim_tempname(x, FALSE);
13039
13040 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13041 * names. Skip 'I' and 'O', they are used for shell redirection. */
13042 do
13043 {
13044 if (x == 'Z')
13045 x = '0';
13046 else if (x == '9')
13047 x = 'A';
13048 else
13049 {
13050#ifdef EBCDIC
13051 if (x == 'I')
13052 x = 'J';
13053 else if (x == 'R')
13054 x = 'S';
13055 else
13056#endif
13057 ++x;
13058 }
13059 } while (x == 'I' || x == 'O');
13060}
13061
13062#ifdef FEAT_FLOAT
13063/*
13064 * "tan()" function
13065 */
13066 static void
13067f_tan(typval_T *argvars, typval_T *rettv)
13068{
13069 float_T f = 0.0;
13070
13071 rettv->v_type = VAR_FLOAT;
13072 if (get_float_arg(argvars, &f) == OK)
13073 rettv->vval.v_float = tan(f);
13074 else
13075 rettv->vval.v_float = 0.0;
13076}
13077
13078/*
13079 * "tanh()" function
13080 */
13081 static void
13082f_tanh(typval_T *argvars, typval_T *rettv)
13083{
13084 float_T f = 0.0;
13085
13086 rettv->v_type = VAR_FLOAT;
13087 if (get_float_arg(argvars, &f) == OK)
13088 rettv->vval.v_float = tanh(f);
13089 else
13090 rettv->vval.v_float = 0.0;
13091}
13092#endif
13093
13094/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013095 * Get a callback from "arg". It can be a Funcref or a function name.
13096 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013097 * "cb_name" is not allocated.
13098 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013100 callback_T
13101get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013103 callback_T res;
13104
13105 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013106 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13107 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013108 res.cb_partial = arg->vval.v_partial;
13109 ++res.cb_partial->pt_refcount;
13110 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013111 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013112 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013113 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013114 res.cb_partial = NULL;
13115 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13116 {
13117 // Note that we don't make a copy of the string.
13118 res.cb_name = arg->vval.v_string;
13119 func_ref(res.cb_name);
13120 }
13121 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13122 {
13123 res.cb_name = (char_u *)"";
13124 }
13125 else
13126 {
13127 emsg(_("E921: Invalid callback argument"));
13128 res.cb_name = NULL;
13129 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013130 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013131 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132}
13133
13134/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013135 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 */
13137 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013138put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013140 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013142 tv->v_type = VAR_PARTIAL;
13143 tv->vval.v_partial = cb->cb_partial;
13144 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013146 else
13147 {
13148 tv->v_type = VAR_FUNC;
13149 tv->vval.v_string = vim_strsave(cb->cb_name);
13150 func_ref(cb->cb_name);
13151 }
13152}
13153
13154/*
13155 * Make a copy of "src" into "dest", allocating the function name if needed,
13156 * without incrementing the refcount.
13157 */
13158 void
13159set_callback(callback_T *dest, callback_T *src)
13160{
13161 if (src->cb_partial == NULL)
13162 {
13163 // just a function name, make a copy
13164 dest->cb_name = vim_strsave(src->cb_name);
13165 dest->cb_free_name = TRUE;
13166 }
13167 else
13168 {
13169 // cb_name is a pointer into cb_partial
13170 dest->cb_name = src->cb_name;
13171 dest->cb_free_name = FALSE;
13172 }
13173 dest->cb_partial = src->cb_partial;
13174}
13175
13176/*
13177 * Unref/free "callback" returned by get_callback() or set_callback().
13178 */
13179 void
13180free_callback(callback_T *callback)
13181{
13182 if (callback->cb_partial != NULL)
13183 {
13184 partial_unref(callback->cb_partial);
13185 callback->cb_partial = NULL;
13186 }
13187 else if (callback->cb_name != NULL)
13188 func_unref(callback->cb_name);
13189 if (callback->cb_free_name)
13190 {
13191 vim_free(callback->cb_name);
13192 callback->cb_free_name = FALSE;
13193 }
13194 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196
13197#ifdef FEAT_TIMERS
13198/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013199 * "timer_info([timer])" function
13200 */
13201 static void
13202f_timer_info(typval_T *argvars, typval_T *rettv)
13203{
13204 timer_T *timer = NULL;
13205
13206 if (rettv_list_alloc(rettv) != OK)
13207 return;
13208 if (argvars[0].v_type != VAR_UNKNOWN)
13209 {
13210 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013211 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013212 else
13213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013214 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013215 if (timer != NULL)
13216 add_timer_info(rettv, timer);
13217 }
13218 }
13219 else
13220 add_timer_info_all(rettv);
13221}
13222
13223/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013224 * "timer_pause(timer, paused)" function
13225 */
13226 static void
13227f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13228{
13229 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013230 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013231
13232 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013233 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013234 else
13235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013236 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013237 if (timer != NULL)
13238 timer->tr_paused = paused;
13239 }
13240}
13241
13242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013243 * "timer_start(time, callback [, options])" function
13244 */
13245 static void
13246f_timer_start(typval_T *argvars, typval_T *rettv)
13247{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013248 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013249 timer_T *timer;
13250 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013251 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013252 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013253
Bram Moolenaar75537a92016-09-05 22:45:28 +020013254 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 if (check_secure())
13256 return;
13257 if (argvars[2].v_type != VAR_UNKNOWN)
13258 {
13259 if (argvars[2].v_type != VAR_DICT
13260 || (dict = argvars[2].vval.v_dict) == NULL)
13261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013262 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263 return;
13264 }
13265 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013266 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 }
13268
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013269 callback = get_callback(&argvars[1]);
13270 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013271 return;
13272
13273 timer = create_timer(msec, repeat);
13274 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013275 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013276 else
13277 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013278 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013279 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013280 }
13281}
13282
13283/*
13284 * "timer_stop(timer)" function
13285 */
13286 static void
13287f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13288{
13289 timer_T *timer;
13290
13291 if (argvars[0].v_type != VAR_NUMBER)
13292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013293 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 return;
13295 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013296 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013297 if (timer != NULL)
13298 stop_timer(timer);
13299}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013300
13301/*
13302 * "timer_stopall()" function
13303 */
13304 static void
13305f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13306{
13307 stop_all_timers();
13308}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309#endif
13310
13311/*
13312 * "tolower(string)" function
13313 */
13314 static void
13315f_tolower(typval_T *argvars, typval_T *rettv)
13316{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013318 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319}
13320
13321/*
13322 * "toupper(string)" function
13323 */
13324 static void
13325f_toupper(typval_T *argvars, typval_T *rettv)
13326{
13327 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013328 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329}
13330
13331/*
13332 * "tr(string, fromstr, tostr)" function
13333 */
13334 static void
13335f_tr(typval_T *argvars, typval_T *rettv)
13336{
13337 char_u *in_str;
13338 char_u *fromstr;
13339 char_u *tostr;
13340 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 int inlen;
13342 int fromlen;
13343 int tolen;
13344 int idx;
13345 char_u *cpstr;
13346 int cplen;
13347 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348 char_u buf[NUMBUFLEN];
13349 char_u buf2[NUMBUFLEN];
13350 garray_T ga;
13351
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013352 in_str = tv_get_string(&argvars[0]);
13353 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13354 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013355
13356 /* Default return value: empty string. */
13357 rettv->v_type = VAR_STRING;
13358 rettv->vval.v_string = NULL;
13359 if (fromstr == NULL || tostr == NULL)
13360 return; /* type error; errmsg already given */
13361 ga_init2(&ga, (int)sizeof(char), 80);
13362
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364 /* not multi-byte: fromstr and tostr must be the same length */
13365 if (STRLEN(fromstr) != STRLEN(tostr))
13366 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013367error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013368 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013369 ga_clear(&ga);
13370 return;
13371 }
13372
13373 /* fromstr and tostr have to contain the same number of chars */
13374 while (*in_str != NUL)
13375 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013376 if (has_mbyte)
13377 {
13378 inlen = (*mb_ptr2len)(in_str);
13379 cpstr = in_str;
13380 cplen = inlen;
13381 idx = 0;
13382 for (p = fromstr; *p != NUL; p += fromlen)
13383 {
13384 fromlen = (*mb_ptr2len)(p);
13385 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13386 {
13387 for (p = tostr; *p != NUL; p += tolen)
13388 {
13389 tolen = (*mb_ptr2len)(p);
13390 if (idx-- == 0)
13391 {
13392 cplen = tolen;
13393 cpstr = p;
13394 break;
13395 }
13396 }
13397 if (*p == NUL) /* tostr is shorter than fromstr */
13398 goto error;
13399 break;
13400 }
13401 ++idx;
13402 }
13403
13404 if (first && cpstr == in_str)
13405 {
13406 /* Check that fromstr and tostr have the same number of
13407 * (multi-byte) characters. Done only once when a character
13408 * of in_str doesn't appear in fromstr. */
13409 first = FALSE;
13410 for (p = tostr; *p != NUL; p += tolen)
13411 {
13412 tolen = (*mb_ptr2len)(p);
13413 --idx;
13414 }
13415 if (idx != 0)
13416 goto error;
13417 }
13418
13419 (void)ga_grow(&ga, cplen);
13420 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13421 ga.ga_len += cplen;
13422
13423 in_str += inlen;
13424 }
13425 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 {
13427 /* When not using multi-byte chars we can do it faster. */
13428 p = vim_strchr(fromstr, *in_str);
13429 if (p != NULL)
13430 ga_append(&ga, tostr[p - fromstr]);
13431 else
13432 ga_append(&ga, *in_str);
13433 ++in_str;
13434 }
13435 }
13436
13437 /* add a terminating NUL */
13438 (void)ga_grow(&ga, 1);
13439 ga_append(&ga, NUL);
13440
13441 rettv->vval.v_string = ga.ga_data;
13442}
13443
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013444/*
13445 * "trim({expr})" function
13446 */
13447 static void
13448f_trim(typval_T *argvars, typval_T *rettv)
13449{
13450 char_u buf1[NUMBUFLEN];
13451 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013452 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013453 char_u *mask = NULL;
13454 char_u *tail;
13455 char_u *prev;
13456 char_u *p;
13457 int c1;
13458
13459 rettv->v_type = VAR_STRING;
13460 if (head == NULL)
13461 {
13462 rettv->vval.v_string = NULL;
13463 return;
13464 }
13465
13466 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013467 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013468
13469 while (*head != NUL)
13470 {
13471 c1 = PTR2CHAR(head);
13472 if (mask == NULL)
13473 {
13474 if (c1 > ' ' && c1 != 0xa0)
13475 break;
13476 }
13477 else
13478 {
13479 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13480 if (c1 == PTR2CHAR(p))
13481 break;
13482 if (*p == NUL)
13483 break;
13484 }
13485 MB_PTR_ADV(head);
13486 }
13487
13488 for (tail = head + STRLEN(head); tail > head; tail = prev)
13489 {
13490 prev = tail;
13491 MB_PTR_BACK(head, prev);
13492 c1 = PTR2CHAR(prev);
13493 if (mask == NULL)
13494 {
13495 if (c1 > ' ' && c1 != 0xa0)
13496 break;
13497 }
13498 else
13499 {
13500 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13501 if (c1 == PTR2CHAR(p))
13502 break;
13503 if (*p == NUL)
13504 break;
13505 }
13506 }
13507 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13508}
13509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510#ifdef FEAT_FLOAT
13511/*
13512 * "trunc({float})" function
13513 */
13514 static void
13515f_trunc(typval_T *argvars, typval_T *rettv)
13516{
13517 float_T f = 0.0;
13518
13519 rettv->v_type = VAR_FLOAT;
13520 if (get_float_arg(argvars, &f) == OK)
13521 /* trunc() is not in C90, use floor() or ceil() instead. */
13522 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13523 else
13524 rettv->vval.v_float = 0.0;
13525}
13526#endif
13527
13528/*
13529 * "type(expr)" function
13530 */
13531 static void
13532f_type(typval_T *argvars, typval_T *rettv)
13533{
13534 int n = -1;
13535
13536 switch (argvars[0].v_type)
13537 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013538 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13539 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013540 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013541 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13542 case VAR_LIST: n = VAR_TYPE_LIST; break;
13543 case VAR_DICT: n = VAR_TYPE_DICT; break;
13544 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013545 case VAR_SPECIAL:
13546 if (argvars[0].vval.v_number == VVAL_FALSE
13547 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013548 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013549 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013550 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013552 case VAR_JOB: n = VAR_TYPE_JOB; break;
13553 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013554 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013555 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013556 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013557 n = -1;
13558 break;
13559 }
13560 rettv->vval.v_number = n;
13561}
13562
13563/*
13564 * "undofile(name)" function
13565 */
13566 static void
13567f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13568{
13569 rettv->v_type = VAR_STRING;
13570#ifdef FEAT_PERSISTENT_UNDO
13571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013572 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013573
13574 if (*fname == NUL)
13575 {
13576 /* If there is no file name there will be no undo file. */
13577 rettv->vval.v_string = NULL;
13578 }
13579 else
13580 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020013581 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582
13583 if (ffname != NULL)
13584 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13585 vim_free(ffname);
13586 }
13587 }
13588#else
13589 rettv->vval.v_string = NULL;
13590#endif
13591}
13592
13593/*
13594 * "undotree()" function
13595 */
13596 static void
13597f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13598{
13599 if (rettv_dict_alloc(rettv) == OK)
13600 {
13601 dict_T *dict = rettv->vval.v_dict;
13602 list_T *list;
13603
Bram Moolenaare0be1672018-07-08 16:50:37 +020013604 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13605 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13606 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13607 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13608 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13609 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610
13611 list = list_alloc();
13612 if (list != NULL)
13613 {
13614 u_eval_tree(curbuf->b_u_oldhead, list);
13615 dict_add_list(dict, "entries", list);
13616 }
13617 }
13618}
13619
13620/*
13621 * "values(dict)" function
13622 */
13623 static void
13624f_values(typval_T *argvars, typval_T *rettv)
13625{
13626 dict_list(argvars, rettv, 1);
13627}
13628
13629/*
13630 * "virtcol(string)" function
13631 */
13632 static void
13633f_virtcol(typval_T *argvars, typval_T *rettv)
13634{
13635 colnr_T vcol = 0;
13636 pos_T *fp;
13637 int fnum = curbuf->b_fnum;
13638
13639 fp = var2fpos(&argvars[0], FALSE, &fnum);
13640 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13641 && fnum == curbuf->b_fnum)
13642 {
13643 getvvcol(curwin, fp, NULL, NULL, &vcol);
13644 ++vcol;
13645 }
13646
13647 rettv->vval.v_number = vcol;
13648}
13649
13650/*
13651 * "visualmode()" function
13652 */
13653 static void
13654f_visualmode(typval_T *argvars, typval_T *rettv)
13655{
13656 char_u str[2];
13657
13658 rettv->v_type = VAR_STRING;
13659 str[0] = curbuf->b_visual_mode_eval;
13660 str[1] = NUL;
13661 rettv->vval.v_string = vim_strsave(str);
13662
13663 /* A non-zero number or non-empty string argument: reset mode. */
13664 if (non_zero_arg(&argvars[0]))
13665 curbuf->b_visual_mode_eval = NUL;
13666}
13667
13668/*
13669 * "wildmenumode()" function
13670 */
13671 static void
13672f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13673{
13674#ifdef FEAT_WILDMENU
13675 if (wild_menu_showing)
13676 rettv->vval.v_number = 1;
13677#endif
13678}
13679
13680/*
13681 * "winbufnr(nr)" function
13682 */
13683 static void
13684f_winbufnr(typval_T *argvars, typval_T *rettv)
13685{
13686 win_T *wp;
13687
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013688 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013689 if (wp == NULL)
13690 rettv->vval.v_number = -1;
13691 else
13692 rettv->vval.v_number = wp->w_buffer->b_fnum;
13693}
13694
13695/*
13696 * "wincol()" function
13697 */
13698 static void
13699f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13700{
13701 validate_cursor();
13702 rettv->vval.v_number = curwin->w_wcol + 1;
13703}
13704
13705/*
13706 * "winheight(nr)" function
13707 */
13708 static void
13709f_winheight(typval_T *argvars, typval_T *rettv)
13710{
13711 win_T *wp;
13712
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013713 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013714 if (wp == NULL)
13715 rettv->vval.v_number = -1;
13716 else
13717 rettv->vval.v_number = wp->w_height;
13718}
13719
13720/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013721 * "winlayout()" function
13722 */
13723 static void
13724f_winlayout(typval_T *argvars, typval_T *rettv)
13725{
13726 tabpage_T *tp;
13727
13728 if (rettv_list_alloc(rettv) != OK)
13729 return;
13730
13731 if (argvars[0].v_type == VAR_UNKNOWN)
13732 tp = curtab;
13733 else
13734 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013735 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013736 if (tp == NULL)
13737 return;
13738 }
13739
13740 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13741}
13742
13743/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013744 * "winline()" function
13745 */
13746 static void
13747f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13748{
13749 validate_cursor();
13750 rettv->vval.v_number = curwin->w_wrow + 1;
13751}
13752
13753/*
13754 * "winnr()" function
13755 */
13756 static void
13757f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13758{
13759 int nr = 1;
13760
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013761 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013762 rettv->vval.v_number = nr;
13763}
13764
13765/*
13766 * "winrestcmd()" function
13767 */
13768 static void
13769f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13770{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013771 win_T *wp;
13772 int winnr = 1;
13773 garray_T ga;
13774 char_u buf[50];
13775
13776 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013777 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778 {
13779 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13780 ga_concat(&ga, buf);
13781 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13782 ga_concat(&ga, buf);
13783 ++winnr;
13784 }
13785 ga_append(&ga, NUL);
13786
13787 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013788 rettv->v_type = VAR_STRING;
13789}
13790
13791/*
13792 * "winrestview()" function
13793 */
13794 static void
13795f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13796{
13797 dict_T *dict;
13798
13799 if (argvars[0].v_type != VAR_DICT
13800 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013801 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013802 else
13803 {
13804 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013805 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013807 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013808 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013809 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013810 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13811 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010013812 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013813 curwin->w_set_curswant = FALSE;
13814 }
13815
13816 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013817 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013818#ifdef FEAT_DIFF
13819 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013820 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821#endif
13822 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013823 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013825 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013826
13827 check_cursor();
13828 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013829 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013830 changed_window_setting();
13831
13832 if (curwin->w_topline <= 0)
13833 curwin->w_topline = 1;
13834 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13835 curwin->w_topline = curbuf->b_ml.ml_line_count;
13836#ifdef FEAT_DIFF
13837 check_topfill(curwin, TRUE);
13838#endif
13839 }
13840}
13841
13842/*
13843 * "winsaveview()" function
13844 */
13845 static void
13846f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13847{
13848 dict_T *dict;
13849
13850 if (rettv_dict_alloc(rettv) == FAIL)
13851 return;
13852 dict = rettv->vval.v_dict;
13853
Bram Moolenaare0be1672018-07-08 16:50:37 +020013854 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13855 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020013856 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013858 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859
Bram Moolenaare0be1672018-07-08 16:50:37 +020013860 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013861#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013862 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013864 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13865 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866}
13867
13868/*
13869 * "winwidth(nr)" function
13870 */
13871 static void
13872f_winwidth(typval_T *argvars, typval_T *rettv)
13873{
13874 win_T *wp;
13875
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013876 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877 if (wp == NULL)
13878 rettv->vval.v_number = -1;
13879 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881}
13882
13883/*
13884 * "wordcount()" function
13885 */
13886 static void
13887f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13888{
13889 if (rettv_dict_alloc(rettv) == FAIL)
13890 return;
13891 cursor_pos_info(rettv->vval.v_dict);
13892}
13893
13894/*
13895 * "writefile()" function
13896 */
13897 static void
13898f_writefile(typval_T *argvars, typval_T *rettv)
13899{
13900 int binary = FALSE;
13901 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013902#ifdef HAVE_FSYNC
13903 int do_fsync = p_fs;
13904#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013905 char_u *fname;
13906 FILE *fd;
13907 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013908 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013909 list_T *list = NULL;
13910 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013912 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010013913 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013914 return;
13915
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013916 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013918 list = argvars[0].vval.v_list;
13919 if (list == NULL)
13920 return;
13921 for (li = list->lv_first; li != NULL; li = li->li_next)
13922 if (tv_get_string_chk(&li->li_tv) == NULL)
13923 return;
13924 }
13925 else if (argvars[0].v_type == VAR_BLOB)
13926 {
13927 blob = argvars[0].vval.v_blob;
13928 if (blob == NULL)
13929 return;
13930 }
13931 else
13932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013933 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013934 return;
13935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936
13937 if (argvars[2].v_type != VAR_UNKNOWN)
13938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013939 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013940
13941 if (arg2 == NULL)
13942 return;
13943 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013945 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013946 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013947#ifdef HAVE_FSYNC
13948 if (vim_strchr(arg2, 's') != NULL)
13949 do_fsync = TRUE;
13950 else if (vim_strchr(arg2, 'S') != NULL)
13951 do_fsync = FALSE;
13952#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953 }
13954
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013955 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013956 if (fname == NULL)
13957 return;
13958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013959 /* Always open the file in binary mode, library functions have a mind of
13960 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013961 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13962 append ? APPENDBIN : WRITEBIN)) == NULL)
13963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013964 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965 ret = -1;
13966 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013967 else if (blob)
13968 {
13969 if (write_blob(fd, blob) == FAIL)
13970 ret = -1;
13971#ifdef HAVE_FSYNC
13972 else if (do_fsync)
13973 // Ignore the error, the user wouldn't know what to do about it.
13974 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010013975 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010013976#endif
13977 fclose(fd);
13978 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013979 else
13980 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013981 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013982 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013983#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013984 else if (do_fsync)
13985 /* Ignore the error, the user wouldn't know what to do about it.
13986 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010013987 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013988#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013989 fclose(fd);
13990 }
13991
13992 rettv->vval.v_number = ret;
13993}
13994
13995/*
13996 * "xor(expr, expr)" function
13997 */
13998 static void
13999f_xor(typval_T *argvars, typval_T *rettv)
14000{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014001 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14002 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014003}
14004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014005#endif /* FEAT_EVAL */