blob: 93cac5aa6853faf3de8504a8c900a05407de0997 [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
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020032static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033
34#ifdef FEAT_FLOAT
35static void f_abs(typval_T *argvars, typval_T *rettv);
36static void f_acos(typval_T *argvars, typval_T *rettv);
37#endif
38static void f_add(typval_T *argvars, typval_T *rettv);
39static void f_and(typval_T *argvars, typval_T *rettv);
40static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020041static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042static void f_argc(typval_T *argvars, typval_T *rettv);
43static void f_argidx(typval_T *argvars, typval_T *rettv);
44static void f_arglistid(typval_T *argvars, typval_T *rettv);
45static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010046static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010048static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
99static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
103static void f_ch_status(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_changenr(typval_T *argvars, typval_T *rettv);
106static void f_char2nr(typval_T *argvars, typval_T *rettv);
107static void f_cindent(typval_T *argvars, typval_T *rettv);
108static void f_clearmatches(typval_T *argvars, typval_T *rettv);
109static void f_col(typval_T *argvars, typval_T *rettv);
110#if defined(FEAT_INS_EXPAND)
111static void f_complete(typval_T *argvars, typval_T *rettv);
112static void f_complete_add(typval_T *argvars, typval_T *rettv);
113static void f_complete_check(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_confirm(typval_T *argvars, typval_T *rettv);
116static void f_copy(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_cos(typval_T *argvars, typval_T *rettv);
119static void f_cosh(typval_T *argvars, typval_T *rettv);
120#endif
121static void f_count(typval_T *argvars, typval_T *rettv);
122static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
123static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200124#ifdef WIN3264
125static void f_debugbreak(typval_T *argvars, typval_T *rettv);
126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_deepcopy(typval_T *argvars, typval_T *rettv);
128static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200129static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200130static void f_did_filetype(typval_T *argvars, typval_T *rettv);
131static void f_diff_filler(typval_T *argvars, typval_T *rettv);
132static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
133static void f_empty(typval_T *argvars, typval_T *rettv);
134static void f_escape(typval_T *argvars, typval_T *rettv);
135static void f_eval(typval_T *argvars, typval_T *rettv);
136static void f_eventhandler(typval_T *argvars, typval_T *rettv);
137static void f_executable(typval_T *argvars, typval_T *rettv);
138static void f_execute(typval_T *argvars, typval_T *rettv);
139static void f_exepath(typval_T *argvars, typval_T *rettv);
140static void f_exists(typval_T *argvars, typval_T *rettv);
141#ifdef FEAT_FLOAT
142static void f_exp(typval_T *argvars, typval_T *rettv);
143#endif
144static void f_expand(typval_T *argvars, typval_T *rettv);
145static void f_extend(typval_T *argvars, typval_T *rettv);
146static void f_feedkeys(typval_T *argvars, typval_T *rettv);
147static void f_filereadable(typval_T *argvars, typval_T *rettv);
148static void f_filewritable(typval_T *argvars, typval_T *rettv);
149static void f_filter(typval_T *argvars, typval_T *rettv);
150static void f_finddir(typval_T *argvars, typval_T *rettv);
151static void f_findfile(typval_T *argvars, typval_T *rettv);
152#ifdef FEAT_FLOAT
153static void f_float2nr(typval_T *argvars, typval_T *rettv);
154static void f_floor(typval_T *argvars, typval_T *rettv);
155static void f_fmod(typval_T *argvars, typval_T *rettv);
156#endif
157static void f_fnameescape(typval_T *argvars, typval_T *rettv);
158static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
159static void f_foldclosed(typval_T *argvars, typval_T *rettv);
160static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
161static void f_foldlevel(typval_T *argvars, typval_T *rettv);
162static void f_foldtext(typval_T *argvars, typval_T *rettv);
163static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
164static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200165static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_function(typval_T *argvars, typval_T *rettv);
167static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
168static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200169static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getbufline(typval_T *argvars, typval_T *rettv);
171static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100172static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getchar(typval_T *argvars, typval_T *rettv);
174static void f_getcharmod(typval_T *argvars, typval_T *rettv);
175static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
176static void f_getcmdline(typval_T *argvars, typval_T *rettv);
177#if defined(FEAT_CMDL_COMPL)
178static void f_getcompletion(typval_T *argvars, typval_T *rettv);
179#endif
180static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
181static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
182static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
183static void f_getcwd(typval_T *argvars, typval_T *rettv);
184static void f_getfontname(typval_T *argvars, typval_T *rettv);
185static void f_getfperm(typval_T *argvars, typval_T *rettv);
186static void f_getfsize(typval_T *argvars, typval_T *rettv);
187static void f_getftime(typval_T *argvars, typval_T *rettv);
188static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100189static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200191static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getmatches(typval_T *argvars, typval_T *rettv);
193static void f_getpid(typval_T *argvars, typval_T *rettv);
194static void f_getcurpos(typval_T *argvars, typval_T *rettv);
195static void f_getpos(typval_T *argvars, typval_T *rettv);
196static void f_getqflist(typval_T *argvars, typval_T *rettv);
197static void f_getreg(typval_T *argvars, typval_T *rettv);
198static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_gettabvar(typval_T *argvars, typval_T *rettv);
201static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100202static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100204static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_getwinposx(typval_T *argvars, typval_T *rettv);
206static void f_getwinposy(typval_T *argvars, typval_T *rettv);
207static void f_getwinvar(typval_T *argvars, typval_T *rettv);
208static void f_glob(typval_T *argvars, typval_T *rettv);
209static void f_globpath(typval_T *argvars, typval_T *rettv);
210static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
211static void f_has(typval_T *argvars, typval_T *rettv);
212static void f_has_key(typval_T *argvars, typval_T *rettv);
213static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
214static void f_hasmapto(typval_T *argvars, typval_T *rettv);
215static void f_histadd(typval_T *argvars, typval_T *rettv);
216static void f_histdel(typval_T *argvars, typval_T *rettv);
217static void f_histget(typval_T *argvars, typval_T *rettv);
218static void f_histnr(typval_T *argvars, typval_T *rettv);
219static void f_hlID(typval_T *argvars, typval_T *rettv);
220static void f_hlexists(typval_T *argvars, typval_T *rettv);
221static void f_hostname(typval_T *argvars, typval_T *rettv);
222static void f_iconv(typval_T *argvars, typval_T *rettv);
223static void f_indent(typval_T *argvars, typval_T *rettv);
224static void f_index(typval_T *argvars, typval_T *rettv);
225static void f_input(typval_T *argvars, typval_T *rettv);
226static void f_inputdialog(typval_T *argvars, typval_T *rettv);
227static void f_inputlist(typval_T *argvars, typval_T *rettv);
228static void f_inputrestore(typval_T *argvars, typval_T *rettv);
229static void f_inputsave(typval_T *argvars, typval_T *rettv);
230static void f_inputsecret(typval_T *argvars, typval_T *rettv);
231static void f_insert(typval_T *argvars, typval_T *rettv);
232static void f_invert(typval_T *argvars, typval_T *rettv);
233static void f_isdirectory(typval_T *argvars, typval_T *rettv);
234static void f_islocked(typval_T *argvars, typval_T *rettv);
235#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
236static void f_isnan(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_items(typval_T *argvars, typval_T *rettv);
239#ifdef FEAT_JOB_CHANNEL
240static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
241static void f_job_info(typval_T *argvars, typval_T *rettv);
242static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
243static void f_job_start(typval_T *argvars, typval_T *rettv);
244static void f_job_stop(typval_T *argvars, typval_T *rettv);
245static void f_job_status(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_join(typval_T *argvars, typval_T *rettv);
248static void f_js_decode(typval_T *argvars, typval_T *rettv);
249static void f_js_encode(typval_T *argvars, typval_T *rettv);
250static void f_json_decode(typval_T *argvars, typval_T *rettv);
251static void f_json_encode(typval_T *argvars, typval_T *rettv);
252static void f_keys(typval_T *argvars, typval_T *rettv);
253static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
254static void f_len(typval_T *argvars, typval_T *rettv);
255static void f_libcall(typval_T *argvars, typval_T *rettv);
256static void f_libcallnr(typval_T *argvars, typval_T *rettv);
257static void f_line(typval_T *argvars, typval_T *rettv);
258static void f_line2byte(typval_T *argvars, typval_T *rettv);
259static void f_lispindent(typval_T *argvars, typval_T *rettv);
260static void f_localtime(typval_T *argvars, typval_T *rettv);
261#ifdef FEAT_FLOAT
262static void f_log(typval_T *argvars, typval_T *rettv);
263static void f_log10(typval_T *argvars, typval_T *rettv);
264#endif
265#ifdef FEAT_LUA
266static void f_luaeval(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_map(typval_T *argvars, typval_T *rettv);
269static void f_maparg(typval_T *argvars, typval_T *rettv);
270static void f_mapcheck(typval_T *argvars, typval_T *rettv);
271static void f_match(typval_T *argvars, typval_T *rettv);
272static void f_matchadd(typval_T *argvars, typval_T *rettv);
273static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
274static void f_matcharg(typval_T *argvars, typval_T *rettv);
275static void f_matchdelete(typval_T *argvars, typval_T *rettv);
276static void f_matchend(typval_T *argvars, typval_T *rettv);
277static void f_matchlist(typval_T *argvars, typval_T *rettv);
278static void f_matchstr(typval_T *argvars, typval_T *rettv);
279static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
280static void f_max(typval_T *argvars, typval_T *rettv);
281static void f_min(typval_T *argvars, typval_T *rettv);
282#ifdef vim_mkdir
283static void f_mkdir(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_mode(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_MZSCHEME
287static void f_mzeval(typval_T *argvars, typval_T *rettv);
288#endif
289static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
290static void f_nr2char(typval_T *argvars, typval_T *rettv);
291static void f_or(typval_T *argvars, typval_T *rettv);
292static void f_pathshorten(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PERL
294static void f_perleval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_FLOAT
297static void f_pow(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
300static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200301#ifdef FEAT_JOB_CHANNEL
302static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200303static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200304static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_pumvisible(typval_T *argvars, typval_T *rettv);
307#ifdef FEAT_PYTHON3
308static void f_py3eval(typval_T *argvars, typval_T *rettv);
309#endif
310#ifdef FEAT_PYTHON
311static void f_pyeval(typval_T *argvars, typval_T *rettv);
312#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100313#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
314static void f_pyxeval(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_range(typval_T *argvars, typval_T *rettv);
317static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200318static void f_reg_executing(typval_T *argvars, typval_T *rettv);
319static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_reltime(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_reltimestr(typval_T *argvars, typval_T *rettv);
325static void f_remote_expr(typval_T *argvars, typval_T *rettv);
326static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
327static void f_remote_peek(typval_T *argvars, typval_T *rettv);
328static void f_remote_read(typval_T *argvars, typval_T *rettv);
329static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100330static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_remove(typval_T *argvars, typval_T *rettv);
332static void f_rename(typval_T *argvars, typval_T *rettv);
333static void f_repeat(typval_T *argvars, typval_T *rettv);
334static void f_resolve(typval_T *argvars, typval_T *rettv);
335static void f_reverse(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_FLOAT
337static void f_round(typval_T *argvars, typval_T *rettv);
338#endif
339static void f_screenattr(typval_T *argvars, typval_T *rettv);
340static void f_screenchar(typval_T *argvars, typval_T *rettv);
341static void f_screencol(typval_T *argvars, typval_T *rettv);
342static void f_screenrow(typval_T *argvars, typval_T *rettv);
343static void f_search(typval_T *argvars, typval_T *rettv);
344static void f_searchdecl(typval_T *argvars, typval_T *rettv);
345static void f_searchpair(typval_T *argvars, typval_T *rettv);
346static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
347static void f_searchpos(typval_T *argvars, typval_T *rettv);
348static void f_server2client(typval_T *argvars, typval_T *rettv);
349static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200350static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_setbufvar(typval_T *argvars, typval_T *rettv);
352static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
353static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
354static void f_setfperm(typval_T *argvars, typval_T *rettv);
355static void f_setline(typval_T *argvars, typval_T *rettv);
356static void f_setloclist(typval_T *argvars, typval_T *rettv);
357static void f_setmatches(typval_T *argvars, typval_T *rettv);
358static void f_setpos(typval_T *argvars, typval_T *rettv);
359static void f_setqflist(typval_T *argvars, typval_T *rettv);
360static void f_setreg(typval_T *argvars, typval_T *rettv);
361static void f_settabvar(typval_T *argvars, typval_T *rettv);
362static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100363static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200401static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200402static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_synID(typval_T *argvars, typval_T *rettv);
404static void f_synIDattr(typval_T *argvars, typval_T *rettv);
405static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
406static void f_synstack(typval_T *argvars, typval_T *rettv);
407static void f_synconcealed(typval_T *argvars, typval_T *rettv);
408static void f_system(typval_T *argvars, typval_T *rettv);
409static void f_systemlist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
411static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
412static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
413static void f_taglist(typval_T *argvars, typval_T *rettv);
414static void f_tagfiles(typval_T *argvars, typval_T *rettv);
415static void f_tempname(typval_T *argvars, typval_T *rettv);
416static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
417static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200418static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200419static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100420static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100422static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423#ifdef FEAT_JOB_CHANNEL
424static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_JOB_CHANNEL
428static void f_test_null_job(typval_T *argvars, typval_T *rettv);
429#endif
430static void f_test_null_list(typval_T *argvars, typval_T *rettv);
431static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
432static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200433#ifdef FEAT_GUI
434static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436static void f_test_settime(typval_T *argvars, typval_T *rettv);
437#ifdef FEAT_FLOAT
438static void f_tan(typval_T *argvars, typval_T *rettv);
439static void f_tanh(typval_T *argvars, typval_T *rettv);
440#endif
441#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200442static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200443static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444static void f_timer_start(typval_T *argvars, typval_T *rettv);
445static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200446static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#endif
448static void f_tolower(typval_T *argvars, typval_T *rettv);
449static void f_toupper(typval_T *argvars, typval_T *rettv);
450static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100451static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_FLOAT
453static void f_trunc(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_type(typval_T *argvars, typval_T *rettv);
456static void f_undofile(typval_T *argvars, typval_T *rettv);
457static void f_undotree(typval_T *argvars, typval_T *rettv);
458static void f_uniq(typval_T *argvars, typval_T *rettv);
459static void f_values(typval_T *argvars, typval_T *rettv);
460static void f_virtcol(typval_T *argvars, typval_T *rettv);
461static void f_visualmode(typval_T *argvars, typval_T *rettv);
462static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
463static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
464static void f_win_getid(typval_T *argvars, typval_T *rettv);
465static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
466static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
467static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100468static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_winbufnr(typval_T *argvars, typval_T *rettv);
470static void f_wincol(typval_T *argvars, typval_T *rettv);
471static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200472static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473static void f_winline(typval_T *argvars, typval_T *rettv);
474static void f_winnr(typval_T *argvars, typval_T *rettv);
475static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
476static void f_winrestview(typval_T *argvars, typval_T *rettv);
477static void f_winsaveview(typval_T *argvars, typval_T *rettv);
478static void f_winwidth(typval_T *argvars, typval_T *rettv);
479static void f_writefile(typval_T *argvars, typval_T *rettv);
480static void f_wordcount(typval_T *argvars, typval_T *rettv);
481static void f_xor(typval_T *argvars, typval_T *rettv);
482
483/*
484 * Array with names and number of arguments of all internal functions
485 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
486 */
487static struct fst
488{
489 char *f_name; /* function name */
490 char f_min_argc; /* minimal number of arguments */
491 char f_max_argc; /* maximal number of arguments */
492 void (*f_func)(typval_T *args, typval_T *rvar);
493 /* implementation of function */
494} functions[] =
495{
496#ifdef FEAT_FLOAT
497 {"abs", 1, 1, f_abs},
498 {"acos", 1, 1, f_acos}, /* WJMc */
499#endif
500 {"add", 2, 2, f_add},
501 {"and", 2, 2, f_and},
502 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200503 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200504 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"argidx", 0, 0, f_argidx},
506 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200507 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508#ifdef FEAT_FLOAT
509 {"asin", 1, 1, f_asin}, /* WJMc */
510#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100511 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100513 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200515 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100517 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_match", 2, 3, f_assert_match},
519 {"assert_notequal", 2, 3, f_assert_notequal},
520 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100521 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522 {"assert_true", 1, 2, f_assert_true},
523#ifdef FEAT_FLOAT
524 {"atan", 1, 1, f_atan},
525 {"atan2", 2, 2, f_atan2},
526#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100527#ifdef FEAT_BEVAL
528 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100529# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100530 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100531# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100532#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"browse", 4, 4, f_browse},
534 {"browsedir", 2, 2, f_browsedir},
535 {"bufexists", 1, 1, f_bufexists},
536 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
537 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
538 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
539 {"buflisted", 1, 1, f_buflisted},
540 {"bufloaded", 1, 1, f_bufloaded},
541 {"bufname", 1, 1, f_bufname},
542 {"bufnr", 1, 2, f_bufnr},
543 {"bufwinid", 1, 1, f_bufwinid},
544 {"bufwinnr", 1, 1, f_bufwinnr},
545 {"byte2line", 1, 1, f_byte2line},
546 {"byteidx", 2, 2, f_byteidx},
547 {"byteidxcomp", 2, 2, f_byteidxcomp},
548 {"call", 2, 3, f_call},
549#ifdef FEAT_FLOAT
550 {"ceil", 1, 1, f_ceil},
551#endif
552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100553 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200555 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
557 {"ch_evalraw", 2, 3, f_ch_evalraw},
558 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
559 {"ch_getjob", 1, 1, f_ch_getjob},
560 {"ch_info", 1, 1, f_ch_info},
561 {"ch_log", 1, 2, f_ch_log},
562 {"ch_logfile", 1, 2, f_ch_logfile},
563 {"ch_open", 1, 2, f_ch_open},
564 {"ch_read", 1, 2, f_ch_read},
565 {"ch_readraw", 1, 2, f_ch_readraw},
566 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
567 {"ch_sendraw", 2, 3, f_ch_sendraw},
568 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200569 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200570#endif
571 {"changenr", 0, 0, f_changenr},
572 {"char2nr", 1, 2, f_char2nr},
573 {"cindent", 1, 1, f_cindent},
574 {"clearmatches", 0, 0, f_clearmatches},
575 {"col", 1, 1, f_col},
576#if defined(FEAT_INS_EXPAND)
577 {"complete", 2, 2, f_complete},
578 {"complete_add", 1, 1, f_complete_add},
579 {"complete_check", 0, 0, f_complete_check},
580#endif
581 {"confirm", 1, 4, f_confirm},
582 {"copy", 1, 1, f_copy},
583#ifdef FEAT_FLOAT
584 {"cos", 1, 1, f_cos},
585 {"cosh", 1, 1, f_cosh},
586#endif
587 {"count", 2, 4, f_count},
588 {"cscope_connection",0,3, f_cscope_connection},
589 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200590#ifdef WIN3264
591 {"debugbreak", 1, 1, f_debugbreak},
592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"deepcopy", 1, 2, f_deepcopy},
594 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200595 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"did_filetype", 0, 0, f_did_filetype},
597 {"diff_filler", 1, 1, f_diff_filler},
598 {"diff_hlID", 2, 2, f_diff_hlID},
599 {"empty", 1, 1, f_empty},
600 {"escape", 2, 2, f_escape},
601 {"eval", 1, 1, f_eval},
602 {"eventhandler", 0, 0, f_eventhandler},
603 {"executable", 1, 1, f_executable},
604 {"execute", 1, 2, f_execute},
605 {"exepath", 1, 1, f_exepath},
606 {"exists", 1, 1, f_exists},
607#ifdef FEAT_FLOAT
608 {"exp", 1, 1, f_exp},
609#endif
610 {"expand", 1, 3, f_expand},
611 {"extend", 2, 3, f_extend},
612 {"feedkeys", 1, 2, f_feedkeys},
613 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
614 {"filereadable", 1, 1, f_filereadable},
615 {"filewritable", 1, 1, f_filewritable},
616 {"filter", 2, 2, f_filter},
617 {"finddir", 1, 3, f_finddir},
618 {"findfile", 1, 3, f_findfile},
619#ifdef FEAT_FLOAT
620 {"float2nr", 1, 1, f_float2nr},
621 {"floor", 1, 1, f_floor},
622 {"fmod", 2, 2, f_fmod},
623#endif
624 {"fnameescape", 1, 1, f_fnameescape},
625 {"fnamemodify", 2, 2, f_fnamemodify},
626 {"foldclosed", 1, 1, f_foldclosed},
627 {"foldclosedend", 1, 1, f_foldclosedend},
628 {"foldlevel", 1, 1, f_foldlevel},
629 {"foldtext", 0, 0, f_foldtext},
630 {"foldtextresult", 1, 1, f_foldtextresult},
631 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"function", 1, 3, f_function},
634 {"garbagecollect", 0, 1, f_garbagecollect},
635 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200636 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"getbufline", 2, 3, f_getbufline},
638 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100639 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getchar", 0, 1, f_getchar},
641 {"getcharmod", 0, 0, f_getcharmod},
642 {"getcharsearch", 0, 0, f_getcharsearch},
643 {"getcmdline", 0, 0, f_getcmdline},
644 {"getcmdpos", 0, 0, f_getcmdpos},
645 {"getcmdtype", 0, 0, f_getcmdtype},
646 {"getcmdwintype", 0, 0, f_getcmdwintype},
647#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200648 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649#endif
650 {"getcurpos", 0, 0, f_getcurpos},
651 {"getcwd", 0, 2, f_getcwd},
652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getmatches", 0, 0, f_getmatches},
661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
703 {"islocked", 1, 1, f_islocked},
704#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
705 {"isnan", 1, 1, f_isnan},
706#endif
707 {"items", 1, 1, f_items},
708#ifdef FEAT_JOB_CHANNEL
709 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200710 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200711 {"job_setoptions", 2, 2, f_job_setoptions},
712 {"job_start", 1, 2, f_job_start},
713 {"job_status", 1, 1, f_job_status},
714 {"job_stop", 1, 2, f_job_stop},
715#endif
716 {"join", 1, 2, f_join},
717 {"js_decode", 1, 1, f_js_decode},
718 {"js_encode", 1, 1, f_js_encode},
719 {"json_decode", 1, 1, f_json_decode},
720 {"json_encode", 1, 1, f_json_encode},
721 {"keys", 1, 1, f_keys},
722 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
723 {"len", 1, 1, f_len},
724 {"libcall", 3, 3, f_libcall},
725 {"libcallnr", 3, 3, f_libcallnr},
726 {"line", 1, 1, f_line},
727 {"line2byte", 1, 1, f_line2byte},
728 {"lispindent", 1, 1, f_lispindent},
729 {"localtime", 0, 0, f_localtime},
730#ifdef FEAT_FLOAT
731 {"log", 1, 1, f_log},
732 {"log10", 1, 1, f_log10},
733#endif
734#ifdef FEAT_LUA
735 {"luaeval", 1, 2, f_luaeval},
736#endif
737 {"map", 2, 2, f_map},
738 {"maparg", 1, 4, f_maparg},
739 {"mapcheck", 1, 3, f_mapcheck},
740 {"match", 2, 4, f_match},
741 {"matchadd", 2, 5, f_matchadd},
742 {"matchaddpos", 2, 5, f_matchaddpos},
743 {"matcharg", 1, 1, f_matcharg},
744 {"matchdelete", 1, 1, f_matchdelete},
745 {"matchend", 2, 4, f_matchend},
746 {"matchlist", 2, 4, f_matchlist},
747 {"matchstr", 2, 4, f_matchstr},
748 {"matchstrpos", 2, 4, f_matchstrpos},
749 {"max", 1, 1, f_max},
750 {"min", 1, 1, f_min},
751#ifdef vim_mkdir
752 {"mkdir", 1, 3, f_mkdir},
753#endif
754 {"mode", 0, 1, f_mode},
755#ifdef FEAT_MZSCHEME
756 {"mzeval", 1, 1, f_mzeval},
757#endif
758 {"nextnonblank", 1, 1, f_nextnonblank},
759 {"nr2char", 1, 2, f_nr2char},
760 {"or", 2, 2, f_or},
761 {"pathshorten", 1, 1, f_pathshorten},
762#ifdef FEAT_PERL
763 {"perleval", 1, 1, f_perleval},
764#endif
765#ifdef FEAT_FLOAT
766 {"pow", 2, 2, f_pow},
767#endif
768 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100769 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200770#ifdef FEAT_JOB_CHANNEL
771 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200772 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200773 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
774#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100775#ifdef FEAT_TEXT_PROP
776 {"prop_add", 3, 3, f_prop_add},
777 {"prop_clear", 1, 3, f_prop_clear},
778 {"prop_list", 1, 2, f_prop_list},
779 {"prop_remove", 2, 3, f_prop_remove},
780 {"prop_type_add", 2, 2, f_prop_type_add},
781 {"prop_type_change", 2, 2, f_prop_type_change},
782 {"prop_type_delete", 1, 2, f_prop_type_delete},
783 {"prop_type_get", 1, 2, f_prop_type_get},
784 {"prop_type_list", 0, 1, f_prop_type_list},
785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"pumvisible", 0, 0, f_pumvisible},
787#ifdef FEAT_PYTHON3
788 {"py3eval", 1, 1, f_py3eval},
789#endif
790#ifdef FEAT_PYTHON
791 {"pyeval", 1, 1, f_pyeval},
792#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100793#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
794 {"pyxeval", 1, 1, f_pyxeval},
795#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"range", 1, 3, f_range},
797 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200798 {"reg_executing", 0, 0, f_reg_executing},
799 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"reltime", 0, 2, f_reltime},
801#ifdef FEAT_FLOAT
802 {"reltimefloat", 1, 1, f_reltimefloat},
803#endif
804 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100805 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200806 {"remote_foreground", 1, 1, f_remote_foreground},
807 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100808 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200809 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100810 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"remove", 2, 3, f_remove},
812 {"rename", 2, 2, f_rename},
813 {"repeat", 2, 2, f_repeat},
814 {"resolve", 1, 1, f_resolve},
815 {"reverse", 1, 1, f_reverse},
816#ifdef FEAT_FLOAT
817 {"round", 1, 1, f_round},
818#endif
819 {"screenattr", 2, 2, f_screenattr},
820 {"screenchar", 2, 2, f_screenchar},
821 {"screencol", 0, 0, f_screencol},
822 {"screenrow", 0, 0, f_screenrow},
823 {"search", 1, 4, f_search},
824 {"searchdecl", 1, 3, f_searchdecl},
825 {"searchpair", 3, 7, f_searchpair},
826 {"searchpairpos", 3, 7, f_searchpairpos},
827 {"searchpos", 1, 4, f_searchpos},
828 {"server2client", 2, 2, f_server2client},
829 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200830 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831 {"setbufvar", 3, 3, f_setbufvar},
832 {"setcharsearch", 1, 1, f_setcharsearch},
833 {"setcmdpos", 1, 1, f_setcmdpos},
834 {"setfperm", 2, 2, f_setfperm},
835 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200836 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"setmatches", 1, 1, f_setmatches},
838 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200839 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840 {"setreg", 2, 3, f_setreg},
841 {"settabvar", 3, 3, f_settabvar},
842 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100843 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"setwinvar", 3, 3, f_setwinvar},
845#ifdef FEAT_CRYPT
846 {"sha256", 1, 1, f_sha256},
847#endif
848 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100849 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"simplify", 1, 1, f_simplify},
851#ifdef FEAT_FLOAT
852 {"sin", 1, 1, f_sin},
853 {"sinh", 1, 1, f_sinh},
854#endif
855 {"sort", 1, 3, f_sort},
856 {"soundfold", 1, 1, f_soundfold},
857 {"spellbadword", 0, 1, f_spellbadword},
858 {"spellsuggest", 1, 3, f_spellsuggest},
859 {"split", 1, 3, f_split},
860#ifdef FEAT_FLOAT
861 {"sqrt", 1, 1, f_sqrt},
862 {"str2float", 1, 1, f_str2float},
863#endif
864 {"str2nr", 1, 2, f_str2nr},
865 {"strcharpart", 2, 3, f_strcharpart},
866 {"strchars", 1, 2, f_strchars},
867 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
868#ifdef HAVE_STRFTIME
869 {"strftime", 1, 2, f_strftime},
870#endif
871 {"strgetchar", 2, 2, f_strgetchar},
872 {"stridx", 2, 3, f_stridx},
873 {"string", 1, 1, f_string},
874 {"strlen", 1, 1, f_strlen},
875 {"strpart", 2, 3, f_strpart},
876 {"strridx", 2, 3, f_strridx},
877 {"strtrans", 1, 1, f_strtrans},
878 {"strwidth", 1, 1, f_strwidth},
879 {"submatch", 1, 2, f_submatch},
880 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200881 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200882 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883 {"synID", 3, 3, f_synID},
884 {"synIDattr", 2, 3, f_synIDattr},
885 {"synIDtrans", 1, 1, f_synIDtrans},
886 {"synconcealed", 2, 2, f_synconcealed},
887 {"synstack", 2, 2, f_synstack},
888 {"system", 1, 2, f_system},
889 {"systemlist", 1, 2, f_systemlist},
890 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
891 {"tabpagenr", 0, 1, f_tabpagenr},
892 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
893 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100894 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895#ifdef FEAT_FLOAT
896 {"tan", 1, 1, f_tan},
897 {"tanh", 1, 1, f_tanh},
898#endif
899 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200900#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100901 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
902 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100903 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200904 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200905# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
906 {"term_getansicolors", 1, 1, f_term_getansicolors},
907# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200909 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200911 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200912 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200914 {"term_getstatus", 1, 1, f_term_getstatus},
915 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200916 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200918 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200919 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200920# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
921 {"term_setansicolors", 2, 2, f_term_setansicolors},
922# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100923 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100924 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200925 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200926 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200927 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
930 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200931 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200932 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100933 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934#ifdef FEAT_JOB_CHANNEL
935 {"test_null_channel", 0, 0, f_test_null_channel},
936#endif
937 {"test_null_dict", 0, 0, f_test_null_dict},
938#ifdef FEAT_JOB_CHANNEL
939 {"test_null_job", 0, 0, f_test_null_job},
940#endif
941 {"test_null_list", 0, 0, f_test_null_list},
942 {"test_null_partial", 0, 0, f_test_null_partial},
943 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200944 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100945 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200946#ifdef FEAT_GUI
947 {"test_scrollbar", 3, 3, f_test_scrollbar},
948#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949 {"test_settime", 1, 1, f_test_settime},
950#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200951 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200952 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953 {"timer_start", 2, 3, f_timer_start},
954 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200955 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956#endif
957 {"tolower", 1, 1, f_tolower},
958 {"toupper", 1, 1, f_toupper},
959 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100960 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200961#ifdef FEAT_FLOAT
962 {"trunc", 1, 1, f_trunc},
963#endif
964 {"type", 1, 1, f_type},
965 {"undofile", 1, 1, f_undofile},
966 {"undotree", 0, 0, f_undotree},
967 {"uniq", 1, 3, f_uniq},
968 {"values", 1, 1, f_values},
969 {"virtcol", 1, 1, f_virtcol},
970 {"visualmode", 0, 1, f_visualmode},
971 {"wildmenumode", 0, 0, f_wildmenumode},
972 {"win_findbuf", 1, 1, f_win_findbuf},
973 {"win_getid", 0, 2, f_win_getid},
974 {"win_gotoid", 1, 1, f_win_gotoid},
975 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
976 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100977 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978 {"winbufnr", 1, 1, f_winbufnr},
979 {"wincol", 0, 0, f_wincol},
980 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200981 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982 {"winline", 0, 0, f_winline},
983 {"winnr", 0, 1, f_winnr},
984 {"winrestcmd", 0, 0, f_winrestcmd},
985 {"winrestview", 1, 1, f_winrestview},
986 {"winsaveview", 0, 0, f_winsaveview},
987 {"winwidth", 1, 1, f_winwidth},
988 {"wordcount", 0, 0, f_wordcount},
989 {"writefile", 2, 3, f_writefile},
990 {"xor", 2, 2, f_xor},
991};
992
993#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
994
995/*
996 * Function given to ExpandGeneric() to obtain the list of internal
997 * or user defined function names.
998 */
999 char_u *
1000get_function_name(expand_T *xp, int idx)
1001{
1002 static int intidx = -1;
1003 char_u *name;
1004
1005 if (idx == 0)
1006 intidx = -1;
1007 if (intidx < 0)
1008 {
1009 name = get_user_func_name(xp, idx);
1010 if (name != NULL)
1011 return name;
1012 }
1013 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1014 {
1015 STRCPY(IObuff, functions[intidx].f_name);
1016 STRCAT(IObuff, "(");
1017 if (functions[intidx].f_max_argc == 0)
1018 STRCAT(IObuff, ")");
1019 return IObuff;
1020 }
1021
1022 return NULL;
1023}
1024
1025/*
1026 * Function given to ExpandGeneric() to obtain the list of internal or
1027 * user defined variable or function names.
1028 */
1029 char_u *
1030get_expr_name(expand_T *xp, int idx)
1031{
1032 static int intidx = -1;
1033 char_u *name;
1034
1035 if (idx == 0)
1036 intidx = -1;
1037 if (intidx < 0)
1038 {
1039 name = get_function_name(xp, idx);
1040 if (name != NULL)
1041 return name;
1042 }
1043 return get_user_var_name(xp, ++intidx);
1044}
1045
1046#endif /* FEAT_CMDL_COMPL */
1047
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048/*
1049 * Find internal function in table above.
1050 * Return index, or -1 if not found
1051 */
1052 int
1053find_internal_func(
1054 char_u *name) /* name of the function */
1055{
1056 int first = 0;
1057 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1058 int cmp;
1059 int x;
1060
1061 /*
1062 * Find the function name in the table. Binary search.
1063 */
1064 while (first <= last)
1065 {
1066 x = first + ((unsigned)(last - first) >> 1);
1067 cmp = STRCMP(name, functions[x].f_name);
1068 if (cmp < 0)
1069 last = x - 1;
1070 else if (cmp > 0)
1071 first = x + 1;
1072 else
1073 return x;
1074 }
1075 return -1;
1076}
1077
1078 int
1079call_internal_func(
1080 char_u *name,
1081 int argcount,
1082 typval_T *argvars,
1083 typval_T *rettv)
1084{
1085 int i;
1086
1087 i = find_internal_func(name);
1088 if (i < 0)
1089 return ERROR_UNKNOWN;
1090 if (argcount < functions[i].f_min_argc)
1091 return ERROR_TOOFEW;
1092 if (argcount > functions[i].f_max_argc)
1093 return ERROR_TOOMANY;
1094 argvars[argcount].v_type = VAR_UNKNOWN;
1095 functions[i].f_func(argvars, rettv);
1096 return ERROR_NONE;
1097}
1098
1099/*
1100 * Return TRUE for a non-zero Number and a non-empty String.
1101 */
1102 static int
1103non_zero_arg(typval_T *argvars)
1104{
1105 return ((argvars[0].v_type == VAR_NUMBER
1106 && argvars[0].vval.v_number != 0)
1107 || (argvars[0].v_type == VAR_SPECIAL
1108 && argvars[0].vval.v_number == VVAL_TRUE)
1109 || (argvars[0].v_type == VAR_STRING
1110 && argvars[0].vval.v_string != NULL
1111 && *argvars[0].vval.v_string != NUL));
1112}
1113
1114/*
1115 * Get the lnum from the first argument.
1116 * Also accepts ".", "$", etc., but that only works for the current buffer.
1117 * Returns -1 on error.
1118 */
1119 static linenr_T
1120get_tv_lnum(typval_T *argvars)
1121{
1122 typval_T rettv;
1123 linenr_T lnum;
1124
1125 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1126 if (lnum == 0) /* no valid number, try using line() */
1127 {
1128 rettv.v_type = VAR_NUMBER;
1129 f_line(argvars, &rettv);
1130 lnum = (linenr_T)rettv.vval.v_number;
1131 clear_tv(&rettv);
1132 }
1133 return lnum;
1134}
1135
1136#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001137/*
1138 * Get the float value of "argvars[0]" into "f".
1139 * Returns FAIL when the argument is not a Number or Float.
1140 */
1141 static int
1142get_float_arg(typval_T *argvars, float_T *f)
1143{
1144 if (argvars[0].v_type == VAR_FLOAT)
1145 {
1146 *f = argvars[0].vval.v_float;
1147 return OK;
1148 }
1149 if (argvars[0].v_type == VAR_NUMBER)
1150 {
1151 *f = (float_T)argvars[0].vval.v_number;
1152 return OK;
1153 }
1154 EMSG(_("E808: Number or Float required"));
1155 return FAIL;
1156}
1157
1158/*
1159 * "abs(expr)" function
1160 */
1161 static void
1162f_abs(typval_T *argvars, typval_T *rettv)
1163{
1164 if (argvars[0].v_type == VAR_FLOAT)
1165 {
1166 rettv->v_type = VAR_FLOAT;
1167 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1168 }
1169 else
1170 {
1171 varnumber_T n;
1172 int error = FALSE;
1173
1174 n = get_tv_number_chk(&argvars[0], &error);
1175 if (error)
1176 rettv->vval.v_number = -1;
1177 else if (n > 0)
1178 rettv->vval.v_number = n;
1179 else
1180 rettv->vval.v_number = -n;
1181 }
1182}
1183
1184/*
1185 * "acos()" function
1186 */
1187 static void
1188f_acos(typval_T *argvars, typval_T *rettv)
1189{
1190 float_T f = 0.0;
1191
1192 rettv->v_type = VAR_FLOAT;
1193 if (get_float_arg(argvars, &f) == OK)
1194 rettv->vval.v_float = acos(f);
1195 else
1196 rettv->vval.v_float = 0.0;
1197}
1198#endif
1199
1200/*
1201 * "add(list, item)" function
1202 */
1203 static void
1204f_add(typval_T *argvars, typval_T *rettv)
1205{
1206 list_T *l;
1207
1208 rettv->vval.v_number = 1; /* Default: Failed */
1209 if (argvars[0].v_type == VAR_LIST)
1210 {
1211 if ((l = argvars[0].vval.v_list) != NULL
1212 && !tv_check_lock(l->lv_lock,
1213 (char_u *)N_("add() argument"), TRUE)
1214 && list_append_tv(l, &argvars[1]) == OK)
1215 copy_tv(&argvars[0], rettv);
1216 }
1217 else
1218 EMSG(_(e_listreq));
1219}
1220
1221/*
1222 * "and(expr, expr)" function
1223 */
1224 static void
1225f_and(typval_T *argvars, typval_T *rettv)
1226{
1227 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1228 & get_tv_number_chk(&argvars[1], NULL);
1229}
1230
1231/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001232 * Get the lnum from the first argument.
1233 * Also accepts "$", then "buf" is used.
1234 * Returns 0 on error.
1235 */
1236 static linenr_T
1237get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1238{
1239 if (argvars[0].v_type == VAR_STRING
1240 && argvars[0].vval.v_string != NULL
1241 && argvars[0].vval.v_string[0] == '$'
1242 && buf != NULL)
1243 return buf->b_ml.ml_line_count;
1244 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1245}
1246
1247/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001248 * If there is a window for "curbuf", make it the current window.
1249 */
1250 static void
1251find_win_for_curbuf(void)
1252{
1253 wininfo_T *wip;
1254
1255 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1256 {
1257 if (wip->wi_win != NULL)
1258 {
1259 curwin = wip->wi_win;
1260 break;
1261 }
1262 }
1263}
1264
1265/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001266 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 */
1268 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001269set_buffer_lines(
1270 buf_T *buf,
1271 linenr_T lnum_arg,
1272 int append,
1273 typval_T *lines,
1274 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001275{
Bram Moolenaarca851592018-06-06 21:04:07 +02001276 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1277 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001278 list_T *l = NULL;
1279 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001281 linenr_T append_lnum;
1282 buf_T *curbuf_save = NULL;
1283 win_T *curwin_save = NULL;
1284 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285
Bram Moolenaarca851592018-06-06 21:04:07 +02001286 /* When using the current buffer ml_mfp will be set if needed. Useful when
1287 * setline() is used on startup. For other buffers the buffer must be
1288 * loaded. */
1289 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001291 rettv->vval.v_number = 1; /* FAIL */
1292 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001293 }
1294
Bram Moolenaarca851592018-06-06 21:04:07 +02001295 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001296 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001297 curbuf_save = curbuf;
1298 curwin_save = curwin;
1299 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001300 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001301 }
1302
1303 if (append)
1304 // appendbufline() uses the line number below which we insert
1305 append_lnum = lnum - 1;
1306 else
1307 // setbufline() uses the line number above which we insert, we only
1308 // append if it's below the last line
1309 append_lnum = curbuf->b_ml.ml_line_count;
1310
1311 if (lines->v_type == VAR_LIST)
1312 {
1313 l = lines->vval.v_list;
1314 li = l->lv_first;
1315 }
1316 else
1317 line = get_tv_string_chk(lines);
1318
1319 /* default result is zero == OK */
1320 for (;;)
1321 {
1322 if (l != NULL)
1323 {
1324 /* list argument, get next string */
1325 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001326 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001327 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 li = li->li_next;
1329 }
1330
Bram Moolenaarca851592018-06-06 21:04:07 +02001331 rettv->vval.v_number = 1; /* FAIL */
1332 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1333 break;
1334
1335 /* When coming here from Insert mode, sync undo, so that this can be
1336 * undone separately from what was previously inserted. */
1337 if (u_sync_once == 2)
1338 {
1339 u_sync_once = 1; /* notify that u_sync() was called */
1340 u_sync(TRUE);
1341 }
1342
1343 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1344 {
1345 /* existing line, replace it */
1346 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1347 {
1348 changed_bytes(lnum, 0);
1349 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1350 check_cursor_col();
1351 rettv->vval.v_number = 0; /* OK */
1352 }
1353 }
1354 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1355 {
1356 /* append the line */
1357 ++added;
1358 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1359 rettv->vval.v_number = 0; /* OK */
1360 }
1361
1362 if (l == NULL) /* only one string argument */
1363 break;
1364 ++lnum;
1365 }
1366
1367 if (added > 0)
1368 {
1369 win_T *wp;
1370 tabpage_T *tp;
1371
1372 appended_lines_mark(append_lnum, added);
1373 FOR_ALL_TAB_WINDOWS(tp, wp)
1374 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1375 wp->w_cursor.lnum += added;
1376 check_cursor_col();
1377
Bram Moolenaarf2732452018-06-03 14:47:35 +02001378#ifdef FEAT_JOB_CHANNEL
1379 if (bt_prompt(curbuf) && (State & INSERT))
1380 // show the line with the prompt
1381 update_topline();
1382#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001384
1385 if (!is_curbuf)
1386 {
1387 curbuf = curbuf_save;
1388 curwin = curwin_save;
1389 }
1390}
1391
1392/*
1393 * "append(lnum, string/list)" function
1394 */
1395 static void
1396f_append(typval_T *argvars, typval_T *rettv)
1397{
1398 linenr_T lnum = get_tv_lnum(&argvars[0]);
1399
1400 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1401}
1402
1403/*
1404 * "appendbufline(buf, lnum, string/list)" function
1405 */
1406 static void
1407f_appendbufline(typval_T *argvars, typval_T *rettv)
1408{
1409 linenr_T lnum;
1410 buf_T *buf;
1411
1412 buf = get_buf_tv(&argvars[0], FALSE);
1413 if (buf == NULL)
1414 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001415 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001416 {
1417 lnum = get_tv_lnum_buf(&argvars[1], buf);
1418 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001420}
1421
1422/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001423 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001424 */
1425 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001426f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001427{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001428 win_T *wp;
1429
1430 if (argvars[0].v_type == VAR_UNKNOWN)
1431 // use the current window
1432 rettv->vval.v_number = ARGCOUNT;
1433 else if (argvars[0].v_type == VAR_NUMBER
1434 && get_tv_number(&argvars[0]) == -1)
1435 // use the global argument list
1436 rettv->vval.v_number = GARGCOUNT;
1437 else
1438 {
1439 // use the argument list of the specified window
1440 wp = find_win_by_nr_or_id(&argvars[0]);
1441 if (wp != NULL)
1442 rettv->vval.v_number = WARGCOUNT(wp);
1443 else
1444 rettv->vval.v_number = -1;
1445 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001446}
1447
1448/*
1449 * "argidx()" function
1450 */
1451 static void
1452f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1453{
1454 rettv->vval.v_number = curwin->w_arg_idx;
1455}
1456
1457/*
1458 * "arglistid()" function
1459 */
1460 static void
1461f_arglistid(typval_T *argvars, typval_T *rettv)
1462{
1463 win_T *wp;
1464
1465 rettv->vval.v_number = -1;
1466 wp = find_tabwin(&argvars[0], &argvars[1]);
1467 if (wp != NULL)
1468 rettv->vval.v_number = wp->w_alist->id;
1469}
1470
1471/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001472 * Get the argument list for a given window
1473 */
1474 static void
1475get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1476{
1477 int idx;
1478
1479 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1480 for (idx = 0; idx < argcount; ++idx)
1481 list_append_string(rettv->vval.v_list,
1482 alist_name(&arglist[idx]), -1);
1483}
1484
1485/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486 * "argv(nr)" function
1487 */
1488 static void
1489f_argv(typval_T *argvars, typval_T *rettv)
1490{
1491 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001492 aentry_T *arglist = NULL;
1493 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494
1495 if (argvars[0].v_type != VAR_UNKNOWN)
1496 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001497 if (argvars[1].v_type == VAR_UNKNOWN)
1498 {
1499 arglist = ARGLIST;
1500 argcount = ARGCOUNT;
1501 }
1502 else if (argvars[1].v_type == VAR_NUMBER
1503 && get_tv_number(&argvars[1]) == -1)
1504 {
1505 arglist = GARGLIST;
1506 argcount = GARGCOUNT;
1507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001509 {
1510 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1511
1512 if (wp != NULL)
1513 {
1514 /* Use the argument list of the specified window */
1515 arglist = WARGLIST(wp);
1516 argcount = WARGCOUNT(wp);
1517 }
1518 }
1519
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001520 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001521 rettv->vval.v_string = NULL;
1522 idx = get_tv_number_chk(&argvars[0], NULL);
1523 if (arglist != NULL && idx >= 0 && idx < argcount)
1524 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1525 else if (idx == -1)
1526 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001528 else
1529 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001530}
1531
1532/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001533 * "assert_beeps(cmd [, error])" function
1534 */
1535 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001536f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001537{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001538 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001539}
1540
1541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 * "assert_equal(expected, actual[, msg])" function
1543 */
1544 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001545f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001547 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001548}
1549
1550/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001551 * "assert_equalfile(fname-one, fname-two)" function
1552 */
1553 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001554f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001555{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001556 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001557}
1558
1559/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560 * "assert_notequal(expected, actual[, msg])" function
1561 */
1562 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001563f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001565 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566}
1567
1568/*
1569 * "assert_exception(string[, msg])" function
1570 */
1571 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001572f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001574 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575}
1576
1577/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001578 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 */
1580 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001581f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001583 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001584}
1585
1586/*
1587 * "assert_false(actual[, msg])" function
1588 */
1589 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001590f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001591{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001592 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593}
1594
1595/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001596 * "assert_inrange(lower, upper[, msg])" function
1597 */
1598 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001599f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001600{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001601 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001602}
1603
1604/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 * "assert_match(pattern, actual[, msg])" function
1606 */
1607 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001608f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001610 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001611}
1612
1613/*
1614 * "assert_notmatch(pattern, actual[, msg])" function
1615 */
1616 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001618{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001619 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620}
1621
1622/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001623 * "assert_report(msg)" function
1624 */
1625 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001627{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001628 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001629}
1630
1631/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 * "assert_true(actual[, msg])" function
1633 */
1634 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001637 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638}
1639
1640#ifdef FEAT_FLOAT
1641/*
1642 * "asin()" function
1643 */
1644 static void
1645f_asin(typval_T *argvars, typval_T *rettv)
1646{
1647 float_T f = 0.0;
1648
1649 rettv->v_type = VAR_FLOAT;
1650 if (get_float_arg(argvars, &f) == OK)
1651 rettv->vval.v_float = asin(f);
1652 else
1653 rettv->vval.v_float = 0.0;
1654}
1655
1656/*
1657 * "atan()" function
1658 */
1659 static void
1660f_atan(typval_T *argvars, typval_T *rettv)
1661{
1662 float_T f = 0.0;
1663
1664 rettv->v_type = VAR_FLOAT;
1665 if (get_float_arg(argvars, &f) == OK)
1666 rettv->vval.v_float = atan(f);
1667 else
1668 rettv->vval.v_float = 0.0;
1669}
1670
1671/*
1672 * "atan2()" function
1673 */
1674 static void
1675f_atan2(typval_T *argvars, typval_T *rettv)
1676{
1677 float_T fx = 0.0, fy = 0.0;
1678
1679 rettv->v_type = VAR_FLOAT;
1680 if (get_float_arg(argvars, &fx) == OK
1681 && get_float_arg(&argvars[1], &fy) == OK)
1682 rettv->vval.v_float = atan2(fx, fy);
1683 else
1684 rettv->vval.v_float = 0.0;
1685}
1686#endif
1687
1688/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001689 * "balloon_show()" function
1690 */
1691#ifdef FEAT_BEVAL
1692 static void
1693f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1694{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001695 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001696 {
1697 if (argvars[0].v_type == VAR_LIST
1698# ifdef FEAT_GUI
1699 && !gui.in_use
1700# endif
1701 )
1702 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1703 else
1704 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1705 }
1706}
1707
Bram Moolenaar669a8282017-11-19 20:13:05 +01001708# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001709 static void
1710f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1711{
1712 if (rettv_list_alloc(rettv) == OK)
1713 {
1714 char_u *msg = get_tv_string_chk(&argvars[0]);
1715
1716 if (msg != NULL)
1717 {
1718 pumitem_T *array;
1719 int size = split_message(msg, &array);
1720 int i;
1721
1722 /* Skip the first and last item, they are always empty. */
1723 for (i = 1; i < size - 1; ++i)
1724 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001725 while (size > 0)
1726 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001727 vim_free(array);
1728 }
1729 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001730}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001731# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001732#endif
1733
1734/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735 * "browse(save, title, initdir, default)" function
1736 */
1737 static void
1738f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1739{
1740#ifdef FEAT_BROWSE
1741 int save;
1742 char_u *title;
1743 char_u *initdir;
1744 char_u *defname;
1745 char_u buf[NUMBUFLEN];
1746 char_u buf2[NUMBUFLEN];
1747 int error = FALSE;
1748
1749 save = (int)get_tv_number_chk(&argvars[0], &error);
1750 title = get_tv_string_chk(&argvars[1]);
1751 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1752 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1753
1754 if (error || title == NULL || initdir == NULL || defname == NULL)
1755 rettv->vval.v_string = NULL;
1756 else
1757 rettv->vval.v_string =
1758 do_browse(save ? BROWSE_SAVE : 0,
1759 title, defname, NULL, initdir, NULL, curbuf);
1760#else
1761 rettv->vval.v_string = NULL;
1762#endif
1763 rettv->v_type = VAR_STRING;
1764}
1765
1766/*
1767 * "browsedir(title, initdir)" function
1768 */
1769 static void
1770f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1771{
1772#ifdef FEAT_BROWSE
1773 char_u *title;
1774 char_u *initdir;
1775 char_u buf[NUMBUFLEN];
1776
1777 title = get_tv_string_chk(&argvars[0]);
1778 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1779
1780 if (title == NULL || initdir == NULL)
1781 rettv->vval.v_string = NULL;
1782 else
1783 rettv->vval.v_string = do_browse(BROWSE_DIR,
1784 title, NULL, NULL, initdir, NULL, curbuf);
1785#else
1786 rettv->vval.v_string = NULL;
1787#endif
1788 rettv->v_type = VAR_STRING;
1789}
1790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001791/*
1792 * Find a buffer by number or exact name.
1793 */
1794 static buf_T *
1795find_buffer(typval_T *avar)
1796{
1797 buf_T *buf = NULL;
1798
1799 if (avar->v_type == VAR_NUMBER)
1800 buf = buflist_findnr((int)avar->vval.v_number);
1801 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1802 {
1803 buf = buflist_findname_exp(avar->vval.v_string);
1804 if (buf == NULL)
1805 {
1806 /* No full path name match, try a match with a URL or a "nofile"
1807 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001808 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001809 if (buf->b_fname != NULL
1810 && (path_with_url(buf->b_fname)
1811#ifdef FEAT_QUICKFIX
1812 || bt_nofile(buf)
1813#endif
1814 )
1815 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1816 break;
1817 }
1818 }
1819 return buf;
1820}
1821
1822/*
1823 * "bufexists(expr)" function
1824 */
1825 static void
1826f_bufexists(typval_T *argvars, typval_T *rettv)
1827{
1828 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1829}
1830
1831/*
1832 * "buflisted(expr)" function
1833 */
1834 static void
1835f_buflisted(typval_T *argvars, typval_T *rettv)
1836{
1837 buf_T *buf;
1838
1839 buf = find_buffer(&argvars[0]);
1840 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1841}
1842
1843/*
1844 * "bufloaded(expr)" function
1845 */
1846 static void
1847f_bufloaded(typval_T *argvars, typval_T *rettv)
1848{
1849 buf_T *buf;
1850
1851 buf = find_buffer(&argvars[0]);
1852 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1853}
1854
1855 buf_T *
1856buflist_find_by_name(char_u *name, int curtab_only)
1857{
1858 int save_magic;
1859 char_u *save_cpo;
1860 buf_T *buf;
1861
1862 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1863 save_magic = p_magic;
1864 p_magic = TRUE;
1865 save_cpo = p_cpo;
1866 p_cpo = (char_u *)"";
1867
1868 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1869 TRUE, FALSE, curtab_only));
1870
1871 p_magic = save_magic;
1872 p_cpo = save_cpo;
1873 return buf;
1874}
1875
1876/*
1877 * Get buffer by number or pattern.
1878 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001879 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880get_buf_tv(typval_T *tv, int curtab_only)
1881{
1882 char_u *name = tv->vval.v_string;
1883 buf_T *buf;
1884
1885 if (tv->v_type == VAR_NUMBER)
1886 return buflist_findnr((int)tv->vval.v_number);
1887 if (tv->v_type != VAR_STRING)
1888 return NULL;
1889 if (name == NULL || *name == NUL)
1890 return curbuf;
1891 if (name[0] == '$' && name[1] == NUL)
1892 return lastbuf;
1893
1894 buf = buflist_find_by_name(name, curtab_only);
1895
1896 /* If not found, try expanding the name, like done for bufexists(). */
1897 if (buf == NULL)
1898 buf = find_buffer(tv);
1899
1900 return buf;
1901}
1902
1903/*
1904 * "bufname(expr)" function
1905 */
1906 static void
1907f_bufname(typval_T *argvars, typval_T *rettv)
1908{
1909 buf_T *buf;
1910
1911 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1912 ++emsg_off;
1913 buf = get_buf_tv(&argvars[0], FALSE);
1914 rettv->v_type = VAR_STRING;
1915 if (buf != NULL && buf->b_fname != NULL)
1916 rettv->vval.v_string = vim_strsave(buf->b_fname);
1917 else
1918 rettv->vval.v_string = NULL;
1919 --emsg_off;
1920}
1921
1922/*
1923 * "bufnr(expr)" function
1924 */
1925 static void
1926f_bufnr(typval_T *argvars, typval_T *rettv)
1927{
1928 buf_T *buf;
1929 int error = FALSE;
1930 char_u *name;
1931
1932 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1933 ++emsg_off;
1934 buf = get_buf_tv(&argvars[0], FALSE);
1935 --emsg_off;
1936
1937 /* If the buffer isn't found and the second argument is not zero create a
1938 * new buffer. */
1939 if (buf == NULL
1940 && argvars[1].v_type != VAR_UNKNOWN
1941 && get_tv_number_chk(&argvars[1], &error) != 0
1942 && !error
1943 && (name = get_tv_string_chk(&argvars[0])) != NULL
1944 && !error)
1945 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1946
1947 if (buf != NULL)
1948 rettv->vval.v_number = buf->b_fnum;
1949 else
1950 rettv->vval.v_number = -1;
1951}
1952
1953 static void
1954buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1955{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001956 win_T *wp;
1957 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001958 buf_T *buf;
1959
1960 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1961 ++emsg_off;
1962 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001963 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 {
1965 ++winnr;
1966 if (wp->w_buffer == buf)
1967 break;
1968 }
1969 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001970 --emsg_off;
1971}
1972
1973/*
1974 * "bufwinid(nr)" function
1975 */
1976 static void
1977f_bufwinid(typval_T *argvars, typval_T *rettv)
1978{
1979 buf_win_common(argvars, rettv, FALSE);
1980}
1981
1982/*
1983 * "bufwinnr(nr)" function
1984 */
1985 static void
1986f_bufwinnr(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_win_common(argvars, rettv, TRUE);
1989}
1990
1991/*
1992 * "byte2line(byte)" function
1993 */
1994 static void
1995f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1996{
1997#ifndef FEAT_BYTEOFF
1998 rettv->vval.v_number = -1;
1999#else
2000 long boff = 0;
2001
2002 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
2003 if (boff < 0)
2004 rettv->vval.v_number = -1;
2005 else
2006 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2007 (linenr_T)0, &boff);
2008#endif
2009}
2010
2011 static void
2012byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2013{
2014#ifdef FEAT_MBYTE
2015 char_u *t;
2016#endif
2017 char_u *str;
2018 varnumber_T idx;
2019
2020 str = get_tv_string_chk(&argvars[0]);
2021 idx = get_tv_number_chk(&argvars[1], NULL);
2022 rettv->vval.v_number = -1;
2023 if (str == NULL || idx < 0)
2024 return;
2025
2026#ifdef FEAT_MBYTE
2027 t = str;
2028 for ( ; idx > 0; idx--)
2029 {
2030 if (*t == NUL) /* EOL reached */
2031 return;
2032 if (enc_utf8 && comp)
2033 t += utf_ptr2len(t);
2034 else
2035 t += (*mb_ptr2len)(t);
2036 }
2037 rettv->vval.v_number = (varnumber_T)(t - str);
2038#else
2039 if ((size_t)idx <= STRLEN(str))
2040 rettv->vval.v_number = idx;
2041#endif
2042}
2043
2044/*
2045 * "byteidx()" function
2046 */
2047 static void
2048f_byteidx(typval_T *argvars, typval_T *rettv)
2049{
2050 byteidx(argvars, rettv, FALSE);
2051}
2052
2053/*
2054 * "byteidxcomp()" function
2055 */
2056 static void
2057f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2058{
2059 byteidx(argvars, rettv, TRUE);
2060}
2061
2062/*
2063 * "call(func, arglist [, dict])" function
2064 */
2065 static void
2066f_call(typval_T *argvars, typval_T *rettv)
2067{
2068 char_u *func;
2069 partial_T *partial = NULL;
2070 dict_T *selfdict = NULL;
2071
2072 if (argvars[1].v_type != VAR_LIST)
2073 {
2074 EMSG(_(e_listreq));
2075 return;
2076 }
2077 if (argvars[1].vval.v_list == NULL)
2078 return;
2079
2080 if (argvars[0].v_type == VAR_FUNC)
2081 func = argvars[0].vval.v_string;
2082 else if (argvars[0].v_type == VAR_PARTIAL)
2083 {
2084 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002085 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 }
2087 else
2088 func = get_tv_string(&argvars[0]);
2089 if (*func == NUL)
2090 return; /* type error or empty name */
2091
2092 if (argvars[2].v_type != VAR_UNKNOWN)
2093 {
2094 if (argvars[2].v_type != VAR_DICT)
2095 {
2096 EMSG(_(e_dictreq));
2097 return;
2098 }
2099 selfdict = argvars[2].vval.v_dict;
2100 }
2101
2102 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2103}
2104
2105#ifdef FEAT_FLOAT
2106/*
2107 * "ceil({float})" function
2108 */
2109 static void
2110f_ceil(typval_T *argvars, typval_T *rettv)
2111{
2112 float_T f = 0.0;
2113
2114 rettv->v_type = VAR_FLOAT;
2115 if (get_float_arg(argvars, &f) == OK)
2116 rettv->vval.v_float = ceil(f);
2117 else
2118 rettv->vval.v_float = 0.0;
2119}
2120#endif
2121
2122#ifdef FEAT_JOB_CHANNEL
2123/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002124 * "ch_canread()" function
2125 */
2126 static void
2127f_ch_canread(typval_T *argvars, typval_T *rettv)
2128{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002129 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002130
2131 rettv->vval.v_number = 0;
2132 if (channel != NULL)
2133 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2134 || channel_has_readahead(channel, PART_OUT)
2135 || channel_has_readahead(channel, PART_ERR);
2136}
2137
2138/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 * "ch_close()" function
2140 */
2141 static void
2142f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2143{
2144 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2145
2146 if (channel != NULL)
2147 {
2148 channel_close(channel, FALSE);
2149 channel_clear(channel);
2150 }
2151}
2152
2153/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002154 * "ch_close()" function
2155 */
2156 static void
2157f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2158{
2159 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2160
2161 if (channel != NULL)
2162 channel_close_in(channel);
2163}
2164
2165/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 * "ch_getbufnr()" function
2167 */
2168 static void
2169f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2170{
2171 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2172
2173 rettv->vval.v_number = -1;
2174 if (channel != NULL)
2175 {
2176 char_u *what = get_tv_string(&argvars[1]);
2177 int part;
2178
2179 if (STRCMP(what, "err") == 0)
2180 part = PART_ERR;
2181 else if (STRCMP(what, "out") == 0)
2182 part = PART_OUT;
2183 else if (STRCMP(what, "in") == 0)
2184 part = PART_IN;
2185 else
2186 part = PART_SOCK;
2187 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2188 rettv->vval.v_number =
2189 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2190 }
2191}
2192
2193/*
2194 * "ch_getjob()" function
2195 */
2196 static void
2197f_ch_getjob(typval_T *argvars, typval_T *rettv)
2198{
2199 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2200
2201 if (channel != NULL)
2202 {
2203 rettv->v_type = VAR_JOB;
2204 rettv->vval.v_job = channel->ch_job;
2205 if (channel->ch_job != NULL)
2206 ++channel->ch_job->jv_refcount;
2207 }
2208}
2209
2210/*
2211 * "ch_info()" function
2212 */
2213 static void
2214f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2215{
2216 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2217
2218 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2219 channel_info(channel, rettv->vval.v_dict);
2220}
2221
2222/*
2223 * "ch_log()" function
2224 */
2225 static void
2226f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2227{
2228 char_u *msg = get_tv_string(&argvars[0]);
2229 channel_T *channel = NULL;
2230
2231 if (argvars[1].v_type != VAR_UNKNOWN)
2232 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2233
Bram Moolenaard5359b22018-04-05 22:44:39 +02002234 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235}
2236
2237/*
2238 * "ch_logfile()" function
2239 */
2240 static void
2241f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2242{
2243 char_u *fname;
2244 char_u *opt = (char_u *)"";
2245 char_u buf[NUMBUFLEN];
2246
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002247 /* Don't open a file in restricted mode. */
2248 if (check_restricted() || check_secure())
2249 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250 fname = get_tv_string(&argvars[0]);
2251 if (argvars[1].v_type == VAR_STRING)
2252 opt = get_tv_string_buf(&argvars[1], buf);
2253 ch_logfile(fname, opt);
2254}
2255
2256/*
2257 * "ch_open()" function
2258 */
2259 static void
2260f_ch_open(typval_T *argvars, typval_T *rettv)
2261{
2262 rettv->v_type = VAR_CHANNEL;
2263 if (check_restricted() || check_secure())
2264 return;
2265 rettv->vval.v_channel = channel_open_func(argvars);
2266}
2267
2268/*
2269 * "ch_read()" function
2270 */
2271 static void
2272f_ch_read(typval_T *argvars, typval_T *rettv)
2273{
2274 common_channel_read(argvars, rettv, FALSE);
2275}
2276
2277/*
2278 * "ch_readraw()" function
2279 */
2280 static void
2281f_ch_readraw(typval_T *argvars, typval_T *rettv)
2282{
2283 common_channel_read(argvars, rettv, TRUE);
2284}
2285
2286/*
2287 * "ch_evalexpr()" function
2288 */
2289 static void
2290f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2291{
2292 ch_expr_common(argvars, rettv, TRUE);
2293}
2294
2295/*
2296 * "ch_sendexpr()" function
2297 */
2298 static void
2299f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2300{
2301 ch_expr_common(argvars, rettv, FALSE);
2302}
2303
2304/*
2305 * "ch_evalraw()" function
2306 */
2307 static void
2308f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2309{
2310 ch_raw_common(argvars, rettv, TRUE);
2311}
2312
2313/*
2314 * "ch_sendraw()" function
2315 */
2316 static void
2317f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2318{
2319 ch_raw_common(argvars, rettv, FALSE);
2320}
2321
2322/*
2323 * "ch_setoptions()" function
2324 */
2325 static void
2326f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2327{
2328 channel_T *channel;
2329 jobopt_T opt;
2330
2331 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2332 if (channel == NULL)
2333 return;
2334 clear_job_options(&opt);
2335 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002336 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 channel_set_options(channel, &opt);
2338 free_job_options(&opt);
2339}
2340
2341/*
2342 * "ch_status()" function
2343 */
2344 static void
2345f_ch_status(typval_T *argvars, typval_T *rettv)
2346{
2347 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002348 jobopt_T opt;
2349 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350
2351 /* return an empty string by default */
2352 rettv->v_type = VAR_STRING;
2353 rettv->vval.v_string = NULL;
2354
2355 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002356
2357 if (argvars[1].v_type != VAR_UNKNOWN)
2358 {
2359 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002360 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002361 && (opt.jo_set & JO_PART))
2362 part = opt.jo_part;
2363 }
2364
2365 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002366}
2367#endif
2368
2369/*
2370 * "changenr()" function
2371 */
2372 static void
2373f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2374{
2375 rettv->vval.v_number = curbuf->b_u_seq_cur;
2376}
2377
2378/*
2379 * "char2nr(string)" function
2380 */
2381 static void
2382f_char2nr(typval_T *argvars, typval_T *rettv)
2383{
2384#ifdef FEAT_MBYTE
2385 if (has_mbyte)
2386 {
2387 int utf8 = 0;
2388
2389 if (argvars[1].v_type != VAR_UNKNOWN)
2390 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2391
2392 if (utf8)
2393 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2394 else
2395 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2396 }
2397 else
2398#endif
2399 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2400}
2401
2402/*
2403 * "cindent(lnum)" function
2404 */
2405 static void
2406f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2407{
2408#ifdef FEAT_CINDENT
2409 pos_T pos;
2410 linenr_T lnum;
2411
2412 pos = curwin->w_cursor;
2413 lnum = get_tv_lnum(argvars);
2414 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2415 {
2416 curwin->w_cursor.lnum = lnum;
2417 rettv->vval.v_number = get_c_indent();
2418 curwin->w_cursor = pos;
2419 }
2420 else
2421#endif
2422 rettv->vval.v_number = -1;
2423}
2424
2425/*
2426 * "clearmatches()" function
2427 */
2428 static void
2429f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2430{
2431#ifdef FEAT_SEARCH_EXTRA
2432 clear_matches(curwin);
2433#endif
2434}
2435
2436/*
2437 * "col(string)" function
2438 */
2439 static void
2440f_col(typval_T *argvars, typval_T *rettv)
2441{
2442 colnr_T col = 0;
2443 pos_T *fp;
2444 int fnum = curbuf->b_fnum;
2445
2446 fp = var2fpos(&argvars[0], FALSE, &fnum);
2447 if (fp != NULL && fnum == curbuf->b_fnum)
2448 {
2449 if (fp->col == MAXCOL)
2450 {
2451 /* '> can be MAXCOL, get the length of the line then */
2452 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2453 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2454 else
2455 col = MAXCOL;
2456 }
2457 else
2458 {
2459 col = fp->col + 1;
2460#ifdef FEAT_VIRTUALEDIT
2461 /* col(".") when the cursor is on the NUL at the end of the line
2462 * because of "coladd" can be seen as an extra column. */
2463 if (virtual_active() && fp == &curwin->w_cursor)
2464 {
2465 char_u *p = ml_get_cursor();
2466
2467 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2468 curwin->w_virtcol - curwin->w_cursor.coladd))
2469 {
2470# ifdef FEAT_MBYTE
2471 int l;
2472
2473 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2474 col += l;
2475# else
2476 if (*p != NUL && p[1] == NUL)
2477 ++col;
2478# endif
2479 }
2480 }
2481#endif
2482 }
2483 }
2484 rettv->vval.v_number = col;
2485}
2486
2487#if defined(FEAT_INS_EXPAND)
2488/*
2489 * "complete()" function
2490 */
2491 static void
2492f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2493{
2494 int startcol;
2495
2496 if ((State & INSERT) == 0)
2497 {
2498 EMSG(_("E785: complete() can only be used in Insert mode"));
2499 return;
2500 }
2501
2502 /* Check for undo allowed here, because if something was already inserted
2503 * the line was already saved for undo and this check isn't done. */
2504 if (!undo_allowed())
2505 return;
2506
2507 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2508 {
2509 EMSG(_(e_invarg));
2510 return;
2511 }
2512
2513 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2514 if (startcol <= 0)
2515 return;
2516
2517 set_completion(startcol - 1, argvars[1].vval.v_list);
2518}
2519
2520/*
2521 * "complete_add()" function
2522 */
2523 static void
2524f_complete_add(typval_T *argvars, typval_T *rettv)
2525{
2526 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2527}
2528
2529/*
2530 * "complete_check()" function
2531 */
2532 static void
2533f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2534{
2535 int saved = RedrawingDisabled;
2536
2537 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002538 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 rettv->vval.v_number = compl_interrupted;
2540 RedrawingDisabled = saved;
2541}
2542#endif
2543
2544/*
2545 * "confirm(message, buttons[, default [, type]])" function
2546 */
2547 static void
2548f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2549{
2550#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2551 char_u *message;
2552 char_u *buttons = NULL;
2553 char_u buf[NUMBUFLEN];
2554 char_u buf2[NUMBUFLEN];
2555 int def = 1;
2556 int type = VIM_GENERIC;
2557 char_u *typestr;
2558 int error = FALSE;
2559
2560 message = get_tv_string_chk(&argvars[0]);
2561 if (message == NULL)
2562 error = TRUE;
2563 if (argvars[1].v_type != VAR_UNKNOWN)
2564 {
2565 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2566 if (buttons == NULL)
2567 error = TRUE;
2568 if (argvars[2].v_type != VAR_UNKNOWN)
2569 {
2570 def = (int)get_tv_number_chk(&argvars[2], &error);
2571 if (argvars[3].v_type != VAR_UNKNOWN)
2572 {
2573 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2574 if (typestr == NULL)
2575 error = TRUE;
2576 else
2577 {
2578 switch (TOUPPER_ASC(*typestr))
2579 {
2580 case 'E': type = VIM_ERROR; break;
2581 case 'Q': type = VIM_QUESTION; break;
2582 case 'I': type = VIM_INFO; break;
2583 case 'W': type = VIM_WARNING; break;
2584 case 'G': type = VIM_GENERIC; break;
2585 }
2586 }
2587 }
2588 }
2589 }
2590
2591 if (buttons == NULL || *buttons == NUL)
2592 buttons = (char_u *)_("&Ok");
2593
2594 if (!error)
2595 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2596 def, NULL, FALSE);
2597#endif
2598}
2599
2600/*
2601 * "copy()" function
2602 */
2603 static void
2604f_copy(typval_T *argvars, typval_T *rettv)
2605{
2606 item_copy(&argvars[0], rettv, FALSE, 0);
2607}
2608
2609#ifdef FEAT_FLOAT
2610/*
2611 * "cos()" function
2612 */
2613 static void
2614f_cos(typval_T *argvars, typval_T *rettv)
2615{
2616 float_T f = 0.0;
2617
2618 rettv->v_type = VAR_FLOAT;
2619 if (get_float_arg(argvars, &f) == OK)
2620 rettv->vval.v_float = cos(f);
2621 else
2622 rettv->vval.v_float = 0.0;
2623}
2624
2625/*
2626 * "cosh()" function
2627 */
2628 static void
2629f_cosh(typval_T *argvars, typval_T *rettv)
2630{
2631 float_T f = 0.0;
2632
2633 rettv->v_type = VAR_FLOAT;
2634 if (get_float_arg(argvars, &f) == OK)
2635 rettv->vval.v_float = cosh(f);
2636 else
2637 rettv->vval.v_float = 0.0;
2638}
2639#endif
2640
2641/*
2642 * "count()" function
2643 */
2644 static void
2645f_count(typval_T *argvars, typval_T *rettv)
2646{
2647 long n = 0;
2648 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002649 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650
Bram Moolenaar9966b212017-07-28 16:46:57 +02002651 if (argvars[2].v_type != VAR_UNKNOWN)
2652 ic = (int)get_tv_number_chk(&argvars[2], &error);
2653
2654 if (argvars[0].v_type == VAR_STRING)
2655 {
2656 char_u *expr = get_tv_string_chk(&argvars[1]);
2657 char_u *p = argvars[0].vval.v_string;
2658 char_u *next;
2659
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002660 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002661 {
2662 if (ic)
2663 {
2664 size_t len = STRLEN(expr);
2665
2666 while (*p != NUL)
2667 {
2668 if (MB_STRNICMP(p, expr, len) == 0)
2669 {
2670 ++n;
2671 p += len;
2672 }
2673 else
2674 MB_PTR_ADV(p);
2675 }
2676 }
2677 else
2678 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2679 != NULL)
2680 {
2681 ++n;
2682 p = next + STRLEN(expr);
2683 }
2684 }
2685
2686 }
2687 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 {
2689 listitem_T *li;
2690 list_T *l;
2691 long idx;
2692
2693 if ((l = argvars[0].vval.v_list) != NULL)
2694 {
2695 li = l->lv_first;
2696 if (argvars[2].v_type != VAR_UNKNOWN)
2697 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 if (argvars[3].v_type != VAR_UNKNOWN)
2699 {
2700 idx = (long)get_tv_number_chk(&argvars[3], &error);
2701 if (!error)
2702 {
2703 li = list_find(l, idx);
2704 if (li == NULL)
2705 EMSGN(_(e_listidx), idx);
2706 }
2707 }
2708 if (error)
2709 li = NULL;
2710 }
2711
2712 for ( ; li != NULL; li = li->li_next)
2713 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2714 ++n;
2715 }
2716 }
2717 else if (argvars[0].v_type == VAR_DICT)
2718 {
2719 int todo;
2720 dict_T *d;
2721 hashitem_T *hi;
2722
2723 if ((d = argvars[0].vval.v_dict) != NULL)
2724 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002725 if (argvars[2].v_type != VAR_UNKNOWN)
2726 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727 if (argvars[3].v_type != VAR_UNKNOWN)
2728 EMSG(_(e_invarg));
2729 }
2730
2731 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2732 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2733 {
2734 if (!HASHITEM_EMPTY(hi))
2735 {
2736 --todo;
2737 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2738 ++n;
2739 }
2740 }
2741 }
2742 }
2743 else
2744 EMSG2(_(e_listdictarg), "count()");
2745 rettv->vval.v_number = n;
2746}
2747
2748/*
2749 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2750 *
2751 * Checks the existence of a cscope connection.
2752 */
2753 static void
2754f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2755{
2756#ifdef FEAT_CSCOPE
2757 int num = 0;
2758 char_u *dbpath = NULL;
2759 char_u *prepend = NULL;
2760 char_u buf[NUMBUFLEN];
2761
2762 if (argvars[0].v_type != VAR_UNKNOWN
2763 && argvars[1].v_type != VAR_UNKNOWN)
2764 {
2765 num = (int)get_tv_number(&argvars[0]);
2766 dbpath = get_tv_string(&argvars[1]);
2767 if (argvars[2].v_type != VAR_UNKNOWN)
2768 prepend = get_tv_string_buf(&argvars[2], buf);
2769 }
2770
2771 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2772#endif
2773}
2774
2775/*
2776 * "cursor(lnum, col)" function, or
2777 * "cursor(list)"
2778 *
2779 * Moves the cursor to the specified line and column.
2780 * Returns 0 when the position could be set, -1 otherwise.
2781 */
2782 static void
2783f_cursor(typval_T *argvars, typval_T *rettv)
2784{
2785 long line, col;
2786#ifdef FEAT_VIRTUALEDIT
2787 long coladd = 0;
2788#endif
2789 int set_curswant = TRUE;
2790
2791 rettv->vval.v_number = -1;
2792 if (argvars[1].v_type == VAR_UNKNOWN)
2793 {
2794 pos_T pos;
2795 colnr_T curswant = -1;
2796
2797 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2798 {
2799 EMSG(_(e_invarg));
2800 return;
2801 }
2802 line = pos.lnum;
2803 col = pos.col;
2804#ifdef FEAT_VIRTUALEDIT
2805 coladd = pos.coladd;
2806#endif
2807 if (curswant >= 0)
2808 {
2809 curwin->w_curswant = curswant - 1;
2810 set_curswant = FALSE;
2811 }
2812 }
2813 else
2814 {
2815 line = get_tv_lnum(argvars);
2816 col = (long)get_tv_number_chk(&argvars[1], NULL);
2817#ifdef FEAT_VIRTUALEDIT
2818 if (argvars[2].v_type != VAR_UNKNOWN)
2819 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2820#endif
2821 }
2822 if (line < 0 || col < 0
2823#ifdef FEAT_VIRTUALEDIT
2824 || coladd < 0
2825#endif
2826 )
2827 return; /* type error; errmsg already given */
2828 if (line > 0)
2829 curwin->w_cursor.lnum = line;
2830 if (col > 0)
2831 curwin->w_cursor.col = col - 1;
2832#ifdef FEAT_VIRTUALEDIT
2833 curwin->w_cursor.coladd = coladd;
2834#endif
2835
2836 /* Make sure the cursor is in a valid position. */
2837 check_cursor();
2838#ifdef FEAT_MBYTE
2839 /* Correct cursor for multi-byte character. */
2840 if (has_mbyte)
2841 mb_adjust_cursor();
2842#endif
2843
2844 curwin->w_set_curswant = set_curswant;
2845 rettv->vval.v_number = 0;
2846}
2847
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002848#ifdef WIN3264
2849/*
2850 * "debugbreak()" function
2851 */
2852 static void
2853f_debugbreak(typval_T *argvars, typval_T *rettv)
2854{
2855 int pid;
2856
2857 rettv->vval.v_number = FAIL;
2858 pid = (int)get_tv_number(&argvars[0]);
2859 if (pid == 0)
2860 EMSG(_(e_invarg));
2861 else
2862 {
2863 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2864
2865 if (hProcess != NULL)
2866 {
2867 DebugBreakProcess(hProcess);
2868 CloseHandle(hProcess);
2869 rettv->vval.v_number = OK;
2870 }
2871 }
2872}
2873#endif
2874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875/*
2876 * "deepcopy()" function
2877 */
2878 static void
2879f_deepcopy(typval_T *argvars, typval_T *rettv)
2880{
2881 int noref = 0;
2882 int copyID;
2883
2884 if (argvars[1].v_type != VAR_UNKNOWN)
2885 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2886 if (noref < 0 || noref > 1)
2887 EMSG(_(e_invarg));
2888 else
2889 {
2890 copyID = get_copyID();
2891 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2892 }
2893}
2894
2895/*
2896 * "delete()" function
2897 */
2898 static void
2899f_delete(typval_T *argvars, typval_T *rettv)
2900{
2901 char_u nbuf[NUMBUFLEN];
2902 char_u *name;
2903 char_u *flags;
2904
2905 rettv->vval.v_number = -1;
2906 if (check_restricted() || check_secure())
2907 return;
2908
2909 name = get_tv_string(&argvars[0]);
2910 if (name == NULL || *name == NUL)
2911 {
2912 EMSG(_(e_invarg));
2913 return;
2914 }
2915
2916 if (argvars[1].v_type != VAR_UNKNOWN)
2917 flags = get_tv_string_buf(&argvars[1], nbuf);
2918 else
2919 flags = (char_u *)"";
2920
2921 if (*flags == NUL)
2922 /* delete a file */
2923 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2924 else if (STRCMP(flags, "d") == 0)
2925 /* delete an empty directory */
2926 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2927 else if (STRCMP(flags, "rf") == 0)
2928 /* delete a directory recursively */
2929 rettv->vval.v_number = delete_recursive(name);
2930 else
2931 EMSG2(_(e_invexpr2), flags);
2932}
2933
2934/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002935 * "deletebufline()" function
2936 */
2937 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002938f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002939{
2940 buf_T *buf;
2941 linenr_T first, last;
2942 linenr_T lnum;
2943 long count;
2944 int is_curbuf;
2945 buf_T *curbuf_save = NULL;
2946 win_T *curwin_save = NULL;
2947 tabpage_T *tp;
2948 win_T *wp;
2949
2950 buf = get_buf_tv(&argvars[0], FALSE);
2951 if (buf == NULL)
2952 {
2953 rettv->vval.v_number = 1; /* FAIL */
2954 return;
2955 }
2956 is_curbuf = buf == curbuf;
2957
2958 first = get_tv_lnum_buf(&argvars[1], buf);
2959 if (argvars[2].v_type != VAR_UNKNOWN)
2960 last = get_tv_lnum_buf(&argvars[2], buf);
2961 else
2962 last = first;
2963
2964 if (buf->b_ml.ml_mfp == NULL || first < 1
2965 || first > buf->b_ml.ml_line_count || last < first)
2966 {
2967 rettv->vval.v_number = 1; /* FAIL */
2968 return;
2969 }
2970
2971 if (!is_curbuf)
2972 {
2973 curbuf_save = curbuf;
2974 curwin_save = curwin;
2975 curbuf = buf;
2976 find_win_for_curbuf();
2977 }
2978 if (last > curbuf->b_ml.ml_line_count)
2979 last = curbuf->b_ml.ml_line_count;
2980 count = last - first + 1;
2981
2982 // When coming here from Insert mode, sync undo, so that this can be
2983 // undone separately from what was previously inserted.
2984 if (u_sync_once == 2)
2985 {
2986 u_sync_once = 1; // notify that u_sync() was called
2987 u_sync(TRUE);
2988 }
2989
2990 if (u_save(first - 1, last + 1) == FAIL)
2991 {
2992 rettv->vval.v_number = 1; /* FAIL */
2993 return;
2994 }
2995
2996 for (lnum = first; lnum <= last; ++lnum)
2997 ml_delete(first, TRUE);
2998
2999 FOR_ALL_TAB_WINDOWS(tp, wp)
3000 if (wp->w_buffer == buf)
3001 {
3002 if (wp->w_cursor.lnum > last)
3003 wp->w_cursor.lnum -= count;
3004 else if (wp->w_cursor.lnum> first)
3005 wp->w_cursor.lnum = first;
3006 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3007 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3008 }
3009 check_cursor_col();
3010 deleted_lines_mark(first, count);
3011
3012 if (!is_curbuf)
3013 {
3014 curbuf = curbuf_save;
3015 curwin = curwin_save;
3016 }
3017}
3018
3019/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 * "did_filetype()" function
3021 */
3022 static void
3023f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3024{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026}
3027
3028/*
3029 * "diff_filler()" function
3030 */
3031 static void
3032f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3033{
3034#ifdef FEAT_DIFF
3035 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
3036#endif
3037}
3038
3039/*
3040 * "diff_hlID()" function
3041 */
3042 static void
3043f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3044{
3045#ifdef FEAT_DIFF
3046 linenr_T lnum = get_tv_lnum(argvars);
3047 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003048 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 static int fnum = 0;
3050 static int change_start = 0;
3051 static int change_end = 0;
3052 static hlf_T hlID = (hlf_T)0;
3053 int filler_lines;
3054 int col;
3055
3056 if (lnum < 0) /* ignore type error in {lnum} arg */
3057 lnum = 0;
3058 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003059 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 || fnum != curbuf->b_fnum)
3061 {
3062 /* New line, buffer, change: need to get the values. */
3063 filler_lines = diff_check(curwin, lnum);
3064 if (filler_lines < 0)
3065 {
3066 if (filler_lines == -1)
3067 {
3068 change_start = MAXCOL;
3069 change_end = -1;
3070 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3071 hlID = HLF_ADD; /* added line */
3072 else
3073 hlID = HLF_CHD; /* changed line */
3074 }
3075 else
3076 hlID = HLF_ADD; /* added line */
3077 }
3078 else
3079 hlID = (hlf_T)0;
3080 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003081 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 fnum = curbuf->b_fnum;
3083 }
3084
3085 if (hlID == HLF_CHD || hlID == HLF_TXD)
3086 {
3087 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3088 if (col >= change_start && col <= change_end)
3089 hlID = HLF_TXD; /* changed text */
3090 else
3091 hlID = HLF_CHD; /* changed line */
3092 }
3093 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3094#endif
3095}
3096
3097/*
3098 * "empty({expr})" function
3099 */
3100 static void
3101f_empty(typval_T *argvars, typval_T *rettv)
3102{
3103 int n = FALSE;
3104
3105 switch (argvars[0].v_type)
3106 {
3107 case VAR_STRING:
3108 case VAR_FUNC:
3109 n = argvars[0].vval.v_string == NULL
3110 || *argvars[0].vval.v_string == NUL;
3111 break;
3112 case VAR_PARTIAL:
3113 n = FALSE;
3114 break;
3115 case VAR_NUMBER:
3116 n = argvars[0].vval.v_number == 0;
3117 break;
3118 case VAR_FLOAT:
3119#ifdef FEAT_FLOAT
3120 n = argvars[0].vval.v_float == 0.0;
3121 break;
3122#endif
3123 case VAR_LIST:
3124 n = argvars[0].vval.v_list == NULL
3125 || argvars[0].vval.v_list->lv_first == NULL;
3126 break;
3127 case VAR_DICT:
3128 n = argvars[0].vval.v_dict == NULL
3129 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3130 break;
3131 case VAR_SPECIAL:
3132 n = argvars[0].vval.v_number != VVAL_TRUE;
3133 break;
3134
3135 case VAR_JOB:
3136#ifdef FEAT_JOB_CHANNEL
3137 n = argvars[0].vval.v_job == NULL
3138 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3139 break;
3140#endif
3141 case VAR_CHANNEL:
3142#ifdef FEAT_JOB_CHANNEL
3143 n = argvars[0].vval.v_channel == NULL
3144 || !channel_is_open(argvars[0].vval.v_channel);
3145 break;
3146#endif
3147 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003148 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149 n = TRUE;
3150 break;
3151 }
3152
3153 rettv->vval.v_number = n;
3154}
3155
3156/*
3157 * "escape({string}, {chars})" function
3158 */
3159 static void
3160f_escape(typval_T *argvars, typval_T *rettv)
3161{
3162 char_u buf[NUMBUFLEN];
3163
3164 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3165 get_tv_string_buf(&argvars[1], buf));
3166 rettv->v_type = VAR_STRING;
3167}
3168
3169/*
3170 * "eval()" function
3171 */
3172 static void
3173f_eval(typval_T *argvars, typval_T *rettv)
3174{
3175 char_u *s, *p;
3176
3177 s = get_tv_string_chk(&argvars[0]);
3178 if (s != NULL)
3179 s = skipwhite(s);
3180
3181 p = s;
3182 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3183 {
3184 if (p != NULL && !aborting())
3185 EMSG2(_(e_invexpr2), p);
3186 need_clr_eos = FALSE;
3187 rettv->v_type = VAR_NUMBER;
3188 rettv->vval.v_number = 0;
3189 }
3190 else if (*s != NUL)
3191 EMSG(_(e_trailing));
3192}
3193
3194/*
3195 * "eventhandler()" function
3196 */
3197 static void
3198f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3199{
3200 rettv->vval.v_number = vgetc_busy;
3201}
3202
3203/*
3204 * "executable()" function
3205 */
3206 static void
3207f_executable(typval_T *argvars, typval_T *rettv)
3208{
3209 char_u *name = get_tv_string(&argvars[0]);
3210
3211 /* Check in $PATH and also check directly if there is a directory name. */
3212 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3213 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3214}
3215
3216static garray_T redir_execute_ga;
3217
3218/*
3219 * Append "value[value_len]" to the execute() output.
3220 */
3221 void
3222execute_redir_str(char_u *value, int value_len)
3223{
3224 int len;
3225
3226 if (value_len == -1)
3227 len = (int)STRLEN(value); /* Append the entire string */
3228 else
3229 len = value_len; /* Append only "value_len" characters */
3230 if (ga_grow(&redir_execute_ga, len) == OK)
3231 {
3232 mch_memmove((char *)redir_execute_ga.ga_data
3233 + redir_execute_ga.ga_len, value, len);
3234 redir_execute_ga.ga_len += len;
3235 }
3236}
3237
3238/*
3239 * Get next line from a list.
3240 * Called by do_cmdline() to get the next line.
3241 * Returns allocated string, or NULL for end of function.
3242 */
3243
3244 static char_u *
3245get_list_line(
3246 int c UNUSED,
3247 void *cookie,
3248 int indent UNUSED)
3249{
3250 listitem_T **p = (listitem_T **)cookie;
3251 listitem_T *item = *p;
3252 char_u buf[NUMBUFLEN];
3253 char_u *s;
3254
3255 if (item == NULL)
3256 return NULL;
3257 s = get_tv_string_buf_chk(&item->li_tv, buf);
3258 *p = item->li_next;
3259 return s == NULL ? NULL : vim_strsave(s);
3260}
3261
3262/*
3263 * "execute()" function
3264 */
3265 static void
3266f_execute(typval_T *argvars, typval_T *rettv)
3267{
3268 char_u *cmd = NULL;
3269 list_T *list = NULL;
3270 int save_msg_silent = msg_silent;
3271 int save_emsg_silent = emsg_silent;
3272 int save_emsg_noredir = emsg_noredir;
3273 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003274 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003276 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003277 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278
3279 rettv->vval.v_string = NULL;
3280 rettv->v_type = VAR_STRING;
3281
3282 if (argvars[0].v_type == VAR_LIST)
3283 {
3284 list = argvars[0].vval.v_list;
3285 if (list == NULL || list->lv_first == NULL)
3286 /* empty list, no commands, empty output */
3287 return;
3288 ++list->lv_refcount;
3289 }
3290 else
3291 {
3292 cmd = get_tv_string_chk(&argvars[0]);
3293 if (cmd == NULL)
3294 return;
3295 }
3296
3297 if (argvars[1].v_type != VAR_UNKNOWN)
3298 {
3299 char_u buf[NUMBUFLEN];
3300 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3301
3302 if (s == NULL)
3303 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003304 if (*s == NUL)
3305 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 if (STRNCMP(s, "silent", 6) == 0)
3307 ++msg_silent;
3308 if (STRCMP(s, "silent!") == 0)
3309 {
3310 emsg_silent = TRUE;
3311 emsg_noredir = TRUE;
3312 }
3313 }
3314 else
3315 ++msg_silent;
3316
3317 if (redir_execute)
3318 save_ga = redir_execute_ga;
3319 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3320 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003321 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003322 if (!echo_output)
3323 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324
3325 if (cmd != NULL)
3326 do_cmdline_cmd(cmd);
3327 else
3328 {
3329 listitem_T *item = list->lv_first;
3330
3331 do_cmdline(NULL, get_list_line, (void *)&item,
3332 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3333 --list->lv_refcount;
3334 }
3335
Bram Moolenaard297f352017-01-29 20:31:21 +01003336 /* Need to append a NUL to the result. */
3337 if (ga_grow(&redir_execute_ga, 1) == OK)
3338 {
3339 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3340 rettv->vval.v_string = redir_execute_ga.ga_data;
3341 }
3342 else
3343 {
3344 ga_clear(&redir_execute_ga);
3345 rettv->vval.v_string = NULL;
3346 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 msg_silent = save_msg_silent;
3348 emsg_silent = save_emsg_silent;
3349 emsg_noredir = save_emsg_noredir;
3350
3351 redir_execute = save_redir_execute;
3352 if (redir_execute)
3353 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003354 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003356 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003357 if (echo_output)
3358 // When not working silently: put it in column zero. A following
3359 // "echon" will overwrite the message, unavoidably.
3360 msg_col = 0;
3361 else
3362 // When working silently: Put it back where it was, since nothing
3363 // should have been written.
3364 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365}
3366
3367/*
3368 * "exepath()" function
3369 */
3370 static void
3371f_exepath(typval_T *argvars, typval_T *rettv)
3372{
3373 char_u *p = NULL;
3374
3375 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3376 rettv->v_type = VAR_STRING;
3377 rettv->vval.v_string = p;
3378}
3379
3380/*
3381 * "exists()" function
3382 */
3383 static void
3384f_exists(typval_T *argvars, typval_T *rettv)
3385{
3386 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003388
3389 p = get_tv_string(&argvars[0]);
3390 if (*p == '$') /* environment variable */
3391 {
3392 /* first try "normal" environment variables (fast) */
3393 if (mch_getenv(p + 1) != NULL)
3394 n = TRUE;
3395 else
3396 {
3397 /* try expanding things like $VIM and ${HOME} */
3398 p = expand_env_save(p);
3399 if (p != NULL && *p != '$')
3400 n = TRUE;
3401 vim_free(p);
3402 }
3403 }
3404 else if (*p == '&' || *p == '+') /* option */
3405 {
3406 n = (get_option_tv(&p, NULL, TRUE) == OK);
3407 if (*skipwhite(p) != NUL)
3408 n = FALSE; /* trailing garbage */
3409 }
3410 else if (*p == '*') /* internal or user defined function */
3411 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003412 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413 }
3414 else if (*p == ':')
3415 {
3416 n = cmd_exists(p + 1);
3417 }
3418 else if (*p == '#')
3419 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 if (p[1] == '#')
3421 n = autocmd_supported(p + 2);
3422 else
3423 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 }
3425 else /* internal variable */
3426 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003427 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 }
3429
3430 rettv->vval.v_number = n;
3431}
3432
3433#ifdef FEAT_FLOAT
3434/*
3435 * "exp()" function
3436 */
3437 static void
3438f_exp(typval_T *argvars, typval_T *rettv)
3439{
3440 float_T f = 0.0;
3441
3442 rettv->v_type = VAR_FLOAT;
3443 if (get_float_arg(argvars, &f) == OK)
3444 rettv->vval.v_float = exp(f);
3445 else
3446 rettv->vval.v_float = 0.0;
3447}
3448#endif
3449
3450/*
3451 * "expand()" function
3452 */
3453 static void
3454f_expand(typval_T *argvars, typval_T *rettv)
3455{
3456 char_u *s;
3457 int len;
3458 char_u *errormsg;
3459 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3460 expand_T xpc;
3461 int error = FALSE;
3462 char_u *result;
3463
3464 rettv->v_type = VAR_STRING;
3465 if (argvars[1].v_type != VAR_UNKNOWN
3466 && argvars[2].v_type != VAR_UNKNOWN
3467 && get_tv_number_chk(&argvars[2], &error)
3468 && !error)
3469 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003470 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 }
3472
3473 s = get_tv_string(&argvars[0]);
3474 if (*s == '%' || *s == '#' || *s == '<')
3475 {
3476 ++emsg_off;
3477 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3478 --emsg_off;
3479 if (rettv->v_type == VAR_LIST)
3480 {
3481 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3482 list_append_string(rettv->vval.v_list, result, -1);
3483 else
3484 vim_free(result);
3485 }
3486 else
3487 rettv->vval.v_string = result;
3488 }
3489 else
3490 {
3491 /* When the optional second argument is non-zero, don't remove matches
3492 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3493 if (argvars[1].v_type != VAR_UNKNOWN
3494 && get_tv_number_chk(&argvars[1], &error))
3495 options |= WILD_KEEP_ALL;
3496 if (!error)
3497 {
3498 ExpandInit(&xpc);
3499 xpc.xp_context = EXPAND_FILES;
3500 if (p_wic)
3501 options += WILD_ICASE;
3502 if (rettv->v_type == VAR_STRING)
3503 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3504 options, WILD_ALL);
3505 else if (rettv_list_alloc(rettv) != FAIL)
3506 {
3507 int i;
3508
3509 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3510 for (i = 0; i < xpc.xp_numfiles; i++)
3511 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3512 ExpandCleanup(&xpc);
3513 }
3514 }
3515 else
3516 rettv->vval.v_string = NULL;
3517 }
3518}
3519
3520/*
3521 * "extend(list, list [, idx])" function
3522 * "extend(dict, dict [, action])" function
3523 */
3524 static void
3525f_extend(typval_T *argvars, typval_T *rettv)
3526{
3527 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3528
3529 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3530 {
3531 list_T *l1, *l2;
3532 listitem_T *item;
3533 long before;
3534 int error = FALSE;
3535
3536 l1 = argvars[0].vval.v_list;
3537 l2 = argvars[1].vval.v_list;
3538 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3539 && l2 != NULL)
3540 {
3541 if (argvars[2].v_type != VAR_UNKNOWN)
3542 {
3543 before = (long)get_tv_number_chk(&argvars[2], &error);
3544 if (error)
3545 return; /* type error; errmsg already given */
3546
3547 if (before == l1->lv_len)
3548 item = NULL;
3549 else
3550 {
3551 item = list_find(l1, before);
3552 if (item == NULL)
3553 {
3554 EMSGN(_(e_listidx), before);
3555 return;
3556 }
3557 }
3558 }
3559 else
3560 item = NULL;
3561 list_extend(l1, l2, item);
3562
3563 copy_tv(&argvars[0], rettv);
3564 }
3565 }
3566 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3567 {
3568 dict_T *d1, *d2;
3569 char_u *action;
3570 int i;
3571
3572 d1 = argvars[0].vval.v_dict;
3573 d2 = argvars[1].vval.v_dict;
3574 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3575 && d2 != NULL)
3576 {
3577 /* Check the third argument. */
3578 if (argvars[2].v_type != VAR_UNKNOWN)
3579 {
3580 static char *(av[]) = {"keep", "force", "error"};
3581
3582 action = get_tv_string_chk(&argvars[2]);
3583 if (action == NULL)
3584 return; /* type error; errmsg already given */
3585 for (i = 0; i < 3; ++i)
3586 if (STRCMP(action, av[i]) == 0)
3587 break;
3588 if (i == 3)
3589 {
3590 EMSG2(_(e_invarg2), action);
3591 return;
3592 }
3593 }
3594 else
3595 action = (char_u *)"force";
3596
3597 dict_extend(d1, d2, action);
3598
3599 copy_tv(&argvars[0], rettv);
3600 }
3601 }
3602 else
3603 EMSG2(_(e_listdictarg), "extend()");
3604}
3605
3606/*
3607 * "feedkeys()" function
3608 */
3609 static void
3610f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3611{
3612 int remap = TRUE;
3613 int insert = FALSE;
3614 char_u *keys, *flags;
3615 char_u nbuf[NUMBUFLEN];
3616 int typed = FALSE;
3617 int execute = FALSE;
3618 int dangerous = FALSE;
3619 char_u *keys_esc;
3620
3621 /* This is not allowed in the sandbox. If the commands would still be
3622 * executed in the sandbox it would be OK, but it probably happens later,
3623 * when "sandbox" is no longer set. */
3624 if (check_secure())
3625 return;
3626
3627 keys = get_tv_string(&argvars[0]);
3628
3629 if (argvars[1].v_type != VAR_UNKNOWN)
3630 {
3631 flags = get_tv_string_buf(&argvars[1], nbuf);
3632 for ( ; *flags != NUL; ++flags)
3633 {
3634 switch (*flags)
3635 {
3636 case 'n': remap = FALSE; break;
3637 case 'm': remap = TRUE; break;
3638 case 't': typed = TRUE; break;
3639 case 'i': insert = TRUE; break;
3640 case 'x': execute = TRUE; break;
3641 case '!': dangerous = TRUE; break;
3642 }
3643 }
3644 }
3645
3646 if (*keys != NUL || execute)
3647 {
3648 /* Need to escape K_SPECIAL and CSI before putting the string in the
3649 * typeahead buffer. */
3650 keys_esc = vim_strsave_escape_csi(keys);
3651 if (keys_esc != NULL)
3652 {
3653 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3654 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3655 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003656 if (vgetc_busy
3657#ifdef FEAT_TIMERS
3658 || timer_busy
3659#endif
3660 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 typebuf_was_filled = TRUE;
3662 if (execute)
3663 {
3664 int save_msg_scroll = msg_scroll;
3665
3666 /* Avoid a 1 second delay when the keys start Insert mode. */
3667 msg_scroll = FALSE;
3668
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003669 if (!dangerous)
3670 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003671 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003672 if (!dangerous)
3673 --ex_normal_busy;
3674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 msg_scroll |= save_msg_scroll;
3676 }
3677 }
3678 }
3679}
3680
3681/*
3682 * "filereadable()" function
3683 */
3684 static void
3685f_filereadable(typval_T *argvars, typval_T *rettv)
3686{
3687 int fd;
3688 char_u *p;
3689 int n;
3690
3691#ifndef O_NONBLOCK
3692# define O_NONBLOCK 0
3693#endif
3694 p = get_tv_string(&argvars[0]);
3695 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3696 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3697 {
3698 n = TRUE;
3699 close(fd);
3700 }
3701 else
3702 n = FALSE;
3703
3704 rettv->vval.v_number = n;
3705}
3706
3707/*
3708 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3709 * rights to write into.
3710 */
3711 static void
3712f_filewritable(typval_T *argvars, typval_T *rettv)
3713{
3714 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3715}
3716
3717 static void
3718findfilendir(
3719 typval_T *argvars UNUSED,
3720 typval_T *rettv,
3721 int find_what UNUSED)
3722{
3723#ifdef FEAT_SEARCHPATH
3724 char_u *fname;
3725 char_u *fresult = NULL;
3726 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3727 char_u *p;
3728 char_u pathbuf[NUMBUFLEN];
3729 int count = 1;
3730 int first = TRUE;
3731 int error = FALSE;
3732#endif
3733
3734 rettv->vval.v_string = NULL;
3735 rettv->v_type = VAR_STRING;
3736
3737#ifdef FEAT_SEARCHPATH
3738 fname = get_tv_string(&argvars[0]);
3739
3740 if (argvars[1].v_type != VAR_UNKNOWN)
3741 {
3742 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3743 if (p == NULL)
3744 error = TRUE;
3745 else
3746 {
3747 if (*p != NUL)
3748 path = p;
3749
3750 if (argvars[2].v_type != VAR_UNKNOWN)
3751 count = (int)get_tv_number_chk(&argvars[2], &error);
3752 }
3753 }
3754
3755 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3756 error = TRUE;
3757
3758 if (*fname != NUL && !error)
3759 {
3760 do
3761 {
3762 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3763 vim_free(fresult);
3764 fresult = find_file_in_path_option(first ? fname : NULL,
3765 first ? (int)STRLEN(fname) : 0,
3766 0, first, path,
3767 find_what,
3768 curbuf->b_ffname,
3769 find_what == FINDFILE_DIR
3770 ? (char_u *)"" : curbuf->b_p_sua);
3771 first = FALSE;
3772
3773 if (fresult != NULL && rettv->v_type == VAR_LIST)
3774 list_append_string(rettv->vval.v_list, fresult, -1);
3775
3776 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3777 }
3778
3779 if (rettv->v_type == VAR_STRING)
3780 rettv->vval.v_string = fresult;
3781#endif
3782}
3783
3784/*
3785 * "filter()" function
3786 */
3787 static void
3788f_filter(typval_T *argvars, typval_T *rettv)
3789{
3790 filter_map(argvars, rettv, FALSE);
3791}
3792
3793/*
3794 * "finddir({fname}[, {path}[, {count}]])" function
3795 */
3796 static void
3797f_finddir(typval_T *argvars, typval_T *rettv)
3798{
3799 findfilendir(argvars, rettv, FINDFILE_DIR);
3800}
3801
3802/*
3803 * "findfile({fname}[, {path}[, {count}]])" function
3804 */
3805 static void
3806f_findfile(typval_T *argvars, typval_T *rettv)
3807{
3808 findfilendir(argvars, rettv, FINDFILE_FILE);
3809}
3810
3811#ifdef FEAT_FLOAT
3812/*
3813 * "float2nr({float})" function
3814 */
3815 static void
3816f_float2nr(typval_T *argvars, typval_T *rettv)
3817{
3818 float_T f = 0.0;
3819
3820 if (get_float_arg(argvars, &f) == OK)
3821 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003822 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003823 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003824 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003825 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 else
3827 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003828 }
3829}
3830
3831/*
3832 * "floor({float})" function
3833 */
3834 static void
3835f_floor(typval_T *argvars, typval_T *rettv)
3836{
3837 float_T f = 0.0;
3838
3839 rettv->v_type = VAR_FLOAT;
3840 if (get_float_arg(argvars, &f) == OK)
3841 rettv->vval.v_float = floor(f);
3842 else
3843 rettv->vval.v_float = 0.0;
3844}
3845
3846/*
3847 * "fmod()" function
3848 */
3849 static void
3850f_fmod(typval_T *argvars, typval_T *rettv)
3851{
3852 float_T fx = 0.0, fy = 0.0;
3853
3854 rettv->v_type = VAR_FLOAT;
3855 if (get_float_arg(argvars, &fx) == OK
3856 && get_float_arg(&argvars[1], &fy) == OK)
3857 rettv->vval.v_float = fmod(fx, fy);
3858 else
3859 rettv->vval.v_float = 0.0;
3860}
3861#endif
3862
3863/*
3864 * "fnameescape({string})" function
3865 */
3866 static void
3867f_fnameescape(typval_T *argvars, typval_T *rettv)
3868{
3869 rettv->vval.v_string = vim_strsave_fnameescape(
3870 get_tv_string(&argvars[0]), FALSE);
3871 rettv->v_type = VAR_STRING;
3872}
3873
3874/*
3875 * "fnamemodify({fname}, {mods})" function
3876 */
3877 static void
3878f_fnamemodify(typval_T *argvars, typval_T *rettv)
3879{
3880 char_u *fname;
3881 char_u *mods;
3882 int usedlen = 0;
3883 int len;
3884 char_u *fbuf = NULL;
3885 char_u buf[NUMBUFLEN];
3886
3887 fname = get_tv_string_chk(&argvars[0]);
3888 mods = get_tv_string_buf_chk(&argvars[1], buf);
3889 if (fname == NULL || mods == NULL)
3890 fname = NULL;
3891 else
3892 {
3893 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003894 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 }
3896
3897 rettv->v_type = VAR_STRING;
3898 if (fname == NULL)
3899 rettv->vval.v_string = NULL;
3900 else
3901 rettv->vval.v_string = vim_strnsave(fname, len);
3902 vim_free(fbuf);
3903}
3904
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003905/*
3906 * "foldclosed()" function
3907 */
3908 static void
3909foldclosed_both(
3910 typval_T *argvars UNUSED,
3911 typval_T *rettv,
3912 int end UNUSED)
3913{
3914#ifdef FEAT_FOLDING
3915 linenr_T lnum;
3916 linenr_T first, last;
3917
3918 lnum = get_tv_lnum(argvars);
3919 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3920 {
3921 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3922 {
3923 if (end)
3924 rettv->vval.v_number = (varnumber_T)last;
3925 else
3926 rettv->vval.v_number = (varnumber_T)first;
3927 return;
3928 }
3929 }
3930#endif
3931 rettv->vval.v_number = -1;
3932}
3933
3934/*
3935 * "foldclosed()" function
3936 */
3937 static void
3938f_foldclosed(typval_T *argvars, typval_T *rettv)
3939{
3940 foldclosed_both(argvars, rettv, FALSE);
3941}
3942
3943/*
3944 * "foldclosedend()" function
3945 */
3946 static void
3947f_foldclosedend(typval_T *argvars, typval_T *rettv)
3948{
3949 foldclosed_both(argvars, rettv, TRUE);
3950}
3951
3952/*
3953 * "foldlevel()" function
3954 */
3955 static void
3956f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3957{
3958#ifdef FEAT_FOLDING
3959 linenr_T lnum;
3960
3961 lnum = get_tv_lnum(argvars);
3962 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3963 rettv->vval.v_number = foldLevel(lnum);
3964#endif
3965}
3966
3967/*
3968 * "foldtext()" function
3969 */
3970 static void
3971f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3972{
3973#ifdef FEAT_FOLDING
3974 linenr_T foldstart;
3975 linenr_T foldend;
3976 char_u *dashes;
3977 linenr_T lnum;
3978 char_u *s;
3979 char_u *r;
3980 int len;
3981 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003982 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003983#endif
3984
3985 rettv->v_type = VAR_STRING;
3986 rettv->vval.v_string = NULL;
3987#ifdef FEAT_FOLDING
3988 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3989 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3990 dashes = get_vim_var_str(VV_FOLDDASHES);
3991 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3992 && dashes != NULL)
3993 {
3994 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003995 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003996 if (!linewhite(lnum))
3997 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998
3999 /* Find interesting text in this line. */
4000 s = skipwhite(ml_get(lnum));
4001 /* skip C comment-start */
4002 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4003 {
4004 s = skipwhite(s + 2);
4005 if (*skipwhite(s) == NUL
4006 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4007 {
4008 s = skipwhite(ml_get(lnum + 1));
4009 if (*s == '*')
4010 s = skipwhite(s + 1);
4011 }
4012 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004013 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004014 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 r = alloc((unsigned)(STRLEN(txt)
4016 + STRLEN(dashes) /* for %s */
4017 + 20 /* for %3ld */
4018 + STRLEN(s))); /* concatenated */
4019 if (r != NULL)
4020 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004021 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 len = (int)STRLEN(r);
4023 STRCAT(r, s);
4024 /* remove 'foldmarker' and 'commentstring' */
4025 foldtext_cleanup(r + len);
4026 rettv->vval.v_string = r;
4027 }
4028 }
4029#endif
4030}
4031
4032/*
4033 * "foldtextresult(lnum)" function
4034 */
4035 static void
4036f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4037{
4038#ifdef FEAT_FOLDING
4039 linenr_T lnum;
4040 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004041 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 foldinfo_T foldinfo;
4043 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004044 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004045#endif
4046
4047 rettv->v_type = VAR_STRING;
4048 rettv->vval.v_string = NULL;
4049#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004050 if (entered)
4051 return; /* reject recursive use */
4052 entered = TRUE;
4053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 lnum = get_tv_lnum(argvars);
4055 /* treat illegal types and illegal string values for {lnum} the same */
4056 if (lnum < 0)
4057 lnum = 0;
4058 fold_count = foldedCount(curwin, lnum, &foldinfo);
4059 if (fold_count > 0)
4060 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004061 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4062 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (text == buf)
4064 text = vim_strsave(text);
4065 rettv->vval.v_string = text;
4066 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004067
4068 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069#endif
4070}
4071
4072/*
4073 * "foreground()" function
4074 */
4075 static void
4076f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4077{
4078#ifdef FEAT_GUI
4079 if (gui.in_use)
4080 gui_mch_set_foreground();
4081#else
4082# ifdef WIN32
4083 win32_set_foreground();
4084# endif
4085#endif
4086}
4087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004089common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090{
4091 char_u *s;
4092 char_u *name;
4093 int use_string = FALSE;
4094 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004095 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096
4097 if (argvars[0].v_type == VAR_FUNC)
4098 {
4099 /* function(MyFunc, [arg], dict) */
4100 s = argvars[0].vval.v_string;
4101 }
4102 else if (argvars[0].v_type == VAR_PARTIAL
4103 && argvars[0].vval.v_partial != NULL)
4104 {
4105 /* function(dict.MyFunc, [arg]) */
4106 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004107 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004108 }
4109 else
4110 {
4111 /* function('MyFunc', [arg], dict) */
4112 s = get_tv_string(&argvars[0]);
4113 use_string = TRUE;
4114 }
4115
Bram Moolenaar843b8842016-08-21 14:36:15 +02004116 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004117 {
4118 name = s;
4119 trans_name = trans_function_name(&name, FALSE,
4120 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4121 if (*name != NUL)
4122 s = NULL;
4123 }
4124
Bram Moolenaar843b8842016-08-21 14:36:15 +02004125 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4126 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004127 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004129 else if (trans_name != NULL && (is_funcref
4130 ? find_func(trans_name) == NULL
4131 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004132 EMSG2(_("E700: Unknown function: %s"), s);
4133 else
4134 {
4135 int dict_idx = 0;
4136 int arg_idx = 0;
4137 list_T *list = NULL;
4138
4139 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4140 {
4141 char sid_buf[25];
4142 int off = *s == 's' ? 2 : 5;
4143
4144 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4145 * also be called from another script. Using trans_function_name()
4146 * would also work, but some plugins depend on the name being
4147 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004148 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4150 if (name != NULL)
4151 {
4152 STRCPY(name, sid_buf);
4153 STRCAT(name, s + off);
4154 }
4155 }
4156 else
4157 name = vim_strsave(s);
4158
4159 if (argvars[1].v_type != VAR_UNKNOWN)
4160 {
4161 if (argvars[2].v_type != VAR_UNKNOWN)
4162 {
4163 /* function(name, [args], dict) */
4164 arg_idx = 1;
4165 dict_idx = 2;
4166 }
4167 else if (argvars[1].v_type == VAR_DICT)
4168 /* function(name, dict) */
4169 dict_idx = 1;
4170 else
4171 /* function(name, [args]) */
4172 arg_idx = 1;
4173 if (dict_idx > 0)
4174 {
4175 if (argvars[dict_idx].v_type != VAR_DICT)
4176 {
4177 EMSG(_("E922: expected a dict"));
4178 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004180 }
4181 if (argvars[dict_idx].vval.v_dict == NULL)
4182 dict_idx = 0;
4183 }
4184 if (arg_idx > 0)
4185 {
4186 if (argvars[arg_idx].v_type != VAR_LIST)
4187 {
4188 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4189 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004190 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 }
4192 list = argvars[arg_idx].vval.v_list;
4193 if (list == NULL || list->lv_len == 0)
4194 arg_idx = 0;
4195 }
4196 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004197 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198 {
4199 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4200
4201 /* result is a VAR_PARTIAL */
4202 if (pt == NULL)
4203 vim_free(name);
4204 else
4205 {
4206 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4207 {
4208 listitem_T *li;
4209 int i = 0;
4210 int arg_len = 0;
4211 int lv_len = 0;
4212
4213 if (arg_pt != NULL)
4214 arg_len = arg_pt->pt_argc;
4215 if (list != NULL)
4216 lv_len = list->lv_len;
4217 pt->pt_argc = arg_len + lv_len;
4218 pt->pt_argv = (typval_T *)alloc(
4219 sizeof(typval_T) * pt->pt_argc);
4220 if (pt->pt_argv == NULL)
4221 {
4222 vim_free(pt);
4223 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004224 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004225 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004226 for (i = 0; i < arg_len; i++)
4227 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4228 if (lv_len > 0)
4229 for (li = list->lv_first; li != NULL;
4230 li = li->li_next)
4231 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004232 }
4233
4234 /* For "function(dict.func, [], dict)" and "func" is a partial
4235 * use "dict". That is backwards compatible. */
4236 if (dict_idx > 0)
4237 {
4238 /* The dict is bound explicitly, pt_auto is FALSE. */
4239 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4240 ++pt->pt_dict->dv_refcount;
4241 }
4242 else if (arg_pt != NULL)
4243 {
4244 /* If the dict was bound automatically the result is also
4245 * bound automatically. */
4246 pt->pt_dict = arg_pt->pt_dict;
4247 pt->pt_auto = arg_pt->pt_auto;
4248 if (pt->pt_dict != NULL)
4249 ++pt->pt_dict->dv_refcount;
4250 }
4251
4252 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4254 {
4255 pt->pt_func = arg_pt->pt_func;
4256 func_ptr_ref(pt->pt_func);
4257 vim_free(name);
4258 }
4259 else if (is_funcref)
4260 {
4261 pt->pt_func = find_func(trans_name);
4262 func_ptr_ref(pt->pt_func);
4263 vim_free(name);
4264 }
4265 else
4266 {
4267 pt->pt_name = name;
4268 func_ref(name);
4269 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004270 }
4271 rettv->v_type = VAR_PARTIAL;
4272 rettv->vval.v_partial = pt;
4273 }
4274 else
4275 {
4276 /* result is a VAR_FUNC */
4277 rettv->v_type = VAR_FUNC;
4278 rettv->vval.v_string = name;
4279 func_ref(name);
4280 }
4281 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282theend:
4283 vim_free(trans_name);
4284}
4285
4286/*
4287 * "funcref()" function
4288 */
4289 static void
4290f_funcref(typval_T *argvars, typval_T *rettv)
4291{
4292 common_function(argvars, rettv, TRUE);
4293}
4294
4295/*
4296 * "function()" function
4297 */
4298 static void
4299f_function(typval_T *argvars, typval_T *rettv)
4300{
4301 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302}
4303
4304/*
4305 * "garbagecollect()" function
4306 */
4307 static void
4308f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4309{
4310 /* This is postponed until we are back at the toplevel, because we may be
4311 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4312 want_garbage_collect = TRUE;
4313
4314 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4315 garbage_collect_at_exit = TRUE;
4316}
4317
4318/*
4319 * "get()" function
4320 */
4321 static void
4322f_get(typval_T *argvars, typval_T *rettv)
4323{
4324 listitem_T *li;
4325 list_T *l;
4326 dictitem_T *di;
4327 dict_T *d;
4328 typval_T *tv = NULL;
4329
4330 if (argvars[0].v_type == VAR_LIST)
4331 {
4332 if ((l = argvars[0].vval.v_list) != NULL)
4333 {
4334 int error = FALSE;
4335
4336 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4337 if (!error && li != NULL)
4338 tv = &li->li_tv;
4339 }
4340 }
4341 else if (argvars[0].v_type == VAR_DICT)
4342 {
4343 if ((d = argvars[0].vval.v_dict) != NULL)
4344 {
4345 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4346 if (di != NULL)
4347 tv = &di->di_tv;
4348 }
4349 }
4350 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4351 {
4352 partial_T *pt;
4353 partial_T fref_pt;
4354
4355 if (argvars[0].v_type == VAR_PARTIAL)
4356 pt = argvars[0].vval.v_partial;
4357 else
4358 {
4359 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4360 fref_pt.pt_name = argvars[0].vval.v_string;
4361 pt = &fref_pt;
4362 }
4363
4364 if (pt != NULL)
4365 {
4366 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004367 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368
4369 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4370 {
4371 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372 n = partial_name(pt);
4373 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374 rettv->vval.v_string = NULL;
4375 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004376 {
4377 rettv->vval.v_string = vim_strsave(n);
4378 if (rettv->v_type == VAR_FUNC)
4379 func_ref(rettv->vval.v_string);
4380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 }
4382 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004383 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004384 else if (STRCMP(what, "args") == 0)
4385 {
4386 rettv->v_type = VAR_LIST;
4387 if (rettv_list_alloc(rettv) == OK)
4388 {
4389 int i;
4390
4391 for (i = 0; i < pt->pt_argc; ++i)
4392 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4393 }
4394 }
4395 else
4396 EMSG2(_(e_invarg2), what);
4397 return;
4398 }
4399 }
4400 else
4401 EMSG2(_(e_listdictarg), "get()");
4402
4403 if (tv == NULL)
4404 {
4405 if (argvars[2].v_type != VAR_UNKNOWN)
4406 copy_tv(&argvars[2], rettv);
4407 }
4408 else
4409 copy_tv(tv, rettv);
4410}
4411
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004412#ifdef FEAT_SIGNS
4413/*
4414 * Returns information about signs placed in a buffer as list of dicts.
4415 */
4416 static void
4417get_buffer_signs(buf_T *buf, list_T *l)
4418{
4419 signlist_T *sign;
4420
4421 for (sign = buf->b_signlist; sign; sign = sign->next)
4422 {
4423 dict_T *d = dict_alloc();
4424
4425 if (d != NULL)
4426 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004427 dict_add_number(d, "id", sign->id);
4428 dict_add_number(d, "lnum", sign->lnum);
4429 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004430
4431 list_append_dict(l, d);
4432 }
4433 }
4434}
4435#endif
4436
4437/*
4438 * Returns buffer options, variables and other attributes in a dictionary.
4439 */
4440 static dict_T *
4441get_buffer_info(buf_T *buf)
4442{
4443 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004444 tabpage_T *tp;
4445 win_T *wp;
4446 list_T *windows;
4447
4448 dict = dict_alloc();
4449 if (dict == NULL)
4450 return NULL;
4451
Bram Moolenaare0be1672018-07-08 16:50:37 +02004452 dict_add_number(dict, "bufnr", buf->b_fnum);
4453 dict_add_string(dict, "name", buf->b_ffname);
4454 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4455 : buflist_findlnum(buf));
4456 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4457 dict_add_number(dict, "listed", buf->b_p_bl);
4458 dict_add_number(dict, "changed", bufIsChanged(buf));
4459 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4460 dict_add_number(dict, "hidden",
4461 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004462
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004463 /* Get a reference to buffer variables */
4464 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004465
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004466 /* List of windows displaying this buffer */
4467 windows = list_alloc();
4468 if (windows != NULL)
4469 {
4470 FOR_ALL_TAB_WINDOWS(tp, wp)
4471 if (wp->w_buffer == buf)
4472 list_append_number(windows, (varnumber_T)wp->w_id);
4473 dict_add_list(dict, "windows", windows);
4474 }
4475
4476#ifdef FEAT_SIGNS
4477 if (buf->b_signlist != NULL)
4478 {
4479 /* List of signs placed in this buffer */
4480 list_T *signs = list_alloc();
4481 if (signs != NULL)
4482 {
4483 get_buffer_signs(buf, signs);
4484 dict_add_list(dict, "signs", signs);
4485 }
4486 }
4487#endif
4488
4489 return dict;
4490}
4491
4492/*
4493 * "getbufinfo()" function
4494 */
4495 static void
4496f_getbufinfo(typval_T *argvars, typval_T *rettv)
4497{
4498 buf_T *buf = NULL;
4499 buf_T *argbuf = NULL;
4500 dict_T *d;
4501 int filtered = FALSE;
4502 int sel_buflisted = FALSE;
4503 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004504 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004505
4506 if (rettv_list_alloc(rettv) != OK)
4507 return;
4508
4509 /* List of all the buffers or selected buffers */
4510 if (argvars[0].v_type == VAR_DICT)
4511 {
4512 dict_T *sel_d = argvars[0].vval.v_dict;
4513
4514 if (sel_d != NULL)
4515 {
4516 dictitem_T *di;
4517
4518 filtered = TRUE;
4519
4520 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4521 if (di != NULL && get_tv_number(&di->di_tv))
4522 sel_buflisted = TRUE;
4523
4524 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4525 if (di != NULL && get_tv_number(&di->di_tv))
4526 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004527
4528 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4529 if (di != NULL && get_tv_number(&di->di_tv))
4530 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004531 }
4532 }
4533 else if (argvars[0].v_type != VAR_UNKNOWN)
4534 {
4535 /* Information about one buffer. Argument specifies the buffer */
4536 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4537 ++emsg_off;
4538 argbuf = get_buf_tv(&argvars[0], FALSE);
4539 --emsg_off;
4540 if (argbuf == NULL)
4541 return;
4542 }
4543
4544 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004545 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004546 {
4547 if (argbuf != NULL && argbuf != buf)
4548 continue;
4549 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004550 || (sel_buflisted && !buf->b_p_bl)
4551 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004552 continue;
4553
4554 d = get_buffer_info(buf);
4555 if (d != NULL)
4556 list_append_dict(rettv->vval.v_list, d);
4557 if (argbuf != NULL)
4558 return;
4559 }
4560}
4561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562/*
4563 * Get line or list of lines from buffer "buf" into "rettv".
4564 * Return a range (from start to end) of lines in rettv from the specified
4565 * buffer.
4566 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4567 */
4568 static void
4569get_buffer_lines(
4570 buf_T *buf,
4571 linenr_T start,
4572 linenr_T end,
4573 int retlist,
4574 typval_T *rettv)
4575{
4576 char_u *p;
4577
4578 rettv->v_type = VAR_STRING;
4579 rettv->vval.v_string = NULL;
4580 if (retlist && rettv_list_alloc(rettv) == FAIL)
4581 return;
4582
4583 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4584 return;
4585
4586 if (!retlist)
4587 {
4588 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4589 p = ml_get_buf(buf, start, FALSE);
4590 else
4591 p = (char_u *)"";
4592 rettv->vval.v_string = vim_strsave(p);
4593 }
4594 else
4595 {
4596 if (end < start)
4597 return;
4598
4599 if (start < 1)
4600 start = 1;
4601 if (end > buf->b_ml.ml_line_count)
4602 end = buf->b_ml.ml_line_count;
4603 while (start <= end)
4604 if (list_append_string(rettv->vval.v_list,
4605 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4606 break;
4607 }
4608}
4609
4610/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611 * "getbufline()" function
4612 */
4613 static void
4614f_getbufline(typval_T *argvars, typval_T *rettv)
4615{
4616 linenr_T lnum;
4617 linenr_T end;
4618 buf_T *buf;
4619
4620 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4621 ++emsg_off;
4622 buf = get_buf_tv(&argvars[0], FALSE);
4623 --emsg_off;
4624
4625 lnum = get_tv_lnum_buf(&argvars[1], buf);
4626 if (argvars[2].v_type == VAR_UNKNOWN)
4627 end = lnum;
4628 else
4629 end = get_tv_lnum_buf(&argvars[2], buf);
4630
4631 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4632}
4633
4634/*
4635 * "getbufvar()" function
4636 */
4637 static void
4638f_getbufvar(typval_T *argvars, typval_T *rettv)
4639{
4640 buf_T *buf;
4641 buf_T *save_curbuf;
4642 char_u *varname;
4643 dictitem_T *v;
4644 int done = FALSE;
4645
4646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4647 varname = get_tv_string_chk(&argvars[1]);
4648 ++emsg_off;
4649 buf = get_buf_tv(&argvars[0], FALSE);
4650
4651 rettv->v_type = VAR_STRING;
4652 rettv->vval.v_string = NULL;
4653
4654 if (buf != NULL && varname != NULL)
4655 {
4656 /* set curbuf to be our buf, temporarily */
4657 save_curbuf = curbuf;
4658 curbuf = buf;
4659
Bram Moolenaar30567352016-08-27 21:25:44 +02004660 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004662 if (varname[1] == NUL)
4663 {
4664 /* get all buffer-local options in a dict */
4665 dict_T *opts = get_winbuf_options(TRUE);
4666
4667 if (opts != NULL)
4668 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004669 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004670 done = TRUE;
4671 }
4672 }
4673 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4674 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 done = TRUE;
4676 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677 else
4678 {
4679 /* Look up the variable. */
4680 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4681 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4682 'b', varname, FALSE);
4683 if (v != NULL)
4684 {
4685 copy_tv(&v->di_tv, rettv);
4686 done = TRUE;
4687 }
4688 }
4689
4690 /* restore previous notion of curbuf */
4691 curbuf = save_curbuf;
4692 }
4693
4694 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4695 /* use the default value */
4696 copy_tv(&argvars[2], rettv);
4697
4698 --emsg_off;
4699}
4700
4701/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004702 * "getchangelist()" function
4703 */
4704 static void
4705f_getchangelist(typval_T *argvars, typval_T *rettv)
4706{
4707#ifdef FEAT_JUMPLIST
4708 buf_T *buf;
4709 int i;
4710 list_T *l;
4711 dict_T *d;
4712#endif
4713
4714 if (rettv_list_alloc(rettv) != OK)
4715 return;
4716
4717#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004718 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4719 ++emsg_off;
4720 buf = get_buf_tv(&argvars[0], FALSE);
4721 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004722 if (buf == NULL)
4723 return;
4724
4725 l = list_alloc();
4726 if (l == NULL)
4727 return;
4728
4729 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4730 return;
4731 /*
4732 * The current window change list index tracks only the position in the
4733 * current buffer change list. For other buffers, use the change list
4734 * length as the current index.
4735 */
4736 list_append_number(rettv->vval.v_list,
4737 (varnumber_T)((buf == curwin->w_buffer)
4738 ? curwin->w_changelistidx : buf->b_changelistlen));
4739
4740 for (i = 0; i < buf->b_changelistlen; ++i)
4741 {
4742 if (buf->b_changelist[i].lnum == 0)
4743 continue;
4744 if ((d = dict_alloc()) == NULL)
4745 return;
4746 if (list_append_dict(l, d) == FAIL)
4747 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004748 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4749 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004750# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004751 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004752# endif
4753 }
4754#endif
4755}
4756/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004757 * "getchar()" function
4758 */
4759 static void
4760f_getchar(typval_T *argvars, typval_T *rettv)
4761{
4762 varnumber_T n;
4763 int error = FALSE;
4764
Bram Moolenaar84d93902018-09-11 20:10:20 +02004765#ifdef MESSAGE_QUEUE
4766 // vpeekc() used to check for messages, but that caused problems, invoking
4767 // a callback where it was not expected. Some plugins use getchar(1) in a
4768 // loop to await a message, therefore make sure we check for messages here.
4769 parse_queued_messages();
4770#endif
4771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004772 /* Position the cursor. Needed after a message that ends in a space. */
4773 windgoto(msg_row, msg_col);
4774
4775 ++no_mapping;
4776 ++allow_keys;
4777 for (;;)
4778 {
4779 if (argvars[0].v_type == VAR_UNKNOWN)
4780 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004781 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4783 /* getchar(1): only check if char avail */
4784 n = vpeekc_any();
4785 else if (error || vpeekc_any() == NUL)
4786 /* illegal argument or getchar(0) and no char avail: return zero */
4787 n = 0;
4788 else
4789 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004790 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791
4792 if (n == K_IGNORE)
4793 continue;
4794 break;
4795 }
4796 --no_mapping;
4797 --allow_keys;
4798
4799 set_vim_var_nr(VV_MOUSE_WIN, 0);
4800 set_vim_var_nr(VV_MOUSE_WINID, 0);
4801 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4802 set_vim_var_nr(VV_MOUSE_COL, 0);
4803
4804 rettv->vval.v_number = n;
4805 if (IS_SPECIAL(n) || mod_mask != 0)
4806 {
4807 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4808 int i = 0;
4809
4810 /* Turn a special key into three bytes, plus modifier. */
4811 if (mod_mask != 0)
4812 {
4813 temp[i++] = K_SPECIAL;
4814 temp[i++] = KS_MODIFIER;
4815 temp[i++] = mod_mask;
4816 }
4817 if (IS_SPECIAL(n))
4818 {
4819 temp[i++] = K_SPECIAL;
4820 temp[i++] = K_SECOND(n);
4821 temp[i++] = K_THIRD(n);
4822 }
4823#ifdef FEAT_MBYTE
4824 else if (has_mbyte)
4825 i += (*mb_char2bytes)(n, temp + i);
4826#endif
4827 else
4828 temp[i++] = n;
4829 temp[i++] = NUL;
4830 rettv->v_type = VAR_STRING;
4831 rettv->vval.v_string = vim_strsave(temp);
4832
4833#ifdef FEAT_MOUSE
4834 if (is_mouse_key(n))
4835 {
4836 int row = mouse_row;
4837 int col = mouse_col;
4838 win_T *win;
4839 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841 int winnr = 1;
4842
4843 if (row >= 0 && col >= 0)
4844 {
4845 /* Find the window at the mouse coordinates and compute the
4846 * text position. */
4847 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004848 if (win == NULL)
4849 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 for (wp = firstwin; wp != win; wp = wp->w_next)
4852 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4854 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4855 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4856 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4857 }
4858 }
4859#endif
4860 }
4861}
4862
4863/*
4864 * "getcharmod()" function
4865 */
4866 static void
4867f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4868{
4869 rettv->vval.v_number = mod_mask;
4870}
4871
4872/*
4873 * "getcharsearch()" function
4874 */
4875 static void
4876f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4877{
4878 if (rettv_dict_alloc(rettv) != FAIL)
4879 {
4880 dict_T *dict = rettv->vval.v_dict;
4881
Bram Moolenaare0be1672018-07-08 16:50:37 +02004882 dict_add_string(dict, "char", last_csearch());
4883 dict_add_number(dict, "forward", last_csearch_forward());
4884 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 }
4886}
4887
4888/*
4889 * "getcmdline()" function
4890 */
4891 static void
4892f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4893{
4894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = get_cmdline_str();
4896}
4897
4898/*
4899 * "getcmdpos()" function
4900 */
4901 static void
4902f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4903{
4904 rettv->vval.v_number = get_cmdline_pos() + 1;
4905}
4906
4907/*
4908 * "getcmdtype()" function
4909 */
4910 static void
4911f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4912{
4913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = alloc(2);
4915 if (rettv->vval.v_string != NULL)
4916 {
4917 rettv->vval.v_string[0] = get_cmdline_type();
4918 rettv->vval.v_string[1] = NUL;
4919 }
4920}
4921
4922/*
4923 * "getcmdwintype()" function
4924 */
4925 static void
4926f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4927{
4928 rettv->v_type = VAR_STRING;
4929 rettv->vval.v_string = NULL;
4930#ifdef FEAT_CMDWIN
4931 rettv->vval.v_string = alloc(2);
4932 if (rettv->vval.v_string != NULL)
4933 {
4934 rettv->vval.v_string[0] = cmdwin_type;
4935 rettv->vval.v_string[1] = NUL;
4936 }
4937#endif
4938}
4939
4940#if defined(FEAT_CMDL_COMPL)
4941/*
4942 * "getcompletion()" function
4943 */
4944 static void
4945f_getcompletion(typval_T *argvars, typval_T *rettv)
4946{
4947 char_u *pat;
4948 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004949 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004950 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4951 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004952
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004953 if (argvars[2].v_type != VAR_UNKNOWN)
4954 filtered = get_tv_number_chk(&argvars[2], NULL);
4955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 if (p_wic)
4957 options |= WILD_ICASE;
4958
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004959 /* For filtered results, 'wildignore' is used */
4960 if (!filtered)
4961 options |= WILD_KEEP_ALL;
4962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 ExpandInit(&xpc);
4964 xpc.xp_pattern = get_tv_string(&argvars[0]);
4965 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4966 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4967 if (xpc.xp_context == EXPAND_NOTHING)
4968 {
4969 if (argvars[1].v_type == VAR_STRING)
4970 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4971 else
4972 EMSG(_(e_invarg));
4973 return;
4974 }
4975
4976# if defined(FEAT_MENU)
4977 if (xpc.xp_context == EXPAND_MENUS)
4978 {
4979 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4980 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4981 }
4982# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004983#ifdef FEAT_CSCOPE
4984 if (xpc.xp_context == EXPAND_CSCOPE)
4985 {
4986 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4987 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4988 }
4989#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004990#ifdef FEAT_SIGNS
4991 if (xpc.xp_context == EXPAND_SIGN)
4992 {
4993 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4994 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4995 }
4996#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004997
4998 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4999 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5000 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005001 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002
5003 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5004
5005 for (i = 0; i < xpc.xp_numfiles; i++)
5006 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5007 }
5008 vim_free(pat);
5009 ExpandCleanup(&xpc);
5010}
5011#endif
5012
5013/*
5014 * "getcwd()" function
5015 */
5016 static void
5017f_getcwd(typval_T *argvars, typval_T *rettv)
5018{
5019 win_T *wp = NULL;
5020 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005021 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022
5023 rettv->v_type = VAR_STRING;
5024 rettv->vval.v_string = NULL;
5025
Bram Moolenaar54591292018-02-09 20:53:59 +01005026 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5027 global = TRUE;
5028 else
5029 wp = find_tabwin(&argvars[0], &argvars[1]);
5030
5031 if (wp != NULL && wp->w_localdir != NULL)
5032 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5033 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005035 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 rettv->vval.v_string = vim_strsave(globaldir);
5037 else
5038 {
5039 cwd = alloc(MAXPATHL);
5040 if (cwd != NULL)
5041 {
5042 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5043 rettv->vval.v_string = vim_strsave(cwd);
5044 vim_free(cwd);
5045 }
5046 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005048#ifdef BACKSLASH_IN_FILENAME
5049 if (rettv->vval.v_string != NULL)
5050 slash_adjust(rettv->vval.v_string);
5051#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052}
5053
5054/*
5055 * "getfontname()" function
5056 */
5057 static void
5058f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5059{
5060 rettv->v_type = VAR_STRING;
5061 rettv->vval.v_string = NULL;
5062#ifdef FEAT_GUI
5063 if (gui.in_use)
5064 {
5065 GuiFont font;
5066 char_u *name = NULL;
5067
5068 if (argvars[0].v_type == VAR_UNKNOWN)
5069 {
5070 /* Get the "Normal" font. Either the name saved by
5071 * hl_set_font_name() or from the font ID. */
5072 font = gui.norm_font;
5073 name = hl_get_font_name();
5074 }
5075 else
5076 {
5077 name = get_tv_string(&argvars[0]);
5078 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5079 return;
5080 font = gui_mch_get_font(name, FALSE);
5081 if (font == NOFONT)
5082 return; /* Invalid font name, return empty string. */
5083 }
5084 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5085 if (argvars[0].v_type != VAR_UNKNOWN)
5086 gui_mch_free_font(font);
5087 }
5088#endif
5089}
5090
5091/*
5092 * "getfperm({fname})" function
5093 */
5094 static void
5095f_getfperm(typval_T *argvars, typval_T *rettv)
5096{
5097 char_u *fname;
5098 stat_T st;
5099 char_u *perm = NULL;
5100 char_u flags[] = "rwx";
5101 int i;
5102
5103 fname = get_tv_string(&argvars[0]);
5104
5105 rettv->v_type = VAR_STRING;
5106 if (mch_stat((char *)fname, &st) >= 0)
5107 {
5108 perm = vim_strsave((char_u *)"---------");
5109 if (perm != NULL)
5110 {
5111 for (i = 0; i < 9; i++)
5112 {
5113 if (st.st_mode & (1 << (8 - i)))
5114 perm[i] = flags[i % 3];
5115 }
5116 }
5117 }
5118 rettv->vval.v_string = perm;
5119}
5120
5121/*
5122 * "getfsize({fname})" function
5123 */
5124 static void
5125f_getfsize(typval_T *argvars, typval_T *rettv)
5126{
5127 char_u *fname;
5128 stat_T st;
5129
5130 fname = get_tv_string(&argvars[0]);
5131
5132 rettv->v_type = VAR_NUMBER;
5133
5134 if (mch_stat((char *)fname, &st) >= 0)
5135 {
5136 if (mch_isdir(fname))
5137 rettv->vval.v_number = 0;
5138 else
5139 {
5140 rettv->vval.v_number = (varnumber_T)st.st_size;
5141
5142 /* non-perfect check for overflow */
5143 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5144 rettv->vval.v_number = -2;
5145 }
5146 }
5147 else
5148 rettv->vval.v_number = -1;
5149}
5150
5151/*
5152 * "getftime({fname})" function
5153 */
5154 static void
5155f_getftime(typval_T *argvars, typval_T *rettv)
5156{
5157 char_u *fname;
5158 stat_T st;
5159
5160 fname = get_tv_string(&argvars[0]);
5161
5162 if (mch_stat((char *)fname, &st) >= 0)
5163 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5164 else
5165 rettv->vval.v_number = -1;
5166}
5167
5168/*
5169 * "getftype({fname})" function
5170 */
5171 static void
5172f_getftype(typval_T *argvars, typval_T *rettv)
5173{
5174 char_u *fname;
5175 stat_T st;
5176 char_u *type = NULL;
5177 char *t;
5178
5179 fname = get_tv_string(&argvars[0]);
5180
5181 rettv->v_type = VAR_STRING;
5182 if (mch_lstat((char *)fname, &st) >= 0)
5183 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005184 if (S_ISREG(st.st_mode))
5185 t = "file";
5186 else if (S_ISDIR(st.st_mode))
5187 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 else if (S_ISLNK(st.st_mode))
5189 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005190 else if (S_ISBLK(st.st_mode))
5191 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 else if (S_ISCHR(st.st_mode))
5193 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005194 else if (S_ISFIFO(st.st_mode))
5195 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005197 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 else
5199 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 type = vim_strsave((char_u *)t);
5201 }
5202 rettv->vval.v_string = type;
5203}
5204
5205/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005206 * "getjumplist()" function
5207 */
5208 static void
5209f_getjumplist(typval_T *argvars, typval_T *rettv)
5210{
5211#ifdef FEAT_JUMPLIST
5212 win_T *wp;
5213 int i;
5214 list_T *l;
5215 dict_T *d;
5216#endif
5217
5218 if (rettv_list_alloc(rettv) != OK)
5219 return;
5220
5221#ifdef FEAT_JUMPLIST
5222 wp = find_tabwin(&argvars[0], &argvars[1]);
5223 if (wp == NULL)
5224 return;
5225
5226 l = list_alloc();
5227 if (l == NULL)
5228 return;
5229
5230 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5231 return;
5232 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5233
Bram Moolenaar48679742018-02-13 13:33:29 +01005234 cleanup_jumplist(wp, TRUE);
5235
Bram Moolenaar4f505882018-02-10 21:06:32 +01005236 for (i = 0; i < wp->w_jumplistlen; ++i)
5237 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005238 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5239 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005240 if ((d = dict_alloc()) == NULL)
5241 return;
5242 if (list_append_dict(l, d) == FAIL)
5243 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005244 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5245 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005246# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005247 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005248# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005249 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005250 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005251 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005252 }
5253#endif
5254}
5255
5256/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005257 * "getline(lnum, [end])" function
5258 */
5259 static void
5260f_getline(typval_T *argvars, typval_T *rettv)
5261{
5262 linenr_T lnum;
5263 linenr_T end;
5264 int retlist;
5265
5266 lnum = get_tv_lnum(argvars);
5267 if (argvars[1].v_type == VAR_UNKNOWN)
5268 {
5269 end = 0;
5270 retlist = FALSE;
5271 }
5272 else
5273 {
5274 end = get_tv_lnum(&argvars[1]);
5275 retlist = TRUE;
5276 }
5277
5278 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5279}
5280
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005281#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005282 static void
5283get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5284{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005285 if (what_arg->v_type == VAR_UNKNOWN)
5286 {
5287 if (rettv_list_alloc(rettv) == OK)
5288 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005289 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005290 }
5291 else
5292 {
5293 if (rettv_dict_alloc(rettv) == OK)
5294 if (is_qf || (wp != NULL))
5295 {
5296 if (what_arg->v_type == VAR_DICT)
5297 {
5298 dict_T *d = what_arg->vval.v_dict;
5299
5300 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005301 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005302 }
5303 else
5304 EMSG(_(e_dictreq));
5305 }
5306 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005307}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005308#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005309
5310/*
5311 * "getloclist()" function
5312 */
5313 static void
5314f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5315{
5316#ifdef FEAT_QUICKFIX
5317 win_T *wp;
5318
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005319 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005320 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5321#endif
5322}
5323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324/*
5325 * "getmatches()" function
5326 */
5327 static void
5328f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5329{
5330#ifdef FEAT_SEARCH_EXTRA
5331 dict_T *dict;
5332 matchitem_T *cur = curwin->w_match_head;
5333 int i;
5334
5335 if (rettv_list_alloc(rettv) == OK)
5336 {
5337 while (cur != NULL)
5338 {
5339 dict = dict_alloc();
5340 if (dict == NULL)
5341 return;
5342 if (cur->match.regprog == NULL)
5343 {
5344 /* match added with matchaddpos() */
5345 for (i = 0; i < MAXPOSMATCH; ++i)
5346 {
5347 llpos_T *llpos;
5348 char buf[6];
5349 list_T *l;
5350
5351 llpos = &cur->pos.pos[i];
5352 if (llpos->lnum == 0)
5353 break;
5354 l = list_alloc();
5355 if (l == NULL)
5356 break;
5357 list_append_number(l, (varnumber_T)llpos->lnum);
5358 if (llpos->col > 0)
5359 {
5360 list_append_number(l, (varnumber_T)llpos->col);
5361 list_append_number(l, (varnumber_T)llpos->len);
5362 }
5363 sprintf(buf, "pos%d", i + 1);
5364 dict_add_list(dict, buf, l);
5365 }
5366 }
5367 else
5368 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005369 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005371 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5372 dict_add_number(dict, "priority", (long)cur->priority);
5373 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005374# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5375 if (cur->conceal_char)
5376 {
5377 char_u buf[MB_MAXBYTES + 1];
5378
5379 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005380 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005381 }
5382# endif
5383 list_append_dict(rettv->vval.v_list, dict);
5384 cur = cur->next;
5385 }
5386 }
5387#endif
5388}
5389
5390/*
5391 * "getpid()" function
5392 */
5393 static void
5394f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5395{
5396 rettv->vval.v_number = mch_get_pid();
5397}
5398
5399 static void
5400getpos_both(
5401 typval_T *argvars,
5402 typval_T *rettv,
5403 int getcurpos)
5404{
5405 pos_T *fp;
5406 list_T *l;
5407 int fnum = -1;
5408
5409 if (rettv_list_alloc(rettv) == OK)
5410 {
5411 l = rettv->vval.v_list;
5412 if (getcurpos)
5413 fp = &curwin->w_cursor;
5414 else
5415 fp = var2fpos(&argvars[0], TRUE, &fnum);
5416 if (fnum != -1)
5417 list_append_number(l, (varnumber_T)fnum);
5418 else
5419 list_append_number(l, (varnumber_T)0);
5420 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5421 : (varnumber_T)0);
5422 list_append_number(l, (fp != NULL)
5423 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5424 : (varnumber_T)0);
5425 list_append_number(l,
5426#ifdef FEAT_VIRTUALEDIT
5427 (fp != NULL) ? (varnumber_T)fp->coladd :
5428#endif
5429 (varnumber_T)0);
5430 if (getcurpos)
5431 {
5432 update_curswant();
5433 list_append_number(l, curwin->w_curswant == MAXCOL ?
5434 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5435 }
5436 }
5437 else
5438 rettv->vval.v_number = FALSE;
5439}
5440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441/*
5442 * "getcurpos()" function
5443 */
5444 static void
5445f_getcurpos(typval_T *argvars, typval_T *rettv)
5446{
5447 getpos_both(argvars, rettv, TRUE);
5448}
5449
5450/*
5451 * "getpos(string)" function
5452 */
5453 static void
5454f_getpos(typval_T *argvars, typval_T *rettv)
5455{
5456 getpos_both(argvars, rettv, FALSE);
5457}
5458
5459/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005460 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 */
5462 static void
5463f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5464{
5465#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005466 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467#endif
5468}
5469
5470/*
5471 * "getreg()" function
5472 */
5473 static void
5474f_getreg(typval_T *argvars, typval_T *rettv)
5475{
5476 char_u *strregname;
5477 int regname;
5478 int arg2 = FALSE;
5479 int return_list = FALSE;
5480 int error = FALSE;
5481
5482 if (argvars[0].v_type != VAR_UNKNOWN)
5483 {
5484 strregname = get_tv_string_chk(&argvars[0]);
5485 error = strregname == NULL;
5486 if (argvars[1].v_type != VAR_UNKNOWN)
5487 {
5488 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5489 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5490 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5491 }
5492 }
5493 else
5494 strregname = get_vim_var_str(VV_REG);
5495
5496 if (error)
5497 return;
5498
5499 regname = (strregname == NULL ? '"' : *strregname);
5500 if (regname == 0)
5501 regname = '"';
5502
5503 if (return_list)
5504 {
5505 rettv->v_type = VAR_LIST;
5506 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5507 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5508 if (rettv->vval.v_list == NULL)
5509 (void)rettv_list_alloc(rettv);
5510 else
5511 ++rettv->vval.v_list->lv_refcount;
5512 }
5513 else
5514 {
5515 rettv->v_type = VAR_STRING;
5516 rettv->vval.v_string = get_reg_contents(regname,
5517 arg2 ? GREG_EXPR_SRC : 0);
5518 }
5519}
5520
5521/*
5522 * "getregtype()" function
5523 */
5524 static void
5525f_getregtype(typval_T *argvars, typval_T *rettv)
5526{
5527 char_u *strregname;
5528 int regname;
5529 char_u buf[NUMBUFLEN + 2];
5530 long reglen = 0;
5531
5532 if (argvars[0].v_type != VAR_UNKNOWN)
5533 {
5534 strregname = get_tv_string_chk(&argvars[0]);
5535 if (strregname == NULL) /* type error; errmsg already given */
5536 {
5537 rettv->v_type = VAR_STRING;
5538 rettv->vval.v_string = NULL;
5539 return;
5540 }
5541 }
5542 else
5543 /* Default to v:register */
5544 strregname = get_vim_var_str(VV_REG);
5545
5546 regname = (strregname == NULL ? '"' : *strregname);
5547 if (regname == 0)
5548 regname = '"';
5549
5550 buf[0] = NUL;
5551 buf[1] = NUL;
5552 switch (get_reg_type(regname, &reglen))
5553 {
5554 case MLINE: buf[0] = 'V'; break;
5555 case MCHAR: buf[0] = 'v'; break;
5556 case MBLOCK:
5557 buf[0] = Ctrl_V;
5558 sprintf((char *)buf + 1, "%ld", reglen + 1);
5559 break;
5560 }
5561 rettv->v_type = VAR_STRING;
5562 rettv->vval.v_string = vim_strsave(buf);
5563}
5564
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005565/*
5566 * Returns information (variables, options, etc.) about a tab page
5567 * as a dictionary.
5568 */
5569 static dict_T *
5570get_tabpage_info(tabpage_T *tp, int tp_idx)
5571{
5572 win_T *wp;
5573 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005574 list_T *l;
5575
5576 dict = dict_alloc();
5577 if (dict == NULL)
5578 return NULL;
5579
Bram Moolenaare0be1672018-07-08 16:50:37 +02005580 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005581
5582 l = list_alloc();
5583 if (l != NULL)
5584 {
5585 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5586 wp; wp = wp->w_next)
5587 list_append_number(l, (varnumber_T)wp->w_id);
5588 dict_add_list(dict, "windows", l);
5589 }
5590
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005591 /* Make a reference to tabpage variables */
5592 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005593
5594 return dict;
5595}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005596
5597/*
5598 * "gettabinfo()" function
5599 */
5600 static void
5601f_gettabinfo(typval_T *argvars, typval_T *rettv)
5602{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005603 tabpage_T *tp, *tparg = NULL;
5604 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005605 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005606
5607 if (rettv_list_alloc(rettv) != OK)
5608 return;
5609
5610 if (argvars[0].v_type != VAR_UNKNOWN)
5611 {
5612 /* Information about one tab page */
5613 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5614 if (tparg == NULL)
5615 return;
5616 }
5617
5618 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005619 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005620 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005621 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622 if (tparg != NULL && tp != tparg)
5623 continue;
5624 d = get_tabpage_info(tp, tpnr);
5625 if (d != NULL)
5626 list_append_dict(rettv->vval.v_list, d);
5627 if (tparg != NULL)
5628 return;
5629 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005630}
5631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632/*
5633 * "gettabvar()" function
5634 */
5635 static void
5636f_gettabvar(typval_T *argvars, typval_T *rettv)
5637{
5638 win_T *oldcurwin;
5639 tabpage_T *tp, *oldtabpage;
5640 dictitem_T *v;
5641 char_u *varname;
5642 int done = FALSE;
5643
5644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = NULL;
5646
5647 varname = get_tv_string_chk(&argvars[1]);
5648 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5649 if (tp != NULL && varname != NULL)
5650 {
5651 /* Set tp to be our tabpage, temporarily. Also set the window to the
5652 * first window in the tabpage, otherwise the window is not valid. */
5653 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005654 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5655 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 {
5657 /* look up the variable */
5658 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5659 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5660 if (v != NULL)
5661 {
5662 copy_tv(&v->di_tv, rettv);
5663 done = TRUE;
5664 }
5665 }
5666
5667 /* restore previous notion of curwin */
5668 restore_win(oldcurwin, oldtabpage, TRUE);
5669 }
5670
5671 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5672 copy_tv(&argvars[2], rettv);
5673}
5674
5675/*
5676 * "gettabwinvar()" function
5677 */
5678 static void
5679f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5680{
5681 getwinvar(argvars, rettv, 1);
5682}
5683
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005684/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005685 * "gettagstack()" function
5686 */
5687 static void
5688f_gettagstack(typval_T *argvars, typval_T *rettv)
5689{
5690 win_T *wp = curwin; // default is current window
5691
5692 if (rettv_dict_alloc(rettv) != OK)
5693 return;
5694
5695 if (argvars[0].v_type != VAR_UNKNOWN)
5696 {
5697 wp = find_win_by_nr_or_id(&argvars[0]);
5698 if (wp == NULL)
5699 return;
5700 }
5701
5702 get_tagstack(wp, rettv->vval.v_dict);
5703}
5704
5705/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706 * Returns information about a window as a dictionary.
5707 */
5708 static dict_T *
5709get_win_info(win_T *wp, short tpnr, short winnr)
5710{
5711 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005712
5713 dict = dict_alloc();
5714 if (dict == NULL)
5715 return NULL;
5716
Bram Moolenaare0be1672018-07-08 16:50:37 +02005717 dict_add_number(dict, "tabnr", tpnr);
5718 dict_add_number(dict, "winnr", winnr);
5719 dict_add_number(dict, "winid", wp->w_id);
5720 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005721 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005722#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005723 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005724#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005725 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005726 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005727 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728
Bram Moolenaar69905d12017-08-13 18:14:47 +02005729#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005730 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005731#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005732#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005733 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5734 dict_add_number(dict, "loclist",
5735 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005736#endif
5737
Bram Moolenaar30567352016-08-27 21:25:44 +02005738 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005739 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005740
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005741 return dict;
5742}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005743
5744/*
5745 * "getwininfo()" function
5746 */
5747 static void
5748f_getwininfo(typval_T *argvars, typval_T *rettv)
5749{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005750 tabpage_T *tp;
5751 win_T *wp = NULL, *wparg = NULL;
5752 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005753 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005754
5755 if (rettv_list_alloc(rettv) != OK)
5756 return;
5757
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758 if (argvars[0].v_type != VAR_UNKNOWN)
5759 {
5760 wparg = win_id2wp(argvars);
5761 if (wparg == NULL)
5762 return;
5763 }
5764
5765 /* Collect information about either all the windows across all the tab
5766 * pages or one particular window.
5767 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005768 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005769 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005770 tabnr++;
5771 winnr = 0;
5772 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005773 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005774 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005775 if (wparg != NULL && wp != wparg)
5776 continue;
5777 d = get_win_info(wp, tabnr, winnr);
5778 if (d != NULL)
5779 list_append_dict(rettv->vval.v_list, d);
5780 if (wparg != NULL)
5781 /* found information about a specific window */
5782 return;
5783 }
5784 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005785}
5786
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005787/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 * "win_findbuf()" function
5789 */
5790 static void
5791f_win_findbuf(typval_T *argvars, typval_T *rettv)
5792{
5793 if (rettv_list_alloc(rettv) != FAIL)
5794 win_findbuf(argvars, rettv->vval.v_list);
5795}
5796
5797/*
5798 * "win_getid()" function
5799 */
5800 static void
5801f_win_getid(typval_T *argvars, typval_T *rettv)
5802{
5803 rettv->vval.v_number = win_getid(argvars);
5804}
5805
5806/*
5807 * "win_gotoid()" function
5808 */
5809 static void
5810f_win_gotoid(typval_T *argvars, typval_T *rettv)
5811{
5812 rettv->vval.v_number = win_gotoid(argvars);
5813}
5814
5815/*
5816 * "win_id2tabwin()" function
5817 */
5818 static void
5819f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5820{
5821 if (rettv_list_alloc(rettv) != FAIL)
5822 win_id2tabwin(argvars, rettv->vval.v_list);
5823}
5824
5825/*
5826 * "win_id2win()" function
5827 */
5828 static void
5829f_win_id2win(typval_T *argvars, typval_T *rettv)
5830{
5831 rettv->vval.v_number = win_id2win(argvars);
5832}
5833
5834/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005835 * "win_screenpos()" function
5836 */
5837 static void
5838f_win_screenpos(typval_T *argvars, typval_T *rettv)
5839{
5840 win_T *wp;
5841
5842 if (rettv_list_alloc(rettv) == FAIL)
5843 return;
5844
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005845 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005846 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5847 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5848}
5849
5850/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005851 * "getwinpos({timeout})" function
5852 */
5853 static void
5854f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5855{
5856 int x = -1;
5857 int y = -1;
5858
5859 if (rettv_list_alloc(rettv) == FAIL)
5860 return;
5861#ifdef FEAT_GUI
5862 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005863 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005864# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5865 else
5866# endif
5867#endif
5868#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5869 {
5870 varnumber_T timeout = 100;
5871
5872 if (argvars[0].v_type != VAR_UNKNOWN)
5873 timeout = get_tv_number(&argvars[0]);
5874 term_get_winpos(&x, &y, timeout);
5875 }
5876#endif
5877 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5878 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5879}
5880
5881
5882/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005883 * "getwinposx()" function
5884 */
5885 static void
5886f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5887{
5888 rettv->vval.v_number = -1;
5889#ifdef FEAT_GUI
5890 if (gui.in_use)
5891 {
5892 int x, y;
5893
5894 if (gui_mch_get_winpos(&x, &y) == OK)
5895 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005896 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005897 }
5898#endif
5899#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5900 {
5901 int x, y;
5902
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005903 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005904 rettv->vval.v_number = x;
5905 }
5906#endif
5907}
5908
5909/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005910 * "getwinposy()" function
5911 */
5912 static void
5913f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5914{
5915 rettv->vval.v_number = -1;
5916#ifdef FEAT_GUI
5917 if (gui.in_use)
5918 {
5919 int x, y;
5920
5921 if (gui_mch_get_winpos(&x, &y) == OK)
5922 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005923 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 }
5925#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005926#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5927 {
5928 int x, y;
5929
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005930 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005931 rettv->vval.v_number = y;
5932 }
5933#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934}
5935
5936/*
5937 * "getwinvar()" function
5938 */
5939 static void
5940f_getwinvar(typval_T *argvars, typval_T *rettv)
5941{
5942 getwinvar(argvars, rettv, 0);
5943}
5944
5945/*
5946 * "glob()" function
5947 */
5948 static void
5949f_glob(typval_T *argvars, typval_T *rettv)
5950{
5951 int options = WILD_SILENT|WILD_USE_NL;
5952 expand_T xpc;
5953 int error = FALSE;
5954
5955 /* When the optional second argument is non-zero, don't remove matches
5956 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5957 rettv->v_type = VAR_STRING;
5958 if (argvars[1].v_type != VAR_UNKNOWN)
5959 {
5960 if (get_tv_number_chk(&argvars[1], &error))
5961 options |= WILD_KEEP_ALL;
5962 if (argvars[2].v_type != VAR_UNKNOWN)
5963 {
5964 if (get_tv_number_chk(&argvars[2], &error))
5965 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005966 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 }
5968 if (argvars[3].v_type != VAR_UNKNOWN
5969 && get_tv_number_chk(&argvars[3], &error))
5970 options |= WILD_ALLLINKS;
5971 }
5972 }
5973 if (!error)
5974 {
5975 ExpandInit(&xpc);
5976 xpc.xp_context = EXPAND_FILES;
5977 if (p_wic)
5978 options += WILD_ICASE;
5979 if (rettv->v_type == VAR_STRING)
5980 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5981 NULL, options, WILD_ALL);
5982 else if (rettv_list_alloc(rettv) != FAIL)
5983 {
5984 int i;
5985
5986 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5987 NULL, options, WILD_ALL_KEEP);
5988 for (i = 0; i < xpc.xp_numfiles; i++)
5989 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5990
5991 ExpandCleanup(&xpc);
5992 }
5993 }
5994 else
5995 rettv->vval.v_string = NULL;
5996}
5997
5998/*
5999 * "globpath()" function
6000 */
6001 static void
6002f_globpath(typval_T *argvars, typval_T *rettv)
6003{
6004 int flags = 0;
6005 char_u buf1[NUMBUFLEN];
6006 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
6007 int error = FALSE;
6008 garray_T ga;
6009 int i;
6010
6011 /* When the optional second argument is non-zero, don't remove matches
6012 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6013 rettv->v_type = VAR_STRING;
6014 if (argvars[2].v_type != VAR_UNKNOWN)
6015 {
6016 if (get_tv_number_chk(&argvars[2], &error))
6017 flags |= WILD_KEEP_ALL;
6018 if (argvars[3].v_type != VAR_UNKNOWN)
6019 {
6020 if (get_tv_number_chk(&argvars[3], &error))
6021 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006022 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006023 }
6024 if (argvars[4].v_type != VAR_UNKNOWN
6025 && get_tv_number_chk(&argvars[4], &error))
6026 flags |= WILD_ALLLINKS;
6027 }
6028 }
6029 if (file != NULL && !error)
6030 {
6031 ga_init2(&ga, (int)sizeof(char_u *), 10);
6032 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
6033 if (rettv->v_type == VAR_STRING)
6034 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6035 else if (rettv_list_alloc(rettv) != FAIL)
6036 for (i = 0; i < ga.ga_len; ++i)
6037 list_append_string(rettv->vval.v_list,
6038 ((char_u **)(ga.ga_data))[i], -1);
6039 ga_clear_strings(&ga);
6040 }
6041 else
6042 rettv->vval.v_string = NULL;
6043}
6044
6045/*
6046 * "glob2regpat()" function
6047 */
6048 static void
6049f_glob2regpat(typval_T *argvars, typval_T *rettv)
6050{
6051 char_u *pat = get_tv_string_chk(&argvars[0]);
6052
6053 rettv->v_type = VAR_STRING;
6054 rettv->vval.v_string = (pat == NULL)
6055 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6056}
6057
6058/* for VIM_VERSION_ defines */
6059#include "version.h"
6060
6061/*
6062 * "has()" function
6063 */
6064 static void
6065f_has(typval_T *argvars, typval_T *rettv)
6066{
6067 int i;
6068 char_u *name;
6069 int n = FALSE;
6070 static char *(has_list[]) =
6071 {
6072#ifdef AMIGA
6073 "amiga",
6074# ifdef FEAT_ARP
6075 "arp",
6076# endif
6077#endif
6078#ifdef __BEOS__
6079 "beos",
6080#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006081#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006082 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6083 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006084# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006085 "macunix", /* Mac OS X, with the darwin feature */
6086 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006087# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088#endif
6089#ifdef __QNX__
6090 "qnx",
6091#endif
6092#ifdef UNIX
6093 "unix",
6094#endif
6095#ifdef VMS
6096 "vms",
6097#endif
6098#ifdef WIN32
6099 "win32",
6100#endif
6101#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6102 "win32unix",
6103#endif
6104#if defined(WIN64) || defined(_WIN64)
6105 "win64",
6106#endif
6107#ifdef EBCDIC
6108 "ebcdic",
6109#endif
6110#ifndef CASE_INSENSITIVE_FILENAME
6111 "fname_case",
6112#endif
6113#ifdef HAVE_ACL
6114 "acl",
6115#endif
6116#ifdef FEAT_ARABIC
6117 "arabic",
6118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006120#ifdef FEAT_AUTOCHDIR
6121 "autochdir",
6122#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006123#ifdef FEAT_AUTOSERVERNAME
6124 "autoservername",
6125#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006126#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 "balloon_eval",
6128# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6129 "balloon_multiline",
6130# endif
6131#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006132#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006133 "balloon_eval_term",
6134#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6136 "builtin_terms",
6137# ifdef ALL_BUILTIN_TCAPS
6138 "all_builtin_terms",
6139# endif
6140#endif
6141#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6142 || defined(FEAT_GUI_W32) \
6143 || defined(FEAT_GUI_MOTIF))
6144 "browsefilter",
6145#endif
6146#ifdef FEAT_BYTEOFF
6147 "byte_offset",
6148#endif
6149#ifdef FEAT_JOB_CHANNEL
6150 "channel",
6151#endif
6152#ifdef FEAT_CINDENT
6153 "cindent",
6154#endif
6155#ifdef FEAT_CLIENTSERVER
6156 "clientserver",
6157#endif
6158#ifdef FEAT_CLIPBOARD
6159 "clipboard",
6160#endif
6161#ifdef FEAT_CMDL_COMPL
6162 "cmdline_compl",
6163#endif
6164#ifdef FEAT_CMDHIST
6165 "cmdline_hist",
6166#endif
6167#ifdef FEAT_COMMENTS
6168 "comments",
6169#endif
6170#ifdef FEAT_CONCEAL
6171 "conceal",
6172#endif
6173#ifdef FEAT_CRYPT
6174 "cryptv",
6175 "crypt-blowfish",
6176 "crypt-blowfish2",
6177#endif
6178#ifdef FEAT_CSCOPE
6179 "cscope",
6180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182#ifdef CURSOR_SHAPE
6183 "cursorshape",
6184#endif
6185#ifdef DEBUG
6186 "debug",
6187#endif
6188#ifdef FEAT_CON_DIALOG
6189 "dialog_con",
6190#endif
6191#ifdef FEAT_GUI_DIALOG
6192 "dialog_gui",
6193#endif
6194#ifdef FEAT_DIFF
6195 "diff",
6196#endif
6197#ifdef FEAT_DIGRAPHS
6198 "digraphs",
6199#endif
6200#ifdef FEAT_DIRECTX
6201 "directx",
6202#endif
6203#ifdef FEAT_DND
6204 "dnd",
6205#endif
6206#ifdef FEAT_EMACS_TAGS
6207 "emacs_tags",
6208#endif
6209 "eval", /* always present, of course! */
6210 "ex_extra", /* graduated feature */
6211#ifdef FEAT_SEARCH_EXTRA
6212 "extra_search",
6213#endif
6214#ifdef FEAT_FKMAP
6215 "farsi",
6216#endif
6217#ifdef FEAT_SEARCHPATH
6218 "file_in_path",
6219#endif
6220#ifdef FEAT_FILTERPIPE
6221 "filterpipe",
6222#endif
6223#ifdef FEAT_FIND_ID
6224 "find_in_path",
6225#endif
6226#ifdef FEAT_FLOAT
6227 "float",
6228#endif
6229#ifdef FEAT_FOLDING
6230 "folding",
6231#endif
6232#ifdef FEAT_FOOTER
6233 "footer",
6234#endif
6235#if !defined(USE_SYSTEM) && defined(UNIX)
6236 "fork",
6237#endif
6238#ifdef FEAT_GETTEXT
6239 "gettext",
6240#endif
6241#ifdef FEAT_GUI
6242 "gui",
6243#endif
6244#ifdef FEAT_GUI_ATHENA
6245# ifdef FEAT_GUI_NEXTAW
6246 "gui_neXtaw",
6247# else
6248 "gui_athena",
6249# endif
6250#endif
6251#ifdef FEAT_GUI_GTK
6252 "gui_gtk",
6253# ifdef USE_GTK3
6254 "gui_gtk3",
6255# else
6256 "gui_gtk2",
6257# endif
6258#endif
6259#ifdef FEAT_GUI_GNOME
6260 "gui_gnome",
6261#endif
6262#ifdef FEAT_GUI_MAC
6263 "gui_mac",
6264#endif
6265#ifdef FEAT_GUI_MOTIF
6266 "gui_motif",
6267#endif
6268#ifdef FEAT_GUI_PHOTON
6269 "gui_photon",
6270#endif
6271#ifdef FEAT_GUI_W32
6272 "gui_win32",
6273#endif
6274#ifdef FEAT_HANGULIN
6275 "hangul_input",
6276#endif
6277#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6278 "iconv",
6279#endif
6280#ifdef FEAT_INS_EXPAND
6281 "insert_expand",
6282#endif
6283#ifdef FEAT_JOB_CHANNEL
6284 "job",
6285#endif
6286#ifdef FEAT_JUMPLIST
6287 "jumplist",
6288#endif
6289#ifdef FEAT_KEYMAP
6290 "keymap",
6291#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006292 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293#ifdef FEAT_LANGMAP
6294 "langmap",
6295#endif
6296#ifdef FEAT_LIBCALL
6297 "libcall",
6298#endif
6299#ifdef FEAT_LINEBREAK
6300 "linebreak",
6301#endif
6302#ifdef FEAT_LISP
6303 "lispindent",
6304#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306#ifdef FEAT_LOCALMAP
6307 "localmap",
6308#endif
6309#ifdef FEAT_LUA
6310# ifndef DYNAMIC_LUA
6311 "lua",
6312# endif
6313#endif
6314#ifdef FEAT_MENU
6315 "menu",
6316#endif
6317#ifdef FEAT_SESSION
6318 "mksession",
6319#endif
6320#ifdef FEAT_MODIFY_FNAME
6321 "modify_fname",
6322#endif
6323#ifdef FEAT_MOUSE
6324 "mouse",
6325#endif
6326#ifdef FEAT_MOUSESHAPE
6327 "mouseshape",
6328#endif
6329#if defined(UNIX) || defined(VMS)
6330# ifdef FEAT_MOUSE_DEC
6331 "mouse_dec",
6332# endif
6333# ifdef FEAT_MOUSE_GPM
6334 "mouse_gpm",
6335# endif
6336# ifdef FEAT_MOUSE_JSB
6337 "mouse_jsbterm",
6338# endif
6339# ifdef FEAT_MOUSE_NET
6340 "mouse_netterm",
6341# endif
6342# ifdef FEAT_MOUSE_PTERM
6343 "mouse_pterm",
6344# endif
6345# ifdef FEAT_MOUSE_SGR
6346 "mouse_sgr",
6347# endif
6348# ifdef FEAT_SYSMOUSE
6349 "mouse_sysmouse",
6350# endif
6351# ifdef FEAT_MOUSE_URXVT
6352 "mouse_urxvt",
6353# endif
6354# ifdef FEAT_MOUSE_XTERM
6355 "mouse_xterm",
6356# endif
6357#endif
6358#ifdef FEAT_MBYTE
6359 "multi_byte",
6360#endif
6361#ifdef FEAT_MBYTE_IME
6362 "multi_byte_ime",
6363#endif
6364#ifdef FEAT_MULTI_LANG
6365 "multi_lang",
6366#endif
6367#ifdef FEAT_MZSCHEME
6368#ifndef DYNAMIC_MZSCHEME
6369 "mzscheme",
6370#endif
6371#endif
6372#ifdef FEAT_NUM64
6373 "num64",
6374#endif
6375#ifdef FEAT_OLE
6376 "ole",
6377#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006378#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006380#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006381#ifdef FEAT_PATH_EXTRA
6382 "path_extra",
6383#endif
6384#ifdef FEAT_PERL
6385#ifndef DYNAMIC_PERL
6386 "perl",
6387#endif
6388#endif
6389#ifdef FEAT_PERSISTENT_UNDO
6390 "persistent_undo",
6391#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006392#if defined(FEAT_PYTHON)
6393 "python_compiled",
6394# if defined(DYNAMIC_PYTHON)
6395 "python_dynamic",
6396# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006398 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006399# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006401#if defined(FEAT_PYTHON3)
6402 "python3_compiled",
6403# if defined(DYNAMIC_PYTHON3)
6404 "python3_dynamic",
6405# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006407 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006408# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409#endif
6410#ifdef FEAT_POSTSCRIPT
6411 "postscript",
6412#endif
6413#ifdef FEAT_PRINTER
6414 "printer",
6415#endif
6416#ifdef FEAT_PROFILE
6417 "profile",
6418#endif
6419#ifdef FEAT_RELTIME
6420 "reltime",
6421#endif
6422#ifdef FEAT_QUICKFIX
6423 "quickfix",
6424#endif
6425#ifdef FEAT_RIGHTLEFT
6426 "rightleft",
6427#endif
6428#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6429 "ruby",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432#ifdef FEAT_CMDL_INFO
6433 "showcmd",
6434 "cmdline_info",
6435#endif
6436#ifdef FEAT_SIGNS
6437 "signs",
6438#endif
6439#ifdef FEAT_SMARTINDENT
6440 "smartindent",
6441#endif
6442#ifdef STARTUPTIME
6443 "startuptime",
6444#endif
6445#ifdef FEAT_STL_OPT
6446 "statusline",
6447#endif
6448#ifdef FEAT_SUN_WORKSHOP
6449 "sun_workshop",
6450#endif
6451#ifdef FEAT_NETBEANS_INTG
6452 "netbeans_intg",
6453#endif
6454#ifdef FEAT_SPELL
6455 "spell",
6456#endif
6457#ifdef FEAT_SYN_HL
6458 "syntax",
6459#endif
6460#if defined(USE_SYSTEM) || !defined(UNIX)
6461 "system",
6462#endif
6463#ifdef FEAT_TAG_BINS
6464 "tag_binary",
6465#endif
6466#ifdef FEAT_TAG_OLDSTATIC
6467 "tag_old_static",
6468#endif
6469#ifdef FEAT_TAG_ANYWHITE
6470 "tag_any_white",
6471#endif
6472#ifdef FEAT_TCL
6473# ifndef DYNAMIC_TCL
6474 "tcl",
6475# endif
6476#endif
6477#ifdef FEAT_TERMGUICOLORS
6478 "termguicolors",
6479#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006480#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006481 "terminal",
6482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483#ifdef TERMINFO
6484 "terminfo",
6485#endif
6486#ifdef FEAT_TERMRESPONSE
6487 "termresponse",
6488#endif
6489#ifdef FEAT_TEXTOBJ
6490 "textobjects",
6491#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006492#ifdef FEAT_TEXT_PROP
6493 "textprop",
6494#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495#ifdef HAVE_TGETENT
6496 "tgetent",
6497#endif
6498#ifdef FEAT_TIMERS
6499 "timers",
6500#endif
6501#ifdef FEAT_TITLE
6502 "title",
6503#endif
6504#ifdef FEAT_TOOLBAR
6505 "toolbar",
6506#endif
6507#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6508 "unnamedplus",
6509#endif
6510#ifdef FEAT_USR_CMDS
6511 "user-commands", /* was accidentally included in 5.4 */
6512 "user_commands",
6513#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006514#ifdef FEAT_VARTABS
6515 "vartabs",
6516#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef FEAT_VIMINFO
6518 "viminfo",
6519#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef FEAT_VIRTUALEDIT
6522 "virtualedit",
6523#endif
6524 "visual",
6525#ifdef FEAT_VISUALEXTRA
6526 "visualextra",
6527#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006529#ifdef FEAT_VTP
6530 "vtp",
6531#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532#ifdef FEAT_WILDIGN
6533 "wildignore",
6534#endif
6535#ifdef FEAT_WILDMENU
6536 "wildmenu",
6537#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef FEAT_WAK
6540 "winaltkeys",
6541#endif
6542#ifdef FEAT_WRITEBACKUP
6543 "writebackup",
6544#endif
6545#ifdef FEAT_XIM
6546 "xim",
6547#endif
6548#ifdef FEAT_XFONTSET
6549 "xfontset",
6550#endif
6551#ifdef FEAT_XPM_W32
6552 "xpm",
6553 "xpm_w32", /* for backward compatibility */
6554#else
6555# if defined(HAVE_XPM)
6556 "xpm",
6557# endif
6558#endif
6559#ifdef USE_XSMP
6560 "xsmp",
6561#endif
6562#ifdef USE_XSMP_INTERACT
6563 "xsmp_interact",
6564#endif
6565#ifdef FEAT_XCLIPBOARD
6566 "xterm_clipboard",
6567#endif
6568#ifdef FEAT_XTERM_SAVE
6569 "xterm_save",
6570#endif
6571#if defined(UNIX) && defined(FEAT_X11)
6572 "X11",
6573#endif
6574 NULL
6575 };
6576
6577 name = get_tv_string(&argvars[0]);
6578 for (i = 0; has_list[i] != NULL; ++i)
6579 if (STRICMP(name, has_list[i]) == 0)
6580 {
6581 n = TRUE;
6582 break;
6583 }
6584
6585 if (n == FALSE)
6586 {
6587 if (STRNICMP(name, "patch", 5) == 0)
6588 {
6589 if (name[5] == '-'
6590 && STRLEN(name) >= 11
6591 && vim_isdigit(name[6])
6592 && vim_isdigit(name[8])
6593 && vim_isdigit(name[10]))
6594 {
6595 int major = atoi((char *)name + 6);
6596 int minor = atoi((char *)name + 8);
6597
6598 /* Expect "patch-9.9.01234". */
6599 n = (major < VIM_VERSION_MAJOR
6600 || (major == VIM_VERSION_MAJOR
6601 && (minor < VIM_VERSION_MINOR
6602 || (minor == VIM_VERSION_MINOR
6603 && has_patch(atoi((char *)name + 10))))));
6604 }
6605 else
6606 n = has_patch(atoi((char *)name + 5));
6607 }
6608 else if (STRICMP(name, "vim_starting") == 0)
6609 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006610 else if (STRICMP(name, "ttyin") == 0)
6611 n = mch_input_isatty();
6612 else if (STRICMP(name, "ttyout") == 0)
6613 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614#ifdef FEAT_MBYTE
6615 else if (STRICMP(name, "multi_byte_encoding") == 0)
6616 n = has_mbyte;
6617#endif
6618#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6619 else if (STRICMP(name, "balloon_multiline") == 0)
6620 n = multiline_balloon_available();
6621#endif
6622#ifdef DYNAMIC_TCL
6623 else if (STRICMP(name, "tcl") == 0)
6624 n = tcl_enabled(FALSE);
6625#endif
6626#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6627 else if (STRICMP(name, "iconv") == 0)
6628 n = iconv_enabled(FALSE);
6629#endif
6630#ifdef DYNAMIC_LUA
6631 else if (STRICMP(name, "lua") == 0)
6632 n = lua_enabled(FALSE);
6633#endif
6634#ifdef DYNAMIC_MZSCHEME
6635 else if (STRICMP(name, "mzscheme") == 0)
6636 n = mzscheme_enabled(FALSE);
6637#endif
6638#ifdef DYNAMIC_RUBY
6639 else if (STRICMP(name, "ruby") == 0)
6640 n = ruby_enabled(FALSE);
6641#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#ifdef DYNAMIC_PYTHON
6643 else if (STRICMP(name, "python") == 0)
6644 n = python_enabled(FALSE);
6645#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646#ifdef DYNAMIC_PYTHON3
6647 else if (STRICMP(name, "python3") == 0)
6648 n = python3_enabled(FALSE);
6649#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006650#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6651 else if (STRICMP(name, "pythonx") == 0)
6652 {
6653# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6654 if (p_pyx == 0)
6655 n = python3_enabled(FALSE) || python_enabled(FALSE);
6656 else if (p_pyx == 3)
6657 n = python3_enabled(FALSE);
6658 else if (p_pyx == 2)
6659 n = python_enabled(FALSE);
6660# elif defined(DYNAMIC_PYTHON)
6661 n = python_enabled(FALSE);
6662# elif defined(DYNAMIC_PYTHON3)
6663 n = python3_enabled(FALSE);
6664# endif
6665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666#endif
6667#ifdef DYNAMIC_PERL
6668 else if (STRICMP(name, "perl") == 0)
6669 n = perl_enabled(FALSE);
6670#endif
6671#ifdef FEAT_GUI
6672 else if (STRICMP(name, "gui_running") == 0)
6673 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674# ifdef FEAT_BROWSE
6675 else if (STRICMP(name, "browse") == 0)
6676 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6677# endif
6678#endif
6679#ifdef FEAT_SYN_HL
6680 else if (STRICMP(name, "syntax_items") == 0)
6681 n = syntax_present(curwin);
6682#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006683#ifdef FEAT_VTP
6684 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006685 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686#endif
6687#ifdef FEAT_NETBEANS_INTG
6688 else if (STRICMP(name, "netbeans_enabled") == 0)
6689 n = netbeans_active();
6690#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006691#if defined(FEAT_TERMINAL) && defined(WIN3264)
6692 else if (STRICMP(name, "terminal") == 0)
6693 n = terminal_enabled();
6694#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 }
6696
6697 rettv->vval.v_number = n;
6698}
6699
6700/*
6701 * "has_key()" function
6702 */
6703 static void
6704f_has_key(typval_T *argvars, typval_T *rettv)
6705{
6706 if (argvars[0].v_type != VAR_DICT)
6707 {
6708 EMSG(_(e_dictreq));
6709 return;
6710 }
6711 if (argvars[0].vval.v_dict == NULL)
6712 return;
6713
6714 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6715 get_tv_string(&argvars[1]), -1) != NULL;
6716}
6717
6718/*
6719 * "haslocaldir()" function
6720 */
6721 static void
6722f_haslocaldir(typval_T *argvars, typval_T *rettv)
6723{
6724 win_T *wp = NULL;
6725
6726 wp = find_tabwin(&argvars[0], &argvars[1]);
6727 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6728}
6729
6730/*
6731 * "hasmapto()" function
6732 */
6733 static void
6734f_hasmapto(typval_T *argvars, typval_T *rettv)
6735{
6736 char_u *name;
6737 char_u *mode;
6738 char_u buf[NUMBUFLEN];
6739 int abbr = FALSE;
6740
6741 name = get_tv_string(&argvars[0]);
6742 if (argvars[1].v_type == VAR_UNKNOWN)
6743 mode = (char_u *)"nvo";
6744 else
6745 {
6746 mode = get_tv_string_buf(&argvars[1], buf);
6747 if (argvars[2].v_type != VAR_UNKNOWN)
6748 abbr = (int)get_tv_number(&argvars[2]);
6749 }
6750
6751 if (map_to_exists(name, mode, abbr))
6752 rettv->vval.v_number = TRUE;
6753 else
6754 rettv->vval.v_number = FALSE;
6755}
6756
6757/*
6758 * "histadd()" function
6759 */
6760 static void
6761f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6762{
6763#ifdef FEAT_CMDHIST
6764 int histype;
6765 char_u *str;
6766 char_u buf[NUMBUFLEN];
6767#endif
6768
6769 rettv->vval.v_number = FALSE;
6770 if (check_restricted() || check_secure())
6771 return;
6772#ifdef FEAT_CMDHIST
6773 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6774 histype = str != NULL ? get_histtype(str) : -1;
6775 if (histype >= 0)
6776 {
6777 str = get_tv_string_buf(&argvars[1], buf);
6778 if (*str != NUL)
6779 {
6780 init_history();
6781 add_to_history(histype, str, FALSE, NUL);
6782 rettv->vval.v_number = TRUE;
6783 return;
6784 }
6785 }
6786#endif
6787}
6788
6789/*
6790 * "histdel()" function
6791 */
6792 static void
6793f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6794{
6795#ifdef FEAT_CMDHIST
6796 int n;
6797 char_u buf[NUMBUFLEN];
6798 char_u *str;
6799
6800 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6801 if (str == NULL)
6802 n = 0;
6803 else if (argvars[1].v_type == VAR_UNKNOWN)
6804 /* only one argument: clear entire history */
6805 n = clr_history(get_histtype(str));
6806 else if (argvars[1].v_type == VAR_NUMBER)
6807 /* index given: remove that entry */
6808 n = del_history_idx(get_histtype(str),
6809 (int)get_tv_number(&argvars[1]));
6810 else
6811 /* string given: remove all matching entries */
6812 n = del_history_entry(get_histtype(str),
6813 get_tv_string_buf(&argvars[1], buf));
6814 rettv->vval.v_number = n;
6815#endif
6816}
6817
6818/*
6819 * "histget()" function
6820 */
6821 static void
6822f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6823{
6824#ifdef FEAT_CMDHIST
6825 int type;
6826 int idx;
6827 char_u *str;
6828
6829 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6830 if (str == NULL)
6831 rettv->vval.v_string = NULL;
6832 else
6833 {
6834 type = get_histtype(str);
6835 if (argvars[1].v_type == VAR_UNKNOWN)
6836 idx = get_history_idx(type);
6837 else
6838 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6839 /* -1 on type error */
6840 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6841 }
6842#else
6843 rettv->vval.v_string = NULL;
6844#endif
6845 rettv->v_type = VAR_STRING;
6846}
6847
6848/*
6849 * "histnr()" function
6850 */
6851 static void
6852f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6853{
6854 int i;
6855
6856#ifdef FEAT_CMDHIST
6857 char_u *history = get_tv_string_chk(&argvars[0]);
6858
6859 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6860 if (i >= HIST_CMD && i < HIST_COUNT)
6861 i = get_history_idx(i);
6862 else
6863#endif
6864 i = -1;
6865 rettv->vval.v_number = i;
6866}
6867
6868/*
6869 * "highlightID(name)" function
6870 */
6871 static void
6872f_hlID(typval_T *argvars, typval_T *rettv)
6873{
6874 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6875}
6876
6877/*
6878 * "highlight_exists()" function
6879 */
6880 static void
6881f_hlexists(typval_T *argvars, typval_T *rettv)
6882{
6883 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6884}
6885
6886/*
6887 * "hostname()" function
6888 */
6889 static void
6890f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6891{
6892 char_u hostname[256];
6893
6894 mch_get_host_name(hostname, 256);
6895 rettv->v_type = VAR_STRING;
6896 rettv->vval.v_string = vim_strsave(hostname);
6897}
6898
6899/*
6900 * iconv() function
6901 */
6902 static void
6903f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6904{
6905#ifdef FEAT_MBYTE
6906 char_u buf1[NUMBUFLEN];
6907 char_u buf2[NUMBUFLEN];
6908 char_u *from, *to, *str;
6909 vimconv_T vimconv;
6910#endif
6911
6912 rettv->v_type = VAR_STRING;
6913 rettv->vval.v_string = NULL;
6914
6915#ifdef FEAT_MBYTE
6916 str = get_tv_string(&argvars[0]);
6917 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6918 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6919 vimconv.vc_type = CONV_NONE;
6920 convert_setup(&vimconv, from, to);
6921
6922 /* If the encodings are equal, no conversion needed. */
6923 if (vimconv.vc_type == CONV_NONE)
6924 rettv->vval.v_string = vim_strsave(str);
6925 else
6926 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6927
6928 convert_setup(&vimconv, NULL, NULL);
6929 vim_free(from);
6930 vim_free(to);
6931#endif
6932}
6933
6934/*
6935 * "indent()" function
6936 */
6937 static void
6938f_indent(typval_T *argvars, typval_T *rettv)
6939{
6940 linenr_T lnum;
6941
6942 lnum = get_tv_lnum(argvars);
6943 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6944 rettv->vval.v_number = get_indent_lnum(lnum);
6945 else
6946 rettv->vval.v_number = -1;
6947}
6948
6949/*
6950 * "index()" function
6951 */
6952 static void
6953f_index(typval_T *argvars, typval_T *rettv)
6954{
6955 list_T *l;
6956 listitem_T *item;
6957 long idx = 0;
6958 int ic = FALSE;
6959
6960 rettv->vval.v_number = -1;
6961 if (argvars[0].v_type != VAR_LIST)
6962 {
6963 EMSG(_(e_listreq));
6964 return;
6965 }
6966 l = argvars[0].vval.v_list;
6967 if (l != NULL)
6968 {
6969 item = l->lv_first;
6970 if (argvars[2].v_type != VAR_UNKNOWN)
6971 {
6972 int error = FALSE;
6973
6974 /* Start at specified item. Use the cached index that list_find()
6975 * sets, so that a negative number also works. */
6976 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6977 idx = l->lv_idx;
6978 if (argvars[3].v_type != VAR_UNKNOWN)
6979 ic = (int)get_tv_number_chk(&argvars[3], &error);
6980 if (error)
6981 item = NULL;
6982 }
6983
6984 for ( ; item != NULL; item = item->li_next, ++idx)
6985 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6986 {
6987 rettv->vval.v_number = idx;
6988 break;
6989 }
6990 }
6991}
6992
6993static int inputsecret_flag = 0;
6994
6995/*
6996 * "input()" function
6997 * Also handles inputsecret() when inputsecret is set.
6998 */
6999 static void
7000f_input(typval_T *argvars, typval_T *rettv)
7001{
7002 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7003}
7004
7005/*
7006 * "inputdialog()" function
7007 */
7008 static void
7009f_inputdialog(typval_T *argvars, typval_T *rettv)
7010{
7011#if defined(FEAT_GUI_TEXTDIALOG)
7012 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7013 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7014 {
7015 char_u *message;
7016 char_u buf[NUMBUFLEN];
7017 char_u *defstr = (char_u *)"";
7018
7019 message = get_tv_string_chk(&argvars[0]);
7020 if (argvars[1].v_type != VAR_UNKNOWN
7021 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
7022 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7023 else
7024 IObuff[0] = NUL;
7025 if (message != NULL && defstr != NULL
7026 && do_dialog(VIM_QUESTION, NULL, message,
7027 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7028 rettv->vval.v_string = vim_strsave(IObuff);
7029 else
7030 {
7031 if (message != NULL && defstr != NULL
7032 && argvars[1].v_type != VAR_UNKNOWN
7033 && argvars[2].v_type != VAR_UNKNOWN)
7034 rettv->vval.v_string = vim_strsave(
7035 get_tv_string_buf(&argvars[2], buf));
7036 else
7037 rettv->vval.v_string = NULL;
7038 }
7039 rettv->v_type = VAR_STRING;
7040 }
7041 else
7042#endif
7043 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7044}
7045
7046/*
7047 * "inputlist()" function
7048 */
7049 static void
7050f_inputlist(typval_T *argvars, typval_T *rettv)
7051{
7052 listitem_T *li;
7053 int selected;
7054 int mouse_used;
7055
7056#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007057 /* While starting up, there is no place to enter text. When running tests
7058 * with --not-a-term we assume feedkeys() will be used. */
7059 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 return;
7061#endif
7062 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7063 {
7064 EMSG2(_(e_listarg), "inputlist()");
7065 return;
7066 }
7067
7068 msg_start();
7069 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7070 lines_left = Rows; /* avoid more prompt */
7071 msg_scroll = TRUE;
7072 msg_clr_eos();
7073
7074 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7075 {
7076 msg_puts(get_tv_string(&li->li_tv));
7077 msg_putchar('\n');
7078 }
7079
7080 /* Ask for choice. */
7081 selected = prompt_for_number(&mouse_used);
7082 if (mouse_used)
7083 selected -= lines_left;
7084
7085 rettv->vval.v_number = selected;
7086}
7087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7089
7090/*
7091 * "inputrestore()" function
7092 */
7093 static void
7094f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7095{
7096 if (ga_userinput.ga_len > 0)
7097 {
7098 --ga_userinput.ga_len;
7099 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7100 + ga_userinput.ga_len);
7101 /* default return is zero == OK */
7102 }
7103 else if (p_verbose > 1)
7104 {
7105 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7106 rettv->vval.v_number = 1; /* Failed */
7107 }
7108}
7109
7110/*
7111 * "inputsave()" function
7112 */
7113 static void
7114f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7115{
7116 /* Add an entry to the stack of typeahead storage. */
7117 if (ga_grow(&ga_userinput, 1) == OK)
7118 {
7119 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7120 + ga_userinput.ga_len);
7121 ++ga_userinput.ga_len;
7122 /* default return is zero == OK */
7123 }
7124 else
7125 rettv->vval.v_number = 1; /* Failed */
7126}
7127
7128/*
7129 * "inputsecret()" function
7130 */
7131 static void
7132f_inputsecret(typval_T *argvars, typval_T *rettv)
7133{
7134 ++cmdline_star;
7135 ++inputsecret_flag;
7136 f_input(argvars, rettv);
7137 --cmdline_star;
7138 --inputsecret_flag;
7139}
7140
7141/*
7142 * "insert()" function
7143 */
7144 static void
7145f_insert(typval_T *argvars, typval_T *rettv)
7146{
7147 long before = 0;
7148 listitem_T *item;
7149 list_T *l;
7150 int error = FALSE;
7151
7152 if (argvars[0].v_type != VAR_LIST)
7153 EMSG2(_(e_listarg), "insert()");
7154 else if ((l = argvars[0].vval.v_list) != NULL
7155 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7156 {
7157 if (argvars[2].v_type != VAR_UNKNOWN)
7158 before = (long)get_tv_number_chk(&argvars[2], &error);
7159 if (error)
7160 return; /* type error; errmsg already given */
7161
7162 if (before == l->lv_len)
7163 item = NULL;
7164 else
7165 {
7166 item = list_find(l, before);
7167 if (item == NULL)
7168 {
7169 EMSGN(_(e_listidx), before);
7170 l = NULL;
7171 }
7172 }
7173 if (l != NULL)
7174 {
7175 list_insert_tv(l, &argvars[1], item);
7176 copy_tv(&argvars[0], rettv);
7177 }
7178 }
7179}
7180
7181/*
7182 * "invert(expr)" function
7183 */
7184 static void
7185f_invert(typval_T *argvars, typval_T *rettv)
7186{
7187 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7188}
7189
7190/*
7191 * "isdirectory()" function
7192 */
7193 static void
7194f_isdirectory(typval_T *argvars, typval_T *rettv)
7195{
7196 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7197}
7198
7199/*
7200 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7201 * or it refers to a List or Dictionary that is locked.
7202 */
7203 static int
7204tv_islocked(typval_T *tv)
7205{
7206 return (tv->v_lock & VAR_LOCKED)
7207 || (tv->v_type == VAR_LIST
7208 && tv->vval.v_list != NULL
7209 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7210 || (tv->v_type == VAR_DICT
7211 && tv->vval.v_dict != NULL
7212 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7213}
7214
7215/*
7216 * "islocked()" function
7217 */
7218 static void
7219f_islocked(typval_T *argvars, typval_T *rettv)
7220{
7221 lval_T lv;
7222 char_u *end;
7223 dictitem_T *di;
7224
7225 rettv->vval.v_number = -1;
7226 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007227 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 if (end != NULL && lv.ll_name != NULL)
7229 {
7230 if (*end != NUL)
7231 EMSG(_(e_trailing));
7232 else
7233 {
7234 if (lv.ll_tv == NULL)
7235 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007236 di = find_var(lv.ll_name, NULL, TRUE);
7237 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007239 /* Consider a variable locked when:
7240 * 1. the variable itself is locked
7241 * 2. the value of the variable is locked.
7242 * 3. the List or Dict value is locked.
7243 */
7244 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7245 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 }
7247 }
7248 else if (lv.ll_range)
7249 EMSG(_("E786: Range not allowed"));
7250 else if (lv.ll_newkey != NULL)
7251 EMSG2(_(e_dictkey), lv.ll_newkey);
7252 else if (lv.ll_list != NULL)
7253 /* List item. */
7254 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7255 else
7256 /* Dictionary item. */
7257 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7258 }
7259 }
7260
7261 clear_lval(&lv);
7262}
7263
7264#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7265/*
7266 * "isnan()" function
7267 */
7268 static void
7269f_isnan(typval_T *argvars, typval_T *rettv)
7270{
7271 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7272 && isnan(argvars[0].vval.v_float);
7273}
7274#endif
7275
7276/*
7277 * "items(dict)" function
7278 */
7279 static void
7280f_items(typval_T *argvars, typval_T *rettv)
7281{
7282 dict_list(argvars, rettv, 2);
7283}
7284
7285#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7286/*
7287 * Get the job from the argument.
7288 * Returns NULL if the job is invalid.
7289 */
7290 static job_T *
7291get_job_arg(typval_T *tv)
7292{
7293 job_T *job;
7294
7295 if (tv->v_type != VAR_JOB)
7296 {
7297 EMSG2(_(e_invarg2), get_tv_string(tv));
7298 return NULL;
7299 }
7300 job = tv->vval.v_job;
7301
7302 if (job == NULL)
7303 EMSG(_("E916: not a valid job"));
7304 return job;
7305}
7306
7307/*
7308 * "job_getchannel()" function
7309 */
7310 static void
7311f_job_getchannel(typval_T *argvars, typval_T *rettv)
7312{
7313 job_T *job = get_job_arg(&argvars[0]);
7314
7315 if (job != NULL)
7316 {
7317 rettv->v_type = VAR_CHANNEL;
7318 rettv->vval.v_channel = job->jv_channel;
7319 if (job->jv_channel != NULL)
7320 ++job->jv_channel->ch_refcount;
7321 }
7322}
7323
7324/*
7325 * "job_info()" function
7326 */
7327 static void
7328f_job_info(typval_T *argvars, typval_T *rettv)
7329{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007330 if (argvars[0].v_type != VAR_UNKNOWN)
7331 {
7332 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007334 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7335 job_info(job, rettv->vval.v_dict);
7336 }
7337 else if (rettv_list_alloc(rettv) == OK)
7338 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339}
7340
7341/*
7342 * "job_setoptions()" function
7343 */
7344 static void
7345f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7346{
7347 job_T *job = get_job_arg(&argvars[0]);
7348 jobopt_T opt;
7349
7350 if (job == NULL)
7351 return;
7352 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007353 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 job_set_options(job, &opt);
7355 free_job_options(&opt);
7356}
7357
7358/*
7359 * "job_start()" function
7360 */
7361 static void
7362f_job_start(typval_T *argvars, typval_T *rettv)
7363{
7364 rettv->v_type = VAR_JOB;
7365 if (check_restricted() || check_secure())
7366 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007367 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368}
7369
7370/*
7371 * "job_status()" function
7372 */
7373 static void
7374f_job_status(typval_T *argvars, typval_T *rettv)
7375{
7376 job_T *job = get_job_arg(&argvars[0]);
7377
7378 if (job != NULL)
7379 {
7380 rettv->v_type = VAR_STRING;
7381 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7382 }
7383}
7384
7385/*
7386 * "job_stop()" function
7387 */
7388 static void
7389f_job_stop(typval_T *argvars, typval_T *rettv)
7390{
7391 job_T *job = get_job_arg(&argvars[0]);
7392
7393 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007394 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395}
7396#endif
7397
7398/*
7399 * "join()" function
7400 */
7401 static void
7402f_join(typval_T *argvars, typval_T *rettv)
7403{
7404 garray_T ga;
7405 char_u *sep;
7406
7407 if (argvars[0].v_type != VAR_LIST)
7408 {
7409 EMSG(_(e_listreq));
7410 return;
7411 }
7412 if (argvars[0].vval.v_list == NULL)
7413 return;
7414 if (argvars[1].v_type == VAR_UNKNOWN)
7415 sep = (char_u *)" ";
7416 else
7417 sep = get_tv_string_chk(&argvars[1]);
7418
7419 rettv->v_type = VAR_STRING;
7420
7421 if (sep != NULL)
7422 {
7423 ga_init2(&ga, (int)sizeof(char), 80);
7424 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7425 ga_append(&ga, NUL);
7426 rettv->vval.v_string = (char_u *)ga.ga_data;
7427 }
7428 else
7429 rettv->vval.v_string = NULL;
7430}
7431
7432/*
7433 * "js_decode()" function
7434 */
7435 static void
7436f_js_decode(typval_T *argvars, typval_T *rettv)
7437{
7438 js_read_T reader;
7439
7440 reader.js_buf = get_tv_string(&argvars[0]);
7441 reader.js_fill = NULL;
7442 reader.js_used = 0;
7443 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7444 EMSG(_(e_invarg));
7445}
7446
7447/*
7448 * "js_encode()" function
7449 */
7450 static void
7451f_js_encode(typval_T *argvars, typval_T *rettv)
7452{
7453 rettv->v_type = VAR_STRING;
7454 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7455}
7456
7457/*
7458 * "json_decode()" function
7459 */
7460 static void
7461f_json_decode(typval_T *argvars, typval_T *rettv)
7462{
7463 js_read_T reader;
7464
7465 reader.js_buf = get_tv_string(&argvars[0]);
7466 reader.js_fill = NULL;
7467 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007468 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007469}
7470
7471/*
7472 * "json_encode()" function
7473 */
7474 static void
7475f_json_encode(typval_T *argvars, typval_T *rettv)
7476{
7477 rettv->v_type = VAR_STRING;
7478 rettv->vval.v_string = json_encode(&argvars[0], 0);
7479}
7480
7481/*
7482 * "keys()" function
7483 */
7484 static void
7485f_keys(typval_T *argvars, typval_T *rettv)
7486{
7487 dict_list(argvars, rettv, 0);
7488}
7489
7490/*
7491 * "last_buffer_nr()" function.
7492 */
7493 static void
7494f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7495{
7496 int n = 0;
7497 buf_T *buf;
7498
Bram Moolenaar29323592016-07-24 22:04:11 +02007499 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 if (n < buf->b_fnum)
7501 n = buf->b_fnum;
7502
7503 rettv->vval.v_number = n;
7504}
7505
7506/*
7507 * "len()" function
7508 */
7509 static void
7510f_len(typval_T *argvars, typval_T *rettv)
7511{
7512 switch (argvars[0].v_type)
7513 {
7514 case VAR_STRING:
7515 case VAR_NUMBER:
7516 rettv->vval.v_number = (varnumber_T)STRLEN(
7517 get_tv_string(&argvars[0]));
7518 break;
7519 case VAR_LIST:
7520 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7521 break;
7522 case VAR_DICT:
7523 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7524 break;
7525 case VAR_UNKNOWN:
7526 case VAR_SPECIAL:
7527 case VAR_FLOAT:
7528 case VAR_FUNC:
7529 case VAR_PARTIAL:
7530 case VAR_JOB:
7531 case VAR_CHANNEL:
7532 EMSG(_("E701: Invalid type for len()"));
7533 break;
7534 }
7535}
7536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007537 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007538libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007539{
7540#ifdef FEAT_LIBCALL
7541 char_u *string_in;
7542 char_u **string_result;
7543 int nr_result;
7544#endif
7545
7546 rettv->v_type = type;
7547 if (type != VAR_NUMBER)
7548 rettv->vval.v_string = NULL;
7549
7550 if (check_restricted() || check_secure())
7551 return;
7552
7553#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007554 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7556 {
7557 string_in = NULL;
7558 if (argvars[2].v_type == VAR_STRING)
7559 string_in = argvars[2].vval.v_string;
7560 if (type == VAR_NUMBER)
7561 string_result = NULL;
7562 else
7563 string_result = &rettv->vval.v_string;
7564 if (mch_libcall(argvars[0].vval.v_string,
7565 argvars[1].vval.v_string,
7566 string_in,
7567 argvars[2].vval.v_number,
7568 string_result,
7569 &nr_result) == OK
7570 && type == VAR_NUMBER)
7571 rettv->vval.v_number = nr_result;
7572 }
7573#endif
7574}
7575
7576/*
7577 * "libcall()" function
7578 */
7579 static void
7580f_libcall(typval_T *argvars, typval_T *rettv)
7581{
7582 libcall_common(argvars, rettv, VAR_STRING);
7583}
7584
7585/*
7586 * "libcallnr()" function
7587 */
7588 static void
7589f_libcallnr(typval_T *argvars, typval_T *rettv)
7590{
7591 libcall_common(argvars, rettv, VAR_NUMBER);
7592}
7593
7594/*
7595 * "line(string)" function
7596 */
7597 static void
7598f_line(typval_T *argvars, typval_T *rettv)
7599{
7600 linenr_T lnum = 0;
7601 pos_T *fp;
7602 int fnum;
7603
7604 fp = var2fpos(&argvars[0], TRUE, &fnum);
7605 if (fp != NULL)
7606 lnum = fp->lnum;
7607 rettv->vval.v_number = lnum;
7608}
7609
7610/*
7611 * "line2byte(lnum)" function
7612 */
7613 static void
7614f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7615{
7616#ifndef FEAT_BYTEOFF
7617 rettv->vval.v_number = -1;
7618#else
7619 linenr_T lnum;
7620
7621 lnum = get_tv_lnum(argvars);
7622 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7623 rettv->vval.v_number = -1;
7624 else
7625 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7626 if (rettv->vval.v_number >= 0)
7627 ++rettv->vval.v_number;
7628#endif
7629}
7630
7631/*
7632 * "lispindent(lnum)" function
7633 */
7634 static void
7635f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7636{
7637#ifdef FEAT_LISP
7638 pos_T pos;
7639 linenr_T lnum;
7640
7641 pos = curwin->w_cursor;
7642 lnum = get_tv_lnum(argvars);
7643 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7644 {
7645 curwin->w_cursor.lnum = lnum;
7646 rettv->vval.v_number = get_lisp_indent();
7647 curwin->w_cursor = pos;
7648 }
7649 else
7650#endif
7651 rettv->vval.v_number = -1;
7652}
7653
7654/*
7655 * "localtime()" function
7656 */
7657 static void
7658f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7659{
7660 rettv->vval.v_number = (varnumber_T)time(NULL);
7661}
7662
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663 static void
7664get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7665{
7666 char_u *keys;
7667 char_u *which;
7668 char_u buf[NUMBUFLEN];
7669 char_u *keys_buf = NULL;
7670 char_u *rhs;
7671 int mode;
7672 int abbr = FALSE;
7673 int get_dict = FALSE;
7674 mapblock_T *mp;
7675 int buffer_local;
7676
7677 /* return empty string for failure */
7678 rettv->v_type = VAR_STRING;
7679 rettv->vval.v_string = NULL;
7680
7681 keys = get_tv_string(&argvars[0]);
7682 if (*keys == NUL)
7683 return;
7684
7685 if (argvars[1].v_type != VAR_UNKNOWN)
7686 {
7687 which = get_tv_string_buf_chk(&argvars[1], buf);
7688 if (argvars[2].v_type != VAR_UNKNOWN)
7689 {
7690 abbr = (int)get_tv_number(&argvars[2]);
7691 if (argvars[3].v_type != VAR_UNKNOWN)
7692 get_dict = (int)get_tv_number(&argvars[3]);
7693 }
7694 }
7695 else
7696 which = (char_u *)"";
7697 if (which == NULL)
7698 return;
7699
7700 mode = get_map_mode(&which, 0);
7701
7702 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7703 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7704 vim_free(keys_buf);
7705
7706 if (!get_dict)
7707 {
7708 /* Return a string. */
7709 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007710 {
7711 if (*rhs == NUL)
7712 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7713 else
7714 rettv->vval.v_string = str2special_save(rhs, FALSE);
7715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716
7717 }
7718 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7719 {
7720 /* Return a dictionary. */
7721 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7722 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7723 dict_T *dict = rettv->vval.v_dict;
7724
Bram Moolenaare0be1672018-07-08 16:50:37 +02007725 dict_add_string(dict, "lhs", lhs);
7726 dict_add_string(dict, "rhs", mp->m_orig_str);
7727 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7728 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7729 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007730 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7731 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007732 dict_add_number(dict, "buffer", (long)buffer_local);
7733 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7734 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735
7736 vim_free(lhs);
7737 vim_free(mapmode);
7738 }
7739}
7740
7741#ifdef FEAT_FLOAT
7742/*
7743 * "log()" function
7744 */
7745 static void
7746f_log(typval_T *argvars, typval_T *rettv)
7747{
7748 float_T f = 0.0;
7749
7750 rettv->v_type = VAR_FLOAT;
7751 if (get_float_arg(argvars, &f) == OK)
7752 rettv->vval.v_float = log(f);
7753 else
7754 rettv->vval.v_float = 0.0;
7755}
7756
7757/*
7758 * "log10()" function
7759 */
7760 static void
7761f_log10(typval_T *argvars, typval_T *rettv)
7762{
7763 float_T f = 0.0;
7764
7765 rettv->v_type = VAR_FLOAT;
7766 if (get_float_arg(argvars, &f) == OK)
7767 rettv->vval.v_float = log10(f);
7768 else
7769 rettv->vval.v_float = 0.0;
7770}
7771#endif
7772
7773#ifdef FEAT_LUA
7774/*
7775 * "luaeval()" function
7776 */
7777 static void
7778f_luaeval(typval_T *argvars, typval_T *rettv)
7779{
7780 char_u *str;
7781 char_u buf[NUMBUFLEN];
7782
7783 str = get_tv_string_buf(&argvars[0], buf);
7784 do_luaeval(str, argvars + 1, rettv);
7785}
7786#endif
7787
7788/*
7789 * "map()" function
7790 */
7791 static void
7792f_map(typval_T *argvars, typval_T *rettv)
7793{
7794 filter_map(argvars, rettv, TRUE);
7795}
7796
7797/*
7798 * "maparg()" function
7799 */
7800 static void
7801f_maparg(typval_T *argvars, typval_T *rettv)
7802{
7803 get_maparg(argvars, rettv, TRUE);
7804}
7805
7806/*
7807 * "mapcheck()" function
7808 */
7809 static void
7810f_mapcheck(typval_T *argvars, typval_T *rettv)
7811{
7812 get_maparg(argvars, rettv, FALSE);
7813}
7814
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007815typedef enum
7816{
7817 MATCH_END, /* matchend() */
7818 MATCH_MATCH, /* match() */
7819 MATCH_STR, /* matchstr() */
7820 MATCH_LIST, /* matchlist() */
7821 MATCH_POS /* matchstrpos() */
7822} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823
7824 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007825find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826{
7827 char_u *str = NULL;
7828 long len = 0;
7829 char_u *expr = NULL;
7830 char_u *pat;
7831 regmatch_T regmatch;
7832 char_u patbuf[NUMBUFLEN];
7833 char_u strbuf[NUMBUFLEN];
7834 char_u *save_cpo;
7835 long start = 0;
7836 long nth = 1;
7837 colnr_T startcol = 0;
7838 int match = 0;
7839 list_T *l = NULL;
7840 listitem_T *li = NULL;
7841 long idx = 0;
7842 char_u *tofree = NULL;
7843
7844 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7845 save_cpo = p_cpo;
7846 p_cpo = (char_u *)"";
7847
7848 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007849 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007851 /* type MATCH_LIST: return empty list when there are no matches.
7852 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 if (rettv_list_alloc(rettv) == FAIL)
7854 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007855 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007856 && (list_append_string(rettv->vval.v_list,
7857 (char_u *)"", 0) == FAIL
7858 || list_append_number(rettv->vval.v_list,
7859 (varnumber_T)-1) == FAIL
7860 || list_append_number(rettv->vval.v_list,
7861 (varnumber_T)-1) == FAIL
7862 || list_append_number(rettv->vval.v_list,
7863 (varnumber_T)-1) == FAIL))
7864 {
7865 list_free(rettv->vval.v_list);
7866 rettv->vval.v_list = NULL;
7867 goto theend;
7868 }
7869 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007870 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007871 {
7872 rettv->v_type = VAR_STRING;
7873 rettv->vval.v_string = NULL;
7874 }
7875
7876 if (argvars[0].v_type == VAR_LIST)
7877 {
7878 if ((l = argvars[0].vval.v_list) == NULL)
7879 goto theend;
7880 li = l->lv_first;
7881 }
7882 else
7883 {
7884 expr = str = get_tv_string(&argvars[0]);
7885 len = (long)STRLEN(str);
7886 }
7887
7888 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7889 if (pat == NULL)
7890 goto theend;
7891
7892 if (argvars[2].v_type != VAR_UNKNOWN)
7893 {
7894 int error = FALSE;
7895
7896 start = (long)get_tv_number_chk(&argvars[2], &error);
7897 if (error)
7898 goto theend;
7899 if (l != NULL)
7900 {
7901 li = list_find(l, start);
7902 if (li == NULL)
7903 goto theend;
7904 idx = l->lv_idx; /* use the cached index */
7905 }
7906 else
7907 {
7908 if (start < 0)
7909 start = 0;
7910 if (start > len)
7911 goto theend;
7912 /* When "count" argument is there ignore matches before "start",
7913 * otherwise skip part of the string. Differs when pattern is "^"
7914 * or "\<". */
7915 if (argvars[3].v_type != VAR_UNKNOWN)
7916 startcol = start;
7917 else
7918 {
7919 str += start;
7920 len -= start;
7921 }
7922 }
7923
7924 if (argvars[3].v_type != VAR_UNKNOWN)
7925 nth = (long)get_tv_number_chk(&argvars[3], &error);
7926 if (error)
7927 goto theend;
7928 }
7929
7930 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7931 if (regmatch.regprog != NULL)
7932 {
7933 regmatch.rm_ic = p_ic;
7934
7935 for (;;)
7936 {
7937 if (l != NULL)
7938 {
7939 if (li == NULL)
7940 {
7941 match = FALSE;
7942 break;
7943 }
7944 vim_free(tofree);
7945 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7946 if (str == NULL)
7947 break;
7948 }
7949
7950 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7951
7952 if (match && --nth <= 0)
7953 break;
7954 if (l == NULL && !match)
7955 break;
7956
7957 /* Advance to just after the match. */
7958 if (l != NULL)
7959 {
7960 li = li->li_next;
7961 ++idx;
7962 }
7963 else
7964 {
7965#ifdef FEAT_MBYTE
7966 startcol = (colnr_T)(regmatch.startp[0]
7967 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7968#else
7969 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7970#endif
7971 if (startcol > (colnr_T)len
7972 || str + startcol <= regmatch.startp[0])
7973 {
7974 match = FALSE;
7975 break;
7976 }
7977 }
7978 }
7979
7980 if (match)
7981 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007982 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 {
7984 listitem_T *li1 = rettv->vval.v_list->lv_first;
7985 listitem_T *li2 = li1->li_next;
7986 listitem_T *li3 = li2->li_next;
7987 listitem_T *li4 = li3->li_next;
7988
7989 vim_free(li1->li_tv.vval.v_string);
7990 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7991 (int)(regmatch.endp[0] - regmatch.startp[0]));
7992 li3->li_tv.vval.v_number =
7993 (varnumber_T)(regmatch.startp[0] - expr);
7994 li4->li_tv.vval.v_number =
7995 (varnumber_T)(regmatch.endp[0] - expr);
7996 if (l != NULL)
7997 li2->li_tv.vval.v_number = (varnumber_T)idx;
7998 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007999 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 {
8001 int i;
8002
8003 /* return list with matched string and submatches */
8004 for (i = 0; i < NSUBEXP; ++i)
8005 {
8006 if (regmatch.endp[i] == NULL)
8007 {
8008 if (list_append_string(rettv->vval.v_list,
8009 (char_u *)"", 0) == FAIL)
8010 break;
8011 }
8012 else if (list_append_string(rettv->vval.v_list,
8013 regmatch.startp[i],
8014 (int)(regmatch.endp[i] - regmatch.startp[i]))
8015 == FAIL)
8016 break;
8017 }
8018 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008019 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 {
8021 /* return matched string */
8022 if (l != NULL)
8023 copy_tv(&li->li_tv, rettv);
8024 else
8025 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8026 (int)(regmatch.endp[0] - regmatch.startp[0]));
8027 }
8028 else if (l != NULL)
8029 rettv->vval.v_number = idx;
8030 else
8031 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008032 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 rettv->vval.v_number =
8034 (varnumber_T)(regmatch.startp[0] - str);
8035 else
8036 rettv->vval.v_number =
8037 (varnumber_T)(regmatch.endp[0] - str);
8038 rettv->vval.v_number += (varnumber_T)(str - expr);
8039 }
8040 }
8041 vim_regfree(regmatch.regprog);
8042 }
8043
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008044theend:
8045 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 /* matchstrpos() without a list: drop the second item. */
8047 listitem_remove(rettv->vval.v_list,
8048 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 vim_free(tofree);
8050 p_cpo = save_cpo;
8051}
8052
8053/*
8054 * "match()" function
8055 */
8056 static void
8057f_match(typval_T *argvars, typval_T *rettv)
8058{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008059 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060}
8061
Bram Moolenaar95e51472018-07-28 16:55:56 +02008062#ifdef FEAT_SEARCH_EXTRA
8063 static int
8064matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8065{
8066 dictitem_T *di;
8067
8068 if (tv->v_type != VAR_DICT)
8069 {
8070 EMSG(_(e_dictreq));
8071 return FAIL;
8072 }
8073
8074 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008075 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008076 (char_u *)"conceal", FALSE);
8077
8078 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8079 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008080 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008081 if (*win == NULL)
8082 {
8083 EMSG(_("E957: Invalid window number"));
8084 return FAIL;
8085 }
8086 }
8087
8088 return OK;
8089}
8090#endif
8091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092/*
8093 * "matchadd()" function
8094 */
8095 static void
8096f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8097{
8098#ifdef FEAT_SEARCH_EXTRA
8099 char_u buf[NUMBUFLEN];
8100 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8101 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8102 int prio = 10; /* default priority */
8103 int id = -1;
8104 int error = FALSE;
8105 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008106 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107
8108 rettv->vval.v_number = -1;
8109
8110 if (grp == NULL || pat == NULL)
8111 return;
8112 if (argvars[2].v_type != VAR_UNKNOWN)
8113 {
8114 prio = (int)get_tv_number_chk(&argvars[2], &error);
8115 if (argvars[3].v_type != VAR_UNKNOWN)
8116 {
8117 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008118 if (argvars[4].v_type != VAR_UNKNOWN
8119 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8120 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 }
8122 }
8123 if (error == TRUE)
8124 return;
8125 if (id >= 1 && id <= 3)
8126 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008127 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128 return;
8129 }
8130
Bram Moolenaar95e51472018-07-28 16:55:56 +02008131 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 conceal_char);
8133#endif
8134}
8135
8136/*
8137 * "matchaddpos()" function
8138 */
8139 static void
8140f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8141{
8142#ifdef FEAT_SEARCH_EXTRA
8143 char_u buf[NUMBUFLEN];
8144 char_u *group;
8145 int prio = 10;
8146 int id = -1;
8147 int error = FALSE;
8148 list_T *l;
8149 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008150 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151
8152 rettv->vval.v_number = -1;
8153
8154 group = get_tv_string_buf_chk(&argvars[0], buf);
8155 if (group == NULL)
8156 return;
8157
8158 if (argvars[1].v_type != VAR_LIST)
8159 {
8160 EMSG2(_(e_listarg), "matchaddpos()");
8161 return;
8162 }
8163 l = argvars[1].vval.v_list;
8164 if (l == NULL)
8165 return;
8166
8167 if (argvars[2].v_type != VAR_UNKNOWN)
8168 {
8169 prio = (int)get_tv_number_chk(&argvars[2], &error);
8170 if (argvars[3].v_type != VAR_UNKNOWN)
8171 {
8172 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008173
8174 if (argvars[4].v_type != VAR_UNKNOWN
8175 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8176 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 }
8178 }
8179 if (error == TRUE)
8180 return;
8181
8182 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8183 if (id == 1 || id == 2)
8184 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008185 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 return;
8187 }
8188
Bram Moolenaar95e51472018-07-28 16:55:56 +02008189 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 conceal_char);
8191#endif
8192}
8193
8194/*
8195 * "matcharg()" function
8196 */
8197 static void
8198f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8199{
8200 if (rettv_list_alloc(rettv) == OK)
8201 {
8202#ifdef FEAT_SEARCH_EXTRA
8203 int id = (int)get_tv_number(&argvars[0]);
8204 matchitem_T *m;
8205
8206 if (id >= 1 && id <= 3)
8207 {
8208 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8209 {
8210 list_append_string(rettv->vval.v_list,
8211 syn_id2name(m->hlg_id), -1);
8212 list_append_string(rettv->vval.v_list, m->pattern, -1);
8213 }
8214 else
8215 {
8216 list_append_string(rettv->vval.v_list, NULL, -1);
8217 list_append_string(rettv->vval.v_list, NULL, -1);
8218 }
8219 }
8220#endif
8221 }
8222}
8223
8224/*
8225 * "matchdelete()" function
8226 */
8227 static void
8228f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8229{
8230#ifdef FEAT_SEARCH_EXTRA
8231 rettv->vval.v_number = match_delete(curwin,
8232 (int)get_tv_number(&argvars[0]), TRUE);
8233#endif
8234}
8235
8236/*
8237 * "matchend()" function
8238 */
8239 static void
8240f_matchend(typval_T *argvars, typval_T *rettv)
8241{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008242 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243}
8244
8245/*
8246 * "matchlist()" function
8247 */
8248 static void
8249f_matchlist(typval_T *argvars, typval_T *rettv)
8250{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008251 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252}
8253
8254/*
8255 * "matchstr()" function
8256 */
8257 static void
8258f_matchstr(typval_T *argvars, typval_T *rettv)
8259{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008260 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261}
8262
8263/*
8264 * "matchstrpos()" function
8265 */
8266 static void
8267f_matchstrpos(typval_T *argvars, typval_T *rettv)
8268{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008269 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270}
8271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 static void
8273max_min(typval_T *argvars, typval_T *rettv, int domax)
8274{
8275 varnumber_T n = 0;
8276 varnumber_T i;
8277 int error = FALSE;
8278
8279 if (argvars[0].v_type == VAR_LIST)
8280 {
8281 list_T *l;
8282 listitem_T *li;
8283
8284 l = argvars[0].vval.v_list;
8285 if (l != NULL)
8286 {
8287 li = l->lv_first;
8288 if (li != NULL)
8289 {
8290 n = get_tv_number_chk(&li->li_tv, &error);
8291 for (;;)
8292 {
8293 li = li->li_next;
8294 if (li == NULL)
8295 break;
8296 i = get_tv_number_chk(&li->li_tv, &error);
8297 if (domax ? i > n : i < n)
8298 n = i;
8299 }
8300 }
8301 }
8302 }
8303 else if (argvars[0].v_type == VAR_DICT)
8304 {
8305 dict_T *d;
8306 int first = TRUE;
8307 hashitem_T *hi;
8308 int todo;
8309
8310 d = argvars[0].vval.v_dict;
8311 if (d != NULL)
8312 {
8313 todo = (int)d->dv_hashtab.ht_used;
8314 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8315 {
8316 if (!HASHITEM_EMPTY(hi))
8317 {
8318 --todo;
8319 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8320 if (first)
8321 {
8322 n = i;
8323 first = FALSE;
8324 }
8325 else if (domax ? i > n : i < n)
8326 n = i;
8327 }
8328 }
8329 }
8330 }
8331 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008332 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 rettv->vval.v_number = error ? 0 : n;
8334}
8335
8336/*
8337 * "max()" function
8338 */
8339 static void
8340f_max(typval_T *argvars, typval_T *rettv)
8341{
8342 max_min(argvars, rettv, TRUE);
8343}
8344
8345/*
8346 * "min()" function
8347 */
8348 static void
8349f_min(typval_T *argvars, typval_T *rettv)
8350{
8351 max_min(argvars, rettv, FALSE);
8352}
8353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354/*
8355 * Create the directory in which "dir" is located, and higher levels when
8356 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008357 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 */
8359 static int
8360mkdir_recurse(char_u *dir, int prot)
8361{
8362 char_u *p;
8363 char_u *updir;
8364 int r = FAIL;
8365
8366 /* Get end of directory name in "dir".
8367 * We're done when it's "/" or "c:/". */
8368 p = gettail_sep(dir);
8369 if (p <= get_past_head(dir))
8370 return OK;
8371
8372 /* If the directory exists we're done. Otherwise: create it.*/
8373 updir = vim_strnsave(dir, (int)(p - dir));
8374 if (updir == NULL)
8375 return FAIL;
8376 if (mch_isdir(updir))
8377 r = OK;
8378 else if (mkdir_recurse(updir, prot) == OK)
8379 r = vim_mkdir_emsg(updir, prot);
8380 vim_free(updir);
8381 return r;
8382}
8383
8384#ifdef vim_mkdir
8385/*
8386 * "mkdir()" function
8387 */
8388 static void
8389f_mkdir(typval_T *argvars, typval_T *rettv)
8390{
8391 char_u *dir;
8392 char_u buf[NUMBUFLEN];
8393 int prot = 0755;
8394
8395 rettv->vval.v_number = FAIL;
8396 if (check_restricted() || check_secure())
8397 return;
8398
8399 dir = get_tv_string_buf(&argvars[0], buf);
8400 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008401 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008403 if (*gettail(dir) == NUL)
8404 /* remove trailing slashes */
8405 *gettail_sep(dir) = NUL;
8406
8407 if (argvars[1].v_type != VAR_UNKNOWN)
8408 {
8409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008411 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8412 if (prot == -1)
8413 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008415 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8416 {
8417 if (mch_isdir(dir))
8418 {
8419 /* With the "p" flag it's OK if the dir already exists. */
8420 rettv->vval.v_number = OK;
8421 return;
8422 }
8423 mkdir_recurse(dir, prot);
8424 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008426 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427}
8428#endif
8429
8430/*
8431 * "mode()" function
8432 */
8433 static void
8434f_mode(typval_T *argvars, typval_T *rettv)
8435{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008436 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437
Bram Moolenaar612cc382018-07-29 15:34:26 +02008438 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439
8440 if (time_for_testing == 93784)
8441 {
8442 /* Testing the two-character code. */
8443 buf[0] = 'x';
8444 buf[1] = '!';
8445 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008446#ifdef FEAT_TERMINAL
8447 else if (term_use_loop())
8448 buf[0] = 't';
8449#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450 else if (VIsual_active)
8451 {
8452 if (VIsual_select)
8453 buf[0] = VIsual_mode + 's' - 'v';
8454 else
8455 buf[0] = VIsual_mode;
8456 }
8457 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8458 || State == CONFIRM)
8459 {
8460 buf[0] = 'r';
8461 if (State == ASKMORE)
8462 buf[1] = 'm';
8463 else if (State == CONFIRM)
8464 buf[1] = '?';
8465 }
8466 else if (State == EXTERNCMD)
8467 buf[0] = '!';
8468 else if (State & INSERT)
8469 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 if (State & VREPLACE_FLAG)
8471 {
8472 buf[0] = 'R';
8473 buf[1] = 'v';
8474 }
8475 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008476 {
8477 if (State & REPLACE_FLAG)
8478 buf[0] = 'R';
8479 else
8480 buf[0] = 'i';
8481#ifdef FEAT_INS_EXPAND
8482 if (ins_compl_active())
8483 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008484 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008485 buf[1] = 'x';
8486#endif
8487 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008489 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 {
8491 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008492 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008494 else if (exmode_active == EXMODE_NORMAL)
8495 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 }
8497 else
8498 {
8499 buf[0] = 'n';
8500 if (finish_op)
8501 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008502 else if (restart_edit == 'I' || restart_edit == 'R'
8503 || restart_edit == 'V')
8504 {
8505 buf[1] = 'i';
8506 buf[2] = restart_edit;
8507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 }
8509
8510 /* Clear out the minor mode when the argument is not a non-zero number or
8511 * non-empty string. */
8512 if (!non_zero_arg(&argvars[0]))
8513 buf[1] = NUL;
8514
8515 rettv->vval.v_string = vim_strsave(buf);
8516 rettv->v_type = VAR_STRING;
8517}
8518
8519#if defined(FEAT_MZSCHEME) || defined(PROTO)
8520/*
8521 * "mzeval()" function
8522 */
8523 static void
8524f_mzeval(typval_T *argvars, typval_T *rettv)
8525{
8526 char_u *str;
8527 char_u buf[NUMBUFLEN];
8528
8529 str = get_tv_string_buf(&argvars[0], buf);
8530 do_mzeval(str, rettv);
8531}
8532
8533 void
8534mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8535{
8536 typval_T argvars[3];
8537
8538 argvars[0].v_type = VAR_STRING;
8539 argvars[0].vval.v_string = name;
8540 copy_tv(args, &argvars[1]);
8541 argvars[2].v_type = VAR_UNKNOWN;
8542 f_call(argvars, rettv);
8543 clear_tv(&argvars[1]);
8544}
8545#endif
8546
8547/*
8548 * "nextnonblank()" function
8549 */
8550 static void
8551f_nextnonblank(typval_T *argvars, typval_T *rettv)
8552{
8553 linenr_T lnum;
8554
8555 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8556 {
8557 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8558 {
8559 lnum = 0;
8560 break;
8561 }
8562 if (*skipwhite(ml_get(lnum)) != NUL)
8563 break;
8564 }
8565 rettv->vval.v_number = lnum;
8566}
8567
8568/*
8569 * "nr2char()" function
8570 */
8571 static void
8572f_nr2char(typval_T *argvars, typval_T *rettv)
8573{
8574 char_u buf[NUMBUFLEN];
8575
8576#ifdef FEAT_MBYTE
8577 if (has_mbyte)
8578 {
8579 int utf8 = 0;
8580
8581 if (argvars[1].v_type != VAR_UNKNOWN)
8582 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8583 if (utf8)
8584 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8585 else
8586 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8587 }
8588 else
8589#endif
8590 {
8591 buf[0] = (char_u)get_tv_number(&argvars[0]);
8592 buf[1] = NUL;
8593 }
8594 rettv->v_type = VAR_STRING;
8595 rettv->vval.v_string = vim_strsave(buf);
8596}
8597
8598/*
8599 * "or(expr, expr)" function
8600 */
8601 static void
8602f_or(typval_T *argvars, typval_T *rettv)
8603{
8604 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8605 | get_tv_number_chk(&argvars[1], NULL);
8606}
8607
8608/*
8609 * "pathshorten()" function
8610 */
8611 static void
8612f_pathshorten(typval_T *argvars, typval_T *rettv)
8613{
8614 char_u *p;
8615
8616 rettv->v_type = VAR_STRING;
8617 p = get_tv_string_chk(&argvars[0]);
8618 if (p == NULL)
8619 rettv->vval.v_string = NULL;
8620 else
8621 {
8622 p = vim_strsave(p);
8623 rettv->vval.v_string = p;
8624 if (p != NULL)
8625 shorten_dir(p);
8626 }
8627}
8628
8629#ifdef FEAT_PERL
8630/*
8631 * "perleval()" function
8632 */
8633 static void
8634f_perleval(typval_T *argvars, typval_T *rettv)
8635{
8636 char_u *str;
8637 char_u buf[NUMBUFLEN];
8638
8639 str = get_tv_string_buf(&argvars[0], buf);
8640 do_perleval(str, rettv);
8641}
8642#endif
8643
8644#ifdef FEAT_FLOAT
8645/*
8646 * "pow()" function
8647 */
8648 static void
8649f_pow(typval_T *argvars, typval_T *rettv)
8650{
8651 float_T fx = 0.0, fy = 0.0;
8652
8653 rettv->v_type = VAR_FLOAT;
8654 if (get_float_arg(argvars, &fx) == OK
8655 && get_float_arg(&argvars[1], &fy) == OK)
8656 rettv->vval.v_float = pow(fx, fy);
8657 else
8658 rettv->vval.v_float = 0.0;
8659}
8660#endif
8661
8662/*
8663 * "prevnonblank()" function
8664 */
8665 static void
8666f_prevnonblank(typval_T *argvars, typval_T *rettv)
8667{
8668 linenr_T lnum;
8669
8670 lnum = get_tv_lnum(argvars);
8671 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8672 lnum = 0;
8673 else
8674 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8675 --lnum;
8676 rettv->vval.v_number = lnum;
8677}
8678
8679/* This dummy va_list is here because:
8680 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8681 * - locally in the function results in a "used before set" warning
8682 * - using va_start() to initialize it gives "function with fixed args" error */
8683static va_list ap;
8684
8685/*
8686 * "printf()" function
8687 */
8688 static void
8689f_printf(typval_T *argvars, typval_T *rettv)
8690{
8691 char_u buf[NUMBUFLEN];
8692 int len;
8693 char_u *s;
8694 int saved_did_emsg = did_emsg;
8695 char *fmt;
8696
8697 rettv->v_type = VAR_STRING;
8698 rettv->vval.v_string = NULL;
8699
8700 /* Get the required length, allocate the buffer and do it for real. */
8701 did_emsg = FALSE;
8702 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008703 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704 if (!did_emsg)
8705 {
8706 s = alloc(len + 1);
8707 if (s != NULL)
8708 {
8709 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008710 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8711 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 }
8713 }
8714 did_emsg |= saved_did_emsg;
8715}
8716
Bram Moolenaarf2732452018-06-03 14:47:35 +02008717#ifdef FEAT_JOB_CHANNEL
8718/*
8719 * "prompt_setcallback({buffer}, {callback})" function
8720 */
8721 static void
8722f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8723{
8724 buf_T *buf;
8725 char_u *callback;
8726 partial_T *partial;
8727
8728 if (check_secure())
8729 return;
8730 buf = get_buf_tv(&argvars[0], FALSE);
8731 if (buf == NULL)
8732 return;
8733
8734 callback = get_callback(&argvars[1], &partial);
8735 if (callback == NULL)
8736 return;
8737
8738 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8739 if (partial == NULL)
8740 buf->b_prompt_callback = vim_strsave(callback);
8741 else
8742 /* pointer into the partial */
8743 buf->b_prompt_callback = callback;
8744 buf->b_prompt_partial = partial;
8745}
8746
8747/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008748 * "prompt_setinterrupt({buffer}, {callback})" function
8749 */
8750 static void
8751f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8752{
8753 buf_T *buf;
8754 char_u *callback;
8755 partial_T *partial;
8756
8757 if (check_secure())
8758 return;
8759 buf = get_buf_tv(&argvars[0], FALSE);
8760 if (buf == NULL)
8761 return;
8762
8763 callback = get_callback(&argvars[1], &partial);
8764 if (callback == NULL)
8765 return;
8766
8767 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8768 if (partial == NULL)
8769 buf->b_prompt_interrupt = vim_strsave(callback);
8770 else
8771 /* pointer into the partial */
8772 buf->b_prompt_interrupt = callback;
8773 buf->b_prompt_int_partial = partial;
8774}
8775
8776/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008777 * "prompt_setprompt({buffer}, {text})" function
8778 */
8779 static void
8780f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8781{
8782 buf_T *buf;
8783 char_u *text;
8784
8785 if (check_secure())
8786 return;
8787 buf = get_buf_tv(&argvars[0], FALSE);
8788 if (buf == NULL)
8789 return;
8790
8791 text = get_tv_string(&argvars[1]);
8792 vim_free(buf->b_prompt_text);
8793 buf->b_prompt_text = vim_strsave(text);
8794}
8795#endif
8796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797/*
8798 * "pumvisible()" function
8799 */
8800 static void
8801f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8802{
8803#ifdef FEAT_INS_EXPAND
8804 if (pum_visible())
8805 rettv->vval.v_number = 1;
8806#endif
8807}
8808
8809#ifdef FEAT_PYTHON3
8810/*
8811 * "py3eval()" function
8812 */
8813 static void
8814f_py3eval(typval_T *argvars, typval_T *rettv)
8815{
8816 char_u *str;
8817 char_u buf[NUMBUFLEN];
8818
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008819 if (p_pyx == 0)
8820 p_pyx = 3;
8821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 str = get_tv_string_buf(&argvars[0], buf);
8823 do_py3eval(str, rettv);
8824}
8825#endif
8826
8827#ifdef FEAT_PYTHON
8828/*
8829 * "pyeval()" function
8830 */
8831 static void
8832f_pyeval(typval_T *argvars, typval_T *rettv)
8833{
8834 char_u *str;
8835 char_u buf[NUMBUFLEN];
8836
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008837 if (p_pyx == 0)
8838 p_pyx = 2;
8839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 str = get_tv_string_buf(&argvars[0], buf);
8841 do_pyeval(str, rettv);
8842}
8843#endif
8844
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8846/*
8847 * "pyxeval()" function
8848 */
8849 static void
8850f_pyxeval(typval_T *argvars, typval_T *rettv)
8851{
8852# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8853 init_pyxversion();
8854 if (p_pyx == 2)
8855 f_pyeval(argvars, rettv);
8856 else
8857 f_py3eval(argvars, rettv);
8858# elif defined(FEAT_PYTHON)
8859 f_pyeval(argvars, rettv);
8860# elif defined(FEAT_PYTHON3)
8861 f_py3eval(argvars, rettv);
8862# endif
8863}
8864#endif
8865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866/*
8867 * "range()" function
8868 */
8869 static void
8870f_range(typval_T *argvars, typval_T *rettv)
8871{
8872 varnumber_T start;
8873 varnumber_T end;
8874 varnumber_T stride = 1;
8875 varnumber_T i;
8876 int error = FALSE;
8877
8878 start = get_tv_number_chk(&argvars[0], &error);
8879 if (argvars[1].v_type == VAR_UNKNOWN)
8880 {
8881 end = start - 1;
8882 start = 0;
8883 }
8884 else
8885 {
8886 end = get_tv_number_chk(&argvars[1], &error);
8887 if (argvars[2].v_type != VAR_UNKNOWN)
8888 stride = get_tv_number_chk(&argvars[2], &error);
8889 }
8890
8891 if (error)
8892 return; /* type error; errmsg already given */
8893 if (stride == 0)
8894 EMSG(_("E726: Stride is zero"));
8895 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8896 EMSG(_("E727: Start past end"));
8897 else
8898 {
8899 if (rettv_list_alloc(rettv) == OK)
8900 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8901 if (list_append_number(rettv->vval.v_list,
8902 (varnumber_T)i) == FAIL)
8903 break;
8904 }
8905}
8906
8907/*
8908 * "readfile()" function
8909 */
8910 static void
8911f_readfile(typval_T *argvars, typval_T *rettv)
8912{
8913 int binary = FALSE;
8914 int failed = FALSE;
8915 char_u *fname;
8916 FILE *fd;
8917 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8918 int io_size = sizeof(buf);
8919 int readlen; /* size of last fread() */
8920 char_u *prev = NULL; /* previously read bytes, if any */
8921 long prevlen = 0; /* length of data in prev */
8922 long prevsize = 0; /* size of prev buffer */
8923 long maxline = MAXLNUM;
8924 long cnt = 0;
8925 char_u *p; /* position in buf */
8926 char_u *start; /* start of current line */
8927
8928 if (argvars[1].v_type != VAR_UNKNOWN)
8929 {
8930 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8931 binary = TRUE;
8932 if (argvars[2].v_type != VAR_UNKNOWN)
8933 maxline = (long)get_tv_number(&argvars[2]);
8934 }
8935
8936 if (rettv_list_alloc(rettv) == FAIL)
8937 return;
8938
8939 /* Always open the file in binary mode, library functions have a mind of
8940 * their own about CR-LF conversion. */
8941 fname = get_tv_string(&argvars[0]);
8942 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8943 {
8944 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8945 return;
8946 }
8947
8948 while (cnt < maxline || maxline < 0)
8949 {
8950 readlen = (int)fread(buf, 1, io_size, fd);
8951
8952 /* This for loop processes what was read, but is also entered at end
8953 * of file so that either:
8954 * - an incomplete line gets written
8955 * - a "binary" file gets an empty line at the end if it ends in a
8956 * newline. */
8957 for (p = buf, start = buf;
8958 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8959 ++p)
8960 {
8961 if (*p == '\n' || readlen <= 0)
8962 {
8963 listitem_T *li;
8964 char_u *s = NULL;
8965 long_u len = p - start;
8966
8967 /* Finished a line. Remove CRs before NL. */
8968 if (readlen > 0 && !binary)
8969 {
8970 while (len > 0 && start[len - 1] == '\r')
8971 --len;
8972 /* removal may cross back to the "prev" string */
8973 if (len == 0)
8974 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8975 --prevlen;
8976 }
8977 if (prevlen == 0)
8978 s = vim_strnsave(start, (int)len);
8979 else
8980 {
8981 /* Change "prev" buffer to be the right size. This way
8982 * the bytes are only copied once, and very long lines are
8983 * allocated only once. */
8984 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8985 {
8986 mch_memmove(s + prevlen, start, len);
8987 s[prevlen + len] = NUL;
8988 prev = NULL; /* the list will own the string */
8989 prevlen = prevsize = 0;
8990 }
8991 }
8992 if (s == NULL)
8993 {
8994 do_outofmem_msg((long_u) prevlen + len + 1);
8995 failed = TRUE;
8996 break;
8997 }
8998
8999 if ((li = listitem_alloc()) == NULL)
9000 {
9001 vim_free(s);
9002 failed = TRUE;
9003 break;
9004 }
9005 li->li_tv.v_type = VAR_STRING;
9006 li->li_tv.v_lock = 0;
9007 li->li_tv.vval.v_string = s;
9008 list_append(rettv->vval.v_list, li);
9009
9010 start = p + 1; /* step over newline */
9011 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9012 break;
9013 }
9014 else if (*p == NUL)
9015 *p = '\n';
9016#ifdef FEAT_MBYTE
9017 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9018 * when finding the BF and check the previous two bytes. */
9019 else if (*p == 0xbf && enc_utf8 && !binary)
9020 {
9021 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9022 * + 1, these may be in the "prev" string. */
9023 char_u back1 = p >= buf + 1 ? p[-1]
9024 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9025 char_u back2 = p >= buf + 2 ? p[-2]
9026 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9027 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9028
9029 if (back2 == 0xef && back1 == 0xbb)
9030 {
9031 char_u *dest = p - 2;
9032
9033 /* Usually a BOM is at the beginning of a file, and so at
9034 * the beginning of a line; then we can just step over it.
9035 */
9036 if (start == dest)
9037 start = p + 1;
9038 else
9039 {
9040 /* have to shuffle buf to close gap */
9041 int adjust_prevlen = 0;
9042
9043 if (dest < buf)
9044 {
9045 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9046 dest = buf;
9047 }
9048 if (readlen > p - buf + 1)
9049 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9050 readlen -= 3 - adjust_prevlen;
9051 prevlen -= adjust_prevlen;
9052 p = dest - 1;
9053 }
9054 }
9055 }
9056#endif
9057 } /* for */
9058
9059 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9060 break;
9061 if (start < p)
9062 {
9063 /* There's part of a line in buf, store it in "prev". */
9064 if (p - start + prevlen >= prevsize)
9065 {
9066 /* need bigger "prev" buffer */
9067 char_u *newprev;
9068
9069 /* A common use case is ordinary text files and "prev" gets a
9070 * fragment of a line, so the first allocation is made
9071 * small, to avoid repeatedly 'allocing' large and
9072 * 'reallocing' small. */
9073 if (prevsize == 0)
9074 prevsize = (long)(p - start);
9075 else
9076 {
9077 long grow50pc = (prevsize * 3) / 2;
9078 long growmin = (long)((p - start) * 2 + prevlen);
9079 prevsize = grow50pc > growmin ? grow50pc : growmin;
9080 }
9081 newprev = prev == NULL ? alloc(prevsize)
9082 : vim_realloc(prev, prevsize);
9083 if (newprev == NULL)
9084 {
9085 do_outofmem_msg((long_u)prevsize);
9086 failed = TRUE;
9087 break;
9088 }
9089 prev = newprev;
9090 }
9091 /* Add the line part to end of "prev". */
9092 mch_memmove(prev + prevlen, start, p - start);
9093 prevlen += (long)(p - start);
9094 }
9095 } /* while */
9096
9097 /*
9098 * For a negative line count use only the lines at the end of the file,
9099 * free the rest.
9100 */
9101 if (!failed && maxline < 0)
9102 while (cnt > -maxline)
9103 {
9104 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9105 --cnt;
9106 }
9107
9108 if (failed)
9109 {
9110 list_free(rettv->vval.v_list);
9111 /* readfile doc says an empty list is returned on error */
9112 rettv->vval.v_list = list_alloc();
9113 }
9114
9115 vim_free(prev);
9116 fclose(fd);
9117}
9118
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009119 static void
9120return_register(int regname, typval_T *rettv)
9121{
9122 char_u buf[2] = {0, 0};
9123
9124 buf[0] = (char_u)regname;
9125 rettv->v_type = VAR_STRING;
9126 rettv->vval.v_string = vim_strsave(buf);
9127}
9128
9129/*
9130 * "reg_executing()" function
9131 */
9132 static void
9133f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9134{
9135 return_register(reg_executing, rettv);
9136}
9137
9138/*
9139 * "reg_recording()" function
9140 */
9141 static void
9142f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9143{
9144 return_register(reg_recording, rettv);
9145}
9146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148/*
9149 * Convert a List to proftime_T.
9150 * Return FAIL when there is something wrong.
9151 */
9152 static int
9153list2proftime(typval_T *arg, proftime_T *tm)
9154{
9155 long n1, n2;
9156 int error = FALSE;
9157
9158 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9159 || arg->vval.v_list->lv_len != 2)
9160 return FAIL;
9161 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9162 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9163# ifdef WIN3264
9164 tm->HighPart = n1;
9165 tm->LowPart = n2;
9166# else
9167 tm->tv_sec = n1;
9168 tm->tv_usec = n2;
9169# endif
9170 return error ? FAIL : OK;
9171}
9172#endif /* FEAT_RELTIME */
9173
9174/*
9175 * "reltime()" function
9176 */
9177 static void
9178f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9179{
9180#ifdef FEAT_RELTIME
9181 proftime_T res;
9182 proftime_T start;
9183
9184 if (argvars[0].v_type == VAR_UNKNOWN)
9185 {
9186 /* No arguments: get current time. */
9187 profile_start(&res);
9188 }
9189 else if (argvars[1].v_type == VAR_UNKNOWN)
9190 {
9191 if (list2proftime(&argvars[0], &res) == FAIL)
9192 return;
9193 profile_end(&res);
9194 }
9195 else
9196 {
9197 /* Two arguments: compute the difference. */
9198 if (list2proftime(&argvars[0], &start) == FAIL
9199 || list2proftime(&argvars[1], &res) == FAIL)
9200 return;
9201 profile_sub(&res, &start);
9202 }
9203
9204 if (rettv_list_alloc(rettv) == OK)
9205 {
9206 long n1, n2;
9207
9208# ifdef WIN3264
9209 n1 = res.HighPart;
9210 n2 = res.LowPart;
9211# else
9212 n1 = res.tv_sec;
9213 n2 = res.tv_usec;
9214# endif
9215 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9216 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9217 }
9218#endif
9219}
9220
9221#ifdef FEAT_FLOAT
9222/*
9223 * "reltimefloat()" function
9224 */
9225 static void
9226f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9227{
9228# ifdef FEAT_RELTIME
9229 proftime_T tm;
9230# endif
9231
9232 rettv->v_type = VAR_FLOAT;
9233 rettv->vval.v_float = 0;
9234# ifdef FEAT_RELTIME
9235 if (list2proftime(&argvars[0], &tm) == OK)
9236 rettv->vval.v_float = profile_float(&tm);
9237# endif
9238}
9239#endif
9240
9241/*
9242 * "reltimestr()" function
9243 */
9244 static void
9245f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9246{
9247#ifdef FEAT_RELTIME
9248 proftime_T tm;
9249#endif
9250
9251 rettv->v_type = VAR_STRING;
9252 rettv->vval.v_string = NULL;
9253#ifdef FEAT_RELTIME
9254 if (list2proftime(&argvars[0], &tm) == OK)
9255 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9256#endif
9257}
9258
9259#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 static void
9261make_connection(void)
9262{
9263 if (X_DISPLAY == NULL
9264# ifdef FEAT_GUI
9265 && !gui.in_use
9266# endif
9267 )
9268 {
9269 x_force_connect = TRUE;
9270 setup_term_clip();
9271 x_force_connect = FALSE;
9272 }
9273}
9274
9275 static int
9276check_connection(void)
9277{
9278 make_connection();
9279 if (X_DISPLAY == NULL)
9280 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009281 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 return FAIL;
9283 }
9284 return OK;
9285}
9286#endif
9287
9288#ifdef FEAT_CLIENTSERVER
9289 static void
9290remote_common(typval_T *argvars, typval_T *rettv, int expr)
9291{
9292 char_u *server_name;
9293 char_u *keys;
9294 char_u *r = NULL;
9295 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009296 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297# ifdef WIN32
9298 HWND w;
9299# else
9300 Window w;
9301# endif
9302
9303 if (check_restricted() || check_secure())
9304 return;
9305
9306# ifdef FEAT_X11
9307 if (check_connection() == FAIL)
9308 return;
9309# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009310 if (argvars[2].v_type != VAR_UNKNOWN
9311 && argvars[3].v_type != VAR_UNKNOWN)
9312 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313
9314 server_name = get_tv_string_chk(&argvars[0]);
9315 if (server_name == NULL)
9316 return; /* type error; errmsg already given */
9317 keys = get_tv_string_buf(&argvars[1], buf);
9318# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009319 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009320# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009321 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9322 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323# endif
9324 {
9325 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009326 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009328 vim_free(r);
9329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 else
9331 EMSG2(_("E241: Unable to send to %s"), server_name);
9332 return;
9333 }
9334
9335 rettv->vval.v_string = r;
9336
9337 if (argvars[2].v_type != VAR_UNKNOWN)
9338 {
9339 dictitem_T v;
9340 char_u str[30];
9341 char_u *idvar;
9342
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009344 if (idvar != NULL && *idvar != NUL)
9345 {
9346 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9347 v.di_tv.v_type = VAR_STRING;
9348 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009350 vim_free(v.di_tv.vval.v_string);
9351 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 }
9353}
9354#endif
9355
9356/*
9357 * "remote_expr()" function
9358 */
9359 static void
9360f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9361{
9362 rettv->v_type = VAR_STRING;
9363 rettv->vval.v_string = NULL;
9364#ifdef FEAT_CLIENTSERVER
9365 remote_common(argvars, rettv, TRUE);
9366#endif
9367}
9368
9369/*
9370 * "remote_foreground()" function
9371 */
9372 static void
9373f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9374{
9375#ifdef FEAT_CLIENTSERVER
9376# ifdef WIN32
9377 /* On Win32 it's done in this application. */
9378 {
9379 char_u *server_name = get_tv_string_chk(&argvars[0]);
9380
9381 if (server_name != NULL)
9382 serverForeground(server_name);
9383 }
9384# else
9385 /* Send a foreground() expression to the server. */
9386 argvars[1].v_type = VAR_STRING;
9387 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9388 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009389 rettv->v_type = VAR_STRING;
9390 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 remote_common(argvars, rettv, TRUE);
9392 vim_free(argvars[1].vval.v_string);
9393# endif
9394#endif
9395}
9396
9397 static void
9398f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9399{
9400#ifdef FEAT_CLIENTSERVER
9401 dictitem_T v;
9402 char_u *s = NULL;
9403# ifdef WIN32
9404 long_u n = 0;
9405# endif
9406 char_u *serverid;
9407
9408 if (check_restricted() || check_secure())
9409 {
9410 rettv->vval.v_number = -1;
9411 return;
9412 }
9413 serverid = get_tv_string_chk(&argvars[0]);
9414 if (serverid == NULL)
9415 {
9416 rettv->vval.v_number = -1;
9417 return; /* type error; errmsg already given */
9418 }
9419# ifdef WIN32
9420 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9421 if (n == 0)
9422 rettv->vval.v_number = -1;
9423 else
9424 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009425 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 rettv->vval.v_number = (s != NULL);
9427 }
9428# else
9429 if (check_connection() == FAIL)
9430 return;
9431
9432 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9433 serverStrToWin(serverid), &s);
9434# endif
9435
9436 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9437 {
9438 char_u *retvar;
9439
9440 v.di_tv.v_type = VAR_STRING;
9441 v.di_tv.vval.v_string = vim_strsave(s);
9442 retvar = get_tv_string_chk(&argvars[1]);
9443 if (retvar != NULL)
9444 set_var(retvar, &v.di_tv, FALSE);
9445 vim_free(v.di_tv.vval.v_string);
9446 }
9447#else
9448 rettv->vval.v_number = -1;
9449#endif
9450}
9451
9452 static void
9453f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9454{
9455 char_u *r = NULL;
9456
9457#ifdef FEAT_CLIENTSERVER
9458 char_u *serverid = get_tv_string_chk(&argvars[0]);
9459
9460 if (serverid != NULL && !check_restricted() && !check_secure())
9461 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009462 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009463# ifdef WIN32
9464 /* The server's HWND is encoded in the 'id' parameter */
9465 long_u n = 0;
9466# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009467
9468 if (argvars[1].v_type != VAR_UNKNOWN)
9469 timeout = get_tv_number(&argvars[1]);
9470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9473 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009474 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 if (r == NULL)
9476# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009477 if (check_connection() == FAIL
9478 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9479 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480# endif
9481 EMSG(_("E277: Unable to read a server reply"));
9482 }
9483#endif
9484 rettv->v_type = VAR_STRING;
9485 rettv->vval.v_string = r;
9486}
9487
9488/*
9489 * "remote_send()" function
9490 */
9491 static void
9492f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9493{
9494 rettv->v_type = VAR_STRING;
9495 rettv->vval.v_string = NULL;
9496#ifdef FEAT_CLIENTSERVER
9497 remote_common(argvars, rettv, FALSE);
9498#endif
9499}
9500
9501/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009502 * "remote_startserver()" function
9503 */
9504 static void
9505f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9506{
9507#ifdef FEAT_CLIENTSERVER
9508 char_u *server = get_tv_string_chk(&argvars[0]);
9509
9510 if (server == NULL)
9511 return; /* type error; errmsg already given */
9512 if (serverName != NULL)
9513 EMSG(_("E941: already started a server"));
9514 else
9515 {
9516# ifdef FEAT_X11
9517 if (check_connection() == OK)
9518 serverRegisterName(X_DISPLAY, server);
9519# else
9520 serverSetName(server);
9521# endif
9522 }
9523#else
9524 EMSG(_("E942: +clientserver feature not available"));
9525#endif
9526}
9527
9528/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 * "remove()" function
9530 */
9531 static void
9532f_remove(typval_T *argvars, typval_T *rettv)
9533{
9534 list_T *l;
9535 listitem_T *item, *item2;
9536 listitem_T *li;
9537 long idx;
9538 long end;
9539 char_u *key;
9540 dict_T *d;
9541 dictitem_T *di;
9542 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9543
9544 if (argvars[0].v_type == VAR_DICT)
9545 {
9546 if (argvars[2].v_type != VAR_UNKNOWN)
9547 EMSG2(_(e_toomanyarg), "remove()");
9548 else if ((d = argvars[0].vval.v_dict) != NULL
9549 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9550 {
9551 key = get_tv_string_chk(&argvars[1]);
9552 if (key != NULL)
9553 {
9554 di = dict_find(d, key, -1);
9555 if (di == NULL)
9556 EMSG2(_(e_dictkey), key);
9557 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9558 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9559 {
9560 *rettv = di->di_tv;
9561 init_tv(&di->di_tv);
9562 dictitem_remove(d, di);
9563 }
9564 }
9565 }
9566 }
9567 else if (argvars[0].v_type != VAR_LIST)
9568 EMSG2(_(e_listdictarg), "remove()");
9569 else if ((l = argvars[0].vval.v_list) != NULL
9570 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9571 {
9572 int error = FALSE;
9573
9574 idx = (long)get_tv_number_chk(&argvars[1], &error);
9575 if (error)
9576 ; /* type error: do nothing, errmsg already given */
9577 else if ((item = list_find(l, idx)) == NULL)
9578 EMSGN(_(e_listidx), idx);
9579 else
9580 {
9581 if (argvars[2].v_type == VAR_UNKNOWN)
9582 {
9583 /* Remove one item, return its value. */
9584 vimlist_remove(l, item, item);
9585 *rettv = item->li_tv;
9586 vim_free(item);
9587 }
9588 else
9589 {
9590 /* Remove range of items, return list with values. */
9591 end = (long)get_tv_number_chk(&argvars[2], &error);
9592 if (error)
9593 ; /* type error: do nothing */
9594 else if ((item2 = list_find(l, end)) == NULL)
9595 EMSGN(_(e_listidx), end);
9596 else
9597 {
9598 int cnt = 0;
9599
9600 for (li = item; li != NULL; li = li->li_next)
9601 {
9602 ++cnt;
9603 if (li == item2)
9604 break;
9605 }
9606 if (li == NULL) /* didn't find "item2" after "item" */
9607 EMSG(_(e_invrange));
9608 else
9609 {
9610 vimlist_remove(l, item, item2);
9611 if (rettv_list_alloc(rettv) == OK)
9612 {
9613 l = rettv->vval.v_list;
9614 l->lv_first = item;
9615 l->lv_last = item2;
9616 item->li_prev = NULL;
9617 item2->li_next = NULL;
9618 l->lv_len = cnt;
9619 }
9620 }
9621 }
9622 }
9623 }
9624 }
9625}
9626
9627/*
9628 * "rename({from}, {to})" function
9629 */
9630 static void
9631f_rename(typval_T *argvars, typval_T *rettv)
9632{
9633 char_u buf[NUMBUFLEN];
9634
9635 if (check_restricted() || check_secure())
9636 rettv->vval.v_number = -1;
9637 else
9638 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9639 get_tv_string_buf(&argvars[1], buf));
9640}
9641
9642/*
9643 * "repeat()" function
9644 */
9645 static void
9646f_repeat(typval_T *argvars, typval_T *rettv)
9647{
9648 char_u *p;
9649 int n;
9650 int slen;
9651 int len;
9652 char_u *r;
9653 int i;
9654
9655 n = (int)get_tv_number(&argvars[1]);
9656 if (argvars[0].v_type == VAR_LIST)
9657 {
9658 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9659 while (n-- > 0)
9660 if (list_extend(rettv->vval.v_list,
9661 argvars[0].vval.v_list, NULL) == FAIL)
9662 break;
9663 }
9664 else
9665 {
9666 p = get_tv_string(&argvars[0]);
9667 rettv->v_type = VAR_STRING;
9668 rettv->vval.v_string = NULL;
9669
9670 slen = (int)STRLEN(p);
9671 len = slen * n;
9672 if (len <= 0)
9673 return;
9674
9675 r = alloc(len + 1);
9676 if (r != NULL)
9677 {
9678 for (i = 0; i < n; i++)
9679 mch_memmove(r + i * slen, p, (size_t)slen);
9680 r[len] = NUL;
9681 }
9682
9683 rettv->vval.v_string = r;
9684 }
9685}
9686
9687/*
9688 * "resolve()" function
9689 */
9690 static void
9691f_resolve(typval_T *argvars, typval_T *rettv)
9692{
9693 char_u *p;
9694#ifdef HAVE_READLINK
9695 char_u *buf = NULL;
9696#endif
9697
9698 p = get_tv_string(&argvars[0]);
9699#ifdef FEAT_SHORTCUT
9700 {
9701 char_u *v = NULL;
9702
9703 v = mch_resolve_shortcut(p);
9704 if (v != NULL)
9705 rettv->vval.v_string = v;
9706 else
9707 rettv->vval.v_string = vim_strsave(p);
9708 }
9709#else
9710# ifdef HAVE_READLINK
9711 {
9712 char_u *cpy;
9713 int len;
9714 char_u *remain = NULL;
9715 char_u *q;
9716 int is_relative_to_current = FALSE;
9717 int has_trailing_pathsep = FALSE;
9718 int limit = 100;
9719
9720 p = vim_strsave(p);
9721
9722 if (p[0] == '.' && (vim_ispathsep(p[1])
9723 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9724 is_relative_to_current = TRUE;
9725
9726 len = STRLEN(p);
9727 if (len > 0 && after_pathsep(p, p + len))
9728 {
9729 has_trailing_pathsep = TRUE;
9730 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9731 }
9732
9733 q = getnextcomp(p);
9734 if (*q != NUL)
9735 {
9736 /* Separate the first path component in "p", and keep the
9737 * remainder (beginning with the path separator). */
9738 remain = vim_strsave(q - 1);
9739 q[-1] = NUL;
9740 }
9741
9742 buf = alloc(MAXPATHL + 1);
9743 if (buf == NULL)
9744 goto fail;
9745
9746 for (;;)
9747 {
9748 for (;;)
9749 {
9750 len = readlink((char *)p, (char *)buf, MAXPATHL);
9751 if (len <= 0)
9752 break;
9753 buf[len] = NUL;
9754
9755 if (limit-- == 0)
9756 {
9757 vim_free(p);
9758 vim_free(remain);
9759 EMSG(_("E655: Too many symbolic links (cycle?)"));
9760 rettv->vval.v_string = NULL;
9761 goto fail;
9762 }
9763
9764 /* Ensure that the result will have a trailing path separator
9765 * if the argument has one. */
9766 if (remain == NULL && has_trailing_pathsep)
9767 add_pathsep(buf);
9768
9769 /* Separate the first path component in the link value and
9770 * concatenate the remainders. */
9771 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9772 if (*q != NUL)
9773 {
9774 if (remain == NULL)
9775 remain = vim_strsave(q - 1);
9776 else
9777 {
9778 cpy = concat_str(q - 1, remain);
9779 if (cpy != NULL)
9780 {
9781 vim_free(remain);
9782 remain = cpy;
9783 }
9784 }
9785 q[-1] = NUL;
9786 }
9787
9788 q = gettail(p);
9789 if (q > p && *q == NUL)
9790 {
9791 /* Ignore trailing path separator. */
9792 q[-1] = NUL;
9793 q = gettail(p);
9794 }
9795 if (q > p && !mch_isFullName(buf))
9796 {
9797 /* symlink is relative to directory of argument */
9798 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9799 if (cpy != NULL)
9800 {
9801 STRCPY(cpy, p);
9802 STRCPY(gettail(cpy), buf);
9803 vim_free(p);
9804 p = cpy;
9805 }
9806 }
9807 else
9808 {
9809 vim_free(p);
9810 p = vim_strsave(buf);
9811 }
9812 }
9813
9814 if (remain == NULL)
9815 break;
9816
9817 /* Append the first path component of "remain" to "p". */
9818 q = getnextcomp(remain + 1);
9819 len = q - remain - (*q != NUL);
9820 cpy = vim_strnsave(p, STRLEN(p) + len);
9821 if (cpy != NULL)
9822 {
9823 STRNCAT(cpy, remain, len);
9824 vim_free(p);
9825 p = cpy;
9826 }
9827 /* Shorten "remain". */
9828 if (*q != NUL)
9829 STRMOVE(remain, q - 1);
9830 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009831 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 }
9833
9834 /* If the result is a relative path name, make it explicitly relative to
9835 * the current directory if and only if the argument had this form. */
9836 if (!vim_ispathsep(*p))
9837 {
9838 if (is_relative_to_current
9839 && *p != NUL
9840 && !(p[0] == '.'
9841 && (p[1] == NUL
9842 || vim_ispathsep(p[1])
9843 || (p[1] == '.'
9844 && (p[2] == NUL
9845 || vim_ispathsep(p[2]))))))
9846 {
9847 /* Prepend "./". */
9848 cpy = concat_str((char_u *)"./", p);
9849 if (cpy != NULL)
9850 {
9851 vim_free(p);
9852 p = cpy;
9853 }
9854 }
9855 else if (!is_relative_to_current)
9856 {
9857 /* Strip leading "./". */
9858 q = p;
9859 while (q[0] == '.' && vim_ispathsep(q[1]))
9860 q += 2;
9861 if (q > p)
9862 STRMOVE(p, p + 2);
9863 }
9864 }
9865
9866 /* Ensure that the result will have no trailing path separator
9867 * if the argument had none. But keep "/" or "//". */
9868 if (!has_trailing_pathsep)
9869 {
9870 q = p + STRLEN(p);
9871 if (after_pathsep(p, q))
9872 *gettail_sep(p) = NUL;
9873 }
9874
9875 rettv->vval.v_string = p;
9876 }
9877# else
9878 rettv->vval.v_string = vim_strsave(p);
9879# endif
9880#endif
9881
9882 simplify_filename(rettv->vval.v_string);
9883
9884#ifdef HAVE_READLINK
9885fail:
9886 vim_free(buf);
9887#endif
9888 rettv->v_type = VAR_STRING;
9889}
9890
9891/*
9892 * "reverse({list})" function
9893 */
9894 static void
9895f_reverse(typval_T *argvars, typval_T *rettv)
9896{
9897 list_T *l;
9898 listitem_T *li, *ni;
9899
9900 if (argvars[0].v_type != VAR_LIST)
9901 EMSG2(_(e_listarg), "reverse()");
9902 else if ((l = argvars[0].vval.v_list) != NULL
9903 && !tv_check_lock(l->lv_lock,
9904 (char_u *)N_("reverse() argument"), TRUE))
9905 {
9906 li = l->lv_last;
9907 l->lv_first = l->lv_last = NULL;
9908 l->lv_len = 0;
9909 while (li != NULL)
9910 {
9911 ni = li->li_prev;
9912 list_append(l, li);
9913 li = ni;
9914 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009915 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 l->lv_idx = l->lv_len - l->lv_idx - 1;
9917 }
9918}
9919
9920#define SP_NOMOVE 0x01 /* don't move cursor */
9921#define SP_REPEAT 0x02 /* repeat to find outer pair */
9922#define SP_RETCOUNT 0x04 /* return matchcount */
9923#define SP_SETPCMARK 0x08 /* set previous context mark */
9924#define SP_START 0x10 /* accept match at start position */
9925#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9926#define SP_END 0x40 /* leave cursor at end of match */
9927#define SP_COLUMN 0x80 /* start at cursor column */
9928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929/*
9930 * Get flags for a search function.
9931 * Possibly sets "p_ws".
9932 * Returns BACKWARD, FORWARD or zero (for an error).
9933 */
9934 static int
9935get_search_arg(typval_T *varp, int *flagsp)
9936{
9937 int dir = FORWARD;
9938 char_u *flags;
9939 char_u nbuf[NUMBUFLEN];
9940 int mask;
9941
9942 if (varp->v_type != VAR_UNKNOWN)
9943 {
9944 flags = get_tv_string_buf_chk(varp, nbuf);
9945 if (flags == NULL)
9946 return 0; /* type error; errmsg already given */
9947 while (*flags != NUL)
9948 {
9949 switch (*flags)
9950 {
9951 case 'b': dir = BACKWARD; break;
9952 case 'w': p_ws = TRUE; break;
9953 case 'W': p_ws = FALSE; break;
9954 default: mask = 0;
9955 if (flagsp != NULL)
9956 switch (*flags)
9957 {
9958 case 'c': mask = SP_START; break;
9959 case 'e': mask = SP_END; break;
9960 case 'm': mask = SP_RETCOUNT; break;
9961 case 'n': mask = SP_NOMOVE; break;
9962 case 'p': mask = SP_SUBPAT; break;
9963 case 'r': mask = SP_REPEAT; break;
9964 case 's': mask = SP_SETPCMARK; break;
9965 case 'z': mask = SP_COLUMN; break;
9966 }
9967 if (mask == 0)
9968 {
9969 EMSG2(_(e_invarg2), flags);
9970 dir = 0;
9971 }
9972 else
9973 *flagsp |= mask;
9974 }
9975 if (dir == 0)
9976 break;
9977 ++flags;
9978 }
9979 }
9980 return dir;
9981}
9982
9983/*
9984 * Shared by search() and searchpos() functions.
9985 */
9986 static int
9987search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9988{
9989 int flags;
9990 char_u *pat;
9991 pos_T pos;
9992 pos_T save_cursor;
9993 int save_p_ws = p_ws;
9994 int dir;
9995 int retval = 0; /* default: FAIL */
9996 long lnum_stop = 0;
9997 proftime_T tm;
9998#ifdef FEAT_RELTIME
9999 long time_limit = 0;
10000#endif
10001 int options = SEARCH_KEEP;
10002 int subpatnum;
10003
10004 pat = get_tv_string(&argvars[0]);
10005 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10006 if (dir == 0)
10007 goto theend;
10008 flags = *flagsp;
10009 if (flags & SP_START)
10010 options |= SEARCH_START;
10011 if (flags & SP_END)
10012 options |= SEARCH_END;
10013 if (flags & SP_COLUMN)
10014 options |= SEARCH_COL;
10015
10016 /* Optional arguments: line number to stop searching and timeout. */
10017 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10018 {
10019 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
10020 if (lnum_stop < 0)
10021 goto theend;
10022#ifdef FEAT_RELTIME
10023 if (argvars[3].v_type != VAR_UNKNOWN)
10024 {
10025 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
10026 if (time_limit < 0)
10027 goto theend;
10028 }
10029#endif
10030 }
10031
10032#ifdef FEAT_RELTIME
10033 /* Set the time limit, if there is one. */
10034 profile_setlimit(time_limit, &tm);
10035#endif
10036
10037 /*
10038 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10039 * Check to make sure only those flags are set.
10040 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10041 * flags cannot be set. Check for that condition also.
10042 */
10043 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10044 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10045 {
10046 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
10047 goto theend;
10048 }
10049
10050 pos = save_cursor = curwin->w_cursor;
10051 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010052 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 if (subpatnum != FAIL)
10054 {
10055 if (flags & SP_SUBPAT)
10056 retval = subpatnum;
10057 else
10058 retval = pos.lnum;
10059 if (flags & SP_SETPCMARK)
10060 setpcmark();
10061 curwin->w_cursor = pos;
10062 if (match_pos != NULL)
10063 {
10064 /* Store the match cursor position */
10065 match_pos->lnum = pos.lnum;
10066 match_pos->col = pos.col + 1;
10067 }
10068 /* "/$" will put the cursor after the end of the line, may need to
10069 * correct that here */
10070 check_cursor();
10071 }
10072
10073 /* If 'n' flag is used: restore cursor position. */
10074 if (flags & SP_NOMOVE)
10075 curwin->w_cursor = save_cursor;
10076 else
10077 curwin->w_set_curswant = TRUE;
10078theend:
10079 p_ws = save_p_ws;
10080
10081 return retval;
10082}
10083
10084#ifdef FEAT_FLOAT
10085
10086/*
10087 * round() is not in C90, use ceil() or floor() instead.
10088 */
10089 float_T
10090vim_round(float_T f)
10091{
10092 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10093}
10094
10095/*
10096 * "round({float})" function
10097 */
10098 static void
10099f_round(typval_T *argvars, typval_T *rettv)
10100{
10101 float_T f = 0.0;
10102
10103 rettv->v_type = VAR_FLOAT;
10104 if (get_float_arg(argvars, &f) == OK)
10105 rettv->vval.v_float = vim_round(f);
10106 else
10107 rettv->vval.v_float = 0.0;
10108}
10109#endif
10110
10111/*
10112 * "screenattr()" function
10113 */
10114 static void
10115f_screenattr(typval_T *argvars, typval_T *rettv)
10116{
10117 int row;
10118 int col;
10119 int c;
10120
10121 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10122 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10123 if (row < 0 || row >= screen_Rows
10124 || col < 0 || col >= screen_Columns)
10125 c = -1;
10126 else
10127 c = ScreenAttrs[LineOffset[row] + col];
10128 rettv->vval.v_number = c;
10129}
10130
10131/*
10132 * "screenchar()" function
10133 */
10134 static void
10135f_screenchar(typval_T *argvars, typval_T *rettv)
10136{
10137 int row;
10138 int col;
10139 int off;
10140 int c;
10141
10142 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10143 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10144 if (row < 0 || row >= screen_Rows
10145 || col < 0 || col >= screen_Columns)
10146 c = -1;
10147 else
10148 {
10149 off = LineOffset[row] + col;
10150#ifdef FEAT_MBYTE
10151 if (enc_utf8 && ScreenLinesUC[off] != 0)
10152 c = ScreenLinesUC[off];
10153 else
10154#endif
10155 c = ScreenLines[off];
10156 }
10157 rettv->vval.v_number = c;
10158}
10159
10160/*
10161 * "screencol()" function
10162 *
10163 * First column is 1 to be consistent with virtcol().
10164 */
10165 static void
10166f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10167{
10168 rettv->vval.v_number = screen_screencol() + 1;
10169}
10170
10171/*
10172 * "screenrow()" function
10173 */
10174 static void
10175f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10176{
10177 rettv->vval.v_number = screen_screenrow() + 1;
10178}
10179
10180/*
10181 * "search()" function
10182 */
10183 static void
10184f_search(typval_T *argvars, typval_T *rettv)
10185{
10186 int flags = 0;
10187
10188 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10189}
10190
10191/*
10192 * "searchdecl()" function
10193 */
10194 static void
10195f_searchdecl(typval_T *argvars, typval_T *rettv)
10196{
10197 int locally = 1;
10198 int thisblock = 0;
10199 int error = FALSE;
10200 char_u *name;
10201
10202 rettv->vval.v_number = 1; /* default: FAIL */
10203
10204 name = get_tv_string_chk(&argvars[0]);
10205 if (argvars[1].v_type != VAR_UNKNOWN)
10206 {
10207 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10208 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10209 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10210 }
10211 if (!error && name != NULL)
10212 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10213 locally, thisblock, SEARCH_KEEP) == FAIL;
10214}
10215
10216/*
10217 * Used by searchpair() and searchpairpos()
10218 */
10219 static int
10220searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10221{
10222 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010223 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010224 int save_p_ws = p_ws;
10225 int dir;
10226 int flags = 0;
10227 char_u nbuf1[NUMBUFLEN];
10228 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010229 int retval = 0; /* default: FAIL */
10230 long lnum_stop = 0;
10231 long time_limit = 0;
10232
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010233 /* Get the three pattern arguments: start, middle, end. Will result in an
10234 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010235 spat = get_tv_string_chk(&argvars[0]);
10236 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10237 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10238 if (spat == NULL || mpat == NULL || epat == NULL)
10239 goto theend; /* type error */
10240
10241 /* Handle the optional fourth argument: flags */
10242 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10243 if (dir == 0)
10244 goto theend;
10245
10246 /* Don't accept SP_END or SP_SUBPAT.
10247 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10248 */
10249 if ((flags & (SP_END | SP_SUBPAT)) != 0
10250 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10251 {
10252 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10253 goto theend;
10254 }
10255
10256 /* Using 'r' implies 'W', otherwise it doesn't work. */
10257 if (flags & SP_REPEAT)
10258 p_ws = FALSE;
10259
10260 /* Optional fifth argument: skip expression */
10261 if (argvars[3].v_type == VAR_UNKNOWN
10262 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010263 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 else
10265 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010266 skip = &argvars[4];
10267 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10268 && skip->v_type != VAR_STRING)
10269 {
10270 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010271 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010272 goto theend;
10273 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010274 if (argvars[5].v_type != VAR_UNKNOWN)
10275 {
10276 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10277 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010278 {
10279 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010281 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282#ifdef FEAT_RELTIME
10283 if (argvars[6].v_type != VAR_UNKNOWN)
10284 {
10285 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10286 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010287 {
10288 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010290 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010291 }
10292#endif
10293 }
10294 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295
10296 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10297 match_pos, lnum_stop, time_limit);
10298
10299theend:
10300 p_ws = save_p_ws;
10301
10302 return retval;
10303}
10304
10305/*
10306 * "searchpair()" function
10307 */
10308 static void
10309f_searchpair(typval_T *argvars, typval_T *rettv)
10310{
10311 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10312}
10313
10314/*
10315 * "searchpairpos()" function
10316 */
10317 static void
10318f_searchpairpos(typval_T *argvars, typval_T *rettv)
10319{
10320 pos_T match_pos;
10321 int lnum = 0;
10322 int col = 0;
10323
10324 if (rettv_list_alloc(rettv) == FAIL)
10325 return;
10326
10327 if (searchpair_cmn(argvars, &match_pos) > 0)
10328 {
10329 lnum = match_pos.lnum;
10330 col = match_pos.col;
10331 }
10332
10333 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10334 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10335}
10336
10337/*
10338 * Search for a start/middle/end thing.
10339 * Used by searchpair(), see its documentation for the details.
10340 * Returns 0 or -1 for no match,
10341 */
10342 long
10343do_searchpair(
10344 char_u *spat, /* start pattern */
10345 char_u *mpat, /* middle pattern */
10346 char_u *epat, /* end pattern */
10347 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010348 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010349 int flags, /* SP_SETPCMARK and other SP_ values */
10350 pos_T *match_pos,
10351 linenr_T lnum_stop, /* stop at this line if not zero */
10352 long time_limit UNUSED) /* stop after this many msec */
10353{
10354 char_u *save_cpo;
10355 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10356 long retval = 0;
10357 pos_T pos;
10358 pos_T firstpos;
10359 pos_T foundpos;
10360 pos_T save_cursor;
10361 pos_T save_pos;
10362 int n;
10363 int r;
10364 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010365 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 int err;
10367 int options = SEARCH_KEEP;
10368 proftime_T tm;
10369
10370 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10371 save_cpo = p_cpo;
10372 p_cpo = empty_option;
10373
10374#ifdef FEAT_RELTIME
10375 /* Set the time limit, if there is one. */
10376 profile_setlimit(time_limit, &tm);
10377#endif
10378
10379 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10380 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010381 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10382 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010383 if (pat2 == NULL || pat3 == NULL)
10384 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010385 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010386 if (*mpat == NUL)
10387 STRCPY(pat3, pat2);
10388 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010389 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010390 spat, epat, mpat);
10391 if (flags & SP_START)
10392 options |= SEARCH_START;
10393
Bram Moolenaar48570482017-10-30 21:48:41 +010010394 if (skip != NULL)
10395 {
10396 /* Empty string means to not use the skip expression. */
10397 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10398 use_skip = skip->vval.v_string != NULL
10399 && *skip->vval.v_string != NUL;
10400 }
10401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 save_cursor = curwin->w_cursor;
10403 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010404 CLEAR_POS(&firstpos);
10405 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010406 pat = pat3;
10407 for (;;)
10408 {
10409 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010410 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010411 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 /* didn't find it or found the first match again: FAIL */
10413 break;
10414
10415 if (firstpos.lnum == 0)
10416 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010417 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 {
10419 /* Found the same position again. Can happen with a pattern that
10420 * has "\zs" at the end and searching backwards. Advance one
10421 * character and try again. */
10422 if (dir == BACKWARD)
10423 decl(&pos);
10424 else
10425 incl(&pos);
10426 }
10427 foundpos = pos;
10428
10429 /* clear the start flag to avoid getting stuck here */
10430 options &= ~SEARCH_START;
10431
10432 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010433 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010434 {
10435 save_pos = curwin->w_cursor;
10436 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010437 err = FALSE;
10438 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010439 curwin->w_cursor = save_pos;
10440 if (err)
10441 {
10442 /* Evaluating {skip} caused an error, break here. */
10443 curwin->w_cursor = save_cursor;
10444 retval = -1;
10445 break;
10446 }
10447 if (r)
10448 continue;
10449 }
10450
10451 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10452 {
10453 /* Found end when searching backwards or start when searching
10454 * forward: nested pair. */
10455 ++nest;
10456 pat = pat2; /* nested, don't search for middle */
10457 }
10458 else
10459 {
10460 /* Found end when searching forward or start when searching
10461 * backward: end of (nested) pair; or found middle in outer pair. */
10462 if (--nest == 1)
10463 pat = pat3; /* outer level, search for middle */
10464 }
10465
10466 if (nest == 0)
10467 {
10468 /* Found the match: return matchcount or line number. */
10469 if (flags & SP_RETCOUNT)
10470 ++retval;
10471 else
10472 retval = pos.lnum;
10473 if (flags & SP_SETPCMARK)
10474 setpcmark();
10475 curwin->w_cursor = pos;
10476 if (!(flags & SP_REPEAT))
10477 break;
10478 nest = 1; /* search for next unmatched */
10479 }
10480 }
10481
10482 if (match_pos != NULL)
10483 {
10484 /* Store the match cursor position */
10485 match_pos->lnum = curwin->w_cursor.lnum;
10486 match_pos->col = curwin->w_cursor.col + 1;
10487 }
10488
10489 /* If 'n' flag is used or search failed: restore cursor position. */
10490 if ((flags & SP_NOMOVE) || retval == 0)
10491 curwin->w_cursor = save_cursor;
10492
10493theend:
10494 vim_free(pat2);
10495 vim_free(pat3);
10496 if (p_cpo == empty_option)
10497 p_cpo = save_cpo;
10498 else
10499 /* Darn, evaluating the {skip} expression changed the value. */
10500 free_string_option(save_cpo);
10501
10502 return retval;
10503}
10504
10505/*
10506 * "searchpos()" function
10507 */
10508 static void
10509f_searchpos(typval_T *argvars, typval_T *rettv)
10510{
10511 pos_T match_pos;
10512 int lnum = 0;
10513 int col = 0;
10514 int n;
10515 int flags = 0;
10516
10517 if (rettv_list_alloc(rettv) == FAIL)
10518 return;
10519
10520 n = search_cmn(argvars, &match_pos, &flags);
10521 if (n > 0)
10522 {
10523 lnum = match_pos.lnum;
10524 col = match_pos.col;
10525 }
10526
10527 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10528 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10529 if (flags & SP_SUBPAT)
10530 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10531}
10532
10533 static void
10534f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10535{
10536#ifdef FEAT_CLIENTSERVER
10537 char_u buf[NUMBUFLEN];
10538 char_u *server = get_tv_string_chk(&argvars[0]);
10539 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10540
10541 rettv->vval.v_number = -1;
10542 if (server == NULL || reply == NULL)
10543 return;
10544 if (check_restricted() || check_secure())
10545 return;
10546# ifdef FEAT_X11
10547 if (check_connection() == FAIL)
10548 return;
10549# endif
10550
10551 if (serverSendReply(server, reply) < 0)
10552 {
10553 EMSG(_("E258: Unable to send to client"));
10554 return;
10555 }
10556 rettv->vval.v_number = 0;
10557#else
10558 rettv->vval.v_number = -1;
10559#endif
10560}
10561
10562 static void
10563f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10564{
10565 char_u *r = NULL;
10566
10567#ifdef FEAT_CLIENTSERVER
10568# ifdef WIN32
10569 r = serverGetVimNames();
10570# else
10571 make_connection();
10572 if (X_DISPLAY != NULL)
10573 r = serverGetVimNames(X_DISPLAY);
10574# endif
10575#endif
10576 rettv->v_type = VAR_STRING;
10577 rettv->vval.v_string = r;
10578}
10579
10580/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010581 * "setbufline()" function
10582 */
10583 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010584f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010585{
10586 linenr_T lnum;
10587 buf_T *buf;
10588
10589 buf = get_buf_tv(&argvars[0], FALSE);
10590 if (buf == NULL)
10591 rettv->vval.v_number = 1; /* FAIL */
10592 else
10593 {
10594 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010595 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010596 }
10597}
10598
10599/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 * "setbufvar()" function
10601 */
10602 static void
10603f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10604{
10605 buf_T *buf;
10606 char_u *varname, *bufvarname;
10607 typval_T *varp;
10608 char_u nbuf[NUMBUFLEN];
10609
10610 if (check_restricted() || check_secure())
10611 return;
10612 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10613 varname = get_tv_string_chk(&argvars[1]);
10614 buf = get_buf_tv(&argvars[0], FALSE);
10615 varp = &argvars[2];
10616
10617 if (buf != NULL && varname != NULL && varp != NULL)
10618 {
10619 if (*varname == '&')
10620 {
10621 long numval;
10622 char_u *strval;
10623 int error = FALSE;
10624 aco_save_T aco;
10625
10626 /* set curbuf to be our buf, temporarily */
10627 aucmd_prepbuf(&aco, buf);
10628
10629 ++varname;
10630 numval = (long)get_tv_number_chk(varp, &error);
10631 strval = get_tv_string_buf_chk(varp, nbuf);
10632 if (!error && strval != NULL)
10633 set_option_value(varname, numval, strval, OPT_LOCAL);
10634
10635 /* reset notion of buffer */
10636 aucmd_restbuf(&aco);
10637 }
10638 else
10639 {
10640 buf_T *save_curbuf = curbuf;
10641
10642 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10643 if (bufvarname != NULL)
10644 {
10645 curbuf = buf;
10646 STRCPY(bufvarname, "b:");
10647 STRCPY(bufvarname + 2, varname);
10648 set_var(bufvarname, varp, TRUE);
10649 vim_free(bufvarname);
10650 curbuf = save_curbuf;
10651 }
10652 }
10653 }
10654}
10655
10656 static void
10657f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10658{
10659 dict_T *d;
10660 dictitem_T *di;
10661 char_u *csearch;
10662
10663 if (argvars[0].v_type != VAR_DICT)
10664 {
10665 EMSG(_(e_dictreq));
10666 return;
10667 }
10668
10669 if ((d = argvars[0].vval.v_dict) != NULL)
10670 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010671 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672 if (csearch != NULL)
10673 {
10674#ifdef FEAT_MBYTE
10675 if (enc_utf8)
10676 {
10677 int pcc[MAX_MCO];
10678 int c = utfc_ptr2char(csearch, pcc);
10679
10680 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10681 }
10682 else
10683#endif
10684 set_last_csearch(PTR2CHAR(csearch),
10685 csearch, MB_PTR2LEN(csearch));
10686 }
10687
10688 di = dict_find(d, (char_u *)"forward", -1);
10689 if (di != NULL)
10690 set_csearch_direction((int)get_tv_number(&di->di_tv)
10691 ? FORWARD : BACKWARD);
10692
10693 di = dict_find(d, (char_u *)"until", -1);
10694 if (di != NULL)
10695 set_csearch_until(!!get_tv_number(&di->di_tv));
10696 }
10697}
10698
10699/*
10700 * "setcmdpos()" function
10701 */
10702 static void
10703f_setcmdpos(typval_T *argvars, typval_T *rettv)
10704{
10705 int pos = (int)get_tv_number(&argvars[0]) - 1;
10706
10707 if (pos >= 0)
10708 rettv->vval.v_number = set_cmdline_pos(pos);
10709}
10710
10711/*
10712 * "setfperm({fname}, {mode})" function
10713 */
10714 static void
10715f_setfperm(typval_T *argvars, typval_T *rettv)
10716{
10717 char_u *fname;
10718 char_u modebuf[NUMBUFLEN];
10719 char_u *mode_str;
10720 int i;
10721 int mask;
10722 int mode = 0;
10723
10724 rettv->vval.v_number = 0;
10725 fname = get_tv_string_chk(&argvars[0]);
10726 if (fname == NULL)
10727 return;
10728 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10729 if (mode_str == NULL)
10730 return;
10731 if (STRLEN(mode_str) != 9)
10732 {
10733 EMSG2(_(e_invarg2), mode_str);
10734 return;
10735 }
10736
10737 mask = 1;
10738 for (i = 8; i >= 0; --i)
10739 {
10740 if (mode_str[i] != '-')
10741 mode |= mask;
10742 mask = mask << 1;
10743 }
10744 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10745}
10746
10747/*
10748 * "setline()" function
10749 */
10750 static void
10751f_setline(typval_T *argvars, typval_T *rettv)
10752{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010753 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754
Bram Moolenaarca851592018-06-06 21:04:07 +020010755 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010756}
10757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010758/*
10759 * Used by "setqflist()" and "setloclist()" functions
10760 */
10761 static void
10762set_qf_ll_list(
10763 win_T *wp UNUSED,
10764 typval_T *list_arg UNUSED,
10765 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010766 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010767 typval_T *rettv)
10768{
10769#ifdef FEAT_QUICKFIX
10770 static char *e_invact = N_("E927: Invalid action: '%s'");
10771 char_u *act;
10772 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010773 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774#endif
10775
10776 rettv->vval.v_number = -1;
10777
10778#ifdef FEAT_QUICKFIX
10779 if (list_arg->v_type != VAR_LIST)
10780 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010781 else if (recursive != 0)
10782 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 else
10784 {
10785 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010786 dict_T *d = NULL;
10787 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010788
10789 if (action_arg->v_type == VAR_STRING)
10790 {
10791 act = get_tv_string_chk(action_arg);
10792 if (act == NULL)
10793 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010794 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10795 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796 action = *act;
10797 else
10798 EMSG2(_(e_invact), act);
10799 }
10800 else if (action_arg->v_type == VAR_UNKNOWN)
10801 action = ' ';
10802 else
10803 EMSG(_(e_stringreq));
10804
Bram Moolenaard823fa92016-08-12 16:29:27 +020010805 if (action_arg->v_type != VAR_UNKNOWN
10806 && what_arg->v_type != VAR_UNKNOWN)
10807 {
10808 if (what_arg->v_type == VAR_DICT)
10809 d = what_arg->vval.v_dict;
10810 else
10811 {
10812 EMSG(_(e_dictreq));
10813 valid_dict = FALSE;
10814 }
10815 }
10816
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010817 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010818 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010819 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10820 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010822 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010823 }
10824#endif
10825}
10826
10827/*
10828 * "setloclist()" function
10829 */
10830 static void
10831f_setloclist(typval_T *argvars, typval_T *rettv)
10832{
10833 win_T *win;
10834
10835 rettv->vval.v_number = -1;
10836
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010837 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010839 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840}
10841
10842/*
10843 * "setmatches()" function
10844 */
10845 static void
10846f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10847{
10848#ifdef FEAT_SEARCH_EXTRA
10849 list_T *l;
10850 listitem_T *li;
10851 dict_T *d;
10852 list_T *s = NULL;
10853
10854 rettv->vval.v_number = -1;
10855 if (argvars[0].v_type != VAR_LIST)
10856 {
10857 EMSG(_(e_listreq));
10858 return;
10859 }
10860 if ((l = argvars[0].vval.v_list) != NULL)
10861 {
10862
10863 /* To some extent make sure that we are dealing with a list from
10864 * "getmatches()". */
10865 li = l->lv_first;
10866 while (li != NULL)
10867 {
10868 if (li->li_tv.v_type != VAR_DICT
10869 || (d = li->li_tv.vval.v_dict) == NULL)
10870 {
10871 EMSG(_(e_invarg));
10872 return;
10873 }
10874 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10875 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10876 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10877 && dict_find(d, (char_u *)"priority", -1) != NULL
10878 && dict_find(d, (char_u *)"id", -1) != NULL))
10879 {
10880 EMSG(_(e_invarg));
10881 return;
10882 }
10883 li = li->li_next;
10884 }
10885
10886 clear_matches(curwin);
10887 li = l->lv_first;
10888 while (li != NULL)
10889 {
10890 int i = 0;
10891 char_u buf[5];
10892 dictitem_T *di;
10893 char_u *group;
10894 int priority;
10895 int id;
10896 char_u *conceal;
10897
10898 d = li->li_tv.vval.v_dict;
10899 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10900 {
10901 if (s == NULL)
10902 {
10903 s = list_alloc();
10904 if (s == NULL)
10905 return;
10906 }
10907
10908 /* match from matchaddpos() */
10909 for (i = 1; i < 9; i++)
10910 {
10911 sprintf((char *)buf, (char *)"pos%d", i);
10912 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10913 {
10914 if (di->di_tv.v_type != VAR_LIST)
10915 return;
10916
10917 list_append_tv(s, &di->di_tv);
10918 s->lv_refcount++;
10919 }
10920 else
10921 break;
10922 }
10923 }
10924
Bram Moolenaar8f667172018-12-14 15:38:31 +010010925 group = dict_get_string(d, (char_u *)"group", TRUE);
10926 priority = (int)dict_get_number(d, (char_u *)"priority");
10927 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010928 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010010929 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010930 : NULL;
10931 if (i == 0)
10932 {
10933 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010010934 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 priority, id, NULL, conceal);
10936 }
10937 else
10938 {
10939 match_add(curwin, group, NULL, priority, id, s, conceal);
10940 list_unref(s);
10941 s = NULL;
10942 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010943 vim_free(group);
10944 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010945
10946 li = li->li_next;
10947 }
10948 rettv->vval.v_number = 0;
10949 }
10950#endif
10951}
10952
10953/*
10954 * "setpos()" function
10955 */
10956 static void
10957f_setpos(typval_T *argvars, typval_T *rettv)
10958{
10959 pos_T pos;
10960 int fnum;
10961 char_u *name;
10962 colnr_T curswant = -1;
10963
10964 rettv->vval.v_number = -1;
10965 name = get_tv_string_chk(argvars);
10966 if (name != NULL)
10967 {
10968 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10969 {
10970 if (--pos.col < 0)
10971 pos.col = 0;
10972 if (name[0] == '.' && name[1] == NUL)
10973 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010974 /* set cursor; "fnum" is ignored */
10975 curwin->w_cursor = pos;
10976 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010978 curwin->w_curswant = curswant - 1;
10979 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010981 check_cursor();
10982 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 }
10984 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10985 {
10986 /* set mark */
10987 if (setmark_pos(name[1], &pos, fnum) == OK)
10988 rettv->vval.v_number = 0;
10989 }
10990 else
10991 EMSG(_(e_invarg));
10992 }
10993 }
10994}
10995
10996/*
10997 * "setqflist()" function
10998 */
10999 static void
11000f_setqflist(typval_T *argvars, typval_T *rettv)
11001{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011002 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003}
11004
11005/*
11006 * "setreg()" function
11007 */
11008 static void
11009f_setreg(typval_T *argvars, typval_T *rettv)
11010{
11011 int regname;
11012 char_u *strregname;
11013 char_u *stropt;
11014 char_u *strval;
11015 int append;
11016 char_u yank_type;
11017 long block_len;
11018
11019 block_len = -1;
11020 yank_type = MAUTO;
11021 append = FALSE;
11022
11023 strregname = get_tv_string_chk(argvars);
11024 rettv->vval.v_number = 1; /* FAIL is default */
11025
11026 if (strregname == NULL)
11027 return; /* type error; errmsg already given */
11028 regname = *strregname;
11029 if (regname == 0 || regname == '@')
11030 regname = '"';
11031
11032 if (argvars[2].v_type != VAR_UNKNOWN)
11033 {
11034 stropt = get_tv_string_chk(&argvars[2]);
11035 if (stropt == NULL)
11036 return; /* type error */
11037 for (; *stropt != NUL; ++stropt)
11038 switch (*stropt)
11039 {
11040 case 'a': case 'A': /* append */
11041 append = TRUE;
11042 break;
11043 case 'v': case 'c': /* character-wise selection */
11044 yank_type = MCHAR;
11045 break;
11046 case 'V': case 'l': /* line-wise selection */
11047 yank_type = MLINE;
11048 break;
11049 case 'b': case Ctrl_V: /* block-wise selection */
11050 yank_type = MBLOCK;
11051 if (VIM_ISDIGIT(stropt[1]))
11052 {
11053 ++stropt;
11054 block_len = getdigits(&stropt) - 1;
11055 --stropt;
11056 }
11057 break;
11058 }
11059 }
11060
11061 if (argvars[1].v_type == VAR_LIST)
11062 {
11063 char_u **lstval;
11064 char_u **allocval;
11065 char_u buf[NUMBUFLEN];
11066 char_u **curval;
11067 char_u **curallocval;
11068 list_T *ll = argvars[1].vval.v_list;
11069 listitem_T *li;
11070 int len;
11071
11072 /* If the list is NULL handle like an empty list. */
11073 len = ll == NULL ? 0 : ll->lv_len;
11074
11075 /* First half: use for pointers to result lines; second half: use for
11076 * pointers to allocated copies. */
11077 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11078 if (lstval == NULL)
11079 return;
11080 curval = lstval;
11081 allocval = lstval + len + 2;
11082 curallocval = allocval;
11083
11084 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11085 li = li->li_next)
11086 {
11087 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11088 if (strval == NULL)
11089 goto free_lstval;
11090 if (strval == buf)
11091 {
11092 /* Need to make a copy, next get_tv_string_buf_chk() will
11093 * overwrite the string. */
11094 strval = vim_strsave(buf);
11095 if (strval == NULL)
11096 goto free_lstval;
11097 *curallocval++ = strval;
11098 }
11099 *curval++ = strval;
11100 }
11101 *curval++ = NULL;
11102
11103 write_reg_contents_lst(regname, lstval, -1,
11104 append, yank_type, block_len);
11105free_lstval:
11106 while (curallocval > allocval)
11107 vim_free(*--curallocval);
11108 vim_free(lstval);
11109 }
11110 else
11111 {
11112 strval = get_tv_string_chk(&argvars[1]);
11113 if (strval == NULL)
11114 return;
11115 write_reg_contents_ex(regname, strval, -1,
11116 append, yank_type, block_len);
11117 }
11118 rettv->vval.v_number = 0;
11119}
11120
11121/*
11122 * "settabvar()" function
11123 */
11124 static void
11125f_settabvar(typval_T *argvars, typval_T *rettv)
11126{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 tabpage_T *save_curtab;
11128 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 char_u *varname, *tabvarname;
11130 typval_T *varp;
11131
11132 rettv->vval.v_number = 0;
11133
11134 if (check_restricted() || check_secure())
11135 return;
11136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 varname = get_tv_string_chk(&argvars[1]);
11139 varp = &argvars[2];
11140
Bram Moolenaar4033c552017-09-16 20:54:51 +020011141 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143 save_curtab = curtab;
11144 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011145
11146 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11147 if (tabvarname != NULL)
11148 {
11149 STRCPY(tabvarname, "t:");
11150 STRCPY(tabvarname + 2, varname);
11151 set_var(tabvarname, varp, TRUE);
11152 vim_free(tabvarname);
11153 }
11154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 /* Restore current tabpage */
11156 if (valid_tabpage(save_curtab))
11157 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158 }
11159}
11160
11161/*
11162 * "settabwinvar()" function
11163 */
11164 static void
11165f_settabwinvar(typval_T *argvars, typval_T *rettv)
11166{
11167 setwinvar(argvars, rettv, 1);
11168}
11169
11170/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011171 * "settagstack()" function
11172 */
11173 static void
11174f_settagstack(typval_T *argvars, typval_T *rettv)
11175{
11176 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11177 win_T *wp;
11178 dict_T *d;
11179 int action = 'r';
11180
11181 rettv->vval.v_number = -1;
11182
11183 // first argument: window number or id
11184 wp = find_win_by_nr_or_id(&argvars[0]);
11185 if (wp == NULL)
11186 return;
11187
11188 // second argument: dict with items to set in the tag stack
11189 if (argvars[1].v_type != VAR_DICT)
11190 {
11191 EMSG(_(e_dictreq));
11192 return;
11193 }
11194 d = argvars[1].vval.v_dict;
11195 if (d == NULL)
11196 return;
11197
11198 // third argument: action - 'a' for append and 'r' for replace.
11199 // default is to replace the stack.
11200 if (argvars[2].v_type == VAR_UNKNOWN)
11201 action = 'r';
11202 else if (argvars[2].v_type == VAR_STRING)
11203 {
11204 char_u *actstr;
11205 actstr = get_tv_string_chk(&argvars[2]);
11206 if (actstr == NULL)
11207 return;
11208 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11209 action = *actstr;
11210 else
11211 {
11212 EMSG2(_(e_invact2), actstr);
11213 return;
11214 }
11215 }
11216 else
11217 {
11218 EMSG(_(e_stringreq));
11219 return;
11220 }
11221
11222 if (set_tagstack(wp, d, action) == OK)
11223 rettv->vval.v_number = 0;
11224}
11225
11226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011227 * "setwinvar()" function
11228 */
11229 static void
11230f_setwinvar(typval_T *argvars, typval_T *rettv)
11231{
11232 setwinvar(argvars, rettv, 0);
11233}
11234
11235#ifdef FEAT_CRYPT
11236/*
11237 * "sha256({string})" function
11238 */
11239 static void
11240f_sha256(typval_T *argvars, typval_T *rettv)
11241{
11242 char_u *p;
11243
11244 p = get_tv_string(&argvars[0]);
11245 rettv->vval.v_string = vim_strsave(
11246 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11247 rettv->v_type = VAR_STRING;
11248}
11249#endif /* FEAT_CRYPT */
11250
11251/*
11252 * "shellescape({string})" function
11253 */
11254 static void
11255f_shellescape(typval_T *argvars, typval_T *rettv)
11256{
Bram Moolenaar20615522017-06-05 18:46:26 +020011257 int do_special = non_zero_arg(&argvars[1]);
11258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011259 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011260 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011261 rettv->v_type = VAR_STRING;
11262}
11263
11264/*
11265 * shiftwidth() function
11266 */
11267 static void
11268f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11269{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011270 rettv->vval.v_number = 0;
11271
11272 if (argvars[0].v_type != VAR_UNKNOWN)
11273 {
11274 long col;
11275
11276 col = (long)get_tv_number_chk(argvars, NULL);
11277 if (col < 0)
11278 return; // type error; errmsg already given
11279#ifdef FEAT_VARTABS
11280 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11281 return;
11282#endif
11283 }
11284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 rettv->vval.v_number = get_sw_value(curbuf);
11286}
11287
11288/*
11289 * "simplify()" function
11290 */
11291 static void
11292f_simplify(typval_T *argvars, typval_T *rettv)
11293{
11294 char_u *p;
11295
11296 p = get_tv_string(&argvars[0]);
11297 rettv->vval.v_string = vim_strsave(p);
11298 simplify_filename(rettv->vval.v_string); /* simplify in place */
11299 rettv->v_type = VAR_STRING;
11300}
11301
11302#ifdef FEAT_FLOAT
11303/*
11304 * "sin()" function
11305 */
11306 static void
11307f_sin(typval_T *argvars, typval_T *rettv)
11308{
11309 float_T f = 0.0;
11310
11311 rettv->v_type = VAR_FLOAT;
11312 if (get_float_arg(argvars, &f) == OK)
11313 rettv->vval.v_float = sin(f);
11314 else
11315 rettv->vval.v_float = 0.0;
11316}
11317
11318/*
11319 * "sinh()" function
11320 */
11321 static void
11322f_sinh(typval_T *argvars, typval_T *rettv)
11323{
11324 float_T f = 0.0;
11325
11326 rettv->v_type = VAR_FLOAT;
11327 if (get_float_arg(argvars, &f) == OK)
11328 rettv->vval.v_float = sinh(f);
11329 else
11330 rettv->vval.v_float = 0.0;
11331}
11332#endif
11333
11334static int
11335#ifdef __BORLANDC__
11336 _RTLENTRYF
11337#endif
11338 item_compare(const void *s1, const void *s2);
11339static int
11340#ifdef __BORLANDC__
11341 _RTLENTRYF
11342#endif
11343 item_compare2(const void *s1, const void *s2);
11344
11345/* struct used in the array that's given to qsort() */
11346typedef struct
11347{
11348 listitem_T *item;
11349 int idx;
11350} sortItem_T;
11351
11352/* struct storing information about current sort */
11353typedef struct
11354{
11355 int item_compare_ic;
11356 int item_compare_numeric;
11357 int item_compare_numbers;
11358#ifdef FEAT_FLOAT
11359 int item_compare_float;
11360#endif
11361 char_u *item_compare_func;
11362 partial_T *item_compare_partial;
11363 dict_T *item_compare_selfdict;
11364 int item_compare_func_err;
11365 int item_compare_keep_zero;
11366} sortinfo_T;
11367static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368#define ITEM_COMPARE_FAIL 999
11369
11370/*
11371 * Compare functions for f_sort() and f_uniq() below.
11372 */
11373 static int
11374#ifdef __BORLANDC__
11375_RTLENTRYF
11376#endif
11377item_compare(const void *s1, const void *s2)
11378{
11379 sortItem_T *si1, *si2;
11380 typval_T *tv1, *tv2;
11381 char_u *p1, *p2;
11382 char_u *tofree1 = NULL, *tofree2 = NULL;
11383 int res;
11384 char_u numbuf1[NUMBUFLEN];
11385 char_u numbuf2[NUMBUFLEN];
11386
11387 si1 = (sortItem_T *)s1;
11388 si2 = (sortItem_T *)s2;
11389 tv1 = &si1->item->li_tv;
11390 tv2 = &si2->item->li_tv;
11391
11392 if (sortinfo->item_compare_numbers)
11393 {
11394 varnumber_T v1 = get_tv_number(tv1);
11395 varnumber_T v2 = get_tv_number(tv2);
11396
11397 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11398 }
11399
11400#ifdef FEAT_FLOAT
11401 if (sortinfo->item_compare_float)
11402 {
11403 float_T v1 = get_tv_float(tv1);
11404 float_T v2 = get_tv_float(tv2);
11405
11406 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11407 }
11408#endif
11409
11410 /* tv2string() puts quotes around a string and allocates memory. Don't do
11411 * that for string variables. Use a single quote when comparing with a
11412 * non-string to do what the docs promise. */
11413 if (tv1->v_type == VAR_STRING)
11414 {
11415 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11416 p1 = (char_u *)"'";
11417 else
11418 p1 = tv1->vval.v_string;
11419 }
11420 else
11421 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11422 if (tv2->v_type == VAR_STRING)
11423 {
11424 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11425 p2 = (char_u *)"'";
11426 else
11427 p2 = tv2->vval.v_string;
11428 }
11429 else
11430 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11431 if (p1 == NULL)
11432 p1 = (char_u *)"";
11433 if (p2 == NULL)
11434 p2 = (char_u *)"";
11435 if (!sortinfo->item_compare_numeric)
11436 {
11437 if (sortinfo->item_compare_ic)
11438 res = STRICMP(p1, p2);
11439 else
11440 res = STRCMP(p1, p2);
11441 }
11442 else
11443 {
11444 double n1, n2;
11445 n1 = strtod((char *)p1, (char **)&p1);
11446 n2 = strtod((char *)p2, (char **)&p2);
11447 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11448 }
11449
11450 /* When the result would be zero, compare the item indexes. Makes the
11451 * sort stable. */
11452 if (res == 0 && !sortinfo->item_compare_keep_zero)
11453 res = si1->idx > si2->idx ? 1 : -1;
11454
11455 vim_free(tofree1);
11456 vim_free(tofree2);
11457 return res;
11458}
11459
11460 static int
11461#ifdef __BORLANDC__
11462_RTLENTRYF
11463#endif
11464item_compare2(const void *s1, const void *s2)
11465{
11466 sortItem_T *si1, *si2;
11467 int res;
11468 typval_T rettv;
11469 typval_T argv[3];
11470 int dummy;
11471 char_u *func_name;
11472 partial_T *partial = sortinfo->item_compare_partial;
11473
11474 /* shortcut after failure in previous call; compare all items equal */
11475 if (sortinfo->item_compare_func_err)
11476 return 0;
11477
11478 si1 = (sortItem_T *)s1;
11479 si2 = (sortItem_T *)s2;
11480
11481 if (partial == NULL)
11482 func_name = sortinfo->item_compare_func;
11483 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011484 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485
11486 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11487 * in the copy without changing the original list items. */
11488 copy_tv(&si1->item->li_tv, &argv[0]);
11489 copy_tv(&si2->item->li_tv, &argv[1]);
11490
11491 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11492 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011493 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 partial, sortinfo->item_compare_selfdict);
11495 clear_tv(&argv[0]);
11496 clear_tv(&argv[1]);
11497
11498 if (res == FAIL)
11499 res = ITEM_COMPARE_FAIL;
11500 else
11501 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11502 if (sortinfo->item_compare_func_err)
11503 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11504 clear_tv(&rettv);
11505
11506 /* When the result would be zero, compare the pointers themselves. Makes
11507 * the sort stable. */
11508 if (res == 0 && !sortinfo->item_compare_keep_zero)
11509 res = si1->idx > si2->idx ? 1 : -1;
11510
11511 return res;
11512}
11513
11514/*
11515 * "sort({list})" function
11516 */
11517 static void
11518do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11519{
11520 list_T *l;
11521 listitem_T *li;
11522 sortItem_T *ptrs;
11523 sortinfo_T *old_sortinfo;
11524 sortinfo_T info;
11525 long len;
11526 long i;
11527
11528 /* Pointer to current info struct used in compare function. Save and
11529 * restore the current one for nested calls. */
11530 old_sortinfo = sortinfo;
11531 sortinfo = &info;
11532
11533 if (argvars[0].v_type != VAR_LIST)
11534 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11535 else
11536 {
11537 l = argvars[0].vval.v_list;
11538 if (l == NULL || tv_check_lock(l->lv_lock,
11539 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11540 TRUE))
11541 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011542 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543
11544 len = list_len(l);
11545 if (len <= 1)
11546 goto theend; /* short list sorts pretty quickly */
11547
11548 info.item_compare_ic = FALSE;
11549 info.item_compare_numeric = FALSE;
11550 info.item_compare_numbers = FALSE;
11551#ifdef FEAT_FLOAT
11552 info.item_compare_float = FALSE;
11553#endif
11554 info.item_compare_func = NULL;
11555 info.item_compare_partial = NULL;
11556 info.item_compare_selfdict = NULL;
11557 if (argvars[1].v_type != VAR_UNKNOWN)
11558 {
11559 /* optional second argument: {func} */
11560 if (argvars[1].v_type == VAR_FUNC)
11561 info.item_compare_func = argvars[1].vval.v_string;
11562 else if (argvars[1].v_type == VAR_PARTIAL)
11563 info.item_compare_partial = argvars[1].vval.v_partial;
11564 else
11565 {
11566 int error = FALSE;
11567
11568 i = (long)get_tv_number_chk(&argvars[1], &error);
11569 if (error)
11570 goto theend; /* type error; errmsg already given */
11571 if (i == 1)
11572 info.item_compare_ic = TRUE;
11573 else if (argvars[1].v_type != VAR_NUMBER)
11574 info.item_compare_func = get_tv_string(&argvars[1]);
11575 else if (i != 0)
11576 {
11577 EMSG(_(e_invarg));
11578 goto theend;
11579 }
11580 if (info.item_compare_func != NULL)
11581 {
11582 if (*info.item_compare_func == NUL)
11583 {
11584 /* empty string means default sort */
11585 info.item_compare_func = NULL;
11586 }
11587 else if (STRCMP(info.item_compare_func, "n") == 0)
11588 {
11589 info.item_compare_func = NULL;
11590 info.item_compare_numeric = TRUE;
11591 }
11592 else if (STRCMP(info.item_compare_func, "N") == 0)
11593 {
11594 info.item_compare_func = NULL;
11595 info.item_compare_numbers = TRUE;
11596 }
11597#ifdef FEAT_FLOAT
11598 else if (STRCMP(info.item_compare_func, "f") == 0)
11599 {
11600 info.item_compare_func = NULL;
11601 info.item_compare_float = TRUE;
11602 }
11603#endif
11604 else if (STRCMP(info.item_compare_func, "i") == 0)
11605 {
11606 info.item_compare_func = NULL;
11607 info.item_compare_ic = TRUE;
11608 }
11609 }
11610 }
11611
11612 if (argvars[2].v_type != VAR_UNKNOWN)
11613 {
11614 /* optional third argument: {dict} */
11615 if (argvars[2].v_type != VAR_DICT)
11616 {
11617 EMSG(_(e_dictreq));
11618 goto theend;
11619 }
11620 info.item_compare_selfdict = argvars[2].vval.v_dict;
11621 }
11622 }
11623
11624 /* Make an array with each entry pointing to an item in the List. */
11625 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11626 if (ptrs == NULL)
11627 goto theend;
11628
11629 i = 0;
11630 if (sort)
11631 {
11632 /* sort(): ptrs will be the list to sort */
11633 for (li = l->lv_first; li != NULL; li = li->li_next)
11634 {
11635 ptrs[i].item = li;
11636 ptrs[i].idx = i;
11637 ++i;
11638 }
11639
11640 info.item_compare_func_err = FALSE;
11641 info.item_compare_keep_zero = FALSE;
11642 /* test the compare function */
11643 if ((info.item_compare_func != NULL
11644 || info.item_compare_partial != NULL)
11645 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11646 == ITEM_COMPARE_FAIL)
11647 EMSG(_("E702: Sort compare function failed"));
11648 else
11649 {
11650 /* Sort the array with item pointers. */
11651 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11652 info.item_compare_func == NULL
11653 && info.item_compare_partial == NULL
11654 ? item_compare : item_compare2);
11655
11656 if (!info.item_compare_func_err)
11657 {
11658 /* Clear the List and append the items in sorted order. */
11659 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11660 l->lv_len = 0;
11661 for (i = 0; i < len; ++i)
11662 list_append(l, ptrs[i].item);
11663 }
11664 }
11665 }
11666 else
11667 {
11668 int (*item_compare_func_ptr)(const void *, const void *);
11669
11670 /* f_uniq(): ptrs will be a stack of items to remove */
11671 info.item_compare_func_err = FALSE;
11672 info.item_compare_keep_zero = TRUE;
11673 item_compare_func_ptr = info.item_compare_func != NULL
11674 || info.item_compare_partial != NULL
11675 ? item_compare2 : item_compare;
11676
11677 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11678 li = li->li_next)
11679 {
11680 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11681 == 0)
11682 ptrs[i++].item = li;
11683 if (info.item_compare_func_err)
11684 {
11685 EMSG(_("E882: Uniq compare function failed"));
11686 break;
11687 }
11688 }
11689
11690 if (!info.item_compare_func_err)
11691 {
11692 while (--i >= 0)
11693 {
11694 li = ptrs[i].item->li_next;
11695 ptrs[i].item->li_next = li->li_next;
11696 if (li->li_next != NULL)
11697 li->li_next->li_prev = ptrs[i].item;
11698 else
11699 l->lv_last = ptrs[i].item;
11700 list_fix_watch(l, li);
11701 listitem_free(li);
11702 l->lv_len--;
11703 }
11704 }
11705 }
11706
11707 vim_free(ptrs);
11708 }
11709theend:
11710 sortinfo = old_sortinfo;
11711}
11712
11713/*
11714 * "sort({list})" function
11715 */
11716 static void
11717f_sort(typval_T *argvars, typval_T *rettv)
11718{
11719 do_sort_uniq(argvars, rettv, TRUE);
11720}
11721
11722/*
11723 * "uniq({list})" function
11724 */
11725 static void
11726f_uniq(typval_T *argvars, typval_T *rettv)
11727{
11728 do_sort_uniq(argvars, rettv, FALSE);
11729}
11730
11731/*
11732 * "soundfold({word})" function
11733 */
11734 static void
11735f_soundfold(typval_T *argvars, typval_T *rettv)
11736{
11737 char_u *s;
11738
11739 rettv->v_type = VAR_STRING;
11740 s = get_tv_string(&argvars[0]);
11741#ifdef FEAT_SPELL
11742 rettv->vval.v_string = eval_soundfold(s);
11743#else
11744 rettv->vval.v_string = vim_strsave(s);
11745#endif
11746}
11747
11748/*
11749 * "spellbadword()" function
11750 */
11751 static void
11752f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11753{
11754 char_u *word = (char_u *)"";
11755 hlf_T attr = HLF_COUNT;
11756 int len = 0;
11757
11758 if (rettv_list_alloc(rettv) == FAIL)
11759 return;
11760
11761#ifdef FEAT_SPELL
11762 if (argvars[0].v_type == VAR_UNKNOWN)
11763 {
11764 /* Find the start and length of the badly spelled word. */
11765 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11766 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011767 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011768 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011769 curwin->w_set_curswant = TRUE;
11770 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011771 }
11772 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11773 {
11774 char_u *str = get_tv_string_chk(&argvars[0]);
11775 int capcol = -1;
11776
11777 if (str != NULL)
11778 {
11779 /* Check the argument for spelling. */
11780 while (*str != NUL)
11781 {
11782 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11783 if (attr != HLF_COUNT)
11784 {
11785 word = str;
11786 break;
11787 }
11788 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011789 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011790 }
11791 }
11792 }
11793#endif
11794
11795 list_append_string(rettv->vval.v_list, word, len);
11796 list_append_string(rettv->vval.v_list, (char_u *)(
11797 attr == HLF_SPB ? "bad" :
11798 attr == HLF_SPR ? "rare" :
11799 attr == HLF_SPL ? "local" :
11800 attr == HLF_SPC ? "caps" :
11801 ""), -1);
11802}
11803
11804/*
11805 * "spellsuggest()" function
11806 */
11807 static void
11808f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11809{
11810#ifdef FEAT_SPELL
11811 char_u *str;
11812 int typeerr = FALSE;
11813 int maxcount;
11814 garray_T ga;
11815 int i;
11816 listitem_T *li;
11817 int need_capital = FALSE;
11818#endif
11819
11820 if (rettv_list_alloc(rettv) == FAIL)
11821 return;
11822
11823#ifdef FEAT_SPELL
11824 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11825 {
11826 str = get_tv_string(&argvars[0]);
11827 if (argvars[1].v_type != VAR_UNKNOWN)
11828 {
11829 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11830 if (maxcount <= 0)
11831 return;
11832 if (argvars[2].v_type != VAR_UNKNOWN)
11833 {
11834 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11835 if (typeerr)
11836 return;
11837 }
11838 }
11839 else
11840 maxcount = 25;
11841
11842 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11843
11844 for (i = 0; i < ga.ga_len; ++i)
11845 {
11846 str = ((char_u **)ga.ga_data)[i];
11847
11848 li = listitem_alloc();
11849 if (li == NULL)
11850 vim_free(str);
11851 else
11852 {
11853 li->li_tv.v_type = VAR_STRING;
11854 li->li_tv.v_lock = 0;
11855 li->li_tv.vval.v_string = str;
11856 list_append(rettv->vval.v_list, li);
11857 }
11858 }
11859 ga_clear(&ga);
11860 }
11861#endif
11862}
11863
11864 static void
11865f_split(typval_T *argvars, typval_T *rettv)
11866{
11867 char_u *str;
11868 char_u *end;
11869 char_u *pat = NULL;
11870 regmatch_T regmatch;
11871 char_u patbuf[NUMBUFLEN];
11872 char_u *save_cpo;
11873 int match;
11874 colnr_T col = 0;
11875 int keepempty = FALSE;
11876 int typeerr = FALSE;
11877
11878 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11879 save_cpo = p_cpo;
11880 p_cpo = (char_u *)"";
11881
11882 str = get_tv_string(&argvars[0]);
11883 if (argvars[1].v_type != VAR_UNKNOWN)
11884 {
11885 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11886 if (pat == NULL)
11887 typeerr = TRUE;
11888 if (argvars[2].v_type != VAR_UNKNOWN)
11889 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11890 }
11891 if (pat == NULL || *pat == NUL)
11892 pat = (char_u *)"[\\x01- ]\\+";
11893
11894 if (rettv_list_alloc(rettv) == FAIL)
11895 return;
11896 if (typeerr)
11897 return;
11898
11899 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11900 if (regmatch.regprog != NULL)
11901 {
11902 regmatch.rm_ic = FALSE;
11903 while (*str != NUL || keepempty)
11904 {
11905 if (*str == NUL)
11906 match = FALSE; /* empty item at the end */
11907 else
11908 match = vim_regexec_nl(&regmatch, str, col);
11909 if (match)
11910 end = regmatch.startp[0];
11911 else
11912 end = str + STRLEN(str);
11913 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11914 && *str != NUL && match && end < regmatch.endp[0]))
11915 {
11916 if (list_append_string(rettv->vval.v_list, str,
11917 (int)(end - str)) == FAIL)
11918 break;
11919 }
11920 if (!match)
11921 break;
11922 /* Advance to just after the match. */
11923 if (regmatch.endp[0] > str)
11924 col = 0;
11925 else
11926 {
11927 /* Don't get stuck at the same match. */
11928#ifdef FEAT_MBYTE
11929 col = (*mb_ptr2len)(regmatch.endp[0]);
11930#else
11931 col = 1;
11932#endif
11933 }
11934 str = regmatch.endp[0];
11935 }
11936
11937 vim_regfree(regmatch.regprog);
11938 }
11939
11940 p_cpo = save_cpo;
11941}
11942
11943#ifdef FEAT_FLOAT
11944/*
11945 * "sqrt()" function
11946 */
11947 static void
11948f_sqrt(typval_T *argvars, typval_T *rettv)
11949{
11950 float_T f = 0.0;
11951
11952 rettv->v_type = VAR_FLOAT;
11953 if (get_float_arg(argvars, &f) == OK)
11954 rettv->vval.v_float = sqrt(f);
11955 else
11956 rettv->vval.v_float = 0.0;
11957}
11958
11959/*
11960 * "str2float()" function
11961 */
11962 static void
11963f_str2float(typval_T *argvars, typval_T *rettv)
11964{
11965 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011966 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011967
Bram Moolenaar08243d22017-01-10 16:12:29 +010011968 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011969 p = skipwhite(p + 1);
11970 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011971 if (isneg)
11972 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973 rettv->v_type = VAR_FLOAT;
11974}
11975#endif
11976
11977/*
11978 * "str2nr()" function
11979 */
11980 static void
11981f_str2nr(typval_T *argvars, typval_T *rettv)
11982{
11983 int base = 10;
11984 char_u *p;
11985 varnumber_T n;
11986 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011987 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011988
11989 if (argvars[1].v_type != VAR_UNKNOWN)
11990 {
11991 base = (int)get_tv_number(&argvars[1]);
11992 if (base != 2 && base != 8 && base != 10 && base != 16)
11993 {
11994 EMSG(_(e_invarg));
11995 return;
11996 }
11997 }
11998
11999 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012000 isneg = (*p == '-');
12001 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012002 p = skipwhite(p + 1);
12003 switch (base)
12004 {
12005 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12006 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12007 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12008 default: what = 0;
12009 }
12010 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012011 if (isneg)
12012 rettv->vval.v_number = -n;
12013 else
12014 rettv->vval.v_number = n;
12015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012016}
12017
12018#ifdef HAVE_STRFTIME
12019/*
12020 * "strftime({format}[, {time}])" function
12021 */
12022 static void
12023f_strftime(typval_T *argvars, typval_T *rettv)
12024{
12025 char_u result_buf[256];
12026 struct tm *curtime;
12027 time_t seconds;
12028 char_u *p;
12029
12030 rettv->v_type = VAR_STRING;
12031
12032 p = get_tv_string(&argvars[0]);
12033 if (argvars[1].v_type == VAR_UNKNOWN)
12034 seconds = time(NULL);
12035 else
12036 seconds = (time_t)get_tv_number(&argvars[1]);
12037 curtime = localtime(&seconds);
12038 /* MSVC returns NULL for an invalid value of seconds. */
12039 if (curtime == NULL)
12040 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12041 else
12042 {
12043# ifdef FEAT_MBYTE
12044 vimconv_T conv;
12045 char_u *enc;
12046
12047 conv.vc_type = CONV_NONE;
12048 enc = enc_locale();
12049 convert_setup(&conv, p_enc, enc);
12050 if (conv.vc_type != CONV_NONE)
12051 p = string_convert(&conv, p, NULL);
12052# endif
12053 if (p != NULL)
12054 (void)strftime((char *)result_buf, sizeof(result_buf),
12055 (char *)p, curtime);
12056 else
12057 result_buf[0] = NUL;
12058
12059# ifdef FEAT_MBYTE
12060 if (conv.vc_type != CONV_NONE)
12061 vim_free(p);
12062 convert_setup(&conv, enc, p_enc);
12063 if (conv.vc_type != CONV_NONE)
12064 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12065 else
12066# endif
12067 rettv->vval.v_string = vim_strsave(result_buf);
12068
12069# ifdef FEAT_MBYTE
12070 /* Release conversion descriptors */
12071 convert_setup(&conv, NULL, NULL);
12072 vim_free(enc);
12073# endif
12074 }
12075}
12076#endif
12077
12078/*
12079 * "strgetchar()" function
12080 */
12081 static void
12082f_strgetchar(typval_T *argvars, typval_T *rettv)
12083{
12084 char_u *str;
12085 int len;
12086 int error = FALSE;
12087 int charidx;
12088
12089 rettv->vval.v_number = -1;
12090 str = get_tv_string_chk(&argvars[0]);
12091 if (str == NULL)
12092 return;
12093 len = (int)STRLEN(str);
12094 charidx = (int)get_tv_number_chk(&argvars[1], &error);
12095 if (error)
12096 return;
12097#ifdef FEAT_MBYTE
12098 {
12099 int byteidx = 0;
12100
12101 while (charidx >= 0 && byteidx < len)
12102 {
12103 if (charidx == 0)
12104 {
12105 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12106 break;
12107 }
12108 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012109 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012110 }
12111 }
12112#else
12113 if (charidx < len)
12114 rettv->vval.v_number = str[charidx];
12115#endif
12116}
12117
12118/*
12119 * "stridx()" function
12120 */
12121 static void
12122f_stridx(typval_T *argvars, typval_T *rettv)
12123{
12124 char_u buf[NUMBUFLEN];
12125 char_u *needle;
12126 char_u *haystack;
12127 char_u *save_haystack;
12128 char_u *pos;
12129 int start_idx;
12130
12131 needle = get_tv_string_chk(&argvars[1]);
12132 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
12133 rettv->vval.v_number = -1;
12134 if (needle == NULL || haystack == NULL)
12135 return; /* type error; errmsg already given */
12136
12137 if (argvars[2].v_type != VAR_UNKNOWN)
12138 {
12139 int error = FALSE;
12140
12141 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
12142 if (error || start_idx >= (int)STRLEN(haystack))
12143 return;
12144 if (start_idx >= 0)
12145 haystack += start_idx;
12146 }
12147
12148 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12149 if (pos != NULL)
12150 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12151}
12152
12153/*
12154 * "string()" function
12155 */
12156 static void
12157f_string(typval_T *argvars, typval_T *rettv)
12158{
12159 char_u *tofree;
12160 char_u numbuf[NUMBUFLEN];
12161
12162 rettv->v_type = VAR_STRING;
12163 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12164 get_copyID());
12165 /* Make a copy if we have a value but it's not in allocated memory. */
12166 if (rettv->vval.v_string != NULL && tofree == NULL)
12167 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12168}
12169
12170/*
12171 * "strlen()" function
12172 */
12173 static void
12174f_strlen(typval_T *argvars, typval_T *rettv)
12175{
12176 rettv->vval.v_number = (varnumber_T)(STRLEN(
12177 get_tv_string(&argvars[0])));
12178}
12179
12180/*
12181 * "strchars()" function
12182 */
12183 static void
12184f_strchars(typval_T *argvars, typval_T *rettv)
12185{
12186 char_u *s = get_tv_string(&argvars[0]);
12187 int skipcc = 0;
12188#ifdef FEAT_MBYTE
12189 varnumber_T len = 0;
12190 int (*func_mb_ptr2char_adv)(char_u **pp);
12191#endif
12192
12193 if (argvars[1].v_type != VAR_UNKNOWN)
12194 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12195 if (skipcc < 0 || skipcc > 1)
12196 EMSG(_(e_invarg));
12197 else
12198 {
12199#ifdef FEAT_MBYTE
12200 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12201 while (*s != NUL)
12202 {
12203 func_mb_ptr2char_adv(&s);
12204 ++len;
12205 }
12206 rettv->vval.v_number = len;
12207#else
12208 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12209#endif
12210 }
12211}
12212
12213/*
12214 * "strdisplaywidth()" function
12215 */
12216 static void
12217f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12218{
12219 char_u *s = get_tv_string(&argvars[0]);
12220 int col = 0;
12221
12222 if (argvars[1].v_type != VAR_UNKNOWN)
12223 col = (int)get_tv_number(&argvars[1]);
12224
12225 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12226}
12227
12228/*
12229 * "strwidth()" function
12230 */
12231 static void
12232f_strwidth(typval_T *argvars, typval_T *rettv)
12233{
12234 char_u *s = get_tv_string(&argvars[0]);
12235
12236 rettv->vval.v_number = (varnumber_T)(
12237#ifdef FEAT_MBYTE
12238 mb_string2cells(s, -1)
12239#else
12240 STRLEN(s)
12241#endif
12242 );
12243}
12244
12245/*
12246 * "strcharpart()" function
12247 */
12248 static void
12249f_strcharpart(typval_T *argvars, typval_T *rettv)
12250{
12251#ifdef FEAT_MBYTE
12252 char_u *p;
12253 int nchar;
12254 int nbyte = 0;
12255 int charlen;
12256 int len = 0;
12257 int slen;
12258 int error = FALSE;
12259
12260 p = get_tv_string(&argvars[0]);
12261 slen = (int)STRLEN(p);
12262
12263 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12264 if (!error)
12265 {
12266 if (nchar > 0)
12267 while (nchar > 0 && nbyte < slen)
12268 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012269 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012270 --nchar;
12271 }
12272 else
12273 nbyte = nchar;
12274 if (argvars[2].v_type != VAR_UNKNOWN)
12275 {
12276 charlen = (int)get_tv_number(&argvars[2]);
12277 while (charlen > 0 && nbyte + len < slen)
12278 {
12279 int off = nbyte + len;
12280
12281 if (off < 0)
12282 len += 1;
12283 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012284 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012285 --charlen;
12286 }
12287 }
12288 else
12289 len = slen - nbyte; /* default: all bytes that are available. */
12290 }
12291
12292 /*
12293 * Only return the overlap between the specified part and the actual
12294 * string.
12295 */
12296 if (nbyte < 0)
12297 {
12298 len += nbyte;
12299 nbyte = 0;
12300 }
12301 else if (nbyte > slen)
12302 nbyte = slen;
12303 if (len < 0)
12304 len = 0;
12305 else if (nbyte + len > slen)
12306 len = slen - nbyte;
12307
12308 rettv->v_type = VAR_STRING;
12309 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12310#else
12311 f_strpart(argvars, rettv);
12312#endif
12313}
12314
12315/*
12316 * "strpart()" function
12317 */
12318 static void
12319f_strpart(typval_T *argvars, typval_T *rettv)
12320{
12321 char_u *p;
12322 int n;
12323 int len;
12324 int slen;
12325 int error = FALSE;
12326
12327 p = get_tv_string(&argvars[0]);
12328 slen = (int)STRLEN(p);
12329
12330 n = (int)get_tv_number_chk(&argvars[1], &error);
12331 if (error)
12332 len = 0;
12333 else if (argvars[2].v_type != VAR_UNKNOWN)
12334 len = (int)get_tv_number(&argvars[2]);
12335 else
12336 len = slen - n; /* default len: all bytes that are available. */
12337
12338 /*
12339 * Only return the overlap between the specified part and the actual
12340 * string.
12341 */
12342 if (n < 0)
12343 {
12344 len += n;
12345 n = 0;
12346 }
12347 else if (n > slen)
12348 n = slen;
12349 if (len < 0)
12350 len = 0;
12351 else if (n + len > slen)
12352 len = slen - n;
12353
12354 rettv->v_type = VAR_STRING;
12355 rettv->vval.v_string = vim_strnsave(p + n, len);
12356}
12357
12358/*
12359 * "strridx()" function
12360 */
12361 static void
12362f_strridx(typval_T *argvars, typval_T *rettv)
12363{
12364 char_u buf[NUMBUFLEN];
12365 char_u *needle;
12366 char_u *haystack;
12367 char_u *rest;
12368 char_u *lastmatch = NULL;
12369 int haystack_len, end_idx;
12370
12371 needle = get_tv_string_chk(&argvars[1]);
12372 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12373
12374 rettv->vval.v_number = -1;
12375 if (needle == NULL || haystack == NULL)
12376 return; /* type error; errmsg already given */
12377
12378 haystack_len = (int)STRLEN(haystack);
12379 if (argvars[2].v_type != VAR_UNKNOWN)
12380 {
12381 /* Third argument: upper limit for index */
12382 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12383 if (end_idx < 0)
12384 return; /* can never find a match */
12385 }
12386 else
12387 end_idx = haystack_len;
12388
12389 if (*needle == NUL)
12390 {
12391 /* Empty string matches past the end. */
12392 lastmatch = haystack + end_idx;
12393 }
12394 else
12395 {
12396 for (rest = haystack; *rest != '\0'; ++rest)
12397 {
12398 rest = (char_u *)strstr((char *)rest, (char *)needle);
12399 if (rest == NULL || rest > haystack + end_idx)
12400 break;
12401 lastmatch = rest;
12402 }
12403 }
12404
12405 if (lastmatch == NULL)
12406 rettv->vval.v_number = -1;
12407 else
12408 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12409}
12410
12411/*
12412 * "strtrans()" function
12413 */
12414 static void
12415f_strtrans(typval_T *argvars, typval_T *rettv)
12416{
12417 rettv->v_type = VAR_STRING;
12418 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12419}
12420
12421/*
12422 * "submatch()" function
12423 */
12424 static void
12425f_submatch(typval_T *argvars, typval_T *rettv)
12426{
12427 int error = FALSE;
12428 int no;
12429 int retList = 0;
12430
12431 no = (int)get_tv_number_chk(&argvars[0], &error);
12432 if (error)
12433 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012434 if (no < 0 || no >= NSUBEXP)
12435 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012436 EMSGN(_("E935: invalid submatch number: %d"), no);
12437 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012438 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 if (argvars[1].v_type != VAR_UNKNOWN)
12440 retList = (int)get_tv_number_chk(&argvars[1], &error);
12441 if (error)
12442 return;
12443
12444 if (retList == 0)
12445 {
12446 rettv->v_type = VAR_STRING;
12447 rettv->vval.v_string = reg_submatch(no);
12448 }
12449 else
12450 {
12451 rettv->v_type = VAR_LIST;
12452 rettv->vval.v_list = reg_submatch_list(no);
12453 }
12454}
12455
12456/*
12457 * "substitute()" function
12458 */
12459 static void
12460f_substitute(typval_T *argvars, typval_T *rettv)
12461{
12462 char_u patbuf[NUMBUFLEN];
12463 char_u subbuf[NUMBUFLEN];
12464 char_u flagsbuf[NUMBUFLEN];
12465
12466 char_u *str = get_tv_string_chk(&argvars[0]);
12467 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012468 char_u *sub = NULL;
12469 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012470 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12471
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012472 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12473 expr = &argvars[2];
12474 else
12475 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12476
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012478 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12479 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012480 rettv->vval.v_string = NULL;
12481 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012482 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483}
12484
12485/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012486 * "swapinfo(swap_filename)" function
12487 */
12488 static void
12489f_swapinfo(typval_T *argvars, typval_T *rettv)
12490{
12491 if (rettv_dict_alloc(rettv) == OK)
12492 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12493}
12494
12495/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012496 * "swapname(expr)" function
12497 */
12498 static void
12499f_swapname(typval_T *argvars, typval_T *rettv)
12500{
12501 buf_T *buf;
12502
12503 rettv->v_type = VAR_STRING;
12504 buf = get_buf_tv(&argvars[0], FALSE);
12505 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12506 || buf->b_ml.ml_mfp->mf_fname == NULL)
12507 rettv->vval.v_string = NULL;
12508 else
12509 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12510}
12511
12512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012513 * "synID(lnum, col, trans)" function
12514 */
12515 static void
12516f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12517{
12518 int id = 0;
12519#ifdef FEAT_SYN_HL
12520 linenr_T lnum;
12521 colnr_T col;
12522 int trans;
12523 int transerr = FALSE;
12524
12525 lnum = get_tv_lnum(argvars); /* -1 on type error */
12526 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12527 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12528
12529 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12530 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12531 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12532#endif
12533
12534 rettv->vval.v_number = id;
12535}
12536
12537/*
12538 * "synIDattr(id, what [, mode])" function
12539 */
12540 static void
12541f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12542{
12543 char_u *p = NULL;
12544#ifdef FEAT_SYN_HL
12545 int id;
12546 char_u *what;
12547 char_u *mode;
12548 char_u modebuf[NUMBUFLEN];
12549 int modec;
12550
12551 id = (int)get_tv_number(&argvars[0]);
12552 what = get_tv_string(&argvars[1]);
12553 if (argvars[2].v_type != VAR_UNKNOWN)
12554 {
12555 mode = get_tv_string_buf(&argvars[2], modebuf);
12556 modec = TOLOWER_ASC(mode[0]);
12557 if (modec != 't' && modec != 'c' && modec != 'g')
12558 modec = 0; /* replace invalid with current */
12559 }
12560 else
12561 {
12562#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12563 if (USE_24BIT)
12564 modec = 'g';
12565 else
12566#endif
12567 if (t_colors > 1)
12568 modec = 'c';
12569 else
12570 modec = 't';
12571 }
12572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012573 switch (TOLOWER_ASC(what[0]))
12574 {
12575 case 'b':
12576 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12577 p = highlight_color(id, what, modec);
12578 else /* bold */
12579 p = highlight_has_attr(id, HL_BOLD, modec);
12580 break;
12581
12582 case 'f': /* fg[#] or font */
12583 p = highlight_color(id, what, modec);
12584 break;
12585
12586 case 'i':
12587 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12588 p = highlight_has_attr(id, HL_INVERSE, modec);
12589 else /* italic */
12590 p = highlight_has_attr(id, HL_ITALIC, modec);
12591 break;
12592
12593 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012594 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 break;
12596
12597 case 'r': /* reverse */
12598 p = highlight_has_attr(id, HL_INVERSE, modec);
12599 break;
12600
12601 case 's':
12602 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12603 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012604 /* strikeout */
12605 else if (TOLOWER_ASC(what[1]) == 't' &&
12606 TOLOWER_ASC(what[2]) == 'r')
12607 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012608 else /* standout */
12609 p = highlight_has_attr(id, HL_STANDOUT, modec);
12610 break;
12611
12612 case 'u':
12613 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12614 /* underline */
12615 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12616 else
12617 /* undercurl */
12618 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12619 break;
12620 }
12621
12622 if (p != NULL)
12623 p = vim_strsave(p);
12624#endif
12625 rettv->v_type = VAR_STRING;
12626 rettv->vval.v_string = p;
12627}
12628
12629/*
12630 * "synIDtrans(id)" function
12631 */
12632 static void
12633f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12634{
12635 int id;
12636
12637#ifdef FEAT_SYN_HL
12638 id = (int)get_tv_number(&argvars[0]);
12639
12640 if (id > 0)
12641 id = syn_get_final_id(id);
12642 else
12643#endif
12644 id = 0;
12645
12646 rettv->vval.v_number = id;
12647}
12648
12649/*
12650 * "synconcealed(lnum, col)" function
12651 */
12652 static void
12653f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12654{
12655#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12656 linenr_T lnum;
12657 colnr_T col;
12658 int syntax_flags = 0;
12659 int cchar;
12660 int matchid = 0;
12661 char_u str[NUMBUFLEN];
12662#endif
12663
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012664 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665
12666#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12667 lnum = get_tv_lnum(argvars); /* -1 on type error */
12668 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12669
12670 vim_memset(str, NUL, sizeof(str));
12671
12672 if (rettv_list_alloc(rettv) != FAIL)
12673 {
12674 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12675 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12676 && curwin->w_p_cole > 0)
12677 {
12678 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12679 syntax_flags = get_syntax_info(&matchid);
12680
12681 /* get the conceal character */
12682 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12683 {
12684 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012685 if (cchar == NUL && curwin->w_p_cole == 1)
12686 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012687 if (cchar != NUL)
12688 {
12689# ifdef FEAT_MBYTE
12690 if (has_mbyte)
12691 (*mb_char2bytes)(cchar, str);
12692 else
12693# endif
12694 str[0] = cchar;
12695 }
12696 }
12697 }
12698
12699 list_append_number(rettv->vval.v_list,
12700 (syntax_flags & HL_CONCEAL) != 0);
12701 /* -1 to auto-determine strlen */
12702 list_append_string(rettv->vval.v_list, str, -1);
12703 list_append_number(rettv->vval.v_list, matchid);
12704 }
12705#endif
12706}
12707
12708/*
12709 * "synstack(lnum, col)" function
12710 */
12711 static void
12712f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12713{
12714#ifdef FEAT_SYN_HL
12715 linenr_T lnum;
12716 colnr_T col;
12717 int i;
12718 int id;
12719#endif
12720
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012721 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012722
12723#ifdef FEAT_SYN_HL
12724 lnum = get_tv_lnum(argvars); /* -1 on type error */
12725 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12726
12727 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12728 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12729 && rettv_list_alloc(rettv) != FAIL)
12730 {
12731 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12732 for (i = 0; ; ++i)
12733 {
12734 id = syn_get_stack_item(i);
12735 if (id < 0)
12736 break;
12737 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12738 break;
12739 }
12740 }
12741#endif
12742}
12743
12744 static void
12745get_cmd_output_as_rettv(
12746 typval_T *argvars,
12747 typval_T *rettv,
12748 int retlist)
12749{
12750 char_u *res = NULL;
12751 char_u *p;
12752 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 int err = FALSE;
12754 FILE *fd;
12755 list_T *list = NULL;
12756 int flags = SHELL_SILENT;
12757
12758 rettv->v_type = VAR_STRING;
12759 rettv->vval.v_string = NULL;
12760 if (check_restricted() || check_secure())
12761 goto errret;
12762
12763 if (argvars[1].v_type != VAR_UNKNOWN)
12764 {
12765 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012766 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767 * command.
12768 */
12769 if ((infile = vim_tempname('i', TRUE)) == NULL)
12770 {
12771 EMSG(_(e_notmp));
12772 goto errret;
12773 }
12774
12775 fd = mch_fopen((char *)infile, WRITEBIN);
12776 if (fd == NULL)
12777 {
12778 EMSG2(_(e_notopen), infile);
12779 goto errret;
12780 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012781 if (argvars[1].v_type == VAR_NUMBER)
12782 {
12783 linenr_T lnum;
12784 buf_T *buf;
12785
12786 buf = buflist_findnr(argvars[1].vval.v_number);
12787 if (buf == NULL)
12788 {
12789 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012790 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012791 goto errret;
12792 }
12793
12794 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12795 {
12796 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12797 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12798 {
12799 err = TRUE;
12800 break;
12801 }
12802 if (putc(NL, fd) == EOF)
12803 {
12804 err = TRUE;
12805 break;
12806 }
12807 }
12808 }
12809 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810 {
12811 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12812 err = TRUE;
12813 }
12814 else
12815 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012816 size_t len;
12817 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818
12819 p = get_tv_string_buf_chk(&argvars[1], buf);
12820 if (p == NULL)
12821 {
12822 fclose(fd);
12823 goto errret; /* type error; errmsg already given */
12824 }
12825 len = STRLEN(p);
12826 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12827 err = TRUE;
12828 }
12829 if (fclose(fd) != 0)
12830 err = TRUE;
12831 if (err)
12832 {
12833 EMSG(_("E677: Error writing temp file"));
12834 goto errret;
12835 }
12836 }
12837
12838 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12839 * echoes typeahead, that messes up the display. */
12840 if (!msg_silent)
12841 flags += SHELL_COOKED;
12842
12843 if (retlist)
12844 {
12845 int len;
12846 listitem_T *li;
12847 char_u *s = NULL;
12848 char_u *start;
12849 char_u *end;
12850 int i;
12851
12852 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12853 if (res == NULL)
12854 goto errret;
12855
12856 list = list_alloc();
12857 if (list == NULL)
12858 goto errret;
12859
12860 for (i = 0; i < len; ++i)
12861 {
12862 start = res + i;
12863 while (i < len && res[i] != NL)
12864 ++i;
12865 end = res + i;
12866
12867 s = alloc((unsigned)(end - start + 1));
12868 if (s == NULL)
12869 goto errret;
12870
12871 for (p = s; start < end; ++p, ++start)
12872 *p = *start == NUL ? NL : *start;
12873 *p = NUL;
12874
12875 li = listitem_alloc();
12876 if (li == NULL)
12877 {
12878 vim_free(s);
12879 goto errret;
12880 }
12881 li->li_tv.v_type = VAR_STRING;
12882 li->li_tv.v_lock = 0;
12883 li->li_tv.vval.v_string = s;
12884 list_append(list, li);
12885 }
12886
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012887 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888 list = NULL;
12889 }
12890 else
12891 {
12892 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12893#ifdef USE_CR
12894 /* translate <CR> into <NL> */
12895 if (res != NULL)
12896 {
12897 char_u *s;
12898
12899 for (s = res; *s; ++s)
12900 {
12901 if (*s == CAR)
12902 *s = NL;
12903 }
12904 }
12905#else
12906# ifdef USE_CRNL
12907 /* translate <CR><NL> into <NL> */
12908 if (res != NULL)
12909 {
12910 char_u *s, *d;
12911
12912 d = res;
12913 for (s = res; *s; ++s)
12914 {
12915 if (s[0] == CAR && s[1] == NL)
12916 ++s;
12917 *d++ = *s;
12918 }
12919 *d = NUL;
12920 }
12921# endif
12922#endif
12923 rettv->vval.v_string = res;
12924 res = NULL;
12925 }
12926
12927errret:
12928 if (infile != NULL)
12929 {
12930 mch_remove(infile);
12931 vim_free(infile);
12932 }
12933 if (res != NULL)
12934 vim_free(res);
12935 if (list != NULL)
12936 list_free(list);
12937}
12938
12939/*
12940 * "system()" function
12941 */
12942 static void
12943f_system(typval_T *argvars, typval_T *rettv)
12944{
12945 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12946}
12947
12948/*
12949 * "systemlist()" function
12950 */
12951 static void
12952f_systemlist(typval_T *argvars, typval_T *rettv)
12953{
12954 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12955}
12956
12957/*
12958 * "tabpagebuflist()" function
12959 */
12960 static void
12961f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12962{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963 tabpage_T *tp;
12964 win_T *wp = NULL;
12965
12966 if (argvars[0].v_type == VAR_UNKNOWN)
12967 wp = firstwin;
12968 else
12969 {
12970 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12971 if (tp != NULL)
12972 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12973 }
12974 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12975 {
12976 for (; wp != NULL; wp = wp->w_next)
12977 if (list_append_number(rettv->vval.v_list,
12978 wp->w_buffer->b_fnum) == FAIL)
12979 break;
12980 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012981}
12982
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012983/*
12984 * "tabpagenr()" function
12985 */
12986 static void
12987f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12988{
12989 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990 char_u *arg;
12991
12992 if (argvars[0].v_type != VAR_UNKNOWN)
12993 {
12994 arg = get_tv_string_chk(&argvars[0]);
12995 nr = 0;
12996 if (arg != NULL)
12997 {
12998 if (STRCMP(arg, "$") == 0)
12999 nr = tabpage_index(NULL) - 1;
13000 else
13001 EMSG2(_(e_invexpr2), arg);
13002 }
13003 }
13004 else
13005 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006 rettv->vval.v_number = nr;
13007}
13008
13009
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010/*
13011 * Common code for tabpagewinnr() and winnr().
13012 */
13013 static int
13014get_winnr(tabpage_T *tp, typval_T *argvar)
13015{
13016 win_T *twin;
13017 int nr = 1;
13018 win_T *wp;
13019 char_u *arg;
13020
13021 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13022 if (argvar->v_type != VAR_UNKNOWN)
13023 {
13024 arg = get_tv_string_chk(argvar);
13025 if (arg == NULL)
13026 nr = 0; /* type error; errmsg already given */
13027 else if (STRCMP(arg, "$") == 0)
13028 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13029 else if (STRCMP(arg, "#") == 0)
13030 {
13031 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13032 if (twin == NULL)
13033 nr = 0;
13034 }
13035 else
13036 {
13037 EMSG2(_(e_invexpr2), arg);
13038 nr = 0;
13039 }
13040 }
13041
13042 if (nr > 0)
13043 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13044 wp != twin; wp = wp->w_next)
13045 {
13046 if (wp == NULL)
13047 {
13048 /* didn't find it in this tabpage */
13049 nr = 0;
13050 break;
13051 }
13052 ++nr;
13053 }
13054 return nr;
13055}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013056
13057/*
13058 * "tabpagewinnr()" function
13059 */
13060 static void
13061f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13062{
13063 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064 tabpage_T *tp;
13065
13066 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13067 if (tp == NULL)
13068 nr = 0;
13069 else
13070 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 rettv->vval.v_number = nr;
13072}
13073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074/*
13075 * "tagfiles()" function
13076 */
13077 static void
13078f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13079{
13080 char_u *fname;
13081 tagname_T tn;
13082 int first;
13083
13084 if (rettv_list_alloc(rettv) == FAIL)
13085 return;
13086 fname = alloc(MAXPATHL);
13087 if (fname == NULL)
13088 return;
13089
13090 for (first = TRUE; ; first = FALSE)
13091 if (get_tagfname(&tn, first, fname) == FAIL
13092 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13093 break;
13094 tagname_free(&tn);
13095 vim_free(fname);
13096}
13097
13098/*
13099 * "taglist()" function
13100 */
13101 static void
13102f_taglist(typval_T *argvars, typval_T *rettv)
13103{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013104 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 char_u *tag_pattern;
13106
13107 tag_pattern = get_tv_string(&argvars[0]);
13108
13109 rettv->vval.v_number = FALSE;
13110 if (*tag_pattern == NUL)
13111 return;
13112
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013113 if (argvars[1].v_type != VAR_UNKNOWN)
13114 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013116 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117}
13118
13119/*
13120 * "tempname()" function
13121 */
13122 static void
13123f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13124{
13125 static int x = 'A';
13126
13127 rettv->v_type = VAR_STRING;
13128 rettv->vval.v_string = vim_tempname(x, FALSE);
13129
13130 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13131 * names. Skip 'I' and 'O', they are used for shell redirection. */
13132 do
13133 {
13134 if (x == 'Z')
13135 x = '0';
13136 else if (x == '9')
13137 x = 'A';
13138 else
13139 {
13140#ifdef EBCDIC
13141 if (x == 'I')
13142 x = 'J';
13143 else if (x == 'R')
13144 x = 'S';
13145 else
13146#endif
13147 ++x;
13148 }
13149 } while (x == 'I' || x == 'O');
13150}
13151
13152#ifdef FEAT_FLOAT
13153/*
13154 * "tan()" function
13155 */
13156 static void
13157f_tan(typval_T *argvars, typval_T *rettv)
13158{
13159 float_T f = 0.0;
13160
13161 rettv->v_type = VAR_FLOAT;
13162 if (get_float_arg(argvars, &f) == OK)
13163 rettv->vval.v_float = tan(f);
13164 else
13165 rettv->vval.v_float = 0.0;
13166}
13167
13168/*
13169 * "tanh()" function
13170 */
13171 static void
13172f_tanh(typval_T *argvars, typval_T *rettv)
13173{
13174 float_T f = 0.0;
13175
13176 rettv->v_type = VAR_FLOAT;
13177 if (get_float_arg(argvars, &f) == OK)
13178 rettv->vval.v_float = tanh(f);
13179 else
13180 rettv->vval.v_float = 0.0;
13181}
13182#endif
13183
13184/*
13185 * "test_alloc_fail(id, countdown, repeat)" function
13186 */
13187 static void
13188f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13189{
13190 if (argvars[0].v_type != VAR_NUMBER
13191 || argvars[0].vval.v_number <= 0
13192 || argvars[1].v_type != VAR_NUMBER
13193 || argvars[1].vval.v_number < 0
13194 || argvars[2].v_type != VAR_NUMBER)
13195 EMSG(_(e_invarg));
13196 else
13197 {
13198 alloc_fail_id = argvars[0].vval.v_number;
13199 if (alloc_fail_id >= aid_last)
13200 EMSG(_(e_invarg));
13201 alloc_fail_countdown = argvars[1].vval.v_number;
13202 alloc_fail_repeat = argvars[2].vval.v_number;
13203 did_outofmem_msg = FALSE;
13204 }
13205}
13206
13207/*
13208 * "test_autochdir()"
13209 */
13210 static void
13211f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13212{
13213#if defined(FEAT_AUTOCHDIR)
13214 test_autochdir = TRUE;
13215#endif
13216}
13217
13218/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013219 * "test_feedinput()"
13220 */
13221 static void
13222f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13223{
13224#ifdef USE_INPUT_BUF
13225 char_u *val = get_tv_string_chk(&argvars[0]);
13226
13227 if (val != NULL)
13228 {
13229 trash_input_buf();
13230 add_to_input_buf_csi(val, (int)STRLEN(val));
13231 }
13232#endif
13233}
13234
13235/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013236 * "test_option_not_set({name})" function
13237 */
13238 static void
13239f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13240{
13241 char_u *name = (char_u *)"";
13242
13243 if (argvars[0].v_type != VAR_STRING)
13244 EMSG(_(e_invarg));
13245 else
13246 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013247 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013248 if (reset_option_was_set(name) == FAIL)
13249 EMSG2(_(e_invarg2), name);
13250 }
13251}
13252
13253/*
13254 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 */
13256 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013257f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013259 char_u *name = (char_u *)"";
13260 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013261 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013262
13263 if (argvars[0].v_type != VAR_STRING
13264 || (argvars[1].v_type) != VAR_NUMBER)
13265 EMSG(_(e_invarg));
13266 else
13267 {
13268 name = get_tv_string_chk(&argvars[0]);
13269 val = (int)get_tv_number(&argvars[1]);
13270
13271 if (STRCMP(name, (char_u *)"redraw") == 0)
13272 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013273 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13274 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013275 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13276 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013277 else if (STRCMP(name, (char_u *)"starting") == 0)
13278 {
13279 if (val)
13280 {
13281 if (save_starting < 0)
13282 save_starting = starting;
13283 starting = 0;
13284 }
13285 else
13286 {
13287 starting = save_starting;
13288 save_starting = -1;
13289 }
13290 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013291 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13292 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013293 else if (STRCMP(name, (char_u *)"ALL") == 0)
13294 {
13295 disable_char_avail_for_testing = FALSE;
13296 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013297 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013298 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013299 if (save_starting >= 0)
13300 {
13301 starting = save_starting;
13302 save_starting = -1;
13303 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013304 }
13305 else
13306 EMSG2(_(e_invarg2), name);
13307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308}
13309
13310/*
13311 * "test_garbagecollect_now()" function
13312 */
13313 static void
13314f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13315{
13316 /* This is dangerous, any Lists and Dicts used internally may be freed
13317 * while still in use. */
13318 garbage_collect(TRUE);
13319}
13320
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013321/*
13322 * "test_ignore_error()" function
13323 */
13324 static void
13325f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13326{
13327 ignore_error_for_testing(get_tv_string(&argvars[0]));
13328}
13329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330#ifdef FEAT_JOB_CHANNEL
13331 static void
13332f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13333{
13334 rettv->v_type = VAR_CHANNEL;
13335 rettv->vval.v_channel = NULL;
13336}
13337#endif
13338
13339 static void
13340f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13341{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013342 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343}
13344
13345#ifdef FEAT_JOB_CHANNEL
13346 static void
13347f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13348{
13349 rettv->v_type = VAR_JOB;
13350 rettv->vval.v_job = NULL;
13351}
13352#endif
13353
13354 static void
13355f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13356{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013357 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013358}
13359
13360 static void
13361f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13362{
13363 rettv->v_type = VAR_PARTIAL;
13364 rettv->vval.v_partial = NULL;
13365}
13366
13367 static void
13368f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13369{
13370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = NULL;
13372}
13373
Bram Moolenaarab186732018-09-14 21:27:06 +020013374#ifdef FEAT_GUI
13375 static void
13376f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13377{
13378 char_u *which;
13379 long value;
13380 int dragging;
13381 scrollbar_T *sb = NULL;
13382
13383 if (argvars[0].v_type != VAR_STRING
13384 || (argvars[1].v_type) != VAR_NUMBER
13385 || (argvars[2].v_type) != VAR_NUMBER)
13386 {
13387 EMSG(_(e_invarg));
13388 return;
13389 }
13390 which = get_tv_string(&argvars[0]);
13391 value = get_tv_number(&argvars[1]);
13392 dragging = get_tv_number(&argvars[2]);
13393
13394 if (STRCMP(which, "left") == 0)
13395 sb = &curwin->w_scrollbars[SBAR_LEFT];
13396 else if (STRCMP(which, "right") == 0)
13397 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13398 else if (STRCMP(which, "hor") == 0)
13399 sb = &gui.bottom_sbar;
13400 if (sb == NULL)
13401 {
13402 EMSG2(_(e_invarg2), which);
13403 return;
13404 }
13405 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013406# ifndef USE_ON_FLY_SCROLL
13407 // need to loop through normal_cmd() to handle the scroll events
13408 exec_normal(FALSE, TRUE, FALSE);
13409# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013410}
13411#endif
13412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413 static void
13414f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13415{
13416 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13417}
13418
13419#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13420/*
13421 * Get a callback from "arg". It can be a Funcref or a function name.
13422 * When "arg" is zero return an empty string.
13423 * Return NULL for an invalid argument.
13424 */
13425 char_u *
13426get_callback(typval_T *arg, partial_T **pp)
13427{
13428 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13429 {
13430 *pp = arg->vval.v_partial;
13431 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013432 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013433 }
13434 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013435 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 {
13437 func_ref(arg->vval.v_string);
13438 return arg->vval.v_string;
13439 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013440 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13441 return (char_u *)"";
13442 EMSG(_("E921: Invalid callback argument"));
13443 return NULL;
13444}
13445
13446/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013447 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 */
13449 void
13450free_callback(char_u *callback, partial_T *partial)
13451{
13452 if (partial != NULL)
13453 partial_unref(partial);
13454 else if (callback != NULL)
13455 {
13456 func_unref(callback);
13457 vim_free(callback);
13458 }
13459}
13460#endif
13461
13462#ifdef FEAT_TIMERS
13463/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013464 * "timer_info([timer])" function
13465 */
13466 static void
13467f_timer_info(typval_T *argvars, typval_T *rettv)
13468{
13469 timer_T *timer = NULL;
13470
13471 if (rettv_list_alloc(rettv) != OK)
13472 return;
13473 if (argvars[0].v_type != VAR_UNKNOWN)
13474 {
13475 if (argvars[0].v_type != VAR_NUMBER)
13476 EMSG(_(e_number_exp));
13477 else
13478 {
13479 timer = find_timer((int)get_tv_number(&argvars[0]));
13480 if (timer != NULL)
13481 add_timer_info(rettv, timer);
13482 }
13483 }
13484 else
13485 add_timer_info_all(rettv);
13486}
13487
13488/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013489 * "timer_pause(timer, paused)" function
13490 */
13491 static void
13492f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13493{
13494 timer_T *timer = NULL;
13495 int paused = (int)get_tv_number(&argvars[1]);
13496
13497 if (argvars[0].v_type != VAR_NUMBER)
13498 EMSG(_(e_number_exp));
13499 else
13500 {
13501 timer = find_timer((int)get_tv_number(&argvars[0]));
13502 if (timer != NULL)
13503 timer->tr_paused = paused;
13504 }
13505}
13506
13507/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013508 * "timer_start(time, callback [, options])" function
13509 */
13510 static void
13511f_timer_start(typval_T *argvars, typval_T *rettv)
13512{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013513 long msec = (long)get_tv_number(&argvars[0]);
13514 timer_T *timer;
13515 int repeat = 0;
13516 char_u *callback;
13517 dict_T *dict;
13518 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013519
Bram Moolenaar75537a92016-09-05 22:45:28 +020013520 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 if (check_secure())
13522 return;
13523 if (argvars[2].v_type != VAR_UNKNOWN)
13524 {
13525 if (argvars[2].v_type != VAR_DICT
13526 || (dict = argvars[2].vval.v_dict) == NULL)
13527 {
13528 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13529 return;
13530 }
13531 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013532 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013533 }
13534
Bram Moolenaar75537a92016-09-05 22:45:28 +020013535 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013537 return;
13538
13539 timer = create_timer(msec, repeat);
13540 if (timer == NULL)
13541 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 else
13543 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013544 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013545 timer->tr_callback = vim_strsave(callback);
13546 else
13547 /* pointer into the partial */
13548 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013549 timer->tr_partial = partial;
13550 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551 }
13552}
13553
13554/*
13555 * "timer_stop(timer)" function
13556 */
13557 static void
13558f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13559{
13560 timer_T *timer;
13561
13562 if (argvars[0].v_type != VAR_NUMBER)
13563 {
13564 EMSG(_(e_number_exp));
13565 return;
13566 }
13567 timer = find_timer((int)get_tv_number(&argvars[0]));
13568 if (timer != NULL)
13569 stop_timer(timer);
13570}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013571
13572/*
13573 * "timer_stopall()" function
13574 */
13575 static void
13576f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13577{
13578 stop_all_timers();
13579}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013580#endif
13581
13582/*
13583 * "tolower(string)" function
13584 */
13585 static void
13586f_tolower(typval_T *argvars, typval_T *rettv)
13587{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013588 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013589 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013590}
13591
13592/*
13593 * "toupper(string)" function
13594 */
13595 static void
13596f_toupper(typval_T *argvars, typval_T *rettv)
13597{
13598 rettv->v_type = VAR_STRING;
13599 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13600}
13601
13602/*
13603 * "tr(string, fromstr, tostr)" function
13604 */
13605 static void
13606f_tr(typval_T *argvars, typval_T *rettv)
13607{
13608 char_u *in_str;
13609 char_u *fromstr;
13610 char_u *tostr;
13611 char_u *p;
13612#ifdef FEAT_MBYTE
13613 int inlen;
13614 int fromlen;
13615 int tolen;
13616 int idx;
13617 char_u *cpstr;
13618 int cplen;
13619 int first = TRUE;
13620#endif
13621 char_u buf[NUMBUFLEN];
13622 char_u buf2[NUMBUFLEN];
13623 garray_T ga;
13624
13625 in_str = get_tv_string(&argvars[0]);
13626 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13627 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13628
13629 /* Default return value: empty string. */
13630 rettv->v_type = VAR_STRING;
13631 rettv->vval.v_string = NULL;
13632 if (fromstr == NULL || tostr == NULL)
13633 return; /* type error; errmsg already given */
13634 ga_init2(&ga, (int)sizeof(char), 80);
13635
13636#ifdef FEAT_MBYTE
13637 if (!has_mbyte)
13638#endif
13639 /* not multi-byte: fromstr and tostr must be the same length */
13640 if (STRLEN(fromstr) != STRLEN(tostr))
13641 {
13642#ifdef FEAT_MBYTE
13643error:
13644#endif
13645 EMSG2(_(e_invarg2), fromstr);
13646 ga_clear(&ga);
13647 return;
13648 }
13649
13650 /* fromstr and tostr have to contain the same number of chars */
13651 while (*in_str != NUL)
13652 {
13653#ifdef FEAT_MBYTE
13654 if (has_mbyte)
13655 {
13656 inlen = (*mb_ptr2len)(in_str);
13657 cpstr = in_str;
13658 cplen = inlen;
13659 idx = 0;
13660 for (p = fromstr; *p != NUL; p += fromlen)
13661 {
13662 fromlen = (*mb_ptr2len)(p);
13663 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13664 {
13665 for (p = tostr; *p != NUL; p += tolen)
13666 {
13667 tolen = (*mb_ptr2len)(p);
13668 if (idx-- == 0)
13669 {
13670 cplen = tolen;
13671 cpstr = p;
13672 break;
13673 }
13674 }
13675 if (*p == NUL) /* tostr is shorter than fromstr */
13676 goto error;
13677 break;
13678 }
13679 ++idx;
13680 }
13681
13682 if (first && cpstr == in_str)
13683 {
13684 /* Check that fromstr and tostr have the same number of
13685 * (multi-byte) characters. Done only once when a character
13686 * of in_str doesn't appear in fromstr. */
13687 first = FALSE;
13688 for (p = tostr; *p != NUL; p += tolen)
13689 {
13690 tolen = (*mb_ptr2len)(p);
13691 --idx;
13692 }
13693 if (idx != 0)
13694 goto error;
13695 }
13696
13697 (void)ga_grow(&ga, cplen);
13698 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13699 ga.ga_len += cplen;
13700
13701 in_str += inlen;
13702 }
13703 else
13704#endif
13705 {
13706 /* When not using multi-byte chars we can do it faster. */
13707 p = vim_strchr(fromstr, *in_str);
13708 if (p != NULL)
13709 ga_append(&ga, tostr[p - fromstr]);
13710 else
13711 ga_append(&ga, *in_str);
13712 ++in_str;
13713 }
13714 }
13715
13716 /* add a terminating NUL */
13717 (void)ga_grow(&ga, 1);
13718 ga_append(&ga, NUL);
13719
13720 rettv->vval.v_string = ga.ga_data;
13721}
13722
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013723/*
13724 * "trim({expr})" function
13725 */
13726 static void
13727f_trim(typval_T *argvars, typval_T *rettv)
13728{
13729 char_u buf1[NUMBUFLEN];
13730 char_u buf2[NUMBUFLEN];
13731 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13732 char_u *mask = NULL;
13733 char_u *tail;
13734 char_u *prev;
13735 char_u *p;
13736 int c1;
13737
13738 rettv->v_type = VAR_STRING;
13739 if (head == NULL)
13740 {
13741 rettv->vval.v_string = NULL;
13742 return;
13743 }
13744
13745 if (argvars[1].v_type == VAR_STRING)
13746 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13747
13748 while (*head != NUL)
13749 {
13750 c1 = PTR2CHAR(head);
13751 if (mask == NULL)
13752 {
13753 if (c1 > ' ' && c1 != 0xa0)
13754 break;
13755 }
13756 else
13757 {
13758 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13759 if (c1 == PTR2CHAR(p))
13760 break;
13761 if (*p == NUL)
13762 break;
13763 }
13764 MB_PTR_ADV(head);
13765 }
13766
13767 for (tail = head + STRLEN(head); tail > head; tail = prev)
13768 {
13769 prev = tail;
13770 MB_PTR_BACK(head, prev);
13771 c1 = PTR2CHAR(prev);
13772 if (mask == NULL)
13773 {
13774 if (c1 > ' ' && c1 != 0xa0)
13775 break;
13776 }
13777 else
13778 {
13779 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13780 if (c1 == PTR2CHAR(p))
13781 break;
13782 if (*p == NUL)
13783 break;
13784 }
13785 }
13786 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13787}
13788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789#ifdef FEAT_FLOAT
13790/*
13791 * "trunc({float})" function
13792 */
13793 static void
13794f_trunc(typval_T *argvars, typval_T *rettv)
13795{
13796 float_T f = 0.0;
13797
13798 rettv->v_type = VAR_FLOAT;
13799 if (get_float_arg(argvars, &f) == OK)
13800 /* trunc() is not in C90, use floor() or ceil() instead. */
13801 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13802 else
13803 rettv->vval.v_float = 0.0;
13804}
13805#endif
13806
13807/*
13808 * "type(expr)" function
13809 */
13810 static void
13811f_type(typval_T *argvars, typval_T *rettv)
13812{
13813 int n = -1;
13814
13815 switch (argvars[0].v_type)
13816 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013817 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13818 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013819 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013820 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13821 case VAR_LIST: n = VAR_TYPE_LIST; break;
13822 case VAR_DICT: n = VAR_TYPE_DICT; break;
13823 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824 case VAR_SPECIAL:
13825 if (argvars[0].vval.v_number == VVAL_FALSE
13826 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013827 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013829 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013830 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013831 case VAR_JOB: n = VAR_TYPE_JOB; break;
13832 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013833 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013834 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835 n = -1;
13836 break;
13837 }
13838 rettv->vval.v_number = n;
13839}
13840
13841/*
13842 * "undofile(name)" function
13843 */
13844 static void
13845f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13846{
13847 rettv->v_type = VAR_STRING;
13848#ifdef FEAT_PERSISTENT_UNDO
13849 {
13850 char_u *fname = get_tv_string(&argvars[0]);
13851
13852 if (*fname == NUL)
13853 {
13854 /* If there is no file name there will be no undo file. */
13855 rettv->vval.v_string = NULL;
13856 }
13857 else
13858 {
13859 char_u *ffname = FullName_save(fname, FALSE);
13860
13861 if (ffname != NULL)
13862 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13863 vim_free(ffname);
13864 }
13865 }
13866#else
13867 rettv->vval.v_string = NULL;
13868#endif
13869}
13870
13871/*
13872 * "undotree()" function
13873 */
13874 static void
13875f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13876{
13877 if (rettv_dict_alloc(rettv) == OK)
13878 {
13879 dict_T *dict = rettv->vval.v_dict;
13880 list_T *list;
13881
Bram Moolenaare0be1672018-07-08 16:50:37 +020013882 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13883 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13884 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13885 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13886 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13887 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013888
13889 list = list_alloc();
13890 if (list != NULL)
13891 {
13892 u_eval_tree(curbuf->b_u_oldhead, list);
13893 dict_add_list(dict, "entries", list);
13894 }
13895 }
13896}
13897
13898/*
13899 * "values(dict)" function
13900 */
13901 static void
13902f_values(typval_T *argvars, typval_T *rettv)
13903{
13904 dict_list(argvars, rettv, 1);
13905}
13906
13907/*
13908 * "virtcol(string)" function
13909 */
13910 static void
13911f_virtcol(typval_T *argvars, typval_T *rettv)
13912{
13913 colnr_T vcol = 0;
13914 pos_T *fp;
13915 int fnum = curbuf->b_fnum;
13916
13917 fp = var2fpos(&argvars[0], FALSE, &fnum);
13918 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13919 && fnum == curbuf->b_fnum)
13920 {
13921 getvvcol(curwin, fp, NULL, NULL, &vcol);
13922 ++vcol;
13923 }
13924
13925 rettv->vval.v_number = vcol;
13926}
13927
13928/*
13929 * "visualmode()" function
13930 */
13931 static void
13932f_visualmode(typval_T *argvars, typval_T *rettv)
13933{
13934 char_u str[2];
13935
13936 rettv->v_type = VAR_STRING;
13937 str[0] = curbuf->b_visual_mode_eval;
13938 str[1] = NUL;
13939 rettv->vval.v_string = vim_strsave(str);
13940
13941 /* A non-zero number or non-empty string argument: reset mode. */
13942 if (non_zero_arg(&argvars[0]))
13943 curbuf->b_visual_mode_eval = NUL;
13944}
13945
13946/*
13947 * "wildmenumode()" function
13948 */
13949 static void
13950f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13951{
13952#ifdef FEAT_WILDMENU
13953 if (wild_menu_showing)
13954 rettv->vval.v_number = 1;
13955#endif
13956}
13957
13958/*
13959 * "winbufnr(nr)" function
13960 */
13961 static void
13962f_winbufnr(typval_T *argvars, typval_T *rettv)
13963{
13964 win_T *wp;
13965
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013966 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967 if (wp == NULL)
13968 rettv->vval.v_number = -1;
13969 else
13970 rettv->vval.v_number = wp->w_buffer->b_fnum;
13971}
13972
13973/*
13974 * "wincol()" function
13975 */
13976 static void
13977f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13978{
13979 validate_cursor();
13980 rettv->vval.v_number = curwin->w_wcol + 1;
13981}
13982
13983/*
13984 * "winheight(nr)" function
13985 */
13986 static void
13987f_winheight(typval_T *argvars, typval_T *rettv)
13988{
13989 win_T *wp;
13990
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013991 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013992 if (wp == NULL)
13993 rettv->vval.v_number = -1;
13994 else
13995 rettv->vval.v_number = wp->w_height;
13996}
13997
13998/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013999 * "winlayout()" function
14000 */
14001 static void
14002f_winlayout(typval_T *argvars, typval_T *rettv)
14003{
14004 tabpage_T *tp;
14005
14006 if (rettv_list_alloc(rettv) != OK)
14007 return;
14008
14009 if (argvars[0].v_type == VAR_UNKNOWN)
14010 tp = curtab;
14011 else
14012 {
14013 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14014 if (tp == NULL)
14015 return;
14016 }
14017
14018 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14019}
14020
14021/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022 * "winline()" function
14023 */
14024 static void
14025f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14026{
14027 validate_cursor();
14028 rettv->vval.v_number = curwin->w_wrow + 1;
14029}
14030
14031/*
14032 * "winnr()" function
14033 */
14034 static void
14035f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14036{
14037 int nr = 1;
14038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014040 rettv->vval.v_number = nr;
14041}
14042
14043/*
14044 * "winrestcmd()" function
14045 */
14046 static void
14047f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14048{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 win_T *wp;
14050 int winnr = 1;
14051 garray_T ga;
14052 char_u buf[50];
14053
14054 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014055 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 {
14057 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14058 ga_concat(&ga, buf);
14059 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14060 ga_concat(&ga, buf);
14061 ++winnr;
14062 }
14063 ga_append(&ga, NUL);
14064
14065 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014066 rettv->v_type = VAR_STRING;
14067}
14068
14069/*
14070 * "winrestview()" function
14071 */
14072 static void
14073f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14074{
14075 dict_T *dict;
14076
14077 if (argvars[0].v_type != VAR_DICT
14078 || (dict = argvars[0].vval.v_dict) == NULL)
14079 EMSG(_(e_invarg));
14080 else
14081 {
14082 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014083 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014085 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086#ifdef FEAT_VIRTUALEDIT
14087 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014088 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089#endif
14090 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14091 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014092 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014093 curwin->w_set_curswant = FALSE;
14094 }
14095
14096 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014097 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014098#ifdef FEAT_DIFF
14099 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014100 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014101#endif
14102 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014103 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014105 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106
14107 check_cursor();
14108 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014109 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014110 changed_window_setting();
14111
14112 if (curwin->w_topline <= 0)
14113 curwin->w_topline = 1;
14114 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14115 curwin->w_topline = curbuf->b_ml.ml_line_count;
14116#ifdef FEAT_DIFF
14117 check_topfill(curwin, TRUE);
14118#endif
14119 }
14120}
14121
14122/*
14123 * "winsaveview()" function
14124 */
14125 static void
14126f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14127{
14128 dict_T *dict;
14129
14130 if (rettv_dict_alloc(rettv) == FAIL)
14131 return;
14132 dict = rettv->vval.v_dict;
14133
Bram Moolenaare0be1672018-07-08 16:50:37 +020014134 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14135 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014137 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138#endif
14139 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014140 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141
Bram Moolenaare0be1672018-07-08 16:50:37 +020014142 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014144 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014146 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14147 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148}
14149
14150/*
14151 * "winwidth(nr)" function
14152 */
14153 static void
14154f_winwidth(typval_T *argvars, typval_T *rettv)
14155{
14156 win_T *wp;
14157
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014158 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014159 if (wp == NULL)
14160 rettv->vval.v_number = -1;
14161 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014163}
14164
14165/*
14166 * "wordcount()" function
14167 */
14168 static void
14169f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14170{
14171 if (rettv_dict_alloc(rettv) == FAIL)
14172 return;
14173 cursor_pos_info(rettv->vval.v_dict);
14174}
14175
14176/*
14177 * "writefile()" function
14178 */
14179 static void
14180f_writefile(typval_T *argvars, typval_T *rettv)
14181{
14182 int binary = FALSE;
14183 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014184#ifdef HAVE_FSYNC
14185 int do_fsync = p_fs;
14186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014187 char_u *fname;
14188 FILE *fd;
14189 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014190 listitem_T *li;
14191 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014193 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194 if (check_restricted() || check_secure())
14195 return;
14196
14197 if (argvars[0].v_type != VAR_LIST)
14198 {
14199 EMSG2(_(e_listarg), "writefile()");
14200 return;
14201 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014202 list = argvars[0].vval.v_list;
14203 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014204 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014205 for (li = list->lv_first; li != NULL; li = li->li_next)
14206 if (get_tv_string_chk(&li->li_tv) == NULL)
14207 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014208
14209 if (argvars[2].v_type != VAR_UNKNOWN)
14210 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014211 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14212
14213 if (arg2 == NULL)
14214 return;
14215 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014217 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014219#ifdef HAVE_FSYNC
14220 if (vim_strchr(arg2, 's') != NULL)
14221 do_fsync = TRUE;
14222 else if (vim_strchr(arg2, 'S') != NULL)
14223 do_fsync = FALSE;
14224#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014225 }
14226
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014227 fname = get_tv_string_chk(&argvars[1]);
14228 if (fname == NULL)
14229 return;
14230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014231 /* Always open the file in binary mode, library functions have a mind of
14232 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014233 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14234 append ? APPENDBIN : WRITEBIN)) == NULL)
14235 {
14236 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14237 ret = -1;
14238 }
14239 else
14240 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014241 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014242 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014243#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014244 else if (do_fsync)
14245 /* Ignore the error, the user wouldn't know what to do about it.
14246 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014247 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014248#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014249 fclose(fd);
14250 }
14251
14252 rettv->vval.v_number = ret;
14253}
14254
14255/*
14256 * "xor(expr, expr)" function
14257 */
14258 static void
14259f_xor(typval_T *argvars, typval_T *rettv)
14260{
14261 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14262 ^ get_tv_number_chk(&argvars[1], NULL);
14263}
14264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014265#endif /* FEAT_EVAL */