blob: 99e68f7e5025de600c582dd72d452f1b055ea6a1 [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);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100370#ifdef FEAT_SIGNS
371static void f_sign_define(typval_T *argvars, typval_T *rettv);
372static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
373static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
374static void f_sign_place(typval_T *argvars, typval_T *rettv);
375static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
376static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
377#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200378static void f_simplify(typval_T *argvars, typval_T *rettv);
379#ifdef FEAT_FLOAT
380static void f_sin(typval_T *argvars, typval_T *rettv);
381static void f_sinh(typval_T *argvars, typval_T *rettv);
382#endif
383static void f_sort(typval_T *argvars, typval_T *rettv);
384static void f_soundfold(typval_T *argvars, typval_T *rettv);
385static void f_spellbadword(typval_T *argvars, typval_T *rettv);
386static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
387static void f_split(typval_T *argvars, typval_T *rettv);
388#ifdef FEAT_FLOAT
389static void f_sqrt(typval_T *argvars, typval_T *rettv);
390static void f_str2float(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_str2nr(typval_T *argvars, typval_T *rettv);
393static void f_strchars(typval_T *argvars, typval_T *rettv);
394#ifdef HAVE_STRFTIME
395static void f_strftime(typval_T *argvars, typval_T *rettv);
396#endif
397static void f_strgetchar(typval_T *argvars, typval_T *rettv);
398static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200399static void f_strlen(typval_T *argvars, typval_T *rettv);
400static void f_strcharpart(typval_T *argvars, typval_T *rettv);
401static void f_strpart(typval_T *argvars, typval_T *rettv);
402static void f_strridx(typval_T *argvars, typval_T *rettv);
403static void f_strtrans(typval_T *argvars, typval_T *rettv);
404static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
405static void f_strwidth(typval_T *argvars, typval_T *rettv);
406static void f_submatch(typval_T *argvars, typval_T *rettv);
407static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200408static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200409static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410static void f_synID(typval_T *argvars, typval_T *rettv);
411static void f_synIDattr(typval_T *argvars, typval_T *rettv);
412static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
413static void f_synstack(typval_T *argvars, typval_T *rettv);
414static void f_synconcealed(typval_T *argvars, typval_T *rettv);
415static void f_system(typval_T *argvars, typval_T *rettv);
416static void f_systemlist(typval_T *argvars, typval_T *rettv);
417static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
418static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
419static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
420static void f_taglist(typval_T *argvars, typval_T *rettv);
421static void f_tagfiles(typval_T *argvars, typval_T *rettv);
422static void f_tempname(typval_T *argvars, typval_T *rettv);
423static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
424static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200425static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200426static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100427static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100429static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430#ifdef FEAT_JOB_CHANNEL
431static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
432#endif
433static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_job(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_list(typval_T *argvars, typval_T *rettv);
438static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
439static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200440#ifdef FEAT_GUI
441static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
442#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_test_settime(typval_T *argvars, typval_T *rettv);
444#ifdef FEAT_FLOAT
445static void f_tan(typval_T *argvars, typval_T *rettv);
446static void f_tanh(typval_T *argvars, typval_T *rettv);
447#endif
448#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200449static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200450static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_timer_start(typval_T *argvars, typval_T *rettv);
452static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200453static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454#endif
455static void f_tolower(typval_T *argvars, typval_T *rettv);
456static void f_toupper(typval_T *argvars, typval_T *rettv);
457static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100458static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459#ifdef FEAT_FLOAT
460static void f_trunc(typval_T *argvars, typval_T *rettv);
461#endif
462static void f_type(typval_T *argvars, typval_T *rettv);
463static void f_undofile(typval_T *argvars, typval_T *rettv);
464static void f_undotree(typval_T *argvars, typval_T *rettv);
465static void f_uniq(typval_T *argvars, typval_T *rettv);
466static void f_values(typval_T *argvars, typval_T *rettv);
467static void f_virtcol(typval_T *argvars, typval_T *rettv);
468static void f_visualmode(typval_T *argvars, typval_T *rettv);
469static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
470static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
471static void f_win_getid(typval_T *argvars, typval_T *rettv);
472static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
473static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
474static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100475static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_winbufnr(typval_T *argvars, typval_T *rettv);
477static void f_wincol(typval_T *argvars, typval_T *rettv);
478static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200479static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winline(typval_T *argvars, typval_T *rettv);
481static void f_winnr(typval_T *argvars, typval_T *rettv);
482static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
483static void f_winrestview(typval_T *argvars, typval_T *rettv);
484static void f_winsaveview(typval_T *argvars, typval_T *rettv);
485static void f_winwidth(typval_T *argvars, typval_T *rettv);
486static void f_writefile(typval_T *argvars, typval_T *rettv);
487static void f_wordcount(typval_T *argvars, typval_T *rettv);
488static void f_xor(typval_T *argvars, typval_T *rettv);
489
490/*
491 * Array with names and number of arguments of all internal functions
492 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
493 */
494static struct fst
495{
496 char *f_name; /* function name */
497 char f_min_argc; /* minimal number of arguments */
498 char f_max_argc; /* maximal number of arguments */
499 void (*f_func)(typval_T *args, typval_T *rvar);
500 /* implementation of function */
501} functions[] =
502{
503#ifdef FEAT_FLOAT
504 {"abs", 1, 1, f_abs},
505 {"acos", 1, 1, f_acos}, /* WJMc */
506#endif
507 {"add", 2, 2, f_add},
508 {"and", 2, 2, f_and},
509 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200510 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200511 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"argidx", 0, 0, f_argidx},
513 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200514 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515#ifdef FEAT_FLOAT
516 {"asin", 1, 1, f_asin}, /* WJMc */
517#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100518 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100520 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200521 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200522 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100524 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_match", 2, 3, f_assert_match},
526 {"assert_notequal", 2, 3, f_assert_notequal},
527 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100528 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_true", 1, 2, f_assert_true},
530#ifdef FEAT_FLOAT
531 {"atan", 1, 1, f_atan},
532 {"atan2", 2, 2, f_atan2},
533#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100534#ifdef FEAT_BEVAL
535 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100536# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100537 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100538# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"browse", 4, 4, f_browse},
541 {"browsedir", 2, 2, f_browsedir},
542 {"bufexists", 1, 1, f_bufexists},
543 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
544 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
545 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
546 {"buflisted", 1, 1, f_buflisted},
547 {"bufloaded", 1, 1, f_bufloaded},
548 {"bufname", 1, 1, f_bufname},
549 {"bufnr", 1, 2, f_bufnr},
550 {"bufwinid", 1, 1, f_bufwinid},
551 {"bufwinnr", 1, 1, f_bufwinnr},
552 {"byte2line", 1, 1, f_byte2line},
553 {"byteidx", 2, 2, f_byteidx},
554 {"byteidxcomp", 2, 2, f_byteidxcomp},
555 {"call", 2, 3, f_call},
556#ifdef FEAT_FLOAT
557 {"ceil", 1, 1, f_ceil},
558#endif
559#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100560 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200561 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200562 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
564 {"ch_evalraw", 2, 3, f_ch_evalraw},
565 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
566 {"ch_getjob", 1, 1, f_ch_getjob},
567 {"ch_info", 1, 1, f_ch_info},
568 {"ch_log", 1, 2, f_ch_log},
569 {"ch_logfile", 1, 2, f_ch_logfile},
570 {"ch_open", 1, 2, f_ch_open},
571 {"ch_read", 1, 2, f_ch_read},
572 {"ch_readraw", 1, 2, f_ch_readraw},
573 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
574 {"ch_sendraw", 2, 3, f_ch_sendraw},
575 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200576 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"changenr", 0, 0, f_changenr},
579 {"char2nr", 1, 2, f_char2nr},
580 {"cindent", 1, 1, f_cindent},
581 {"clearmatches", 0, 0, f_clearmatches},
582 {"col", 1, 1, f_col},
583#if defined(FEAT_INS_EXPAND)
584 {"complete", 2, 2, f_complete},
585 {"complete_add", 1, 1, f_complete_add},
586 {"complete_check", 0, 0, f_complete_check},
587#endif
588 {"confirm", 1, 4, f_confirm},
589 {"copy", 1, 1, f_copy},
590#ifdef FEAT_FLOAT
591 {"cos", 1, 1, f_cos},
592 {"cosh", 1, 1, f_cosh},
593#endif
594 {"count", 2, 4, f_count},
595 {"cscope_connection",0,3, f_cscope_connection},
596 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200597#ifdef WIN3264
598 {"debugbreak", 1, 1, f_debugbreak},
599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"deepcopy", 1, 2, f_deepcopy},
601 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200602 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603 {"did_filetype", 0, 0, f_did_filetype},
604 {"diff_filler", 1, 1, f_diff_filler},
605 {"diff_hlID", 2, 2, f_diff_hlID},
606 {"empty", 1, 1, f_empty},
607 {"escape", 2, 2, f_escape},
608 {"eval", 1, 1, f_eval},
609 {"eventhandler", 0, 0, f_eventhandler},
610 {"executable", 1, 1, f_executable},
611 {"execute", 1, 2, f_execute},
612 {"exepath", 1, 1, f_exepath},
613 {"exists", 1, 1, f_exists},
614#ifdef FEAT_FLOAT
615 {"exp", 1, 1, f_exp},
616#endif
617 {"expand", 1, 3, f_expand},
618 {"extend", 2, 3, f_extend},
619 {"feedkeys", 1, 2, f_feedkeys},
620 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
621 {"filereadable", 1, 1, f_filereadable},
622 {"filewritable", 1, 1, f_filewritable},
623 {"filter", 2, 2, f_filter},
624 {"finddir", 1, 3, f_finddir},
625 {"findfile", 1, 3, f_findfile},
626#ifdef FEAT_FLOAT
627 {"float2nr", 1, 1, f_float2nr},
628 {"floor", 1, 1, f_floor},
629 {"fmod", 2, 2, f_fmod},
630#endif
631 {"fnameescape", 1, 1, f_fnameescape},
632 {"fnamemodify", 2, 2, f_fnamemodify},
633 {"foldclosed", 1, 1, f_foldclosed},
634 {"foldclosedend", 1, 1, f_foldclosedend},
635 {"foldlevel", 1, 1, f_foldlevel},
636 {"foldtext", 0, 0, f_foldtext},
637 {"foldtextresult", 1, 1, f_foldtextresult},
638 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200639 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"function", 1, 3, f_function},
641 {"garbagecollect", 0, 1, f_garbagecollect},
642 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200643 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644 {"getbufline", 2, 3, f_getbufline},
645 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100646 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getchar", 0, 1, f_getchar},
648 {"getcharmod", 0, 0, f_getcharmod},
649 {"getcharsearch", 0, 0, f_getcharsearch},
650 {"getcmdline", 0, 0, f_getcmdline},
651 {"getcmdpos", 0, 0, f_getcmdpos},
652 {"getcmdtype", 0, 0, f_getcmdtype},
653 {"getcmdwintype", 0, 0, f_getcmdwintype},
654#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200655 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656#endif
657 {"getcurpos", 0, 0, f_getcurpos},
658 {"getcwd", 0, 2, f_getcwd},
659 {"getfontname", 0, 1, f_getfontname},
660 {"getfperm", 1, 1, f_getfperm},
661 {"getfsize", 1, 1, f_getfsize},
662 {"getftime", 1, 1, f_getftime},
663 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100664 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200666 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"getmatches", 0, 0, f_getmatches},
668 {"getpid", 0, 0, f_getpid},
669 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200670 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"getreg", 0, 3, f_getreg},
672 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"gettabvar", 2, 3, f_gettabvar},
675 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100676 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200677 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100678 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"getwinposx", 0, 0, f_getwinposx},
680 {"getwinposy", 0, 0, f_getwinposy},
681 {"getwinvar", 2, 3, f_getwinvar},
682 {"glob", 1, 4, f_glob},
683 {"glob2regpat", 1, 1, f_glob2regpat},
684 {"globpath", 2, 5, f_globpath},
685 {"has", 1, 1, f_has},
686 {"has_key", 2, 2, f_has_key},
687 {"haslocaldir", 0, 2, f_haslocaldir},
688 {"hasmapto", 1, 3, f_hasmapto},
689 {"highlightID", 1, 1, f_hlID}, /* obsolete */
690 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
691 {"histadd", 2, 2, f_histadd},
692 {"histdel", 1, 2, f_histdel},
693 {"histget", 1, 2, f_histget},
694 {"histnr", 1, 1, f_histnr},
695 {"hlID", 1, 1, f_hlID},
696 {"hlexists", 1, 1, f_hlexists},
697 {"hostname", 0, 0, f_hostname},
698 {"iconv", 3, 3, f_iconv},
699 {"indent", 1, 1, f_indent},
700 {"index", 2, 4, f_index},
701 {"input", 1, 3, f_input},
702 {"inputdialog", 1, 3, f_inputdialog},
703 {"inputlist", 1, 1, f_inputlist},
704 {"inputrestore", 0, 0, f_inputrestore},
705 {"inputsave", 0, 0, f_inputsave},
706 {"inputsecret", 1, 2, f_inputsecret},
707 {"insert", 2, 3, f_insert},
708 {"invert", 1, 1, f_invert},
709 {"isdirectory", 1, 1, f_isdirectory},
710 {"islocked", 1, 1, f_islocked},
711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
712 {"isnan", 1, 1, f_isnan},
713#endif
714 {"items", 1, 1, f_items},
715#ifdef FEAT_JOB_CHANNEL
716 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200717 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200718 {"job_setoptions", 2, 2, f_job_setoptions},
719 {"job_start", 1, 2, f_job_start},
720 {"job_status", 1, 1, f_job_status},
721 {"job_stop", 1, 2, f_job_stop},
722#endif
723 {"join", 1, 2, f_join},
724 {"js_decode", 1, 1, f_js_decode},
725 {"js_encode", 1, 1, f_js_encode},
726 {"json_decode", 1, 1, f_json_decode},
727 {"json_encode", 1, 1, f_json_encode},
728 {"keys", 1, 1, f_keys},
729 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
730 {"len", 1, 1, f_len},
731 {"libcall", 3, 3, f_libcall},
732 {"libcallnr", 3, 3, f_libcallnr},
733 {"line", 1, 1, f_line},
734 {"line2byte", 1, 1, f_line2byte},
735 {"lispindent", 1, 1, f_lispindent},
736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
751 {"matchdelete", 1, 1, f_matchdelete},
752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
772#ifdef FEAT_FLOAT
773 {"pow", 2, 2, f_pow},
774#endif
775 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100776 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200777#ifdef FEAT_JOB_CHANNEL
778 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200779 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200780 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
781#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782#ifdef FEAT_TEXT_PROP
783 {"prop_add", 3, 3, f_prop_add},
784 {"prop_clear", 1, 3, f_prop_clear},
785 {"prop_list", 1, 2, f_prop_list},
786 {"prop_remove", 2, 3, f_prop_remove},
787 {"prop_type_add", 2, 2, f_prop_type_add},
788 {"prop_type_change", 2, 2, f_prop_type_change},
789 {"prop_type_delete", 1, 2, f_prop_type_delete},
790 {"prop_type_get", 1, 2, f_prop_type_get},
791 {"prop_type_list", 0, 1, f_prop_type_list},
792#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"pumvisible", 0, 0, f_pumvisible},
794#ifdef FEAT_PYTHON3
795 {"py3eval", 1, 1, f_py3eval},
796#endif
797#ifdef FEAT_PYTHON
798 {"pyeval", 1, 1, f_pyeval},
799#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100800#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
801 {"pyxeval", 1, 1, f_pyxeval},
802#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200803 {"range", 1, 3, f_range},
804 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200805 {"reg_executing", 0, 0, f_reg_executing},
806 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200807 {"reltime", 0, 2, f_reltime},
808#ifdef FEAT_FLOAT
809 {"reltimefloat", 1, 1, f_reltimefloat},
810#endif
811 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100812 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813 {"remote_foreground", 1, 1, f_remote_foreground},
814 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100815 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100817 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remove", 2, 3, f_remove},
819 {"rename", 2, 2, f_rename},
820 {"repeat", 2, 2, f_repeat},
821 {"resolve", 1, 1, f_resolve},
822 {"reverse", 1, 1, f_reverse},
823#ifdef FEAT_FLOAT
824 {"round", 1, 1, f_round},
825#endif
826 {"screenattr", 2, 2, f_screenattr},
827 {"screenchar", 2, 2, f_screenchar},
828 {"screencol", 0, 0, f_screencol},
829 {"screenrow", 0, 0, f_screenrow},
830 {"search", 1, 4, f_search},
831 {"searchdecl", 1, 3, f_searchdecl},
832 {"searchpair", 3, 7, f_searchpair},
833 {"searchpairpos", 3, 7, f_searchpairpos},
834 {"searchpos", 1, 4, f_searchpos},
835 {"server2client", 2, 2, f_server2client},
836 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200837 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"setbufvar", 3, 3, f_setbufvar},
839 {"setcharsearch", 1, 1, f_setcharsearch},
840 {"setcmdpos", 1, 1, f_setcmdpos},
841 {"setfperm", 2, 2, f_setfperm},
842 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200843 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"setmatches", 1, 1, f_setmatches},
845 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200846 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200847 {"setreg", 2, 3, f_setreg},
848 {"settabvar", 3, 3, f_settabvar},
849 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100850 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"setwinvar", 3, 3, f_setwinvar},
852#ifdef FEAT_CRYPT
853 {"sha256", 1, 1, f_sha256},
854#endif
855 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100856 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100857#ifdef FEAT_SIGNS
858 {"sign_define", 1, 2, f_sign_define},
859 {"sign_getdefined", 0, 1, f_sign_getdefined},
860 {"sign_getplaced", 0, 2, f_sign_getplaced},
861 {"sign_place", 4, 5, f_sign_place},
862 {"sign_undefine", 0, 1, f_sign_undefine},
863 {"sign_unplace", 1, 2, f_sign_unplace},
864#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"simplify", 1, 1, f_simplify},
866#ifdef FEAT_FLOAT
867 {"sin", 1, 1, f_sin},
868 {"sinh", 1, 1, f_sinh},
869#endif
870 {"sort", 1, 3, f_sort},
871 {"soundfold", 1, 1, f_soundfold},
872 {"spellbadword", 0, 1, f_spellbadword},
873 {"spellsuggest", 1, 3, f_spellsuggest},
874 {"split", 1, 3, f_split},
875#ifdef FEAT_FLOAT
876 {"sqrt", 1, 1, f_sqrt},
877 {"str2float", 1, 1, f_str2float},
878#endif
879 {"str2nr", 1, 2, f_str2nr},
880 {"strcharpart", 2, 3, f_strcharpart},
881 {"strchars", 1, 2, f_strchars},
882 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
883#ifdef HAVE_STRFTIME
884 {"strftime", 1, 2, f_strftime},
885#endif
886 {"strgetchar", 2, 2, f_strgetchar},
887 {"stridx", 2, 3, f_stridx},
888 {"string", 1, 1, f_string},
889 {"strlen", 1, 1, f_strlen},
890 {"strpart", 2, 3, f_strpart},
891 {"strridx", 2, 3, f_strridx},
892 {"strtrans", 1, 1, f_strtrans},
893 {"strwidth", 1, 1, f_strwidth},
894 {"submatch", 1, 2, f_submatch},
895 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200896 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200897 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"synID", 3, 3, f_synID},
899 {"synIDattr", 2, 3, f_synIDattr},
900 {"synIDtrans", 1, 1, f_synIDtrans},
901 {"synconcealed", 2, 2, f_synconcealed},
902 {"synstack", 2, 2, f_synstack},
903 {"system", 1, 2, f_system},
904 {"systemlist", 1, 2, f_systemlist},
905 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
906 {"tabpagenr", 0, 1, f_tabpagenr},
907 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100909 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#ifdef FEAT_FLOAT
911 {"tan", 1, 1, f_tan},
912 {"tanh", 1, 1, f_tanh},
913#endif
914 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100916 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
917 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100918 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200919 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200920# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
921 {"term_getansicolors", 1, 1, f_term_getansicolors},
922# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200924 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200925 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200926 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200927 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200929 {"term_getstatus", 1, 1, f_term_getstatus},
930 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200931 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200932 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200933 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200935# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
936 {"term_setansicolors", 2, 2, f_term_setansicolors},
937# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100938 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200940 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200942 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
945 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200946 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100948 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949#ifdef FEAT_JOB_CHANNEL
950 {"test_null_channel", 0, 0, f_test_null_channel},
951#endif
952 {"test_null_dict", 0, 0, f_test_null_dict},
953#ifdef FEAT_JOB_CHANNEL
954 {"test_null_job", 0, 0, f_test_null_job},
955#endif
956 {"test_null_list", 0, 0, f_test_null_list},
957 {"test_null_partial", 0, 0, f_test_null_partial},
958 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200959 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100960 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200961#ifdef FEAT_GUI
962 {"test_scrollbar", 3, 3, f_test_scrollbar},
963#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964 {"test_settime", 1, 1, f_test_settime},
965#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200966 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200967 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968 {"timer_start", 2, 3, f_timer_start},
969 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200970 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971#endif
972 {"tolower", 1, 1, f_tolower},
973 {"toupper", 1, 1, f_toupper},
974 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100975 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#ifdef FEAT_FLOAT
977 {"trunc", 1, 1, f_trunc},
978#endif
979 {"type", 1, 1, f_type},
980 {"undofile", 1, 1, f_undofile},
981 {"undotree", 0, 0, f_undotree},
982 {"uniq", 1, 3, f_uniq},
983 {"values", 1, 1, f_values},
984 {"virtcol", 1, 1, f_virtcol},
985 {"visualmode", 0, 1, f_visualmode},
986 {"wildmenumode", 0, 0, f_wildmenumode},
987 {"win_findbuf", 1, 1, f_win_findbuf},
988 {"win_getid", 0, 2, f_win_getid},
989 {"win_gotoid", 1, 1, f_win_gotoid},
990 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
991 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100992 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200993 {"winbufnr", 1, 1, f_winbufnr},
994 {"wincol", 0, 0, f_wincol},
995 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200996 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997 {"winline", 0, 0, f_winline},
998 {"winnr", 0, 1, f_winnr},
999 {"winrestcmd", 0, 0, f_winrestcmd},
1000 {"winrestview", 1, 1, f_winrestview},
1001 {"winsaveview", 0, 0, f_winsaveview},
1002 {"winwidth", 1, 1, f_winwidth},
1003 {"wordcount", 0, 0, f_wordcount},
1004 {"writefile", 2, 3, f_writefile},
1005 {"xor", 2, 2, f_xor},
1006};
1007
1008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1009
1010/*
1011 * Function given to ExpandGeneric() to obtain the list of internal
1012 * or user defined function names.
1013 */
1014 char_u *
1015get_function_name(expand_T *xp, int idx)
1016{
1017 static int intidx = -1;
1018 char_u *name;
1019
1020 if (idx == 0)
1021 intidx = -1;
1022 if (intidx < 0)
1023 {
1024 name = get_user_func_name(xp, idx);
1025 if (name != NULL)
1026 return name;
1027 }
1028 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1029 {
1030 STRCPY(IObuff, functions[intidx].f_name);
1031 STRCAT(IObuff, "(");
1032 if (functions[intidx].f_max_argc == 0)
1033 STRCAT(IObuff, ")");
1034 return IObuff;
1035 }
1036
1037 return NULL;
1038}
1039
1040/*
1041 * Function given to ExpandGeneric() to obtain the list of internal or
1042 * user defined variable or function names.
1043 */
1044 char_u *
1045get_expr_name(expand_T *xp, int idx)
1046{
1047 static int intidx = -1;
1048 char_u *name;
1049
1050 if (idx == 0)
1051 intidx = -1;
1052 if (intidx < 0)
1053 {
1054 name = get_function_name(xp, idx);
1055 if (name != NULL)
1056 return name;
1057 }
1058 return get_user_var_name(xp, ++intidx);
1059}
1060
1061#endif /* FEAT_CMDL_COMPL */
1062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001063/*
1064 * Find internal function in table above.
1065 * Return index, or -1 if not found
1066 */
1067 int
1068find_internal_func(
1069 char_u *name) /* name of the function */
1070{
1071 int first = 0;
1072 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1073 int cmp;
1074 int x;
1075
1076 /*
1077 * Find the function name in the table. Binary search.
1078 */
1079 while (first <= last)
1080 {
1081 x = first + ((unsigned)(last - first) >> 1);
1082 cmp = STRCMP(name, functions[x].f_name);
1083 if (cmp < 0)
1084 last = x - 1;
1085 else if (cmp > 0)
1086 first = x + 1;
1087 else
1088 return x;
1089 }
1090 return -1;
1091}
1092
1093 int
1094call_internal_func(
1095 char_u *name,
1096 int argcount,
1097 typval_T *argvars,
1098 typval_T *rettv)
1099{
1100 int i;
1101
1102 i = find_internal_func(name);
1103 if (i < 0)
1104 return ERROR_UNKNOWN;
1105 if (argcount < functions[i].f_min_argc)
1106 return ERROR_TOOFEW;
1107 if (argcount > functions[i].f_max_argc)
1108 return ERROR_TOOMANY;
1109 argvars[argcount].v_type = VAR_UNKNOWN;
1110 functions[i].f_func(argvars, rettv);
1111 return ERROR_NONE;
1112}
1113
1114/*
1115 * Return TRUE for a non-zero Number and a non-empty String.
1116 */
1117 static int
1118non_zero_arg(typval_T *argvars)
1119{
1120 return ((argvars[0].v_type == VAR_NUMBER
1121 && argvars[0].vval.v_number != 0)
1122 || (argvars[0].v_type == VAR_SPECIAL
1123 && argvars[0].vval.v_number == VVAL_TRUE)
1124 || (argvars[0].v_type == VAR_STRING
1125 && argvars[0].vval.v_string != NULL
1126 && *argvars[0].vval.v_string != NUL));
1127}
1128
1129/*
1130 * Get the lnum from the first argument.
1131 * Also accepts ".", "$", etc., but that only works for the current buffer.
1132 * Returns -1 on error.
1133 */
1134 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001135tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136{
1137 typval_T rettv;
1138 linenr_T lnum;
1139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001140 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001141 if (lnum == 0) /* no valid number, try using line() */
1142 {
1143 rettv.v_type = VAR_NUMBER;
1144 f_line(argvars, &rettv);
1145 lnum = (linenr_T)rettv.vval.v_number;
1146 clear_tv(&rettv);
1147 }
1148 return lnum;
1149}
1150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151/*
1152 * Get the lnum from the first argument.
1153 * Also accepts "$", then "buf" is used.
1154 * Returns 0 on error.
1155 */
1156 static linenr_T
1157tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1158{
1159 if (argvars[0].v_type == VAR_STRING
1160 && argvars[0].vval.v_string != NULL
1161 && argvars[0].vval.v_string[0] == '$'
1162 && buf != NULL)
1163 return buf->b_ml.ml_line_count;
1164 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1165}
1166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001167#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168/*
1169 * Get the float value of "argvars[0]" into "f".
1170 * Returns FAIL when the argument is not a Number or Float.
1171 */
1172 static int
1173get_float_arg(typval_T *argvars, float_T *f)
1174{
1175 if (argvars[0].v_type == VAR_FLOAT)
1176 {
1177 *f = argvars[0].vval.v_float;
1178 return OK;
1179 }
1180 if (argvars[0].v_type == VAR_NUMBER)
1181 {
1182 *f = (float_T)argvars[0].vval.v_number;
1183 return OK;
1184 }
1185 EMSG(_("E808: Number or Float required"));
1186 return FAIL;
1187}
1188
1189/*
1190 * "abs(expr)" function
1191 */
1192 static void
1193f_abs(typval_T *argvars, typval_T *rettv)
1194{
1195 if (argvars[0].v_type == VAR_FLOAT)
1196 {
1197 rettv->v_type = VAR_FLOAT;
1198 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1199 }
1200 else
1201 {
1202 varnumber_T n;
1203 int error = FALSE;
1204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206 if (error)
1207 rettv->vval.v_number = -1;
1208 else if (n > 0)
1209 rettv->vval.v_number = n;
1210 else
1211 rettv->vval.v_number = -n;
1212 }
1213}
1214
1215/*
1216 * "acos()" function
1217 */
1218 static void
1219f_acos(typval_T *argvars, typval_T *rettv)
1220{
1221 float_T f = 0.0;
1222
1223 rettv->v_type = VAR_FLOAT;
1224 if (get_float_arg(argvars, &f) == OK)
1225 rettv->vval.v_float = acos(f);
1226 else
1227 rettv->vval.v_float = 0.0;
1228}
1229#endif
1230
1231/*
1232 * "add(list, item)" function
1233 */
1234 static void
1235f_add(typval_T *argvars, typval_T *rettv)
1236{
1237 list_T *l;
1238
1239 rettv->vval.v_number = 1; /* Default: Failed */
1240 if (argvars[0].v_type == VAR_LIST)
1241 {
1242 if ((l = argvars[0].vval.v_list) != NULL
1243 && !tv_check_lock(l->lv_lock,
1244 (char_u *)N_("add() argument"), TRUE)
1245 && list_append_tv(l, &argvars[1]) == OK)
1246 copy_tv(&argvars[0], rettv);
1247 }
1248 else
1249 EMSG(_(e_listreq));
1250}
1251
1252/*
1253 * "and(expr, expr)" function
1254 */
1255 static void
1256f_and(typval_T *argvars, typval_T *rettv)
1257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1259 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001260}
1261
1262/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001263 * If there is a window for "curbuf", make it the current window.
1264 */
1265 static void
1266find_win_for_curbuf(void)
1267{
1268 wininfo_T *wip;
1269
1270 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1271 {
1272 if (wip->wi_win != NULL)
1273 {
1274 curwin = wip->wi_win;
1275 break;
1276 }
1277 }
1278}
1279
1280/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001281 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 */
1283 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001284set_buffer_lines(
1285 buf_T *buf,
1286 linenr_T lnum_arg,
1287 int append,
1288 typval_T *lines,
1289 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290{
Bram Moolenaarca851592018-06-06 21:04:07 +02001291 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1292 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001293 list_T *l = NULL;
1294 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001296 linenr_T append_lnum;
1297 buf_T *curbuf_save = NULL;
1298 win_T *curwin_save = NULL;
1299 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300
Bram Moolenaarca851592018-06-06 21:04:07 +02001301 /* When using the current buffer ml_mfp will be set if needed. Useful when
1302 * setline() is used on startup. For other buffers the buffer must be
1303 * loaded. */
1304 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 }
1309
Bram Moolenaarca851592018-06-06 21:04:07 +02001310 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001312 curbuf_save = curbuf;
1313 curwin_save = curwin;
1314 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001315 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 }
1317
1318 if (append)
1319 // appendbufline() uses the line number below which we insert
1320 append_lnum = lnum - 1;
1321 else
1322 // setbufline() uses the line number above which we insert, we only
1323 // append if it's below the last line
1324 append_lnum = curbuf->b_ml.ml_line_count;
1325
1326 if (lines->v_type == VAR_LIST)
1327 {
1328 l = lines->vval.v_list;
1329 li = l->lv_first;
1330 }
1331 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001333
1334 /* default result is zero == OK */
1335 for (;;)
1336 {
1337 if (l != NULL)
1338 {
1339 /* list argument, get next string */
1340 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 li = li->li_next;
1344 }
1345
Bram Moolenaarca851592018-06-06 21:04:07 +02001346 rettv->vval.v_number = 1; /* FAIL */
1347 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1348 break;
1349
1350 /* When coming here from Insert mode, sync undo, so that this can be
1351 * undone separately from what was previously inserted. */
1352 if (u_sync_once == 2)
1353 {
1354 u_sync_once = 1; /* notify that u_sync() was called */
1355 u_sync(TRUE);
1356 }
1357
1358 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1359 {
1360 /* existing line, replace it */
1361 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1362 {
1363 changed_bytes(lnum, 0);
1364 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1365 check_cursor_col();
1366 rettv->vval.v_number = 0; /* OK */
1367 }
1368 }
1369 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1370 {
1371 /* append the line */
1372 ++added;
1373 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1374 rettv->vval.v_number = 0; /* OK */
1375 }
1376
1377 if (l == NULL) /* only one string argument */
1378 break;
1379 ++lnum;
1380 }
1381
1382 if (added > 0)
1383 {
1384 win_T *wp;
1385 tabpage_T *tp;
1386
1387 appended_lines_mark(append_lnum, added);
1388 FOR_ALL_TAB_WINDOWS(tp, wp)
1389 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1390 wp->w_cursor.lnum += added;
1391 check_cursor_col();
1392
Bram Moolenaarf2732452018-06-03 14:47:35 +02001393#ifdef FEAT_JOB_CHANNEL
1394 if (bt_prompt(curbuf) && (State & INSERT))
1395 // show the line with the prompt
1396 update_topline();
1397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001399
1400 if (!is_curbuf)
1401 {
1402 curbuf = curbuf_save;
1403 curwin = curwin_save;
1404 }
1405}
1406
1407/*
1408 * "append(lnum, string/list)" function
1409 */
1410 static void
1411f_append(typval_T *argvars, typval_T *rettv)
1412{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001413 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001414
1415 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1416}
1417
1418/*
1419 * "appendbufline(buf, lnum, string/list)" function
1420 */
1421 static void
1422f_appendbufline(typval_T *argvars, typval_T *rettv)
1423{
1424 linenr_T lnum;
1425 buf_T *buf;
1426
1427 buf = get_buf_tv(&argvars[0], FALSE);
1428 if (buf == NULL)
1429 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001430 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001431 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001432 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001433 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1434 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435}
1436
1437/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001438 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001439 */
1440 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001441f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001442{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001443 win_T *wp;
1444
1445 if (argvars[0].v_type == VAR_UNKNOWN)
1446 // use the current window
1447 rettv->vval.v_number = ARGCOUNT;
1448 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001450 // use the global argument list
1451 rettv->vval.v_number = GARGCOUNT;
1452 else
1453 {
1454 // use the argument list of the specified window
1455 wp = find_win_by_nr_or_id(&argvars[0]);
1456 if (wp != NULL)
1457 rettv->vval.v_number = WARGCOUNT(wp);
1458 else
1459 rettv->vval.v_number = -1;
1460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461}
1462
1463/*
1464 * "argidx()" function
1465 */
1466 static void
1467f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1468{
1469 rettv->vval.v_number = curwin->w_arg_idx;
1470}
1471
1472/*
1473 * "arglistid()" function
1474 */
1475 static void
1476f_arglistid(typval_T *argvars, typval_T *rettv)
1477{
1478 win_T *wp;
1479
1480 rettv->vval.v_number = -1;
1481 wp = find_tabwin(&argvars[0], &argvars[1]);
1482 if (wp != NULL)
1483 rettv->vval.v_number = wp->w_alist->id;
1484}
1485
1486/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001487 * Get the argument list for a given window
1488 */
1489 static void
1490get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1491{
1492 int idx;
1493
1494 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1495 for (idx = 0; idx < argcount; ++idx)
1496 list_append_string(rettv->vval.v_list,
1497 alist_name(&arglist[idx]), -1);
1498}
1499
1500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501 * "argv(nr)" function
1502 */
1503 static void
1504f_argv(typval_T *argvars, typval_T *rettv)
1505{
1506 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001507 aentry_T *arglist = NULL;
1508 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509
1510 if (argvars[0].v_type != VAR_UNKNOWN)
1511 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 if (argvars[1].v_type == VAR_UNKNOWN)
1513 {
1514 arglist = ARGLIST;
1515 argcount = ARGCOUNT;
1516 }
1517 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519 {
1520 arglist = GARGLIST;
1521 argcount = GARGCOUNT;
1522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001524 {
1525 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1526
1527 if (wp != NULL)
1528 {
1529 /* Use the argument list of the specified window */
1530 arglist = WARGLIST(wp);
1531 argcount = WARGCOUNT(wp);
1532 }
1533 }
1534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001536 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 if (arglist != NULL && idx >= 0 && idx < argcount)
1539 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1540 else if (idx == -1)
1541 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001543 else
1544 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545}
1546
1547/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001548 * "assert_beeps(cmd [, error])" function
1549 */
1550 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001552{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001553 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001554}
1555
1556/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557 * "assert_equal(expected, actual[, msg])" function
1558 */
1559 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001560f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001562 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001563}
1564
1565/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001566 * "assert_equalfile(fname-one, fname-two)" function
1567 */
1568 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001569f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001570{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001571 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001572}
1573
1574/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 * "assert_notequal(expected, actual[, msg])" function
1576 */
1577 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001578f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001580 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581}
1582
1583/*
1584 * "assert_exception(string[, msg])" function
1585 */
1586 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001587f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001589 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590}
1591
1592/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001593 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 */
1595 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001596f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001598 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599}
1600
1601/*
1602 * "assert_false(actual[, msg])" function
1603 */
1604 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001605f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001607 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608}
1609
1610/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001611 * "assert_inrange(lower, upper[, msg])" function
1612 */
1613 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001614f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001615{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001617}
1618
1619/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 * "assert_match(pattern, actual[, msg])" function
1621 */
1622 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626}
1627
1628/*
1629 * "assert_notmatch(pattern, actual[, msg])" function
1630 */
1631 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635}
1636
1637/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001638 * "assert_report(msg)" function
1639 */
1640 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001642{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001644}
1645
1646/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647 * "assert_true(actual[, msg])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653}
1654
1655#ifdef FEAT_FLOAT
1656/*
1657 * "asin()" function
1658 */
1659 static void
1660f_asin(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 = asin(f);
1667 else
1668 rettv->vval.v_float = 0.0;
1669}
1670
1671/*
1672 * "atan()" function
1673 */
1674 static void
1675f_atan(typval_T *argvars, typval_T *rettv)
1676{
1677 float_T f = 0.0;
1678
1679 rettv->v_type = VAR_FLOAT;
1680 if (get_float_arg(argvars, &f) == OK)
1681 rettv->vval.v_float = atan(f);
1682 else
1683 rettv->vval.v_float = 0.0;
1684}
1685
1686/*
1687 * "atan2()" function
1688 */
1689 static void
1690f_atan2(typval_T *argvars, typval_T *rettv)
1691{
1692 float_T fx = 0.0, fy = 0.0;
1693
1694 rettv->v_type = VAR_FLOAT;
1695 if (get_float_arg(argvars, &fx) == OK
1696 && get_float_arg(&argvars[1], &fy) == OK)
1697 rettv->vval.v_float = atan2(fx, fy);
1698 else
1699 rettv->vval.v_float = 0.0;
1700}
1701#endif
1702
1703/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001704 * "balloon_show()" function
1705 */
1706#ifdef FEAT_BEVAL
1707 static void
1708f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1709{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001710 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001711 {
1712 if (argvars[0].v_type == VAR_LIST
1713# ifdef FEAT_GUI
1714 && !gui.in_use
1715# endif
1716 )
1717 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1718 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001720 }
1721}
1722
Bram Moolenaar669a8282017-11-19 20:13:05 +01001723# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001724 static void
1725f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1726{
1727 if (rettv_list_alloc(rettv) == OK)
1728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001729 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001730
1731 if (msg != NULL)
1732 {
1733 pumitem_T *array;
1734 int size = split_message(msg, &array);
1735 int i;
1736
1737 /* Skip the first and last item, they are always empty. */
1738 for (i = 1; i < size - 1; ++i)
1739 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001740 while (size > 0)
1741 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001742 vim_free(array);
1743 }
1744 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001745}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001746# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001747#endif
1748
1749/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001750 * "browse(save, title, initdir, default)" function
1751 */
1752 static void
1753f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1754{
1755#ifdef FEAT_BROWSE
1756 int save;
1757 char_u *title;
1758 char_u *initdir;
1759 char_u *defname;
1760 char_u buf[NUMBUFLEN];
1761 char_u buf2[NUMBUFLEN];
1762 int error = FALSE;
1763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001764 save = (int)tv_get_number_chk(&argvars[0], &error);
1765 title = tv_get_string_chk(&argvars[1]);
1766 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1767 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001768
1769 if (error || title == NULL || initdir == NULL || defname == NULL)
1770 rettv->vval.v_string = NULL;
1771 else
1772 rettv->vval.v_string =
1773 do_browse(save ? BROWSE_SAVE : 0,
1774 title, defname, NULL, initdir, NULL, curbuf);
1775#else
1776 rettv->vval.v_string = NULL;
1777#endif
1778 rettv->v_type = VAR_STRING;
1779}
1780
1781/*
1782 * "browsedir(title, initdir)" function
1783 */
1784 static void
1785f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1786{
1787#ifdef FEAT_BROWSE
1788 char_u *title;
1789 char_u *initdir;
1790 char_u buf[NUMBUFLEN];
1791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001792 title = tv_get_string_chk(&argvars[0]);
1793 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001794
1795 if (title == NULL || initdir == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string = do_browse(BROWSE_DIR,
1799 title, NULL, NULL, initdir, NULL, curbuf);
1800#else
1801 rettv->vval.v_string = NULL;
1802#endif
1803 rettv->v_type = VAR_STRING;
1804}
1805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001806/*
1807 * Find a buffer by number or exact name.
1808 */
1809 static buf_T *
1810find_buffer(typval_T *avar)
1811{
1812 buf_T *buf = NULL;
1813
1814 if (avar->v_type == VAR_NUMBER)
1815 buf = buflist_findnr((int)avar->vval.v_number);
1816 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1817 {
1818 buf = buflist_findname_exp(avar->vval.v_string);
1819 if (buf == NULL)
1820 {
1821 /* No full path name match, try a match with a URL or a "nofile"
1822 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001823 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001824 if (buf->b_fname != NULL
1825 && (path_with_url(buf->b_fname)
1826#ifdef FEAT_QUICKFIX
1827 || bt_nofile(buf)
1828#endif
1829 )
1830 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1831 break;
1832 }
1833 }
1834 return buf;
1835}
1836
1837/*
1838 * "bufexists(expr)" function
1839 */
1840 static void
1841f_bufexists(typval_T *argvars, typval_T *rettv)
1842{
1843 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1844}
1845
1846/*
1847 * "buflisted(expr)" function
1848 */
1849 static void
1850f_buflisted(typval_T *argvars, typval_T *rettv)
1851{
1852 buf_T *buf;
1853
1854 buf = find_buffer(&argvars[0]);
1855 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1856}
1857
1858/*
1859 * "bufloaded(expr)" function
1860 */
1861 static void
1862f_bufloaded(typval_T *argvars, typval_T *rettv)
1863{
1864 buf_T *buf;
1865
1866 buf = find_buffer(&argvars[0]);
1867 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1868}
1869
1870 buf_T *
1871buflist_find_by_name(char_u *name, int curtab_only)
1872{
1873 int save_magic;
1874 char_u *save_cpo;
1875 buf_T *buf;
1876
1877 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1878 save_magic = p_magic;
1879 p_magic = TRUE;
1880 save_cpo = p_cpo;
1881 p_cpo = (char_u *)"";
1882
1883 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1884 TRUE, FALSE, curtab_only));
1885
1886 p_magic = save_magic;
1887 p_cpo = save_cpo;
1888 return buf;
1889}
1890
1891/*
1892 * Get buffer by number or pattern.
1893 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001894 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895get_buf_tv(typval_T *tv, int curtab_only)
1896{
1897 char_u *name = tv->vval.v_string;
1898 buf_T *buf;
1899
1900 if (tv->v_type == VAR_NUMBER)
1901 return buflist_findnr((int)tv->vval.v_number);
1902 if (tv->v_type != VAR_STRING)
1903 return NULL;
1904 if (name == NULL || *name == NUL)
1905 return curbuf;
1906 if (name[0] == '$' && name[1] == NUL)
1907 return lastbuf;
1908
1909 buf = buflist_find_by_name(name, curtab_only);
1910
1911 /* If not found, try expanding the name, like done for bufexists(). */
1912 if (buf == NULL)
1913 buf = find_buffer(tv);
1914
1915 return buf;
1916}
1917
1918/*
1919 * "bufname(expr)" function
1920 */
1921 static void
1922f_bufname(typval_T *argvars, typval_T *rettv)
1923{
1924 buf_T *buf;
1925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001926 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 ++emsg_off;
1928 buf = get_buf_tv(&argvars[0], FALSE);
1929 rettv->v_type = VAR_STRING;
1930 if (buf != NULL && buf->b_fname != NULL)
1931 rettv->vval.v_string = vim_strsave(buf->b_fname);
1932 else
1933 rettv->vval.v_string = NULL;
1934 --emsg_off;
1935}
1936
1937/*
1938 * "bufnr(expr)" function
1939 */
1940 static void
1941f_bufnr(typval_T *argvars, typval_T *rettv)
1942{
1943 buf_T *buf;
1944 int error = FALSE;
1945 char_u *name;
1946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001947 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 ++emsg_off;
1949 buf = get_buf_tv(&argvars[0], FALSE);
1950 --emsg_off;
1951
1952 /* If the buffer isn't found and the second argument is not zero create a
1953 * new buffer. */
1954 if (buf == NULL
1955 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001956 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001957 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001958 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 && !error)
1960 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1961
1962 if (buf != NULL)
1963 rettv->vval.v_number = buf->b_fnum;
1964 else
1965 rettv->vval.v_number = -1;
1966}
1967
1968 static void
1969buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1970{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 win_T *wp;
1972 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973 buf_T *buf;
1974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001975 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 ++emsg_off;
1977 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001978 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 {
1980 ++winnr;
1981 if (wp->w_buffer == buf)
1982 break;
1983 }
1984 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001985 --emsg_off;
1986}
1987
1988/*
1989 * "bufwinid(nr)" function
1990 */
1991 static void
1992f_bufwinid(typval_T *argvars, typval_T *rettv)
1993{
1994 buf_win_common(argvars, rettv, FALSE);
1995}
1996
1997/*
1998 * "bufwinnr(nr)" function
1999 */
2000 static void
2001f_bufwinnr(typval_T *argvars, typval_T *rettv)
2002{
2003 buf_win_common(argvars, rettv, TRUE);
2004}
2005
2006/*
2007 * "byte2line(byte)" function
2008 */
2009 static void
2010f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2011{
2012#ifndef FEAT_BYTEOFF
2013 rettv->vval.v_number = -1;
2014#else
2015 long boff = 0;
2016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002017 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 if (boff < 0)
2019 rettv->vval.v_number = -1;
2020 else
2021 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2022 (linenr_T)0, &boff);
2023#endif
2024}
2025
2026 static void
2027byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2028{
2029#ifdef FEAT_MBYTE
2030 char_u *t;
2031#endif
2032 char_u *str;
2033 varnumber_T idx;
2034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002035 str = tv_get_string_chk(&argvars[0]);
2036 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 rettv->vval.v_number = -1;
2038 if (str == NULL || idx < 0)
2039 return;
2040
2041#ifdef FEAT_MBYTE
2042 t = str;
2043 for ( ; idx > 0; idx--)
2044 {
2045 if (*t == NUL) /* EOL reached */
2046 return;
2047 if (enc_utf8 && comp)
2048 t += utf_ptr2len(t);
2049 else
2050 t += (*mb_ptr2len)(t);
2051 }
2052 rettv->vval.v_number = (varnumber_T)(t - str);
2053#else
2054 if ((size_t)idx <= STRLEN(str))
2055 rettv->vval.v_number = idx;
2056#endif
2057}
2058
2059/*
2060 * "byteidx()" function
2061 */
2062 static void
2063f_byteidx(typval_T *argvars, typval_T *rettv)
2064{
2065 byteidx(argvars, rettv, FALSE);
2066}
2067
2068/*
2069 * "byteidxcomp()" function
2070 */
2071 static void
2072f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2073{
2074 byteidx(argvars, rettv, TRUE);
2075}
2076
2077/*
2078 * "call(func, arglist [, dict])" function
2079 */
2080 static void
2081f_call(typval_T *argvars, typval_T *rettv)
2082{
2083 char_u *func;
2084 partial_T *partial = NULL;
2085 dict_T *selfdict = NULL;
2086
2087 if (argvars[1].v_type != VAR_LIST)
2088 {
2089 EMSG(_(e_listreq));
2090 return;
2091 }
2092 if (argvars[1].vval.v_list == NULL)
2093 return;
2094
2095 if (argvars[0].v_type == VAR_FUNC)
2096 func = argvars[0].vval.v_string;
2097 else if (argvars[0].v_type == VAR_PARTIAL)
2098 {
2099 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002100 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 }
2102 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 if (*func == NUL)
2105 return; /* type error or empty name */
2106
2107 if (argvars[2].v_type != VAR_UNKNOWN)
2108 {
2109 if (argvars[2].v_type != VAR_DICT)
2110 {
2111 EMSG(_(e_dictreq));
2112 return;
2113 }
2114 selfdict = argvars[2].vval.v_dict;
2115 }
2116
2117 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2118}
2119
2120#ifdef FEAT_FLOAT
2121/*
2122 * "ceil({float})" function
2123 */
2124 static void
2125f_ceil(typval_T *argvars, typval_T *rettv)
2126{
2127 float_T f = 0.0;
2128
2129 rettv->v_type = VAR_FLOAT;
2130 if (get_float_arg(argvars, &f) == OK)
2131 rettv->vval.v_float = ceil(f);
2132 else
2133 rettv->vval.v_float = 0.0;
2134}
2135#endif
2136
2137#ifdef FEAT_JOB_CHANNEL
2138/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002139 * "ch_canread()" function
2140 */
2141 static void
2142f_ch_canread(typval_T *argvars, typval_T *rettv)
2143{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002144 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002145
2146 rettv->vval.v_number = 0;
2147 if (channel != NULL)
2148 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2149 || channel_has_readahead(channel, PART_OUT)
2150 || channel_has_readahead(channel, PART_ERR);
2151}
2152
2153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 * "ch_close()" function
2155 */
2156 static void
2157f_ch_close(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 {
2163 channel_close(channel, FALSE);
2164 channel_clear(channel);
2165 }
2166}
2167
2168/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002169 * "ch_close()" function
2170 */
2171 static void
2172f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2173{
2174 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2175
2176 if (channel != NULL)
2177 channel_close_in(channel);
2178}
2179
2180/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 * "ch_getbufnr()" function
2182 */
2183 static void
2184f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2185{
2186 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2187
2188 rettv->vval.v_number = -1;
2189 if (channel != NULL)
2190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002191 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 int part;
2193
2194 if (STRCMP(what, "err") == 0)
2195 part = PART_ERR;
2196 else if (STRCMP(what, "out") == 0)
2197 part = PART_OUT;
2198 else if (STRCMP(what, "in") == 0)
2199 part = PART_IN;
2200 else
2201 part = PART_SOCK;
2202 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2203 rettv->vval.v_number =
2204 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2205 }
2206}
2207
2208/*
2209 * "ch_getjob()" function
2210 */
2211 static void
2212f_ch_getjob(typval_T *argvars, typval_T *rettv)
2213{
2214 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2215
2216 if (channel != NULL)
2217 {
2218 rettv->v_type = VAR_JOB;
2219 rettv->vval.v_job = channel->ch_job;
2220 if (channel->ch_job != NULL)
2221 ++channel->ch_job->jv_refcount;
2222 }
2223}
2224
2225/*
2226 * "ch_info()" function
2227 */
2228 static void
2229f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2230{
2231 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2232
2233 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2234 channel_info(channel, rettv->vval.v_dict);
2235}
2236
2237/*
2238 * "ch_log()" function
2239 */
2240 static void
2241f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2242{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002243 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 channel_T *channel = NULL;
2245
2246 if (argvars[1].v_type != VAR_UNKNOWN)
2247 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2248
Bram Moolenaard5359b22018-04-05 22:44:39 +02002249 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250}
2251
2252/*
2253 * "ch_logfile()" function
2254 */
2255 static void
2256f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2257{
2258 char_u *fname;
2259 char_u *opt = (char_u *)"";
2260 char_u buf[NUMBUFLEN];
2261
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002262 /* Don't open a file in restricted mode. */
2263 if (check_restricted() || check_secure())
2264 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002265 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002267 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 ch_logfile(fname, opt);
2269}
2270
2271/*
2272 * "ch_open()" function
2273 */
2274 static void
2275f_ch_open(typval_T *argvars, typval_T *rettv)
2276{
2277 rettv->v_type = VAR_CHANNEL;
2278 if (check_restricted() || check_secure())
2279 return;
2280 rettv->vval.v_channel = channel_open_func(argvars);
2281}
2282
2283/*
2284 * "ch_read()" function
2285 */
2286 static void
2287f_ch_read(typval_T *argvars, typval_T *rettv)
2288{
2289 common_channel_read(argvars, rettv, FALSE);
2290}
2291
2292/*
2293 * "ch_readraw()" function
2294 */
2295 static void
2296f_ch_readraw(typval_T *argvars, typval_T *rettv)
2297{
2298 common_channel_read(argvars, rettv, TRUE);
2299}
2300
2301/*
2302 * "ch_evalexpr()" function
2303 */
2304 static void
2305f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2306{
2307 ch_expr_common(argvars, rettv, TRUE);
2308}
2309
2310/*
2311 * "ch_sendexpr()" function
2312 */
2313 static void
2314f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2315{
2316 ch_expr_common(argvars, rettv, FALSE);
2317}
2318
2319/*
2320 * "ch_evalraw()" function
2321 */
2322 static void
2323f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2324{
2325 ch_raw_common(argvars, rettv, TRUE);
2326}
2327
2328/*
2329 * "ch_sendraw()" function
2330 */
2331 static void
2332f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2333{
2334 ch_raw_common(argvars, rettv, FALSE);
2335}
2336
2337/*
2338 * "ch_setoptions()" function
2339 */
2340 static void
2341f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2342{
2343 channel_T *channel;
2344 jobopt_T opt;
2345
2346 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2347 if (channel == NULL)
2348 return;
2349 clear_job_options(&opt);
2350 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002351 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352 channel_set_options(channel, &opt);
2353 free_job_options(&opt);
2354}
2355
2356/*
2357 * "ch_status()" function
2358 */
2359 static void
2360f_ch_status(typval_T *argvars, typval_T *rettv)
2361{
2362 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002363 jobopt_T opt;
2364 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365
2366 /* return an empty string by default */
2367 rettv->v_type = VAR_STRING;
2368 rettv->vval.v_string = NULL;
2369
2370 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002371
2372 if (argvars[1].v_type != VAR_UNKNOWN)
2373 {
2374 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002375 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002376 && (opt.jo_set & JO_PART))
2377 part = opt.jo_part;
2378 }
2379
2380 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381}
2382#endif
2383
2384/*
2385 * "changenr()" function
2386 */
2387 static void
2388f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2389{
2390 rettv->vval.v_number = curbuf->b_u_seq_cur;
2391}
2392
2393/*
2394 * "char2nr(string)" function
2395 */
2396 static void
2397f_char2nr(typval_T *argvars, typval_T *rettv)
2398{
2399#ifdef FEAT_MBYTE
2400 if (has_mbyte)
2401 {
2402 int utf8 = 0;
2403
2404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002405 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406
2407 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002408 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 }
2412 else
2413#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002414 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
2417/*
2418 * "cindent(lnum)" function
2419 */
2420 static void
2421f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2422{
2423#ifdef FEAT_CINDENT
2424 pos_T pos;
2425 linenr_T lnum;
2426
2427 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002428 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2430 {
2431 curwin->w_cursor.lnum = lnum;
2432 rettv->vval.v_number = get_c_indent();
2433 curwin->w_cursor = pos;
2434 }
2435 else
2436#endif
2437 rettv->vval.v_number = -1;
2438}
2439
2440/*
2441 * "clearmatches()" function
2442 */
2443 static void
2444f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2445{
2446#ifdef FEAT_SEARCH_EXTRA
2447 clear_matches(curwin);
2448#endif
2449}
2450
2451/*
2452 * "col(string)" function
2453 */
2454 static void
2455f_col(typval_T *argvars, typval_T *rettv)
2456{
2457 colnr_T col = 0;
2458 pos_T *fp;
2459 int fnum = curbuf->b_fnum;
2460
2461 fp = var2fpos(&argvars[0], FALSE, &fnum);
2462 if (fp != NULL && fnum == curbuf->b_fnum)
2463 {
2464 if (fp->col == MAXCOL)
2465 {
2466 /* '> can be MAXCOL, get the length of the line then */
2467 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2468 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2469 else
2470 col = MAXCOL;
2471 }
2472 else
2473 {
2474 col = fp->col + 1;
2475#ifdef FEAT_VIRTUALEDIT
2476 /* col(".") when the cursor is on the NUL at the end of the line
2477 * because of "coladd" can be seen as an extra column. */
2478 if (virtual_active() && fp == &curwin->w_cursor)
2479 {
2480 char_u *p = ml_get_cursor();
2481
2482 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2483 curwin->w_virtcol - curwin->w_cursor.coladd))
2484 {
2485# ifdef FEAT_MBYTE
2486 int l;
2487
2488 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2489 col += l;
2490# else
2491 if (*p != NUL && p[1] == NUL)
2492 ++col;
2493# endif
2494 }
2495 }
2496#endif
2497 }
2498 }
2499 rettv->vval.v_number = col;
2500}
2501
2502#if defined(FEAT_INS_EXPAND)
2503/*
2504 * "complete()" function
2505 */
2506 static void
2507f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2508{
2509 int startcol;
2510
2511 if ((State & INSERT) == 0)
2512 {
2513 EMSG(_("E785: complete() can only be used in Insert mode"));
2514 return;
2515 }
2516
2517 /* Check for undo allowed here, because if something was already inserted
2518 * the line was already saved for undo and this check isn't done. */
2519 if (!undo_allowed())
2520 return;
2521
2522 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2523 {
2524 EMSG(_(e_invarg));
2525 return;
2526 }
2527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 if (startcol <= 0)
2530 return;
2531
2532 set_completion(startcol - 1, argvars[1].vval.v_list);
2533}
2534
2535/*
2536 * "complete_add()" function
2537 */
2538 static void
2539f_complete_add(typval_T *argvars, typval_T *rettv)
2540{
2541 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2542}
2543
2544/*
2545 * "complete_check()" function
2546 */
2547 static void
2548f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2549{
2550 int saved = RedrawingDisabled;
2551
2552 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002553 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554 rettv->vval.v_number = compl_interrupted;
2555 RedrawingDisabled = saved;
2556}
2557#endif
2558
2559/*
2560 * "confirm(message, buttons[, default [, type]])" function
2561 */
2562 static void
2563f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2564{
2565#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2566 char_u *message;
2567 char_u *buttons = NULL;
2568 char_u buf[NUMBUFLEN];
2569 char_u buf2[NUMBUFLEN];
2570 int def = 1;
2571 int type = VIM_GENERIC;
2572 char_u *typestr;
2573 int error = FALSE;
2574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002575 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 if (message == NULL)
2577 error = TRUE;
2578 if (argvars[1].v_type != VAR_UNKNOWN)
2579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002580 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581 if (buttons == NULL)
2582 error = TRUE;
2583 if (argvars[2].v_type != VAR_UNKNOWN)
2584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002585 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 if (argvars[3].v_type != VAR_UNKNOWN)
2587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 if (typestr == NULL)
2590 error = TRUE;
2591 else
2592 {
2593 switch (TOUPPER_ASC(*typestr))
2594 {
2595 case 'E': type = VIM_ERROR; break;
2596 case 'Q': type = VIM_QUESTION; break;
2597 case 'I': type = VIM_INFO; break;
2598 case 'W': type = VIM_WARNING; break;
2599 case 'G': type = VIM_GENERIC; break;
2600 }
2601 }
2602 }
2603 }
2604 }
2605
2606 if (buttons == NULL || *buttons == NUL)
2607 buttons = (char_u *)_("&Ok");
2608
2609 if (!error)
2610 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2611 def, NULL, FALSE);
2612#endif
2613}
2614
2615/*
2616 * "copy()" function
2617 */
2618 static void
2619f_copy(typval_T *argvars, typval_T *rettv)
2620{
2621 item_copy(&argvars[0], rettv, FALSE, 0);
2622}
2623
2624#ifdef FEAT_FLOAT
2625/*
2626 * "cos()" function
2627 */
2628 static void
2629f_cos(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 = cos(f);
2636 else
2637 rettv->vval.v_float = 0.0;
2638}
2639
2640/*
2641 * "cosh()" function
2642 */
2643 static void
2644f_cosh(typval_T *argvars, typval_T *rettv)
2645{
2646 float_T f = 0.0;
2647
2648 rettv->v_type = VAR_FLOAT;
2649 if (get_float_arg(argvars, &f) == OK)
2650 rettv->vval.v_float = cosh(f);
2651 else
2652 rettv->vval.v_float = 0.0;
2653}
2654#endif
2655
2656/*
2657 * "count()" function
2658 */
2659 static void
2660f_count(typval_T *argvars, typval_T *rettv)
2661{
2662 long n = 0;
2663 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002664 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665
Bram Moolenaar9966b212017-07-28 16:46:57 +02002666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002667 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002668
2669 if (argvars[0].v_type == VAR_STRING)
2670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002671 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002672 char_u *p = argvars[0].vval.v_string;
2673 char_u *next;
2674
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002675 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002676 {
2677 if (ic)
2678 {
2679 size_t len = STRLEN(expr);
2680
2681 while (*p != NUL)
2682 {
2683 if (MB_STRNICMP(p, expr, len) == 0)
2684 {
2685 ++n;
2686 p += len;
2687 }
2688 else
2689 MB_PTR_ADV(p);
2690 }
2691 }
2692 else
2693 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2694 != NULL)
2695 {
2696 ++n;
2697 p = next + STRLEN(expr);
2698 }
2699 }
2700
2701 }
2702 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 {
2704 listitem_T *li;
2705 list_T *l;
2706 long idx;
2707
2708 if ((l = argvars[0].vval.v_list) != NULL)
2709 {
2710 li = l->lv_first;
2711 if (argvars[2].v_type != VAR_UNKNOWN)
2712 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 if (argvars[3].v_type != VAR_UNKNOWN)
2714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (!error)
2717 {
2718 li = list_find(l, idx);
2719 if (li == NULL)
2720 EMSGN(_(e_listidx), idx);
2721 }
2722 }
2723 if (error)
2724 li = NULL;
2725 }
2726
2727 for ( ; li != NULL; li = li->li_next)
2728 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2729 ++n;
2730 }
2731 }
2732 else if (argvars[0].v_type == VAR_DICT)
2733 {
2734 int todo;
2735 dict_T *d;
2736 hashitem_T *hi;
2737
2738 if ((d = argvars[0].vval.v_dict) != NULL)
2739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 if (argvars[2].v_type != VAR_UNKNOWN)
2741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 if (argvars[3].v_type != VAR_UNKNOWN)
2743 EMSG(_(e_invarg));
2744 }
2745
2746 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2747 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2748 {
2749 if (!HASHITEM_EMPTY(hi))
2750 {
2751 --todo;
2752 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2753 ++n;
2754 }
2755 }
2756 }
2757 }
2758 else
2759 EMSG2(_(e_listdictarg), "count()");
2760 rettv->vval.v_number = n;
2761}
2762
2763/*
2764 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2765 *
2766 * Checks the existence of a cscope connection.
2767 */
2768 static void
2769f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2770{
2771#ifdef FEAT_CSCOPE
2772 int num = 0;
2773 char_u *dbpath = NULL;
2774 char_u *prepend = NULL;
2775 char_u buf[NUMBUFLEN];
2776
2777 if (argvars[0].v_type != VAR_UNKNOWN
2778 && argvars[1].v_type != VAR_UNKNOWN)
2779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002780 num = (int)tv_get_number(&argvars[0]);
2781 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002783 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 }
2785
2786 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2787#endif
2788}
2789
2790/*
2791 * "cursor(lnum, col)" function, or
2792 * "cursor(list)"
2793 *
2794 * Moves the cursor to the specified line and column.
2795 * Returns 0 when the position could be set, -1 otherwise.
2796 */
2797 static void
2798f_cursor(typval_T *argvars, typval_T *rettv)
2799{
2800 long line, col;
2801#ifdef FEAT_VIRTUALEDIT
2802 long coladd = 0;
2803#endif
2804 int set_curswant = TRUE;
2805
2806 rettv->vval.v_number = -1;
2807 if (argvars[1].v_type == VAR_UNKNOWN)
2808 {
2809 pos_T pos;
2810 colnr_T curswant = -1;
2811
2812 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2813 {
2814 EMSG(_(e_invarg));
2815 return;
2816 }
2817 line = pos.lnum;
2818 col = pos.col;
2819#ifdef FEAT_VIRTUALEDIT
2820 coladd = pos.coladd;
2821#endif
2822 if (curswant >= 0)
2823 {
2824 curwin->w_curswant = curswant - 1;
2825 set_curswant = FALSE;
2826 }
2827 }
2828 else
2829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002830 line = tv_get_lnum(argvars);
2831 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832#ifdef FEAT_VIRTUALEDIT
2833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002834 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835#endif
2836 }
2837 if (line < 0 || col < 0
2838#ifdef FEAT_VIRTUALEDIT
2839 || coladd < 0
2840#endif
2841 )
2842 return; /* type error; errmsg already given */
2843 if (line > 0)
2844 curwin->w_cursor.lnum = line;
2845 if (col > 0)
2846 curwin->w_cursor.col = col - 1;
2847#ifdef FEAT_VIRTUALEDIT
2848 curwin->w_cursor.coladd = coladd;
2849#endif
2850
2851 /* Make sure the cursor is in a valid position. */
2852 check_cursor();
2853#ifdef FEAT_MBYTE
2854 /* Correct cursor for multi-byte character. */
2855 if (has_mbyte)
2856 mb_adjust_cursor();
2857#endif
2858
2859 curwin->w_set_curswant = set_curswant;
2860 rettv->vval.v_number = 0;
2861}
2862
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002863#ifdef WIN3264
2864/*
2865 * "debugbreak()" function
2866 */
2867 static void
2868f_debugbreak(typval_T *argvars, typval_T *rettv)
2869{
2870 int pid;
2871
2872 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002873 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002874 if (pid == 0)
2875 EMSG(_(e_invarg));
2876 else
2877 {
2878 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2879
2880 if (hProcess != NULL)
2881 {
2882 DebugBreakProcess(hProcess);
2883 CloseHandle(hProcess);
2884 rettv->vval.v_number = OK;
2885 }
2886 }
2887}
2888#endif
2889
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890/*
2891 * "deepcopy()" function
2892 */
2893 static void
2894f_deepcopy(typval_T *argvars, typval_T *rettv)
2895{
2896 int noref = 0;
2897 int copyID;
2898
2899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002900 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901 if (noref < 0 || noref > 1)
2902 EMSG(_(e_invarg));
2903 else
2904 {
2905 copyID = get_copyID();
2906 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2907 }
2908}
2909
2910/*
2911 * "delete()" function
2912 */
2913 static void
2914f_delete(typval_T *argvars, typval_T *rettv)
2915{
2916 char_u nbuf[NUMBUFLEN];
2917 char_u *name;
2918 char_u *flags;
2919
2920 rettv->vval.v_number = -1;
2921 if (check_restricted() || check_secure())
2922 return;
2923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002924 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 if (name == NULL || *name == NUL)
2926 {
2927 EMSG(_(e_invarg));
2928 return;
2929 }
2930
2931 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002932 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 else
2934 flags = (char_u *)"";
2935
2936 if (*flags == NUL)
2937 /* delete a file */
2938 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2939 else if (STRCMP(flags, "d") == 0)
2940 /* delete an empty directory */
2941 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2942 else if (STRCMP(flags, "rf") == 0)
2943 /* delete a directory recursively */
2944 rettv->vval.v_number = delete_recursive(name);
2945 else
2946 EMSG2(_(e_invexpr2), flags);
2947}
2948
2949/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002950 * "deletebufline()" function
2951 */
2952 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002953f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002954{
2955 buf_T *buf;
2956 linenr_T first, last;
2957 linenr_T lnum;
2958 long count;
2959 int is_curbuf;
2960 buf_T *curbuf_save = NULL;
2961 win_T *curwin_save = NULL;
2962 tabpage_T *tp;
2963 win_T *wp;
2964
2965 buf = get_buf_tv(&argvars[0], FALSE);
2966 if (buf == NULL)
2967 {
2968 rettv->vval.v_number = 1; /* FAIL */
2969 return;
2970 }
2971 is_curbuf = buf == curbuf;
2972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002973 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002975 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002976 else
2977 last = first;
2978
2979 if (buf->b_ml.ml_mfp == NULL || first < 1
2980 || first > buf->b_ml.ml_line_count || last < first)
2981 {
2982 rettv->vval.v_number = 1; /* FAIL */
2983 return;
2984 }
2985
2986 if (!is_curbuf)
2987 {
2988 curbuf_save = curbuf;
2989 curwin_save = curwin;
2990 curbuf = buf;
2991 find_win_for_curbuf();
2992 }
2993 if (last > curbuf->b_ml.ml_line_count)
2994 last = curbuf->b_ml.ml_line_count;
2995 count = last - first + 1;
2996
2997 // When coming here from Insert mode, sync undo, so that this can be
2998 // undone separately from what was previously inserted.
2999 if (u_sync_once == 2)
3000 {
3001 u_sync_once = 1; // notify that u_sync() was called
3002 u_sync(TRUE);
3003 }
3004
3005 if (u_save(first - 1, last + 1) == FAIL)
3006 {
3007 rettv->vval.v_number = 1; /* FAIL */
3008 return;
3009 }
3010
3011 for (lnum = first; lnum <= last; ++lnum)
3012 ml_delete(first, TRUE);
3013
3014 FOR_ALL_TAB_WINDOWS(tp, wp)
3015 if (wp->w_buffer == buf)
3016 {
3017 if (wp->w_cursor.lnum > last)
3018 wp->w_cursor.lnum -= count;
3019 else if (wp->w_cursor.lnum> first)
3020 wp->w_cursor.lnum = first;
3021 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3022 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3023 }
3024 check_cursor_col();
3025 deleted_lines_mark(first, count);
3026
3027 if (!is_curbuf)
3028 {
3029 curbuf = curbuf_save;
3030 curwin = curwin_save;
3031 }
3032}
3033
3034/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 * "did_filetype()" function
3036 */
3037 static void
3038f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3039{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041}
3042
3043/*
3044 * "diff_filler()" function
3045 */
3046 static void
3047f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3048{
3049#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003050 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051#endif
3052}
3053
3054/*
3055 * "diff_hlID()" function
3056 */
3057 static void
3058f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3059{
3060#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003061 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003063 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 static int fnum = 0;
3065 static int change_start = 0;
3066 static int change_end = 0;
3067 static hlf_T hlID = (hlf_T)0;
3068 int filler_lines;
3069 int col;
3070
3071 if (lnum < 0) /* ignore type error in {lnum} arg */
3072 lnum = 0;
3073 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003074 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075 || fnum != curbuf->b_fnum)
3076 {
3077 /* New line, buffer, change: need to get the values. */
3078 filler_lines = diff_check(curwin, lnum);
3079 if (filler_lines < 0)
3080 {
3081 if (filler_lines == -1)
3082 {
3083 change_start = MAXCOL;
3084 change_end = -1;
3085 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3086 hlID = HLF_ADD; /* added line */
3087 else
3088 hlID = HLF_CHD; /* changed line */
3089 }
3090 else
3091 hlID = HLF_ADD; /* added line */
3092 }
3093 else
3094 hlID = (hlf_T)0;
3095 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003096 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 fnum = curbuf->b_fnum;
3098 }
3099
3100 if (hlID == HLF_CHD || hlID == HLF_TXD)
3101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 if (col >= change_start && col <= change_end)
3104 hlID = HLF_TXD; /* changed text */
3105 else
3106 hlID = HLF_CHD; /* changed line */
3107 }
3108 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3109#endif
3110}
3111
3112/*
3113 * "empty({expr})" function
3114 */
3115 static void
3116f_empty(typval_T *argvars, typval_T *rettv)
3117{
3118 int n = FALSE;
3119
3120 switch (argvars[0].v_type)
3121 {
3122 case VAR_STRING:
3123 case VAR_FUNC:
3124 n = argvars[0].vval.v_string == NULL
3125 || *argvars[0].vval.v_string == NUL;
3126 break;
3127 case VAR_PARTIAL:
3128 n = FALSE;
3129 break;
3130 case VAR_NUMBER:
3131 n = argvars[0].vval.v_number == 0;
3132 break;
3133 case VAR_FLOAT:
3134#ifdef FEAT_FLOAT
3135 n = argvars[0].vval.v_float == 0.0;
3136 break;
3137#endif
3138 case VAR_LIST:
3139 n = argvars[0].vval.v_list == NULL
3140 || argvars[0].vval.v_list->lv_first == NULL;
3141 break;
3142 case VAR_DICT:
3143 n = argvars[0].vval.v_dict == NULL
3144 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3145 break;
3146 case VAR_SPECIAL:
3147 n = argvars[0].vval.v_number != VVAL_TRUE;
3148 break;
3149
3150 case VAR_JOB:
3151#ifdef FEAT_JOB_CHANNEL
3152 n = argvars[0].vval.v_job == NULL
3153 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3154 break;
3155#endif
3156 case VAR_CHANNEL:
3157#ifdef FEAT_JOB_CHANNEL
3158 n = argvars[0].vval.v_channel == NULL
3159 || !channel_is_open(argvars[0].vval.v_channel);
3160 break;
3161#endif
3162 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003163 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 n = TRUE;
3165 break;
3166 }
3167
3168 rettv->vval.v_number = n;
3169}
3170
3171/*
3172 * "escape({string}, {chars})" function
3173 */
3174 static void
3175f_escape(typval_T *argvars, typval_T *rettv)
3176{
3177 char_u buf[NUMBUFLEN];
3178
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3180 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 rettv->v_type = VAR_STRING;
3182}
3183
3184/*
3185 * "eval()" function
3186 */
3187 static void
3188f_eval(typval_T *argvars, typval_T *rettv)
3189{
3190 char_u *s, *p;
3191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003192 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 if (s != NULL)
3194 s = skipwhite(s);
3195
3196 p = s;
3197 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3198 {
3199 if (p != NULL && !aborting())
3200 EMSG2(_(e_invexpr2), p);
3201 need_clr_eos = FALSE;
3202 rettv->v_type = VAR_NUMBER;
3203 rettv->vval.v_number = 0;
3204 }
3205 else if (*s != NUL)
3206 EMSG(_(e_trailing));
3207}
3208
3209/*
3210 * "eventhandler()" function
3211 */
3212 static void
3213f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3214{
3215 rettv->vval.v_number = vgetc_busy;
3216}
3217
3218/*
3219 * "executable()" function
3220 */
3221 static void
3222f_executable(typval_T *argvars, typval_T *rettv)
3223{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003224 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225
3226 /* Check in $PATH and also check directly if there is a directory name. */
3227 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3228 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3229}
3230
3231static garray_T redir_execute_ga;
3232
3233/*
3234 * Append "value[value_len]" to the execute() output.
3235 */
3236 void
3237execute_redir_str(char_u *value, int value_len)
3238{
3239 int len;
3240
3241 if (value_len == -1)
3242 len = (int)STRLEN(value); /* Append the entire string */
3243 else
3244 len = value_len; /* Append only "value_len" characters */
3245 if (ga_grow(&redir_execute_ga, len) == OK)
3246 {
3247 mch_memmove((char *)redir_execute_ga.ga_data
3248 + redir_execute_ga.ga_len, value, len);
3249 redir_execute_ga.ga_len += len;
3250 }
3251}
3252
3253/*
3254 * Get next line from a list.
3255 * Called by do_cmdline() to get the next line.
3256 * Returns allocated string, or NULL for end of function.
3257 */
3258
3259 static char_u *
3260get_list_line(
3261 int c UNUSED,
3262 void *cookie,
3263 int indent UNUSED)
3264{
3265 listitem_T **p = (listitem_T **)cookie;
3266 listitem_T *item = *p;
3267 char_u buf[NUMBUFLEN];
3268 char_u *s;
3269
3270 if (item == NULL)
3271 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003272 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 *p = item->li_next;
3274 return s == NULL ? NULL : vim_strsave(s);
3275}
3276
3277/*
3278 * "execute()" function
3279 */
3280 static void
3281f_execute(typval_T *argvars, typval_T *rettv)
3282{
3283 char_u *cmd = NULL;
3284 list_T *list = NULL;
3285 int save_msg_silent = msg_silent;
3286 int save_emsg_silent = emsg_silent;
3287 int save_emsg_noredir = emsg_noredir;
3288 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003289 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003291 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003292 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293
3294 rettv->vval.v_string = NULL;
3295 rettv->v_type = VAR_STRING;
3296
3297 if (argvars[0].v_type == VAR_LIST)
3298 {
3299 list = argvars[0].vval.v_list;
3300 if (list == NULL || list->lv_first == NULL)
3301 /* empty list, no commands, empty output */
3302 return;
3303 ++list->lv_refcount;
3304 }
3305 else
3306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003307 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 if (cmd == NULL)
3309 return;
3310 }
3311
3312 if (argvars[1].v_type != VAR_UNKNOWN)
3313 {
3314 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003315 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316
3317 if (s == NULL)
3318 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003319 if (*s == NUL)
3320 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003321 if (STRNCMP(s, "silent", 6) == 0)
3322 ++msg_silent;
3323 if (STRCMP(s, "silent!") == 0)
3324 {
3325 emsg_silent = TRUE;
3326 emsg_noredir = TRUE;
3327 }
3328 }
3329 else
3330 ++msg_silent;
3331
3332 if (redir_execute)
3333 save_ga = redir_execute_ga;
3334 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3335 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003336 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003337 if (!echo_output)
3338 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003339
3340 if (cmd != NULL)
3341 do_cmdline_cmd(cmd);
3342 else
3343 {
3344 listitem_T *item = list->lv_first;
3345
3346 do_cmdline(NULL, get_list_line, (void *)&item,
3347 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3348 --list->lv_refcount;
3349 }
3350
Bram Moolenaard297f352017-01-29 20:31:21 +01003351 /* Need to append a NUL to the result. */
3352 if (ga_grow(&redir_execute_ga, 1) == OK)
3353 {
3354 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3355 rettv->vval.v_string = redir_execute_ga.ga_data;
3356 }
3357 else
3358 {
3359 ga_clear(&redir_execute_ga);
3360 rettv->vval.v_string = NULL;
3361 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362 msg_silent = save_msg_silent;
3363 emsg_silent = save_emsg_silent;
3364 emsg_noredir = save_emsg_noredir;
3365
3366 redir_execute = save_redir_execute;
3367 if (redir_execute)
3368 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003369 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003371 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003372 if (echo_output)
3373 // When not working silently: put it in column zero. A following
3374 // "echon" will overwrite the message, unavoidably.
3375 msg_col = 0;
3376 else
3377 // When working silently: Put it back where it was, since nothing
3378 // should have been written.
3379 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380}
3381
3382/*
3383 * "exepath()" function
3384 */
3385 static void
3386f_exepath(typval_T *argvars, typval_T *rettv)
3387{
3388 char_u *p = NULL;
3389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003390 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 rettv->v_type = VAR_STRING;
3392 rettv->vval.v_string = p;
3393}
3394
3395/*
3396 * "exists()" function
3397 */
3398 static void
3399f_exists(typval_T *argvars, typval_T *rettv)
3400{
3401 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 if (*p == '$') /* environment variable */
3406 {
3407 /* first try "normal" environment variables (fast) */
3408 if (mch_getenv(p + 1) != NULL)
3409 n = TRUE;
3410 else
3411 {
3412 /* try expanding things like $VIM and ${HOME} */
3413 p = expand_env_save(p);
3414 if (p != NULL && *p != '$')
3415 n = TRUE;
3416 vim_free(p);
3417 }
3418 }
3419 else if (*p == '&' || *p == '+') /* option */
3420 {
3421 n = (get_option_tv(&p, NULL, TRUE) == OK);
3422 if (*skipwhite(p) != NUL)
3423 n = FALSE; /* trailing garbage */
3424 }
3425 else if (*p == '*') /* internal or user defined function */
3426 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003427 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 }
3429 else if (*p == ':')
3430 {
3431 n = cmd_exists(p + 1);
3432 }
3433 else if (*p == '#')
3434 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 if (p[1] == '#')
3436 n = autocmd_supported(p + 2);
3437 else
3438 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 }
3440 else /* internal variable */
3441 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003442 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 }
3444
3445 rettv->vval.v_number = n;
3446}
3447
3448#ifdef FEAT_FLOAT
3449/*
3450 * "exp()" function
3451 */
3452 static void
3453f_exp(typval_T *argvars, typval_T *rettv)
3454{
3455 float_T f = 0.0;
3456
3457 rettv->v_type = VAR_FLOAT;
3458 if (get_float_arg(argvars, &f) == OK)
3459 rettv->vval.v_float = exp(f);
3460 else
3461 rettv->vval.v_float = 0.0;
3462}
3463#endif
3464
3465/*
3466 * "expand()" function
3467 */
3468 static void
3469f_expand(typval_T *argvars, typval_T *rettv)
3470{
3471 char_u *s;
3472 int len;
3473 char_u *errormsg;
3474 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3475 expand_T xpc;
3476 int error = FALSE;
3477 char_u *result;
3478
3479 rettv->v_type = VAR_STRING;
3480 if (argvars[1].v_type != VAR_UNKNOWN
3481 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003482 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 && !error)
3484 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003485 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003488 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003489 if (*s == '%' || *s == '#' || *s == '<')
3490 {
3491 ++emsg_off;
3492 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3493 --emsg_off;
3494 if (rettv->v_type == VAR_LIST)
3495 {
3496 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3497 list_append_string(rettv->vval.v_list, result, -1);
3498 else
3499 vim_free(result);
3500 }
3501 else
3502 rettv->vval.v_string = result;
3503 }
3504 else
3505 {
3506 /* When the optional second argument is non-zero, don't remove matches
3507 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3508 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003510 options |= WILD_KEEP_ALL;
3511 if (!error)
3512 {
3513 ExpandInit(&xpc);
3514 xpc.xp_context = EXPAND_FILES;
3515 if (p_wic)
3516 options += WILD_ICASE;
3517 if (rettv->v_type == VAR_STRING)
3518 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3519 options, WILD_ALL);
3520 else if (rettv_list_alloc(rettv) != FAIL)
3521 {
3522 int i;
3523
3524 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3525 for (i = 0; i < xpc.xp_numfiles; i++)
3526 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3527 ExpandCleanup(&xpc);
3528 }
3529 }
3530 else
3531 rettv->vval.v_string = NULL;
3532 }
3533}
3534
3535/*
3536 * "extend(list, list [, idx])" function
3537 * "extend(dict, dict [, action])" function
3538 */
3539 static void
3540f_extend(typval_T *argvars, typval_T *rettv)
3541{
3542 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3543
3544 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3545 {
3546 list_T *l1, *l2;
3547 listitem_T *item;
3548 long before;
3549 int error = FALSE;
3550
3551 l1 = argvars[0].vval.v_list;
3552 l2 = argvars[1].vval.v_list;
3553 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3554 && l2 != NULL)
3555 {
3556 if (argvars[2].v_type != VAR_UNKNOWN)
3557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003558 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003559 if (error)
3560 return; /* type error; errmsg already given */
3561
3562 if (before == l1->lv_len)
3563 item = NULL;
3564 else
3565 {
3566 item = list_find(l1, before);
3567 if (item == NULL)
3568 {
3569 EMSGN(_(e_listidx), before);
3570 return;
3571 }
3572 }
3573 }
3574 else
3575 item = NULL;
3576 list_extend(l1, l2, item);
3577
3578 copy_tv(&argvars[0], rettv);
3579 }
3580 }
3581 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3582 {
3583 dict_T *d1, *d2;
3584 char_u *action;
3585 int i;
3586
3587 d1 = argvars[0].vval.v_dict;
3588 d2 = argvars[1].vval.v_dict;
3589 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3590 && d2 != NULL)
3591 {
3592 /* Check the third argument. */
3593 if (argvars[2].v_type != VAR_UNKNOWN)
3594 {
3595 static char *(av[]) = {"keep", "force", "error"};
3596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 if (action == NULL)
3599 return; /* type error; errmsg already given */
3600 for (i = 0; i < 3; ++i)
3601 if (STRCMP(action, av[i]) == 0)
3602 break;
3603 if (i == 3)
3604 {
3605 EMSG2(_(e_invarg2), action);
3606 return;
3607 }
3608 }
3609 else
3610 action = (char_u *)"force";
3611
3612 dict_extend(d1, d2, action);
3613
3614 copy_tv(&argvars[0], rettv);
3615 }
3616 }
3617 else
3618 EMSG2(_(e_listdictarg), "extend()");
3619}
3620
3621/*
3622 * "feedkeys()" function
3623 */
3624 static void
3625f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3626{
3627 int remap = TRUE;
3628 int insert = FALSE;
3629 char_u *keys, *flags;
3630 char_u nbuf[NUMBUFLEN];
3631 int typed = FALSE;
3632 int execute = FALSE;
3633 int dangerous = FALSE;
3634 char_u *keys_esc;
3635
3636 /* This is not allowed in the sandbox. If the commands would still be
3637 * executed in the sandbox it would be OK, but it probably happens later,
3638 * when "sandbox" is no longer set. */
3639 if (check_secure())
3640 return;
3641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003642 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643
3644 if (argvars[1].v_type != VAR_UNKNOWN)
3645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003646 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647 for ( ; *flags != NUL; ++flags)
3648 {
3649 switch (*flags)
3650 {
3651 case 'n': remap = FALSE; break;
3652 case 'm': remap = TRUE; break;
3653 case 't': typed = TRUE; break;
3654 case 'i': insert = TRUE; break;
3655 case 'x': execute = TRUE; break;
3656 case '!': dangerous = TRUE; break;
3657 }
3658 }
3659 }
3660
3661 if (*keys != NUL || execute)
3662 {
3663 /* Need to escape K_SPECIAL and CSI before putting the string in the
3664 * typeahead buffer. */
3665 keys_esc = vim_strsave_escape_csi(keys);
3666 if (keys_esc != NULL)
3667 {
3668 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3669 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3670 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003671 if (vgetc_busy
3672#ifdef FEAT_TIMERS
3673 || timer_busy
3674#endif
3675 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 typebuf_was_filled = TRUE;
3677 if (execute)
3678 {
3679 int save_msg_scroll = msg_scroll;
3680
3681 /* Avoid a 1 second delay when the keys start Insert mode. */
3682 msg_scroll = FALSE;
3683
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003684 if (!dangerous)
3685 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003686 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003687 if (!dangerous)
3688 --ex_normal_busy;
3689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 msg_scroll |= save_msg_scroll;
3691 }
3692 }
3693 }
3694}
3695
3696/*
3697 * "filereadable()" function
3698 */
3699 static void
3700f_filereadable(typval_T *argvars, typval_T *rettv)
3701{
3702 int fd;
3703 char_u *p;
3704 int n;
3705
3706#ifndef O_NONBLOCK
3707# define O_NONBLOCK 0
3708#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003709 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3711 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3712 {
3713 n = TRUE;
3714 close(fd);
3715 }
3716 else
3717 n = FALSE;
3718
3719 rettv->vval.v_number = n;
3720}
3721
3722/*
3723 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3724 * rights to write into.
3725 */
3726 static void
3727f_filewritable(typval_T *argvars, typval_T *rettv)
3728{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003729 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730}
3731
3732 static void
3733findfilendir(
3734 typval_T *argvars UNUSED,
3735 typval_T *rettv,
3736 int find_what UNUSED)
3737{
3738#ifdef FEAT_SEARCHPATH
3739 char_u *fname;
3740 char_u *fresult = NULL;
3741 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3742 char_u *p;
3743 char_u pathbuf[NUMBUFLEN];
3744 int count = 1;
3745 int first = TRUE;
3746 int error = FALSE;
3747#endif
3748
3749 rettv->vval.v_string = NULL;
3750 rettv->v_type = VAR_STRING;
3751
3752#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754
3755 if (argvars[1].v_type != VAR_UNKNOWN)
3756 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003757 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003758 if (p == NULL)
3759 error = TRUE;
3760 else
3761 {
3762 if (*p != NUL)
3763 path = p;
3764
3765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003766 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 }
3768 }
3769
3770 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3771 error = TRUE;
3772
3773 if (*fname != NUL && !error)
3774 {
3775 do
3776 {
3777 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3778 vim_free(fresult);
3779 fresult = find_file_in_path_option(first ? fname : NULL,
3780 first ? (int)STRLEN(fname) : 0,
3781 0, first, path,
3782 find_what,
3783 curbuf->b_ffname,
3784 find_what == FINDFILE_DIR
3785 ? (char_u *)"" : curbuf->b_p_sua);
3786 first = FALSE;
3787
3788 if (fresult != NULL && rettv->v_type == VAR_LIST)
3789 list_append_string(rettv->vval.v_list, fresult, -1);
3790
3791 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3792 }
3793
3794 if (rettv->v_type == VAR_STRING)
3795 rettv->vval.v_string = fresult;
3796#endif
3797}
3798
3799/*
3800 * "filter()" function
3801 */
3802 static void
3803f_filter(typval_T *argvars, typval_T *rettv)
3804{
3805 filter_map(argvars, rettv, FALSE);
3806}
3807
3808/*
3809 * "finddir({fname}[, {path}[, {count}]])" function
3810 */
3811 static void
3812f_finddir(typval_T *argvars, typval_T *rettv)
3813{
3814 findfilendir(argvars, rettv, FINDFILE_DIR);
3815}
3816
3817/*
3818 * "findfile({fname}[, {path}[, {count}]])" function
3819 */
3820 static void
3821f_findfile(typval_T *argvars, typval_T *rettv)
3822{
3823 findfilendir(argvars, rettv, FINDFILE_FILE);
3824}
3825
3826#ifdef FEAT_FLOAT
3827/*
3828 * "float2nr({float})" function
3829 */
3830 static void
3831f_float2nr(typval_T *argvars, typval_T *rettv)
3832{
3833 float_T f = 0.0;
3834
3835 if (get_float_arg(argvars, &f) == OK)
3836 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003837 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003838 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003839 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003840 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003841 else
3842 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 }
3844}
3845
3846/*
3847 * "floor({float})" function
3848 */
3849 static void
3850f_floor(typval_T *argvars, typval_T *rettv)
3851{
3852 float_T f = 0.0;
3853
3854 rettv->v_type = VAR_FLOAT;
3855 if (get_float_arg(argvars, &f) == OK)
3856 rettv->vval.v_float = floor(f);
3857 else
3858 rettv->vval.v_float = 0.0;
3859}
3860
3861/*
3862 * "fmod()" function
3863 */
3864 static void
3865f_fmod(typval_T *argvars, typval_T *rettv)
3866{
3867 float_T fx = 0.0, fy = 0.0;
3868
3869 rettv->v_type = VAR_FLOAT;
3870 if (get_float_arg(argvars, &fx) == OK
3871 && get_float_arg(&argvars[1], &fy) == OK)
3872 rettv->vval.v_float = fmod(fx, fy);
3873 else
3874 rettv->vval.v_float = 0.0;
3875}
3876#endif
3877
3878/*
3879 * "fnameescape({string})" function
3880 */
3881 static void
3882f_fnameescape(typval_T *argvars, typval_T *rettv)
3883{
3884 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 rettv->v_type = VAR_STRING;
3887}
3888
3889/*
3890 * "fnamemodify({fname}, {mods})" function
3891 */
3892 static void
3893f_fnamemodify(typval_T *argvars, typval_T *rettv)
3894{
3895 char_u *fname;
3896 char_u *mods;
3897 int usedlen = 0;
3898 int len;
3899 char_u *fbuf = NULL;
3900 char_u buf[NUMBUFLEN];
3901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003902 fname = tv_get_string_chk(&argvars[0]);
3903 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003904 if (fname == NULL || mods == NULL)
3905 fname = NULL;
3906 else
3907 {
3908 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003909 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910 }
3911
3912 rettv->v_type = VAR_STRING;
3913 if (fname == NULL)
3914 rettv->vval.v_string = NULL;
3915 else
3916 rettv->vval.v_string = vim_strnsave(fname, len);
3917 vim_free(fbuf);
3918}
3919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920/*
3921 * "foldclosed()" function
3922 */
3923 static void
3924foldclosed_both(
3925 typval_T *argvars UNUSED,
3926 typval_T *rettv,
3927 int end UNUSED)
3928{
3929#ifdef FEAT_FOLDING
3930 linenr_T lnum;
3931 linenr_T first, last;
3932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003933 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3935 {
3936 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3937 {
3938 if (end)
3939 rettv->vval.v_number = (varnumber_T)last;
3940 else
3941 rettv->vval.v_number = (varnumber_T)first;
3942 return;
3943 }
3944 }
3945#endif
3946 rettv->vval.v_number = -1;
3947}
3948
3949/*
3950 * "foldclosed()" function
3951 */
3952 static void
3953f_foldclosed(typval_T *argvars, typval_T *rettv)
3954{
3955 foldclosed_both(argvars, rettv, FALSE);
3956}
3957
3958/*
3959 * "foldclosedend()" function
3960 */
3961 static void
3962f_foldclosedend(typval_T *argvars, typval_T *rettv)
3963{
3964 foldclosed_both(argvars, rettv, TRUE);
3965}
3966
3967/*
3968 * "foldlevel()" function
3969 */
3970 static void
3971f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3972{
3973#ifdef FEAT_FOLDING
3974 linenr_T lnum;
3975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003976 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3978 rettv->vval.v_number = foldLevel(lnum);
3979#endif
3980}
3981
3982/*
3983 * "foldtext()" function
3984 */
3985 static void
3986f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3987{
3988#ifdef FEAT_FOLDING
3989 linenr_T foldstart;
3990 linenr_T foldend;
3991 char_u *dashes;
3992 linenr_T lnum;
3993 char_u *s;
3994 char_u *r;
3995 int len;
3996 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003997 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998#endif
3999
4000 rettv->v_type = VAR_STRING;
4001 rettv->vval.v_string = NULL;
4002#ifdef FEAT_FOLDING
4003 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4004 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4005 dashes = get_vim_var_str(VV_FOLDDASHES);
4006 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4007 && dashes != NULL)
4008 {
4009 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004010 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011 if (!linewhite(lnum))
4012 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013
4014 /* Find interesting text in this line. */
4015 s = skipwhite(ml_get(lnum));
4016 /* skip C comment-start */
4017 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4018 {
4019 s = skipwhite(s + 2);
4020 if (*skipwhite(s) == NUL
4021 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4022 {
4023 s = skipwhite(ml_get(lnum + 1));
4024 if (*s == '*')
4025 s = skipwhite(s + 1);
4026 }
4027 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004028 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004029 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 r = alloc((unsigned)(STRLEN(txt)
4031 + STRLEN(dashes) /* for %s */
4032 + 20 /* for %3ld */
4033 + STRLEN(s))); /* concatenated */
4034 if (r != NULL)
4035 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004036 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037 len = (int)STRLEN(r);
4038 STRCAT(r, s);
4039 /* remove 'foldmarker' and 'commentstring' */
4040 foldtext_cleanup(r + len);
4041 rettv->vval.v_string = r;
4042 }
4043 }
4044#endif
4045}
4046
4047/*
4048 * "foldtextresult(lnum)" function
4049 */
4050 static void
4051f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4052{
4053#ifdef FEAT_FOLDING
4054 linenr_T lnum;
4055 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004056 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057 foldinfo_T foldinfo;
4058 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004059 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060#endif
4061
4062 rettv->v_type = VAR_STRING;
4063 rettv->vval.v_string = NULL;
4064#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004065 if (entered)
4066 return; /* reject recursive use */
4067 entered = TRUE;
4068
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004069 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 /* treat illegal types and illegal string values for {lnum} the same */
4071 if (lnum < 0)
4072 lnum = 0;
4073 fold_count = foldedCount(curwin, lnum, &foldinfo);
4074 if (fold_count > 0)
4075 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004076 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4077 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078 if (text == buf)
4079 text = vim_strsave(text);
4080 rettv->vval.v_string = text;
4081 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004082
4083 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004084#endif
4085}
4086
4087/*
4088 * "foreground()" function
4089 */
4090 static void
4091f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4092{
4093#ifdef FEAT_GUI
4094 if (gui.in_use)
4095 gui_mch_set_foreground();
4096#else
4097# ifdef WIN32
4098 win32_set_foreground();
4099# endif
4100#endif
4101}
4102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004104common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105{
4106 char_u *s;
4107 char_u *name;
4108 int use_string = FALSE;
4109 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111
4112 if (argvars[0].v_type == VAR_FUNC)
4113 {
4114 /* function(MyFunc, [arg], dict) */
4115 s = argvars[0].vval.v_string;
4116 }
4117 else if (argvars[0].v_type == VAR_PARTIAL
4118 && argvars[0].vval.v_partial != NULL)
4119 {
4120 /* function(dict.MyFunc, [arg]) */
4121 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004122 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123 }
4124 else
4125 {
4126 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004127 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 use_string = TRUE;
4129 }
4130
Bram Moolenaar843b8842016-08-21 14:36:15 +02004131 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 {
4133 name = s;
4134 trans_name = trans_function_name(&name, FALSE,
4135 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4136 if (*name != NUL)
4137 s = NULL;
4138 }
4139
Bram Moolenaar843b8842016-08-21 14:36:15 +02004140 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4141 || (is_funcref && trans_name == NULL))
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004142 EMSG2(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 else if (trans_name != NULL && (is_funcref
4145 ? find_func(trans_name) == NULL
4146 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 EMSG2(_("E700: Unknown function: %s"), s);
4148 else
4149 {
4150 int dict_idx = 0;
4151 int arg_idx = 0;
4152 list_T *list = NULL;
4153
4154 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4155 {
4156 char sid_buf[25];
4157 int off = *s == 's' ? 2 : 5;
4158
4159 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4160 * also be called from another script. Using trans_function_name()
4161 * would also work, but some plugins depend on the name being
4162 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004163 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4165 if (name != NULL)
4166 {
4167 STRCPY(name, sid_buf);
4168 STRCAT(name, s + off);
4169 }
4170 }
4171 else
4172 name = vim_strsave(s);
4173
4174 if (argvars[1].v_type != VAR_UNKNOWN)
4175 {
4176 if (argvars[2].v_type != VAR_UNKNOWN)
4177 {
4178 /* function(name, [args], dict) */
4179 arg_idx = 1;
4180 dict_idx = 2;
4181 }
4182 else if (argvars[1].v_type == VAR_DICT)
4183 /* function(name, dict) */
4184 dict_idx = 1;
4185 else
4186 /* function(name, [args]) */
4187 arg_idx = 1;
4188 if (dict_idx > 0)
4189 {
4190 if (argvars[dict_idx].v_type != VAR_DICT)
4191 {
4192 EMSG(_("E922: expected a dict"));
4193 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004195 }
4196 if (argvars[dict_idx].vval.v_dict == NULL)
4197 dict_idx = 0;
4198 }
4199 if (arg_idx > 0)
4200 {
4201 if (argvars[arg_idx].v_type != VAR_LIST)
4202 {
4203 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4204 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004205 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 }
4207 list = argvars[arg_idx].vval.v_list;
4208 if (list == NULL || list->lv_len == 0)
4209 arg_idx = 0;
4210 }
4211 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004212 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213 {
4214 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4215
4216 /* result is a VAR_PARTIAL */
4217 if (pt == NULL)
4218 vim_free(name);
4219 else
4220 {
4221 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4222 {
4223 listitem_T *li;
4224 int i = 0;
4225 int arg_len = 0;
4226 int lv_len = 0;
4227
4228 if (arg_pt != NULL)
4229 arg_len = arg_pt->pt_argc;
4230 if (list != NULL)
4231 lv_len = list->lv_len;
4232 pt->pt_argc = arg_len + lv_len;
4233 pt->pt_argv = (typval_T *)alloc(
4234 sizeof(typval_T) * pt->pt_argc);
4235 if (pt->pt_argv == NULL)
4236 {
4237 vim_free(pt);
4238 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004239 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 for (i = 0; i < arg_len; i++)
4242 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4243 if (lv_len > 0)
4244 for (li = list->lv_first; li != NULL;
4245 li = li->li_next)
4246 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 }
4248
4249 /* For "function(dict.func, [], dict)" and "func" is a partial
4250 * use "dict". That is backwards compatible. */
4251 if (dict_idx > 0)
4252 {
4253 /* The dict is bound explicitly, pt_auto is FALSE. */
4254 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4255 ++pt->pt_dict->dv_refcount;
4256 }
4257 else if (arg_pt != NULL)
4258 {
4259 /* If the dict was bound automatically the result is also
4260 * bound automatically. */
4261 pt->pt_dict = arg_pt->pt_dict;
4262 pt->pt_auto = arg_pt->pt_auto;
4263 if (pt->pt_dict != NULL)
4264 ++pt->pt_dict->dv_refcount;
4265 }
4266
4267 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004268 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4269 {
4270 pt->pt_func = arg_pt->pt_func;
4271 func_ptr_ref(pt->pt_func);
4272 vim_free(name);
4273 }
4274 else if (is_funcref)
4275 {
4276 pt->pt_func = find_func(trans_name);
4277 func_ptr_ref(pt->pt_func);
4278 vim_free(name);
4279 }
4280 else
4281 {
4282 pt->pt_name = name;
4283 func_ref(name);
4284 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285 }
4286 rettv->v_type = VAR_PARTIAL;
4287 rettv->vval.v_partial = pt;
4288 }
4289 else
4290 {
4291 /* result is a VAR_FUNC */
4292 rettv->v_type = VAR_FUNC;
4293 rettv->vval.v_string = name;
4294 func_ref(name);
4295 }
4296 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297theend:
4298 vim_free(trans_name);
4299}
4300
4301/*
4302 * "funcref()" function
4303 */
4304 static void
4305f_funcref(typval_T *argvars, typval_T *rettv)
4306{
4307 common_function(argvars, rettv, TRUE);
4308}
4309
4310/*
4311 * "function()" function
4312 */
4313 static void
4314f_function(typval_T *argvars, typval_T *rettv)
4315{
4316 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317}
4318
4319/*
4320 * "garbagecollect()" function
4321 */
4322 static void
4323f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4324{
4325 /* This is postponed until we are back at the toplevel, because we may be
4326 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4327 want_garbage_collect = TRUE;
4328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004329 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 garbage_collect_at_exit = TRUE;
4331}
4332
4333/*
4334 * "get()" function
4335 */
4336 static void
4337f_get(typval_T *argvars, typval_T *rettv)
4338{
4339 listitem_T *li;
4340 list_T *l;
4341 dictitem_T *di;
4342 dict_T *d;
4343 typval_T *tv = NULL;
4344
4345 if (argvars[0].v_type == VAR_LIST)
4346 {
4347 if ((l = argvars[0].vval.v_list) != NULL)
4348 {
4349 int error = FALSE;
4350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004351 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352 if (!error && li != NULL)
4353 tv = &li->li_tv;
4354 }
4355 }
4356 else if (argvars[0].v_type == VAR_DICT)
4357 {
4358 if ((d = argvars[0].vval.v_dict) != NULL)
4359 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004360 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 if (di != NULL)
4362 tv = &di->di_tv;
4363 }
4364 }
4365 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4366 {
4367 partial_T *pt;
4368 partial_T fref_pt;
4369
4370 if (argvars[0].v_type == VAR_PARTIAL)
4371 pt = argvars[0].vval.v_partial;
4372 else
4373 {
4374 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4375 fref_pt.pt_name = argvars[0].vval.v_string;
4376 pt = &fref_pt;
4377 }
4378
4379 if (pt != NULL)
4380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004381 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004382 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383
4384 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4385 {
4386 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004387 n = partial_name(pt);
4388 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389 rettv->vval.v_string = NULL;
4390 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 {
4392 rettv->vval.v_string = vim_strsave(n);
4393 if (rettv->v_type == VAR_FUNC)
4394 func_ref(rettv->vval.v_string);
4395 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 }
4397 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004398 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004399 else if (STRCMP(what, "args") == 0)
4400 {
4401 rettv->v_type = VAR_LIST;
4402 if (rettv_list_alloc(rettv) == OK)
4403 {
4404 int i;
4405
4406 for (i = 0; i < pt->pt_argc; ++i)
4407 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4408 }
4409 }
4410 else
4411 EMSG2(_(e_invarg2), what);
4412 return;
4413 }
4414 }
4415 else
4416 EMSG2(_(e_listdictarg), "get()");
4417
4418 if (tv == NULL)
4419 {
4420 if (argvars[2].v_type != VAR_UNKNOWN)
4421 copy_tv(&argvars[2], rettv);
4422 }
4423 else
4424 copy_tv(tv, rettv);
4425}
4426
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004427#ifdef FEAT_SIGNS
4428/*
4429 * Returns information about signs placed in a buffer as list of dicts.
4430 */
4431 static void
4432get_buffer_signs(buf_T *buf, list_T *l)
4433{
4434 signlist_T *sign;
Bram Moolenaar162b7142018-12-21 15:17:36 +01004435 dict_T *d;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004436
Bram Moolenaar162b7142018-12-21 15:17:36 +01004437 FOR_ALL_SIGNS_IN_BUF(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004438 {
Bram Moolenaar162b7142018-12-21 15:17:36 +01004439 if ((d = sign_get_info(sign)) != NULL)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004440 list_append_dict(l, d);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004441 }
4442}
4443#endif
4444
4445/*
4446 * Returns buffer options, variables and other attributes in a dictionary.
4447 */
4448 static dict_T *
4449get_buffer_info(buf_T *buf)
4450{
4451 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004452 tabpage_T *tp;
4453 win_T *wp;
4454 list_T *windows;
4455
4456 dict = dict_alloc();
4457 if (dict == NULL)
4458 return NULL;
4459
Bram Moolenaare0be1672018-07-08 16:50:37 +02004460 dict_add_number(dict, "bufnr", buf->b_fnum);
4461 dict_add_string(dict, "name", buf->b_ffname);
4462 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4463 : buflist_findlnum(buf));
4464 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4465 dict_add_number(dict, "listed", buf->b_p_bl);
4466 dict_add_number(dict, "changed", bufIsChanged(buf));
4467 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4468 dict_add_number(dict, "hidden",
4469 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004470
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004471 /* Get a reference to buffer variables */
4472 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004473
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004474 /* List of windows displaying this buffer */
4475 windows = list_alloc();
4476 if (windows != NULL)
4477 {
4478 FOR_ALL_TAB_WINDOWS(tp, wp)
4479 if (wp->w_buffer == buf)
4480 list_append_number(windows, (varnumber_T)wp->w_id);
4481 dict_add_list(dict, "windows", windows);
4482 }
4483
4484#ifdef FEAT_SIGNS
4485 if (buf->b_signlist != NULL)
4486 {
4487 /* List of signs placed in this buffer */
4488 list_T *signs = list_alloc();
4489 if (signs != NULL)
4490 {
4491 get_buffer_signs(buf, signs);
4492 dict_add_list(dict, "signs", signs);
4493 }
4494 }
4495#endif
4496
4497 return dict;
4498}
4499
4500/*
4501 * "getbufinfo()" function
4502 */
4503 static void
4504f_getbufinfo(typval_T *argvars, typval_T *rettv)
4505{
4506 buf_T *buf = NULL;
4507 buf_T *argbuf = NULL;
4508 dict_T *d;
4509 int filtered = FALSE;
4510 int sel_buflisted = FALSE;
4511 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004512 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004513
4514 if (rettv_list_alloc(rettv) != OK)
4515 return;
4516
4517 /* List of all the buffers or selected buffers */
4518 if (argvars[0].v_type == VAR_DICT)
4519 {
4520 dict_T *sel_d = argvars[0].vval.v_dict;
4521
4522 if (sel_d != NULL)
4523 {
4524 dictitem_T *di;
4525
4526 filtered = TRUE;
4527
4528 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004529 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004530 sel_buflisted = TRUE;
4531
4532 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004533 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004534 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004535
4536 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004537 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004538 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004539 }
4540 }
4541 else if (argvars[0].v_type != VAR_UNKNOWN)
4542 {
4543 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004545 ++emsg_off;
4546 argbuf = get_buf_tv(&argvars[0], FALSE);
4547 --emsg_off;
4548 if (argbuf == NULL)
4549 return;
4550 }
4551
4552 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004553 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004554 {
4555 if (argbuf != NULL && argbuf != buf)
4556 continue;
4557 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004558 || (sel_buflisted && !buf->b_p_bl)
4559 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004560 continue;
4561
4562 d = get_buffer_info(buf);
4563 if (d != NULL)
4564 list_append_dict(rettv->vval.v_list, d);
4565 if (argbuf != NULL)
4566 return;
4567 }
4568}
4569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570/*
4571 * Get line or list of lines from buffer "buf" into "rettv".
4572 * Return a range (from start to end) of lines in rettv from the specified
4573 * buffer.
4574 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4575 */
4576 static void
4577get_buffer_lines(
4578 buf_T *buf,
4579 linenr_T start,
4580 linenr_T end,
4581 int retlist,
4582 typval_T *rettv)
4583{
4584 char_u *p;
4585
4586 rettv->v_type = VAR_STRING;
4587 rettv->vval.v_string = NULL;
4588 if (retlist && rettv_list_alloc(rettv) == FAIL)
4589 return;
4590
4591 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4592 return;
4593
4594 if (!retlist)
4595 {
4596 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4597 p = ml_get_buf(buf, start, FALSE);
4598 else
4599 p = (char_u *)"";
4600 rettv->vval.v_string = vim_strsave(p);
4601 }
4602 else
4603 {
4604 if (end < start)
4605 return;
4606
4607 if (start < 1)
4608 start = 1;
4609 if (end > buf->b_ml.ml_line_count)
4610 end = buf->b_ml.ml_line_count;
4611 while (start <= end)
4612 if (list_append_string(rettv->vval.v_list,
4613 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4614 break;
4615 }
4616}
4617
4618/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619 * "getbufline()" function
4620 */
4621 static void
4622f_getbufline(typval_T *argvars, typval_T *rettv)
4623{
4624 linenr_T lnum;
4625 linenr_T end;
4626 buf_T *buf;
4627
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 ++emsg_off;
4630 buf = get_buf_tv(&argvars[0], FALSE);
4631 --emsg_off;
4632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004633 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634 if (argvars[2].v_type == VAR_UNKNOWN)
4635 end = lnum;
4636 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638
4639 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4640}
4641
4642/*
4643 * "getbufvar()" function
4644 */
4645 static void
4646f_getbufvar(typval_T *argvars, typval_T *rettv)
4647{
4648 buf_T *buf;
4649 buf_T *save_curbuf;
4650 char_u *varname;
4651 dictitem_T *v;
4652 int done = FALSE;
4653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4655 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656 ++emsg_off;
4657 buf = get_buf_tv(&argvars[0], FALSE);
4658
4659 rettv->v_type = VAR_STRING;
4660 rettv->vval.v_string = NULL;
4661
4662 if (buf != NULL && varname != NULL)
4663 {
4664 /* set curbuf to be our buf, temporarily */
4665 save_curbuf = curbuf;
4666 curbuf = buf;
4667
Bram Moolenaar30567352016-08-27 21:25:44 +02004668 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004670 if (varname[1] == NUL)
4671 {
4672 /* get all buffer-local options in a dict */
4673 dict_T *opts = get_winbuf_options(TRUE);
4674
4675 if (opts != NULL)
4676 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004677 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004678 done = TRUE;
4679 }
4680 }
4681 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4682 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 done = TRUE;
4684 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 else
4686 {
4687 /* Look up the variable. */
4688 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4689 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4690 'b', varname, FALSE);
4691 if (v != NULL)
4692 {
4693 copy_tv(&v->di_tv, rettv);
4694 done = TRUE;
4695 }
4696 }
4697
4698 /* restore previous notion of curbuf */
4699 curbuf = save_curbuf;
4700 }
4701
4702 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4703 /* use the default value */
4704 copy_tv(&argvars[2], rettv);
4705
4706 --emsg_off;
4707}
4708
4709/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004710 * "getchangelist()" function
4711 */
4712 static void
4713f_getchangelist(typval_T *argvars, typval_T *rettv)
4714{
4715#ifdef FEAT_JUMPLIST
4716 buf_T *buf;
4717 int i;
4718 list_T *l;
4719 dict_T *d;
4720#endif
4721
4722 if (rettv_list_alloc(rettv) != OK)
4723 return;
4724
4725#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004726 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004727 ++emsg_off;
4728 buf = get_buf_tv(&argvars[0], FALSE);
4729 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004730 if (buf == NULL)
4731 return;
4732
4733 l = list_alloc();
4734 if (l == NULL)
4735 return;
4736
4737 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4738 return;
4739 /*
4740 * The current window change list index tracks only the position in the
4741 * current buffer change list. For other buffers, use the change list
4742 * length as the current index.
4743 */
4744 list_append_number(rettv->vval.v_list,
4745 (varnumber_T)((buf == curwin->w_buffer)
4746 ? curwin->w_changelistidx : buf->b_changelistlen));
4747
4748 for (i = 0; i < buf->b_changelistlen; ++i)
4749 {
4750 if (buf->b_changelist[i].lnum == 0)
4751 continue;
4752 if ((d = dict_alloc()) == NULL)
4753 return;
4754 if (list_append_dict(l, d) == FAIL)
4755 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004756 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4757 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004758# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004759 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004760# endif
4761 }
4762#endif
4763}
4764/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004765 * "getchar()" function
4766 */
4767 static void
4768f_getchar(typval_T *argvars, typval_T *rettv)
4769{
4770 varnumber_T n;
4771 int error = FALSE;
4772
Bram Moolenaar84d93902018-09-11 20:10:20 +02004773#ifdef MESSAGE_QUEUE
4774 // vpeekc() used to check for messages, but that caused problems, invoking
4775 // a callback where it was not expected. Some plugins use getchar(1) in a
4776 // loop to await a message, therefore make sure we check for messages here.
4777 parse_queued_messages();
4778#endif
4779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 /* Position the cursor. Needed after a message that ends in a space. */
4781 windgoto(msg_row, msg_col);
4782
4783 ++no_mapping;
4784 ++allow_keys;
4785 for (;;)
4786 {
4787 if (argvars[0].v_type == VAR_UNKNOWN)
4788 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004789 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004790 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 /* getchar(1): only check if char avail */
4792 n = vpeekc_any();
4793 else if (error || vpeekc_any() == NUL)
4794 /* illegal argument or getchar(0) and no char avail: return zero */
4795 n = 0;
4796 else
4797 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004798 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004799
4800 if (n == K_IGNORE)
4801 continue;
4802 break;
4803 }
4804 --no_mapping;
4805 --allow_keys;
4806
4807 set_vim_var_nr(VV_MOUSE_WIN, 0);
4808 set_vim_var_nr(VV_MOUSE_WINID, 0);
4809 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4810 set_vim_var_nr(VV_MOUSE_COL, 0);
4811
4812 rettv->vval.v_number = n;
4813 if (IS_SPECIAL(n) || mod_mask != 0)
4814 {
4815 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4816 int i = 0;
4817
4818 /* Turn a special key into three bytes, plus modifier. */
4819 if (mod_mask != 0)
4820 {
4821 temp[i++] = K_SPECIAL;
4822 temp[i++] = KS_MODIFIER;
4823 temp[i++] = mod_mask;
4824 }
4825 if (IS_SPECIAL(n))
4826 {
4827 temp[i++] = K_SPECIAL;
4828 temp[i++] = K_SECOND(n);
4829 temp[i++] = K_THIRD(n);
4830 }
4831#ifdef FEAT_MBYTE
4832 else if (has_mbyte)
4833 i += (*mb_char2bytes)(n, temp + i);
4834#endif
4835 else
4836 temp[i++] = n;
4837 temp[i++] = NUL;
4838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = vim_strsave(temp);
4840
4841#ifdef FEAT_MOUSE
4842 if (is_mouse_key(n))
4843 {
4844 int row = mouse_row;
4845 int col = mouse_col;
4846 win_T *win;
4847 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 int winnr = 1;
4850
4851 if (row >= 0 && col >= 0)
4852 {
4853 /* Find the window at the mouse coordinates and compute the
4854 * text position. */
4855 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004856 if (win == NULL)
4857 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004858 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859 for (wp = firstwin; wp != win; wp = wp->w_next)
4860 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4862 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4863 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4864 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4865 }
4866 }
4867#endif
4868 }
4869}
4870
4871/*
4872 * "getcharmod()" function
4873 */
4874 static void
4875f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4876{
4877 rettv->vval.v_number = mod_mask;
4878}
4879
4880/*
4881 * "getcharsearch()" function
4882 */
4883 static void
4884f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4885{
4886 if (rettv_dict_alloc(rettv) != FAIL)
4887 {
4888 dict_T *dict = rettv->vval.v_dict;
4889
Bram Moolenaare0be1672018-07-08 16:50:37 +02004890 dict_add_string(dict, "char", last_csearch());
4891 dict_add_number(dict, "forward", last_csearch_forward());
4892 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893 }
4894}
4895
4896/*
4897 * "getcmdline()" function
4898 */
4899 static void
4900f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4901{
4902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = get_cmdline_str();
4904}
4905
4906/*
4907 * "getcmdpos()" function
4908 */
4909 static void
4910f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4911{
4912 rettv->vval.v_number = get_cmdline_pos() + 1;
4913}
4914
4915/*
4916 * "getcmdtype()" function
4917 */
4918 static void
4919f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4920{
4921 rettv->v_type = VAR_STRING;
4922 rettv->vval.v_string = alloc(2);
4923 if (rettv->vval.v_string != NULL)
4924 {
4925 rettv->vval.v_string[0] = get_cmdline_type();
4926 rettv->vval.v_string[1] = NUL;
4927 }
4928}
4929
4930/*
4931 * "getcmdwintype()" function
4932 */
4933 static void
4934f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4935{
4936 rettv->v_type = VAR_STRING;
4937 rettv->vval.v_string = NULL;
4938#ifdef FEAT_CMDWIN
4939 rettv->vval.v_string = alloc(2);
4940 if (rettv->vval.v_string != NULL)
4941 {
4942 rettv->vval.v_string[0] = cmdwin_type;
4943 rettv->vval.v_string[1] = NUL;
4944 }
4945#endif
4946}
4947
4948#if defined(FEAT_CMDL_COMPL)
4949/*
4950 * "getcompletion()" function
4951 */
4952 static void
4953f_getcompletion(typval_T *argvars, typval_T *rettv)
4954{
4955 char_u *pat;
4956 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004957 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004958 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4959 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 if (p_wic)
4965 options |= WILD_ICASE;
4966
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004967 /* For filtered results, 'wildignore' is used */
4968 if (!filtered)
4969 options |= WILD_KEEP_ALL;
4970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004972 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004973 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 if (xpc.xp_context == EXPAND_NOTHING)
4976 {
4977 if (argvars[1].v_type == VAR_STRING)
4978 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4979 else
4980 EMSG(_(e_invarg));
4981 return;
4982 }
4983
4984# if defined(FEAT_MENU)
4985 if (xpc.xp_context == EXPAND_MENUS)
4986 {
4987 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4988 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4989 }
4990# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004991#ifdef FEAT_CSCOPE
4992 if (xpc.xp_context == EXPAND_CSCOPE)
4993 {
4994 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4995 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4996 }
4997#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004998#ifdef FEAT_SIGNS
4999 if (xpc.xp_context == EXPAND_SIGN)
5000 {
5001 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5002 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5003 }
5004#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005005
5006 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5007 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5008 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005009 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010
5011 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5012
5013 for (i = 0; i < xpc.xp_numfiles; i++)
5014 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5015 }
5016 vim_free(pat);
5017 ExpandCleanup(&xpc);
5018}
5019#endif
5020
5021/*
5022 * "getcwd()" function
5023 */
5024 static void
5025f_getcwd(typval_T *argvars, typval_T *rettv)
5026{
5027 win_T *wp = NULL;
5028 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005029 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030
5031 rettv->v_type = VAR_STRING;
5032 rettv->vval.v_string = NULL;
5033
Bram Moolenaar54591292018-02-09 20:53:59 +01005034 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5035 global = TRUE;
5036 else
5037 wp = find_tabwin(&argvars[0], &argvars[1]);
5038
5039 if (wp != NULL && wp->w_localdir != NULL)
5040 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5041 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005043 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 rettv->vval.v_string = vim_strsave(globaldir);
5045 else
5046 {
5047 cwd = alloc(MAXPATHL);
5048 if (cwd != NULL)
5049 {
5050 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5051 rettv->vval.v_string = vim_strsave(cwd);
5052 vim_free(cwd);
5053 }
5054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005055 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005056#ifdef BACKSLASH_IN_FILENAME
5057 if (rettv->vval.v_string != NULL)
5058 slash_adjust(rettv->vval.v_string);
5059#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060}
5061
5062/*
5063 * "getfontname()" function
5064 */
5065 static void
5066f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5067{
5068 rettv->v_type = VAR_STRING;
5069 rettv->vval.v_string = NULL;
5070#ifdef FEAT_GUI
5071 if (gui.in_use)
5072 {
5073 GuiFont font;
5074 char_u *name = NULL;
5075
5076 if (argvars[0].v_type == VAR_UNKNOWN)
5077 {
5078 /* Get the "Normal" font. Either the name saved by
5079 * hl_set_font_name() or from the font ID. */
5080 font = gui.norm_font;
5081 name = hl_get_font_name();
5082 }
5083 else
5084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005085 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5087 return;
5088 font = gui_mch_get_font(name, FALSE);
5089 if (font == NOFONT)
5090 return; /* Invalid font name, return empty string. */
5091 }
5092 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5093 if (argvars[0].v_type != VAR_UNKNOWN)
5094 gui_mch_free_font(font);
5095 }
5096#endif
5097}
5098
5099/*
5100 * "getfperm({fname})" function
5101 */
5102 static void
5103f_getfperm(typval_T *argvars, typval_T *rettv)
5104{
5105 char_u *fname;
5106 stat_T st;
5107 char_u *perm = NULL;
5108 char_u flags[] = "rwx";
5109 int i;
5110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112
5113 rettv->v_type = VAR_STRING;
5114 if (mch_stat((char *)fname, &st) >= 0)
5115 {
5116 perm = vim_strsave((char_u *)"---------");
5117 if (perm != NULL)
5118 {
5119 for (i = 0; i < 9; i++)
5120 {
5121 if (st.st_mode & (1 << (8 - i)))
5122 perm[i] = flags[i % 3];
5123 }
5124 }
5125 }
5126 rettv->vval.v_string = perm;
5127}
5128
5129/*
5130 * "getfsize({fname})" function
5131 */
5132 static void
5133f_getfsize(typval_T *argvars, typval_T *rettv)
5134{
5135 char_u *fname;
5136 stat_T st;
5137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005138 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139
5140 rettv->v_type = VAR_NUMBER;
5141
5142 if (mch_stat((char *)fname, &st) >= 0)
5143 {
5144 if (mch_isdir(fname))
5145 rettv->vval.v_number = 0;
5146 else
5147 {
5148 rettv->vval.v_number = (varnumber_T)st.st_size;
5149
5150 /* non-perfect check for overflow */
5151 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5152 rettv->vval.v_number = -2;
5153 }
5154 }
5155 else
5156 rettv->vval.v_number = -1;
5157}
5158
5159/*
5160 * "getftime({fname})" function
5161 */
5162 static void
5163f_getftime(typval_T *argvars, typval_T *rettv)
5164{
5165 char_u *fname;
5166 stat_T st;
5167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169
5170 if (mch_stat((char *)fname, &st) >= 0)
5171 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5172 else
5173 rettv->vval.v_number = -1;
5174}
5175
5176/*
5177 * "getftype({fname})" function
5178 */
5179 static void
5180f_getftype(typval_T *argvars, typval_T *rettv)
5181{
5182 char_u *fname;
5183 stat_T st;
5184 char_u *type = NULL;
5185 char *t;
5186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005187 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188
5189 rettv->v_type = VAR_STRING;
5190 if (mch_lstat((char *)fname, &st) >= 0)
5191 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 if (S_ISREG(st.st_mode))
5193 t = "file";
5194 else if (S_ISDIR(st.st_mode))
5195 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 else if (S_ISLNK(st.st_mode))
5197 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 else if (S_ISBLK(st.st_mode))
5199 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 else if (S_ISCHR(st.st_mode))
5201 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 else if (S_ISFIFO(st.st_mode))
5203 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005205 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 else
5207 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 type = vim_strsave((char_u *)t);
5209 }
5210 rettv->vval.v_string = type;
5211}
5212
5213/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005214 * "getjumplist()" function
5215 */
5216 static void
5217f_getjumplist(typval_T *argvars, typval_T *rettv)
5218{
5219#ifdef FEAT_JUMPLIST
5220 win_T *wp;
5221 int i;
5222 list_T *l;
5223 dict_T *d;
5224#endif
5225
5226 if (rettv_list_alloc(rettv) != OK)
5227 return;
5228
5229#ifdef FEAT_JUMPLIST
5230 wp = find_tabwin(&argvars[0], &argvars[1]);
5231 if (wp == NULL)
5232 return;
5233
5234 l = list_alloc();
5235 if (l == NULL)
5236 return;
5237
5238 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5239 return;
5240 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5241
Bram Moolenaar48679742018-02-13 13:33:29 +01005242 cleanup_jumplist(wp, TRUE);
5243
Bram Moolenaar4f505882018-02-10 21:06:32 +01005244 for (i = 0; i < wp->w_jumplistlen; ++i)
5245 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005246 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5247 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005248 if ((d = dict_alloc()) == NULL)
5249 return;
5250 if (list_append_dict(l, d) == FAIL)
5251 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005252 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5253 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005254# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005255 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005256# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005257 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005258 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005259 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005260 }
5261#endif
5262}
5263
5264/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005265 * "getline(lnum, [end])" function
5266 */
5267 static void
5268f_getline(typval_T *argvars, typval_T *rettv)
5269{
5270 linenr_T lnum;
5271 linenr_T end;
5272 int retlist;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 if (argvars[1].v_type == VAR_UNKNOWN)
5276 {
5277 end = 0;
5278 retlist = FALSE;
5279 }
5280 else
5281 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005282 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 retlist = TRUE;
5284 }
5285
5286 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5287}
5288
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005289#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005290 static void
5291get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5292{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005293 if (what_arg->v_type == VAR_UNKNOWN)
5294 {
5295 if (rettv_list_alloc(rettv) == OK)
5296 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005297 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005298 }
5299 else
5300 {
5301 if (rettv_dict_alloc(rettv) == OK)
5302 if (is_qf || (wp != NULL))
5303 {
5304 if (what_arg->v_type == VAR_DICT)
5305 {
5306 dict_T *d = what_arg->vval.v_dict;
5307
5308 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005309 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005310 }
5311 else
5312 EMSG(_(e_dictreq));
5313 }
5314 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005315}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005316#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005317
5318/*
5319 * "getloclist()" function
5320 */
5321 static void
5322f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5323{
5324#ifdef FEAT_QUICKFIX
5325 win_T *wp;
5326
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005327 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005328 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5329#endif
5330}
5331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332/*
5333 * "getmatches()" function
5334 */
5335 static void
5336f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5337{
5338#ifdef FEAT_SEARCH_EXTRA
5339 dict_T *dict;
5340 matchitem_T *cur = curwin->w_match_head;
5341 int i;
5342
5343 if (rettv_list_alloc(rettv) == OK)
5344 {
5345 while (cur != NULL)
5346 {
5347 dict = dict_alloc();
5348 if (dict == NULL)
5349 return;
5350 if (cur->match.regprog == NULL)
5351 {
5352 /* match added with matchaddpos() */
5353 for (i = 0; i < MAXPOSMATCH; ++i)
5354 {
5355 llpos_T *llpos;
5356 char buf[6];
5357 list_T *l;
5358
5359 llpos = &cur->pos.pos[i];
5360 if (llpos->lnum == 0)
5361 break;
5362 l = list_alloc();
5363 if (l == NULL)
5364 break;
5365 list_append_number(l, (varnumber_T)llpos->lnum);
5366 if (llpos->col > 0)
5367 {
5368 list_append_number(l, (varnumber_T)llpos->col);
5369 list_append_number(l, (varnumber_T)llpos->len);
5370 }
5371 sprintf(buf, "pos%d", i + 1);
5372 dict_add_list(dict, buf, l);
5373 }
5374 }
5375 else
5376 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005377 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005379 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5380 dict_add_number(dict, "priority", (long)cur->priority);
5381 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5383 if (cur->conceal_char)
5384 {
5385 char_u buf[MB_MAXBYTES + 1];
5386
5387 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005388 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005389 }
5390# endif
5391 list_append_dict(rettv->vval.v_list, dict);
5392 cur = cur->next;
5393 }
5394 }
5395#endif
5396}
5397
5398/*
5399 * "getpid()" function
5400 */
5401 static void
5402f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5403{
5404 rettv->vval.v_number = mch_get_pid();
5405}
5406
5407 static void
5408getpos_both(
5409 typval_T *argvars,
5410 typval_T *rettv,
5411 int getcurpos)
5412{
5413 pos_T *fp;
5414 list_T *l;
5415 int fnum = -1;
5416
5417 if (rettv_list_alloc(rettv) == OK)
5418 {
5419 l = rettv->vval.v_list;
5420 if (getcurpos)
5421 fp = &curwin->w_cursor;
5422 else
5423 fp = var2fpos(&argvars[0], TRUE, &fnum);
5424 if (fnum != -1)
5425 list_append_number(l, (varnumber_T)fnum);
5426 else
5427 list_append_number(l, (varnumber_T)0);
5428 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5429 : (varnumber_T)0);
5430 list_append_number(l, (fp != NULL)
5431 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5432 : (varnumber_T)0);
5433 list_append_number(l,
5434#ifdef FEAT_VIRTUALEDIT
5435 (fp != NULL) ? (varnumber_T)fp->coladd :
5436#endif
5437 (varnumber_T)0);
5438 if (getcurpos)
5439 {
5440 update_curswant();
5441 list_append_number(l, curwin->w_curswant == MAXCOL ?
5442 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5443 }
5444 }
5445 else
5446 rettv->vval.v_number = FALSE;
5447}
5448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449/*
5450 * "getcurpos()" function
5451 */
5452 static void
5453f_getcurpos(typval_T *argvars, typval_T *rettv)
5454{
5455 getpos_both(argvars, rettv, TRUE);
5456}
5457
5458/*
5459 * "getpos(string)" function
5460 */
5461 static void
5462f_getpos(typval_T *argvars, typval_T *rettv)
5463{
5464 getpos_both(argvars, rettv, FALSE);
5465}
5466
5467/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005468 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469 */
5470 static void
5471f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5472{
5473#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005474 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475#endif
5476}
5477
5478/*
5479 * "getreg()" function
5480 */
5481 static void
5482f_getreg(typval_T *argvars, typval_T *rettv)
5483{
5484 char_u *strregname;
5485 int regname;
5486 int arg2 = FALSE;
5487 int return_list = FALSE;
5488 int error = FALSE;
5489
5490 if (argvars[0].v_type != VAR_UNKNOWN)
5491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 error = strregname == NULL;
5494 if (argvars[1].v_type != VAR_UNKNOWN)
5495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005496 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005498 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499 }
5500 }
5501 else
5502 strregname = get_vim_var_str(VV_REG);
5503
5504 if (error)
5505 return;
5506
5507 regname = (strregname == NULL ? '"' : *strregname);
5508 if (regname == 0)
5509 regname = '"';
5510
5511 if (return_list)
5512 {
5513 rettv->v_type = VAR_LIST;
5514 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5515 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5516 if (rettv->vval.v_list == NULL)
5517 (void)rettv_list_alloc(rettv);
5518 else
5519 ++rettv->vval.v_list->lv_refcount;
5520 }
5521 else
5522 {
5523 rettv->v_type = VAR_STRING;
5524 rettv->vval.v_string = get_reg_contents(regname,
5525 arg2 ? GREG_EXPR_SRC : 0);
5526 }
5527}
5528
5529/*
5530 * "getregtype()" function
5531 */
5532 static void
5533f_getregtype(typval_T *argvars, typval_T *rettv)
5534{
5535 char_u *strregname;
5536 int regname;
5537 char_u buf[NUMBUFLEN + 2];
5538 long reglen = 0;
5539
5540 if (argvars[0].v_type != VAR_UNKNOWN)
5541 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005542 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 if (strregname == NULL) /* type error; errmsg already given */
5544 {
5545 rettv->v_type = VAR_STRING;
5546 rettv->vval.v_string = NULL;
5547 return;
5548 }
5549 }
5550 else
5551 /* Default to v:register */
5552 strregname = get_vim_var_str(VV_REG);
5553
5554 regname = (strregname == NULL ? '"' : *strregname);
5555 if (regname == 0)
5556 regname = '"';
5557
5558 buf[0] = NUL;
5559 buf[1] = NUL;
5560 switch (get_reg_type(regname, &reglen))
5561 {
5562 case MLINE: buf[0] = 'V'; break;
5563 case MCHAR: buf[0] = 'v'; break;
5564 case MBLOCK:
5565 buf[0] = Ctrl_V;
5566 sprintf((char *)buf + 1, "%ld", reglen + 1);
5567 break;
5568 }
5569 rettv->v_type = VAR_STRING;
5570 rettv->vval.v_string = vim_strsave(buf);
5571}
5572
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573/*
5574 * Returns information (variables, options, etc.) about a tab page
5575 * as a dictionary.
5576 */
5577 static dict_T *
5578get_tabpage_info(tabpage_T *tp, int tp_idx)
5579{
5580 win_T *wp;
5581 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582 list_T *l;
5583
5584 dict = dict_alloc();
5585 if (dict == NULL)
5586 return NULL;
5587
Bram Moolenaare0be1672018-07-08 16:50:37 +02005588 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005589
5590 l = list_alloc();
5591 if (l != NULL)
5592 {
5593 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5594 wp; wp = wp->w_next)
5595 list_append_number(l, (varnumber_T)wp->w_id);
5596 dict_add_list(dict, "windows", l);
5597 }
5598
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005599 /* Make a reference to tabpage variables */
5600 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005601
5602 return dict;
5603}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005604
5605/*
5606 * "gettabinfo()" function
5607 */
5608 static void
5609f_gettabinfo(typval_T *argvars, typval_T *rettv)
5610{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005611 tabpage_T *tp, *tparg = NULL;
5612 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005613 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005614
5615 if (rettv_list_alloc(rettv) != OK)
5616 return;
5617
5618 if (argvars[0].v_type != VAR_UNKNOWN)
5619 {
5620 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622 if (tparg == NULL)
5623 return;
5624 }
5625
5626 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005627 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005629 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005630 if (tparg != NULL && tp != tparg)
5631 continue;
5632 d = get_tabpage_info(tp, tpnr);
5633 if (d != NULL)
5634 list_append_dict(rettv->vval.v_list, d);
5635 if (tparg != NULL)
5636 return;
5637 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005638}
5639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640/*
5641 * "gettabvar()" function
5642 */
5643 static void
5644f_gettabvar(typval_T *argvars, typval_T *rettv)
5645{
5646 win_T *oldcurwin;
5647 tabpage_T *tp, *oldtabpage;
5648 dictitem_T *v;
5649 char_u *varname;
5650 int done = FALSE;
5651
5652 rettv->v_type = VAR_STRING;
5653 rettv->vval.v_string = NULL;
5654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 varname = tv_get_string_chk(&argvars[1]);
5656 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 if (tp != NULL && varname != NULL)
5658 {
5659 /* Set tp to be our tabpage, temporarily. Also set the window to the
5660 * first window in the tabpage, otherwise the window is not valid. */
5661 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005662 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5663 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664 {
5665 /* look up the variable */
5666 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5667 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5668 if (v != NULL)
5669 {
5670 copy_tv(&v->di_tv, rettv);
5671 done = TRUE;
5672 }
5673 }
5674
5675 /* restore previous notion of curwin */
5676 restore_win(oldcurwin, oldtabpage, TRUE);
5677 }
5678
5679 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5680 copy_tv(&argvars[2], rettv);
5681}
5682
5683/*
5684 * "gettabwinvar()" function
5685 */
5686 static void
5687f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5688{
5689 getwinvar(argvars, rettv, 1);
5690}
5691
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005692/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005693 * "gettagstack()" function
5694 */
5695 static void
5696f_gettagstack(typval_T *argvars, typval_T *rettv)
5697{
5698 win_T *wp = curwin; // default is current window
5699
5700 if (rettv_dict_alloc(rettv) != OK)
5701 return;
5702
5703 if (argvars[0].v_type != VAR_UNKNOWN)
5704 {
5705 wp = find_win_by_nr_or_id(&argvars[0]);
5706 if (wp == NULL)
5707 return;
5708 }
5709
5710 get_tagstack(wp, rettv->vval.v_dict);
5711}
5712
5713/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714 * Returns information about a window as a dictionary.
5715 */
5716 static dict_T *
5717get_win_info(win_T *wp, short tpnr, short winnr)
5718{
5719 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720
5721 dict = dict_alloc();
5722 if (dict == NULL)
5723 return NULL;
5724
Bram Moolenaare0be1672018-07-08 16:50:37 +02005725 dict_add_number(dict, "tabnr", tpnr);
5726 dict_add_number(dict, "winnr", winnr);
5727 dict_add_number(dict, "winid", wp->w_id);
5728 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005729 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005730#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005731 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005732#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005733 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005734 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005735 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005736
Bram Moolenaar69905d12017-08-13 18:14:47 +02005737#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005738 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005739#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005740#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005741 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5742 dict_add_number(dict, "loclist",
5743 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005744#endif
5745
Bram Moolenaar30567352016-08-27 21:25:44 +02005746 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005747 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005748
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005749 return dict;
5750}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751
5752/*
5753 * "getwininfo()" function
5754 */
5755 static void
5756f_getwininfo(typval_T *argvars, typval_T *rettv)
5757{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758 tabpage_T *tp;
5759 win_T *wp = NULL, *wparg = NULL;
5760 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005761 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005762
5763 if (rettv_list_alloc(rettv) != OK)
5764 return;
5765
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005766 if (argvars[0].v_type != VAR_UNKNOWN)
5767 {
5768 wparg = win_id2wp(argvars);
5769 if (wparg == NULL)
5770 return;
5771 }
5772
5773 /* Collect information about either all the windows across all the tab
5774 * pages or one particular window.
5775 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005776 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005777 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005778 tabnr++;
5779 winnr = 0;
5780 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005781 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005782 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005783 if (wparg != NULL && wp != wparg)
5784 continue;
5785 d = get_win_info(wp, tabnr, winnr);
5786 if (d != NULL)
5787 list_append_dict(rettv->vval.v_list, d);
5788 if (wparg != NULL)
5789 /* found information about a specific window */
5790 return;
5791 }
5792 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005793}
5794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 * "win_findbuf()" function
5797 */
5798 static void
5799f_win_findbuf(typval_T *argvars, typval_T *rettv)
5800{
5801 if (rettv_list_alloc(rettv) != FAIL)
5802 win_findbuf(argvars, rettv->vval.v_list);
5803}
5804
5805/*
5806 * "win_getid()" function
5807 */
5808 static void
5809f_win_getid(typval_T *argvars, typval_T *rettv)
5810{
5811 rettv->vval.v_number = win_getid(argvars);
5812}
5813
5814/*
5815 * "win_gotoid()" function
5816 */
5817 static void
5818f_win_gotoid(typval_T *argvars, typval_T *rettv)
5819{
5820 rettv->vval.v_number = win_gotoid(argvars);
5821}
5822
5823/*
5824 * "win_id2tabwin()" function
5825 */
5826 static void
5827f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5828{
5829 if (rettv_list_alloc(rettv) != FAIL)
5830 win_id2tabwin(argvars, rettv->vval.v_list);
5831}
5832
5833/*
5834 * "win_id2win()" function
5835 */
5836 static void
5837f_win_id2win(typval_T *argvars, typval_T *rettv)
5838{
5839 rettv->vval.v_number = win_id2win(argvars);
5840}
5841
5842/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005843 * "win_screenpos()" function
5844 */
5845 static void
5846f_win_screenpos(typval_T *argvars, typval_T *rettv)
5847{
5848 win_T *wp;
5849
5850 if (rettv_list_alloc(rettv) == FAIL)
5851 return;
5852
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005853 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005854 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5855 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5856}
5857
5858/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005859 * "getwinpos({timeout})" function
5860 */
5861 static void
5862f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5863{
5864 int x = -1;
5865 int y = -1;
5866
5867 if (rettv_list_alloc(rettv) == FAIL)
5868 return;
5869#ifdef FEAT_GUI
5870 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005871 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005872# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5873 else
5874# endif
5875#endif
5876#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5877 {
5878 varnumber_T timeout = 100;
5879
5880 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005882 term_get_winpos(&x, &y, timeout);
5883 }
5884#endif
5885 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5886 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5887}
5888
5889
5890/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005891 * "getwinposx()" function
5892 */
5893 static void
5894f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5895{
5896 rettv->vval.v_number = -1;
5897#ifdef FEAT_GUI
5898 if (gui.in_use)
5899 {
5900 int x, y;
5901
5902 if (gui_mch_get_winpos(&x, &y) == OK)
5903 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005904 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005905 }
5906#endif
5907#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5908 {
5909 int x, y;
5910
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005911 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005912 rettv->vval.v_number = x;
5913 }
5914#endif
5915}
5916
5917/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 * "getwinposy()" function
5919 */
5920 static void
5921f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5922{
5923 rettv->vval.v_number = -1;
5924#ifdef FEAT_GUI
5925 if (gui.in_use)
5926 {
5927 int x, y;
5928
5929 if (gui_mch_get_winpos(&x, &y) == OK)
5930 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005931 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 }
5933#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005934#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5935 {
5936 int x, y;
5937
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005938 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005939 rettv->vval.v_number = y;
5940 }
5941#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942}
5943
5944/*
5945 * "getwinvar()" function
5946 */
5947 static void
5948f_getwinvar(typval_T *argvars, typval_T *rettv)
5949{
5950 getwinvar(argvars, rettv, 0);
5951}
5952
5953/*
5954 * "glob()" function
5955 */
5956 static void
5957f_glob(typval_T *argvars, typval_T *rettv)
5958{
5959 int options = WILD_SILENT|WILD_USE_NL;
5960 expand_T xpc;
5961 int error = FALSE;
5962
5963 /* When the optional second argument is non-zero, don't remove matches
5964 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5965 rettv->v_type = VAR_STRING;
5966 if (argvars[1].v_type != VAR_UNKNOWN)
5967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 options |= WILD_KEEP_ALL;
5970 if (argvars[2].v_type != VAR_UNKNOWN)
5971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005972 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005974 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 }
5976 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005977 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 options |= WILD_ALLLINKS;
5979 }
5980 }
5981 if (!error)
5982 {
5983 ExpandInit(&xpc);
5984 xpc.xp_context = EXPAND_FILES;
5985 if (p_wic)
5986 options += WILD_ICASE;
5987 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005988 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 NULL, options, WILD_ALL);
5990 else if (rettv_list_alloc(rettv) != FAIL)
5991 {
5992 int i;
5993
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005994 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 NULL, options, WILD_ALL_KEEP);
5996 for (i = 0; i < xpc.xp_numfiles; i++)
5997 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5998
5999 ExpandCleanup(&xpc);
6000 }
6001 }
6002 else
6003 rettv->vval.v_string = NULL;
6004}
6005
6006/*
6007 * "globpath()" function
6008 */
6009 static void
6010f_globpath(typval_T *argvars, typval_T *rettv)
6011{
6012 int flags = 0;
6013 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 int error = FALSE;
6016 garray_T ga;
6017 int i;
6018
6019 /* When the optional second argument is non-zero, don't remove matches
6020 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6021 rettv->v_type = VAR_STRING;
6022 if (argvars[2].v_type != VAR_UNKNOWN)
6023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006024 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 flags |= WILD_KEEP_ALL;
6026 if (argvars[3].v_type != VAR_UNKNOWN)
6027 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006028 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006030 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 }
6032 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006033 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034 flags |= WILD_ALLLINKS;
6035 }
6036 }
6037 if (file != NULL && !error)
6038 {
6039 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006040 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 if (rettv->v_type == VAR_STRING)
6042 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6043 else if (rettv_list_alloc(rettv) != FAIL)
6044 for (i = 0; i < ga.ga_len; ++i)
6045 list_append_string(rettv->vval.v_list,
6046 ((char_u **)(ga.ga_data))[i], -1);
6047 ga_clear_strings(&ga);
6048 }
6049 else
6050 rettv->vval.v_string = NULL;
6051}
6052
6053/*
6054 * "glob2regpat()" function
6055 */
6056 static void
6057f_glob2regpat(typval_T *argvars, typval_T *rettv)
6058{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006059 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060
6061 rettv->v_type = VAR_STRING;
6062 rettv->vval.v_string = (pat == NULL)
6063 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6064}
6065
6066/* for VIM_VERSION_ defines */
6067#include "version.h"
6068
6069/*
6070 * "has()" function
6071 */
6072 static void
6073f_has(typval_T *argvars, typval_T *rettv)
6074{
6075 int i;
6076 char_u *name;
6077 int n = FALSE;
6078 static char *(has_list[]) =
6079 {
6080#ifdef AMIGA
6081 "amiga",
6082# ifdef FEAT_ARP
6083 "arp",
6084# endif
6085#endif
6086#ifdef __BEOS__
6087 "beos",
6088#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006089#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006090 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6091 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006092# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006093 "macunix", /* Mac OS X, with the darwin feature */
6094 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006095# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096#endif
6097#ifdef __QNX__
6098 "qnx",
6099#endif
6100#ifdef UNIX
6101 "unix",
6102#endif
6103#ifdef VMS
6104 "vms",
6105#endif
6106#ifdef WIN32
6107 "win32",
6108#endif
6109#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6110 "win32unix",
6111#endif
6112#if defined(WIN64) || defined(_WIN64)
6113 "win64",
6114#endif
6115#ifdef EBCDIC
6116 "ebcdic",
6117#endif
6118#ifndef CASE_INSENSITIVE_FILENAME
6119 "fname_case",
6120#endif
6121#ifdef HAVE_ACL
6122 "acl",
6123#endif
6124#ifdef FEAT_ARABIC
6125 "arabic",
6126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006128#ifdef FEAT_AUTOCHDIR
6129 "autochdir",
6130#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006131#ifdef FEAT_AUTOSERVERNAME
6132 "autoservername",
6133#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006134#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 "balloon_eval",
6136# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6137 "balloon_multiline",
6138# endif
6139#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006140#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006141 "balloon_eval_term",
6142#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6144 "builtin_terms",
6145# ifdef ALL_BUILTIN_TCAPS
6146 "all_builtin_terms",
6147# endif
6148#endif
6149#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6150 || defined(FEAT_GUI_W32) \
6151 || defined(FEAT_GUI_MOTIF))
6152 "browsefilter",
6153#endif
6154#ifdef FEAT_BYTEOFF
6155 "byte_offset",
6156#endif
6157#ifdef FEAT_JOB_CHANNEL
6158 "channel",
6159#endif
6160#ifdef FEAT_CINDENT
6161 "cindent",
6162#endif
6163#ifdef FEAT_CLIENTSERVER
6164 "clientserver",
6165#endif
6166#ifdef FEAT_CLIPBOARD
6167 "clipboard",
6168#endif
6169#ifdef FEAT_CMDL_COMPL
6170 "cmdline_compl",
6171#endif
6172#ifdef FEAT_CMDHIST
6173 "cmdline_hist",
6174#endif
6175#ifdef FEAT_COMMENTS
6176 "comments",
6177#endif
6178#ifdef FEAT_CONCEAL
6179 "conceal",
6180#endif
6181#ifdef FEAT_CRYPT
6182 "cryptv",
6183 "crypt-blowfish",
6184 "crypt-blowfish2",
6185#endif
6186#ifdef FEAT_CSCOPE
6187 "cscope",
6188#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190#ifdef CURSOR_SHAPE
6191 "cursorshape",
6192#endif
6193#ifdef DEBUG
6194 "debug",
6195#endif
6196#ifdef FEAT_CON_DIALOG
6197 "dialog_con",
6198#endif
6199#ifdef FEAT_GUI_DIALOG
6200 "dialog_gui",
6201#endif
6202#ifdef FEAT_DIFF
6203 "diff",
6204#endif
6205#ifdef FEAT_DIGRAPHS
6206 "digraphs",
6207#endif
6208#ifdef FEAT_DIRECTX
6209 "directx",
6210#endif
6211#ifdef FEAT_DND
6212 "dnd",
6213#endif
6214#ifdef FEAT_EMACS_TAGS
6215 "emacs_tags",
6216#endif
6217 "eval", /* always present, of course! */
6218 "ex_extra", /* graduated feature */
6219#ifdef FEAT_SEARCH_EXTRA
6220 "extra_search",
6221#endif
6222#ifdef FEAT_FKMAP
6223 "farsi",
6224#endif
6225#ifdef FEAT_SEARCHPATH
6226 "file_in_path",
6227#endif
6228#ifdef FEAT_FILTERPIPE
6229 "filterpipe",
6230#endif
6231#ifdef FEAT_FIND_ID
6232 "find_in_path",
6233#endif
6234#ifdef FEAT_FLOAT
6235 "float",
6236#endif
6237#ifdef FEAT_FOLDING
6238 "folding",
6239#endif
6240#ifdef FEAT_FOOTER
6241 "footer",
6242#endif
6243#if !defined(USE_SYSTEM) && defined(UNIX)
6244 "fork",
6245#endif
6246#ifdef FEAT_GETTEXT
6247 "gettext",
6248#endif
6249#ifdef FEAT_GUI
6250 "gui",
6251#endif
6252#ifdef FEAT_GUI_ATHENA
6253# ifdef FEAT_GUI_NEXTAW
6254 "gui_neXtaw",
6255# else
6256 "gui_athena",
6257# endif
6258#endif
6259#ifdef FEAT_GUI_GTK
6260 "gui_gtk",
6261# ifdef USE_GTK3
6262 "gui_gtk3",
6263# else
6264 "gui_gtk2",
6265# endif
6266#endif
6267#ifdef FEAT_GUI_GNOME
6268 "gui_gnome",
6269#endif
6270#ifdef FEAT_GUI_MAC
6271 "gui_mac",
6272#endif
6273#ifdef FEAT_GUI_MOTIF
6274 "gui_motif",
6275#endif
6276#ifdef FEAT_GUI_PHOTON
6277 "gui_photon",
6278#endif
6279#ifdef FEAT_GUI_W32
6280 "gui_win32",
6281#endif
6282#ifdef FEAT_HANGULIN
6283 "hangul_input",
6284#endif
6285#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6286 "iconv",
6287#endif
6288#ifdef FEAT_INS_EXPAND
6289 "insert_expand",
6290#endif
6291#ifdef FEAT_JOB_CHANNEL
6292 "job",
6293#endif
6294#ifdef FEAT_JUMPLIST
6295 "jumplist",
6296#endif
6297#ifdef FEAT_KEYMAP
6298 "keymap",
6299#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006300 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301#ifdef FEAT_LANGMAP
6302 "langmap",
6303#endif
6304#ifdef FEAT_LIBCALL
6305 "libcall",
6306#endif
6307#ifdef FEAT_LINEBREAK
6308 "linebreak",
6309#endif
6310#ifdef FEAT_LISP
6311 "lispindent",
6312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef FEAT_LOCALMAP
6315 "localmap",
6316#endif
6317#ifdef FEAT_LUA
6318# ifndef DYNAMIC_LUA
6319 "lua",
6320# endif
6321#endif
6322#ifdef FEAT_MENU
6323 "menu",
6324#endif
6325#ifdef FEAT_SESSION
6326 "mksession",
6327#endif
6328#ifdef FEAT_MODIFY_FNAME
6329 "modify_fname",
6330#endif
6331#ifdef FEAT_MOUSE
6332 "mouse",
6333#endif
6334#ifdef FEAT_MOUSESHAPE
6335 "mouseshape",
6336#endif
6337#if defined(UNIX) || defined(VMS)
6338# ifdef FEAT_MOUSE_DEC
6339 "mouse_dec",
6340# endif
6341# ifdef FEAT_MOUSE_GPM
6342 "mouse_gpm",
6343# endif
6344# ifdef FEAT_MOUSE_JSB
6345 "mouse_jsbterm",
6346# endif
6347# ifdef FEAT_MOUSE_NET
6348 "mouse_netterm",
6349# endif
6350# ifdef FEAT_MOUSE_PTERM
6351 "mouse_pterm",
6352# endif
6353# ifdef FEAT_MOUSE_SGR
6354 "mouse_sgr",
6355# endif
6356# ifdef FEAT_SYSMOUSE
6357 "mouse_sysmouse",
6358# endif
6359# ifdef FEAT_MOUSE_URXVT
6360 "mouse_urxvt",
6361# endif
6362# ifdef FEAT_MOUSE_XTERM
6363 "mouse_xterm",
6364# endif
6365#endif
6366#ifdef FEAT_MBYTE
6367 "multi_byte",
6368#endif
6369#ifdef FEAT_MBYTE_IME
6370 "multi_byte_ime",
6371#endif
6372#ifdef FEAT_MULTI_LANG
6373 "multi_lang",
6374#endif
6375#ifdef FEAT_MZSCHEME
6376#ifndef DYNAMIC_MZSCHEME
6377 "mzscheme",
6378#endif
6379#endif
6380#ifdef FEAT_NUM64
6381 "num64",
6382#endif
6383#ifdef FEAT_OLE
6384 "ole",
6385#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006386#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006388#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389#ifdef FEAT_PATH_EXTRA
6390 "path_extra",
6391#endif
6392#ifdef FEAT_PERL
6393#ifndef DYNAMIC_PERL
6394 "perl",
6395#endif
6396#endif
6397#ifdef FEAT_PERSISTENT_UNDO
6398 "persistent_undo",
6399#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006400#if defined(FEAT_PYTHON)
6401 "python_compiled",
6402# if defined(DYNAMIC_PYTHON)
6403 "python_dynamic",
6404# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006406 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006407# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006409#if defined(FEAT_PYTHON3)
6410 "python3_compiled",
6411# if defined(DYNAMIC_PYTHON3)
6412 "python3_dynamic",
6413# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006415 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006416# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417#endif
6418#ifdef FEAT_POSTSCRIPT
6419 "postscript",
6420#endif
6421#ifdef FEAT_PRINTER
6422 "printer",
6423#endif
6424#ifdef FEAT_PROFILE
6425 "profile",
6426#endif
6427#ifdef FEAT_RELTIME
6428 "reltime",
6429#endif
6430#ifdef FEAT_QUICKFIX
6431 "quickfix",
6432#endif
6433#ifdef FEAT_RIGHTLEFT
6434 "rightleft",
6435#endif
6436#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6437 "ruby",
6438#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440#ifdef FEAT_CMDL_INFO
6441 "showcmd",
6442 "cmdline_info",
6443#endif
6444#ifdef FEAT_SIGNS
6445 "signs",
6446#endif
6447#ifdef FEAT_SMARTINDENT
6448 "smartindent",
6449#endif
6450#ifdef STARTUPTIME
6451 "startuptime",
6452#endif
6453#ifdef FEAT_STL_OPT
6454 "statusline",
6455#endif
6456#ifdef FEAT_SUN_WORKSHOP
6457 "sun_workshop",
6458#endif
6459#ifdef FEAT_NETBEANS_INTG
6460 "netbeans_intg",
6461#endif
6462#ifdef FEAT_SPELL
6463 "spell",
6464#endif
6465#ifdef FEAT_SYN_HL
6466 "syntax",
6467#endif
6468#if defined(USE_SYSTEM) || !defined(UNIX)
6469 "system",
6470#endif
6471#ifdef FEAT_TAG_BINS
6472 "tag_binary",
6473#endif
6474#ifdef FEAT_TAG_OLDSTATIC
6475 "tag_old_static",
6476#endif
6477#ifdef FEAT_TAG_ANYWHITE
6478 "tag_any_white",
6479#endif
6480#ifdef FEAT_TCL
6481# ifndef DYNAMIC_TCL
6482 "tcl",
6483# endif
6484#endif
6485#ifdef FEAT_TERMGUICOLORS
6486 "termguicolors",
6487#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006488#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006489 "terminal",
6490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491#ifdef TERMINFO
6492 "terminfo",
6493#endif
6494#ifdef FEAT_TERMRESPONSE
6495 "termresponse",
6496#endif
6497#ifdef FEAT_TEXTOBJ
6498 "textobjects",
6499#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006500#ifdef FEAT_TEXT_PROP
6501 "textprop",
6502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503#ifdef HAVE_TGETENT
6504 "tgetent",
6505#endif
6506#ifdef FEAT_TIMERS
6507 "timers",
6508#endif
6509#ifdef FEAT_TITLE
6510 "title",
6511#endif
6512#ifdef FEAT_TOOLBAR
6513 "toolbar",
6514#endif
6515#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6516 "unnamedplus",
6517#endif
6518#ifdef FEAT_USR_CMDS
6519 "user-commands", /* was accidentally included in 5.4 */
6520 "user_commands",
6521#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006522#ifdef FEAT_VARTABS
6523 "vartabs",
6524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525#ifdef FEAT_VIMINFO
6526 "viminfo",
6527#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529#ifdef FEAT_VIRTUALEDIT
6530 "virtualedit",
6531#endif
6532 "visual",
6533#ifdef FEAT_VISUALEXTRA
6534 "visualextra",
6535#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006537#ifdef FEAT_VTP
6538 "vtp",
6539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540#ifdef FEAT_WILDIGN
6541 "wildignore",
6542#endif
6543#ifdef FEAT_WILDMENU
6544 "wildmenu",
6545#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547#ifdef FEAT_WAK
6548 "winaltkeys",
6549#endif
6550#ifdef FEAT_WRITEBACKUP
6551 "writebackup",
6552#endif
6553#ifdef FEAT_XIM
6554 "xim",
6555#endif
6556#ifdef FEAT_XFONTSET
6557 "xfontset",
6558#endif
6559#ifdef FEAT_XPM_W32
6560 "xpm",
6561 "xpm_w32", /* for backward compatibility */
6562#else
6563# if defined(HAVE_XPM)
6564 "xpm",
6565# endif
6566#endif
6567#ifdef USE_XSMP
6568 "xsmp",
6569#endif
6570#ifdef USE_XSMP_INTERACT
6571 "xsmp_interact",
6572#endif
6573#ifdef FEAT_XCLIPBOARD
6574 "xterm_clipboard",
6575#endif
6576#ifdef FEAT_XTERM_SAVE
6577 "xterm_save",
6578#endif
6579#if defined(UNIX) && defined(FEAT_X11)
6580 "X11",
6581#endif
6582 NULL
6583 };
6584
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006585 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 for (i = 0; has_list[i] != NULL; ++i)
6587 if (STRICMP(name, has_list[i]) == 0)
6588 {
6589 n = TRUE;
6590 break;
6591 }
6592
6593 if (n == FALSE)
6594 {
6595 if (STRNICMP(name, "patch", 5) == 0)
6596 {
6597 if (name[5] == '-'
6598 && STRLEN(name) >= 11
6599 && vim_isdigit(name[6])
6600 && vim_isdigit(name[8])
6601 && vim_isdigit(name[10]))
6602 {
6603 int major = atoi((char *)name + 6);
6604 int minor = atoi((char *)name + 8);
6605
6606 /* Expect "patch-9.9.01234". */
6607 n = (major < VIM_VERSION_MAJOR
6608 || (major == VIM_VERSION_MAJOR
6609 && (minor < VIM_VERSION_MINOR
6610 || (minor == VIM_VERSION_MINOR
6611 && has_patch(atoi((char *)name + 10))))));
6612 }
6613 else
6614 n = has_patch(atoi((char *)name + 5));
6615 }
6616 else if (STRICMP(name, "vim_starting") == 0)
6617 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006618 else if (STRICMP(name, "ttyin") == 0)
6619 n = mch_input_isatty();
6620 else if (STRICMP(name, "ttyout") == 0)
6621 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef FEAT_MBYTE
6623 else if (STRICMP(name, "multi_byte_encoding") == 0)
6624 n = has_mbyte;
6625#endif
6626#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6627 else if (STRICMP(name, "balloon_multiline") == 0)
6628 n = multiline_balloon_available();
6629#endif
6630#ifdef DYNAMIC_TCL
6631 else if (STRICMP(name, "tcl") == 0)
6632 n = tcl_enabled(FALSE);
6633#endif
6634#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6635 else if (STRICMP(name, "iconv") == 0)
6636 n = iconv_enabled(FALSE);
6637#endif
6638#ifdef DYNAMIC_LUA
6639 else if (STRICMP(name, "lua") == 0)
6640 n = lua_enabled(FALSE);
6641#endif
6642#ifdef DYNAMIC_MZSCHEME
6643 else if (STRICMP(name, "mzscheme") == 0)
6644 n = mzscheme_enabled(FALSE);
6645#endif
6646#ifdef DYNAMIC_RUBY
6647 else if (STRICMP(name, "ruby") == 0)
6648 n = ruby_enabled(FALSE);
6649#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650#ifdef DYNAMIC_PYTHON
6651 else if (STRICMP(name, "python") == 0)
6652 n = python_enabled(FALSE);
6653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654#ifdef DYNAMIC_PYTHON3
6655 else if (STRICMP(name, "python3") == 0)
6656 n = python3_enabled(FALSE);
6657#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006658#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6659 else if (STRICMP(name, "pythonx") == 0)
6660 {
6661# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6662 if (p_pyx == 0)
6663 n = python3_enabled(FALSE) || python_enabled(FALSE);
6664 else if (p_pyx == 3)
6665 n = python3_enabled(FALSE);
6666 else if (p_pyx == 2)
6667 n = python_enabled(FALSE);
6668# elif defined(DYNAMIC_PYTHON)
6669 n = python_enabled(FALSE);
6670# elif defined(DYNAMIC_PYTHON3)
6671 n = python3_enabled(FALSE);
6672# endif
6673 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674#endif
6675#ifdef DYNAMIC_PERL
6676 else if (STRICMP(name, "perl") == 0)
6677 n = perl_enabled(FALSE);
6678#endif
6679#ifdef FEAT_GUI
6680 else if (STRICMP(name, "gui_running") == 0)
6681 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682# ifdef FEAT_BROWSE
6683 else if (STRICMP(name, "browse") == 0)
6684 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6685# endif
6686#endif
6687#ifdef FEAT_SYN_HL
6688 else if (STRICMP(name, "syntax_items") == 0)
6689 n = syntax_present(curwin);
6690#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006691#ifdef FEAT_VTP
6692 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006693 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694#endif
6695#ifdef FEAT_NETBEANS_INTG
6696 else if (STRICMP(name, "netbeans_enabled") == 0)
6697 n = netbeans_active();
6698#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006699#if defined(FEAT_TERMINAL) && defined(WIN3264)
6700 else if (STRICMP(name, "terminal") == 0)
6701 n = terminal_enabled();
6702#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 }
6704
6705 rettv->vval.v_number = n;
6706}
6707
6708/*
6709 * "has_key()" function
6710 */
6711 static void
6712f_has_key(typval_T *argvars, typval_T *rettv)
6713{
6714 if (argvars[0].v_type != VAR_DICT)
6715 {
6716 EMSG(_(e_dictreq));
6717 return;
6718 }
6719 if (argvars[0].vval.v_dict == NULL)
6720 return;
6721
6722 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006723 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006724}
6725
6726/*
6727 * "haslocaldir()" function
6728 */
6729 static void
6730f_haslocaldir(typval_T *argvars, typval_T *rettv)
6731{
6732 win_T *wp = NULL;
6733
6734 wp = find_tabwin(&argvars[0], &argvars[1]);
6735 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6736}
6737
6738/*
6739 * "hasmapto()" function
6740 */
6741 static void
6742f_hasmapto(typval_T *argvars, typval_T *rettv)
6743{
6744 char_u *name;
6745 char_u *mode;
6746 char_u buf[NUMBUFLEN];
6747 int abbr = FALSE;
6748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006749 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 if (argvars[1].v_type == VAR_UNKNOWN)
6751 mode = (char_u *)"nvo";
6752 else
6753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006754 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006756 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 }
6758
6759 if (map_to_exists(name, mode, abbr))
6760 rettv->vval.v_number = TRUE;
6761 else
6762 rettv->vval.v_number = FALSE;
6763}
6764
6765/*
6766 * "histadd()" function
6767 */
6768 static void
6769f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6770{
6771#ifdef FEAT_CMDHIST
6772 int histype;
6773 char_u *str;
6774 char_u buf[NUMBUFLEN];
6775#endif
6776
6777 rettv->vval.v_number = FALSE;
6778 if (check_restricted() || check_secure())
6779 return;
6780#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006781 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 histype = str != NULL ? get_histtype(str) : -1;
6783 if (histype >= 0)
6784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006785 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 if (*str != NUL)
6787 {
6788 init_history();
6789 add_to_history(histype, str, FALSE, NUL);
6790 rettv->vval.v_number = TRUE;
6791 return;
6792 }
6793 }
6794#endif
6795}
6796
6797/*
6798 * "histdel()" function
6799 */
6800 static void
6801f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6802{
6803#ifdef FEAT_CMDHIST
6804 int n;
6805 char_u buf[NUMBUFLEN];
6806 char_u *str;
6807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006808 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 if (str == NULL)
6810 n = 0;
6811 else if (argvars[1].v_type == VAR_UNKNOWN)
6812 /* only one argument: clear entire history */
6813 n = clr_history(get_histtype(str));
6814 else if (argvars[1].v_type == VAR_NUMBER)
6815 /* index given: remove that entry */
6816 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006817 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 else
6819 /* string given: remove all matching entries */
6820 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006821 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 rettv->vval.v_number = n;
6823#endif
6824}
6825
6826/*
6827 * "histget()" function
6828 */
6829 static void
6830f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6831{
6832#ifdef FEAT_CMDHIST
6833 int type;
6834 int idx;
6835 char_u *str;
6836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006837 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838 if (str == NULL)
6839 rettv->vval.v_string = NULL;
6840 else
6841 {
6842 type = get_histtype(str);
6843 if (argvars[1].v_type == VAR_UNKNOWN)
6844 idx = get_history_idx(type);
6845 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 /* -1 on type error */
6848 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6849 }
6850#else
6851 rettv->vval.v_string = NULL;
6852#endif
6853 rettv->v_type = VAR_STRING;
6854}
6855
6856/*
6857 * "histnr()" function
6858 */
6859 static void
6860f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6861{
6862 int i;
6863
6864#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006865 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866
6867 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6868 if (i >= HIST_CMD && i < HIST_COUNT)
6869 i = get_history_idx(i);
6870 else
6871#endif
6872 i = -1;
6873 rettv->vval.v_number = i;
6874}
6875
6876/*
6877 * "highlightID(name)" function
6878 */
6879 static void
6880f_hlID(typval_T *argvars, typval_T *rettv)
6881{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006882 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006883}
6884
6885/*
6886 * "highlight_exists()" function
6887 */
6888 static void
6889f_hlexists(typval_T *argvars, typval_T *rettv)
6890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892}
6893
6894/*
6895 * "hostname()" function
6896 */
6897 static void
6898f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6899{
6900 char_u hostname[256];
6901
6902 mch_get_host_name(hostname, 256);
6903 rettv->v_type = VAR_STRING;
6904 rettv->vval.v_string = vim_strsave(hostname);
6905}
6906
6907/*
6908 * iconv() function
6909 */
6910 static void
6911f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6912{
6913#ifdef FEAT_MBYTE
6914 char_u buf1[NUMBUFLEN];
6915 char_u buf2[NUMBUFLEN];
6916 char_u *from, *to, *str;
6917 vimconv_T vimconv;
6918#endif
6919
6920 rettv->v_type = VAR_STRING;
6921 rettv->vval.v_string = NULL;
6922
6923#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 str = tv_get_string(&argvars[0]);
6925 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6926 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 vimconv.vc_type = CONV_NONE;
6928 convert_setup(&vimconv, from, to);
6929
6930 /* If the encodings are equal, no conversion needed. */
6931 if (vimconv.vc_type == CONV_NONE)
6932 rettv->vval.v_string = vim_strsave(str);
6933 else
6934 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6935
6936 convert_setup(&vimconv, NULL, NULL);
6937 vim_free(from);
6938 vim_free(to);
6939#endif
6940}
6941
6942/*
6943 * "indent()" function
6944 */
6945 static void
6946f_indent(typval_T *argvars, typval_T *rettv)
6947{
6948 linenr_T lnum;
6949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006950 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6952 rettv->vval.v_number = get_indent_lnum(lnum);
6953 else
6954 rettv->vval.v_number = -1;
6955}
6956
6957/*
6958 * "index()" function
6959 */
6960 static void
6961f_index(typval_T *argvars, typval_T *rettv)
6962{
6963 list_T *l;
6964 listitem_T *item;
6965 long idx = 0;
6966 int ic = FALSE;
6967
6968 rettv->vval.v_number = -1;
6969 if (argvars[0].v_type != VAR_LIST)
6970 {
6971 EMSG(_(e_listreq));
6972 return;
6973 }
6974 l = argvars[0].vval.v_list;
6975 if (l != NULL)
6976 {
6977 item = l->lv_first;
6978 if (argvars[2].v_type != VAR_UNKNOWN)
6979 {
6980 int error = FALSE;
6981
6982 /* Start at specified item. Use the cached index that list_find()
6983 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006984 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 idx = l->lv_idx;
6986 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006987 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006988 if (error)
6989 item = NULL;
6990 }
6991
6992 for ( ; item != NULL; item = item->li_next, ++idx)
6993 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6994 {
6995 rettv->vval.v_number = idx;
6996 break;
6997 }
6998 }
6999}
7000
7001static int inputsecret_flag = 0;
7002
7003/*
7004 * "input()" function
7005 * Also handles inputsecret() when inputsecret is set.
7006 */
7007 static void
7008f_input(typval_T *argvars, typval_T *rettv)
7009{
7010 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7011}
7012
7013/*
7014 * "inputdialog()" function
7015 */
7016 static void
7017f_inputdialog(typval_T *argvars, typval_T *rettv)
7018{
7019#if defined(FEAT_GUI_TEXTDIALOG)
7020 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7021 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7022 {
7023 char_u *message;
7024 char_u buf[NUMBUFLEN];
7025 char_u *defstr = (char_u *)"";
7026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007027 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007029 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7031 else
7032 IObuff[0] = NUL;
7033 if (message != NULL && defstr != NULL
7034 && do_dialog(VIM_QUESTION, NULL, message,
7035 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7036 rettv->vval.v_string = vim_strsave(IObuff);
7037 else
7038 {
7039 if (message != NULL && defstr != NULL
7040 && argvars[1].v_type != VAR_UNKNOWN
7041 && argvars[2].v_type != VAR_UNKNOWN)
7042 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 else
7045 rettv->vval.v_string = NULL;
7046 }
7047 rettv->v_type = VAR_STRING;
7048 }
7049 else
7050#endif
7051 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7052}
7053
7054/*
7055 * "inputlist()" function
7056 */
7057 static void
7058f_inputlist(typval_T *argvars, typval_T *rettv)
7059{
7060 listitem_T *li;
7061 int selected;
7062 int mouse_used;
7063
7064#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007065 /* While starting up, there is no place to enter text. When running tests
7066 * with --not-a-term we assume feedkeys() will be used. */
7067 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 return;
7069#endif
7070 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7071 {
7072 EMSG2(_(e_listarg), "inputlist()");
7073 return;
7074 }
7075
7076 msg_start();
7077 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7078 lines_left = Rows; /* avoid more prompt */
7079 msg_scroll = TRUE;
7080 msg_clr_eos();
7081
7082 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007084 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 msg_putchar('\n');
7086 }
7087
7088 /* Ask for choice. */
7089 selected = prompt_for_number(&mouse_used);
7090 if (mouse_used)
7091 selected -= lines_left;
7092
7093 rettv->vval.v_number = selected;
7094}
7095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7097
7098/*
7099 * "inputrestore()" function
7100 */
7101 static void
7102f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7103{
7104 if (ga_userinput.ga_len > 0)
7105 {
7106 --ga_userinput.ga_len;
7107 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7108 + ga_userinput.ga_len);
7109 /* default return is zero == OK */
7110 }
7111 else if (p_verbose > 1)
7112 {
7113 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7114 rettv->vval.v_number = 1; /* Failed */
7115 }
7116}
7117
7118/*
7119 * "inputsave()" function
7120 */
7121 static void
7122f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7123{
7124 /* Add an entry to the stack of typeahead storage. */
7125 if (ga_grow(&ga_userinput, 1) == OK)
7126 {
7127 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7128 + ga_userinput.ga_len);
7129 ++ga_userinput.ga_len;
7130 /* default return is zero == OK */
7131 }
7132 else
7133 rettv->vval.v_number = 1; /* Failed */
7134}
7135
7136/*
7137 * "inputsecret()" function
7138 */
7139 static void
7140f_inputsecret(typval_T *argvars, typval_T *rettv)
7141{
7142 ++cmdline_star;
7143 ++inputsecret_flag;
7144 f_input(argvars, rettv);
7145 --cmdline_star;
7146 --inputsecret_flag;
7147}
7148
7149/*
7150 * "insert()" function
7151 */
7152 static void
7153f_insert(typval_T *argvars, typval_T *rettv)
7154{
7155 long before = 0;
7156 listitem_T *item;
7157 list_T *l;
7158 int error = FALSE;
7159
7160 if (argvars[0].v_type != VAR_LIST)
7161 EMSG2(_(e_listarg), "insert()");
7162 else if ((l = argvars[0].vval.v_list) != NULL
7163 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7164 {
7165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007166 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 if (error)
7168 return; /* type error; errmsg already given */
7169
7170 if (before == l->lv_len)
7171 item = NULL;
7172 else
7173 {
7174 item = list_find(l, before);
7175 if (item == NULL)
7176 {
7177 EMSGN(_(e_listidx), before);
7178 l = NULL;
7179 }
7180 }
7181 if (l != NULL)
7182 {
7183 list_insert_tv(l, &argvars[1], item);
7184 copy_tv(&argvars[0], rettv);
7185 }
7186 }
7187}
7188
7189/*
7190 * "invert(expr)" function
7191 */
7192 static void
7193f_invert(typval_T *argvars, typval_T *rettv)
7194{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196}
7197
7198/*
7199 * "isdirectory()" function
7200 */
7201 static void
7202f_isdirectory(typval_T *argvars, typval_T *rettv)
7203{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007204 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205}
7206
7207/*
7208 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7209 * or it refers to a List or Dictionary that is locked.
7210 */
7211 static int
7212tv_islocked(typval_T *tv)
7213{
7214 return (tv->v_lock & VAR_LOCKED)
7215 || (tv->v_type == VAR_LIST
7216 && tv->vval.v_list != NULL
7217 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7218 || (tv->v_type == VAR_DICT
7219 && tv->vval.v_dict != NULL
7220 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7221}
7222
7223/*
7224 * "islocked()" function
7225 */
7226 static void
7227f_islocked(typval_T *argvars, typval_T *rettv)
7228{
7229 lval_T lv;
7230 char_u *end;
7231 dictitem_T *di;
7232
7233 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007235 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236 if (end != NULL && lv.ll_name != NULL)
7237 {
7238 if (*end != NUL)
7239 EMSG(_(e_trailing));
7240 else
7241 {
7242 if (lv.ll_tv == NULL)
7243 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007244 di = find_var(lv.ll_name, NULL, TRUE);
7245 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007247 /* Consider a variable locked when:
7248 * 1. the variable itself is locked
7249 * 2. the value of the variable is locked.
7250 * 3. the List or Dict value is locked.
7251 */
7252 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7253 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 }
7255 }
7256 else if (lv.ll_range)
7257 EMSG(_("E786: Range not allowed"));
7258 else if (lv.ll_newkey != NULL)
7259 EMSG2(_(e_dictkey), lv.ll_newkey);
7260 else if (lv.ll_list != NULL)
7261 /* List item. */
7262 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7263 else
7264 /* Dictionary item. */
7265 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7266 }
7267 }
7268
7269 clear_lval(&lv);
7270}
7271
7272#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7273/*
7274 * "isnan()" function
7275 */
7276 static void
7277f_isnan(typval_T *argvars, typval_T *rettv)
7278{
7279 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7280 && isnan(argvars[0].vval.v_float);
7281}
7282#endif
7283
7284/*
7285 * "items(dict)" function
7286 */
7287 static void
7288f_items(typval_T *argvars, typval_T *rettv)
7289{
7290 dict_list(argvars, rettv, 2);
7291}
7292
7293#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7294/*
7295 * Get the job from the argument.
7296 * Returns NULL if the job is invalid.
7297 */
7298 static job_T *
7299get_job_arg(typval_T *tv)
7300{
7301 job_T *job;
7302
7303 if (tv->v_type != VAR_JOB)
7304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007305 EMSG2(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 return NULL;
7307 }
7308 job = tv->vval.v_job;
7309
7310 if (job == NULL)
7311 EMSG(_("E916: not a valid job"));
7312 return job;
7313}
7314
7315/*
7316 * "job_getchannel()" function
7317 */
7318 static void
7319f_job_getchannel(typval_T *argvars, typval_T *rettv)
7320{
7321 job_T *job = get_job_arg(&argvars[0]);
7322
7323 if (job != NULL)
7324 {
7325 rettv->v_type = VAR_CHANNEL;
7326 rettv->vval.v_channel = job->jv_channel;
7327 if (job->jv_channel != NULL)
7328 ++job->jv_channel->ch_refcount;
7329 }
7330}
7331
7332/*
7333 * "job_info()" function
7334 */
7335 static void
7336f_job_info(typval_T *argvars, typval_T *rettv)
7337{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007338 if (argvars[0].v_type != VAR_UNKNOWN)
7339 {
7340 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007342 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7343 job_info(job, rettv->vval.v_dict);
7344 }
7345 else if (rettv_list_alloc(rettv) == OK)
7346 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347}
7348
7349/*
7350 * "job_setoptions()" function
7351 */
7352 static void
7353f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7354{
7355 job_T *job = get_job_arg(&argvars[0]);
7356 jobopt_T opt;
7357
7358 if (job == NULL)
7359 return;
7360 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007361 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 job_set_options(job, &opt);
7363 free_job_options(&opt);
7364}
7365
7366/*
7367 * "job_start()" function
7368 */
7369 static void
7370f_job_start(typval_T *argvars, typval_T *rettv)
7371{
7372 rettv->v_type = VAR_JOB;
7373 if (check_restricted() || check_secure())
7374 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007375 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376}
7377
7378/*
7379 * "job_status()" function
7380 */
7381 static void
7382f_job_status(typval_T *argvars, typval_T *rettv)
7383{
7384 job_T *job = get_job_arg(&argvars[0]);
7385
7386 if (job != NULL)
7387 {
7388 rettv->v_type = VAR_STRING;
7389 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7390 }
7391}
7392
7393/*
7394 * "job_stop()" function
7395 */
7396 static void
7397f_job_stop(typval_T *argvars, typval_T *rettv)
7398{
7399 job_T *job = get_job_arg(&argvars[0]);
7400
7401 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007402 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403}
7404#endif
7405
7406/*
7407 * "join()" function
7408 */
7409 static void
7410f_join(typval_T *argvars, typval_T *rettv)
7411{
7412 garray_T ga;
7413 char_u *sep;
7414
7415 if (argvars[0].v_type != VAR_LIST)
7416 {
7417 EMSG(_(e_listreq));
7418 return;
7419 }
7420 if (argvars[0].vval.v_list == NULL)
7421 return;
7422 if (argvars[1].v_type == VAR_UNKNOWN)
7423 sep = (char_u *)" ";
7424 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007425 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426
7427 rettv->v_type = VAR_STRING;
7428
7429 if (sep != NULL)
7430 {
7431 ga_init2(&ga, (int)sizeof(char), 80);
7432 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7433 ga_append(&ga, NUL);
7434 rettv->vval.v_string = (char_u *)ga.ga_data;
7435 }
7436 else
7437 rettv->vval.v_string = NULL;
7438}
7439
7440/*
7441 * "js_decode()" function
7442 */
7443 static void
7444f_js_decode(typval_T *argvars, typval_T *rettv)
7445{
7446 js_read_T reader;
7447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 reader.js_fill = NULL;
7450 reader.js_used = 0;
7451 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7452 EMSG(_(e_invarg));
7453}
7454
7455/*
7456 * "js_encode()" function
7457 */
7458 static void
7459f_js_encode(typval_T *argvars, typval_T *rettv)
7460{
7461 rettv->v_type = VAR_STRING;
7462 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7463}
7464
7465/*
7466 * "json_decode()" function
7467 */
7468 static void
7469f_json_decode(typval_T *argvars, typval_T *rettv)
7470{
7471 js_read_T reader;
7472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007473 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 reader.js_fill = NULL;
7475 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007476 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477}
7478
7479/*
7480 * "json_encode()" function
7481 */
7482 static void
7483f_json_encode(typval_T *argvars, typval_T *rettv)
7484{
7485 rettv->v_type = VAR_STRING;
7486 rettv->vval.v_string = json_encode(&argvars[0], 0);
7487}
7488
7489/*
7490 * "keys()" function
7491 */
7492 static void
7493f_keys(typval_T *argvars, typval_T *rettv)
7494{
7495 dict_list(argvars, rettv, 0);
7496}
7497
7498/*
7499 * "last_buffer_nr()" function.
7500 */
7501 static void
7502f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7503{
7504 int n = 0;
7505 buf_T *buf;
7506
Bram Moolenaar29323592016-07-24 22:04:11 +02007507 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 if (n < buf->b_fnum)
7509 n = buf->b_fnum;
7510
7511 rettv->vval.v_number = n;
7512}
7513
7514/*
7515 * "len()" function
7516 */
7517 static void
7518f_len(typval_T *argvars, typval_T *rettv)
7519{
7520 switch (argvars[0].v_type)
7521 {
7522 case VAR_STRING:
7523 case VAR_NUMBER:
7524 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 break;
7527 case VAR_LIST:
7528 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7529 break;
7530 case VAR_DICT:
7531 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7532 break;
7533 case VAR_UNKNOWN:
7534 case VAR_SPECIAL:
7535 case VAR_FLOAT:
7536 case VAR_FUNC:
7537 case VAR_PARTIAL:
7538 case VAR_JOB:
7539 case VAR_CHANNEL:
7540 EMSG(_("E701: Invalid type for len()"));
7541 break;
7542 }
7543}
7544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007546libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547{
7548#ifdef FEAT_LIBCALL
7549 char_u *string_in;
7550 char_u **string_result;
7551 int nr_result;
7552#endif
7553
7554 rettv->v_type = type;
7555 if (type != VAR_NUMBER)
7556 rettv->vval.v_string = NULL;
7557
7558 if (check_restricted() || check_secure())
7559 return;
7560
7561#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007562 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7564 {
7565 string_in = NULL;
7566 if (argvars[2].v_type == VAR_STRING)
7567 string_in = argvars[2].vval.v_string;
7568 if (type == VAR_NUMBER)
7569 string_result = NULL;
7570 else
7571 string_result = &rettv->vval.v_string;
7572 if (mch_libcall(argvars[0].vval.v_string,
7573 argvars[1].vval.v_string,
7574 string_in,
7575 argvars[2].vval.v_number,
7576 string_result,
7577 &nr_result) == OK
7578 && type == VAR_NUMBER)
7579 rettv->vval.v_number = nr_result;
7580 }
7581#endif
7582}
7583
7584/*
7585 * "libcall()" function
7586 */
7587 static void
7588f_libcall(typval_T *argvars, typval_T *rettv)
7589{
7590 libcall_common(argvars, rettv, VAR_STRING);
7591}
7592
7593/*
7594 * "libcallnr()" function
7595 */
7596 static void
7597f_libcallnr(typval_T *argvars, typval_T *rettv)
7598{
7599 libcall_common(argvars, rettv, VAR_NUMBER);
7600}
7601
7602/*
7603 * "line(string)" function
7604 */
7605 static void
7606f_line(typval_T *argvars, typval_T *rettv)
7607{
7608 linenr_T lnum = 0;
7609 pos_T *fp;
7610 int fnum;
7611
7612 fp = var2fpos(&argvars[0], TRUE, &fnum);
7613 if (fp != NULL)
7614 lnum = fp->lnum;
7615 rettv->vval.v_number = lnum;
7616}
7617
7618/*
7619 * "line2byte(lnum)" function
7620 */
7621 static void
7622f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7623{
7624#ifndef FEAT_BYTEOFF
7625 rettv->vval.v_number = -1;
7626#else
7627 linenr_T lnum;
7628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7631 rettv->vval.v_number = -1;
7632 else
7633 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7634 if (rettv->vval.v_number >= 0)
7635 ++rettv->vval.v_number;
7636#endif
7637}
7638
7639/*
7640 * "lispindent(lnum)" function
7641 */
7642 static void
7643f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7644{
7645#ifdef FEAT_LISP
7646 pos_T pos;
7647 linenr_T lnum;
7648
7649 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7652 {
7653 curwin->w_cursor.lnum = lnum;
7654 rettv->vval.v_number = get_lisp_indent();
7655 curwin->w_cursor = pos;
7656 }
7657 else
7658#endif
7659 rettv->vval.v_number = -1;
7660}
7661
7662/*
7663 * "localtime()" function
7664 */
7665 static void
7666f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7667{
7668 rettv->vval.v_number = (varnumber_T)time(NULL);
7669}
7670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 static void
7672get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7673{
7674 char_u *keys;
7675 char_u *which;
7676 char_u buf[NUMBUFLEN];
7677 char_u *keys_buf = NULL;
7678 char_u *rhs;
7679 int mode;
7680 int abbr = FALSE;
7681 int get_dict = FALSE;
7682 mapblock_T *mp;
7683 int buffer_local;
7684
7685 /* return empty string for failure */
7686 rettv->v_type = VAR_STRING;
7687 rettv->vval.v_string = NULL;
7688
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007689 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 if (*keys == NUL)
7691 return;
7692
7693 if (argvars[1].v_type != VAR_UNKNOWN)
7694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007695 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696 if (argvars[2].v_type != VAR_UNKNOWN)
7697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007698 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 }
7702 }
7703 else
7704 which = (char_u *)"";
7705 if (which == NULL)
7706 return;
7707
7708 mode = get_map_mode(&which, 0);
7709
7710 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7711 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7712 vim_free(keys_buf);
7713
7714 if (!get_dict)
7715 {
7716 /* Return a string. */
7717 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007718 {
7719 if (*rhs == NUL)
7720 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7721 else
7722 rettv->vval.v_string = str2special_save(rhs, FALSE);
7723 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724
7725 }
7726 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7727 {
7728 /* Return a dictionary. */
7729 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7730 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7731 dict_T *dict = rettv->vval.v_dict;
7732
Bram Moolenaare0be1672018-07-08 16:50:37 +02007733 dict_add_string(dict, "lhs", lhs);
7734 dict_add_string(dict, "rhs", mp->m_orig_str);
7735 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7736 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7737 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007738 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7739 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007740 dict_add_number(dict, "buffer", (long)buffer_local);
7741 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7742 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743
7744 vim_free(lhs);
7745 vim_free(mapmode);
7746 }
7747}
7748
7749#ifdef FEAT_FLOAT
7750/*
7751 * "log()" function
7752 */
7753 static void
7754f_log(typval_T *argvars, typval_T *rettv)
7755{
7756 float_T f = 0.0;
7757
7758 rettv->v_type = VAR_FLOAT;
7759 if (get_float_arg(argvars, &f) == OK)
7760 rettv->vval.v_float = log(f);
7761 else
7762 rettv->vval.v_float = 0.0;
7763}
7764
7765/*
7766 * "log10()" function
7767 */
7768 static void
7769f_log10(typval_T *argvars, typval_T *rettv)
7770{
7771 float_T f = 0.0;
7772
7773 rettv->v_type = VAR_FLOAT;
7774 if (get_float_arg(argvars, &f) == OK)
7775 rettv->vval.v_float = log10(f);
7776 else
7777 rettv->vval.v_float = 0.0;
7778}
7779#endif
7780
7781#ifdef FEAT_LUA
7782/*
7783 * "luaeval()" function
7784 */
7785 static void
7786f_luaeval(typval_T *argvars, typval_T *rettv)
7787{
7788 char_u *str;
7789 char_u buf[NUMBUFLEN];
7790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007791 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792 do_luaeval(str, argvars + 1, rettv);
7793}
7794#endif
7795
7796/*
7797 * "map()" function
7798 */
7799 static void
7800f_map(typval_T *argvars, typval_T *rettv)
7801{
7802 filter_map(argvars, rettv, TRUE);
7803}
7804
7805/*
7806 * "maparg()" function
7807 */
7808 static void
7809f_maparg(typval_T *argvars, typval_T *rettv)
7810{
7811 get_maparg(argvars, rettv, TRUE);
7812}
7813
7814/*
7815 * "mapcheck()" function
7816 */
7817 static void
7818f_mapcheck(typval_T *argvars, typval_T *rettv)
7819{
7820 get_maparg(argvars, rettv, FALSE);
7821}
7822
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007823typedef enum
7824{
7825 MATCH_END, /* matchend() */
7826 MATCH_MATCH, /* match() */
7827 MATCH_STR, /* matchstr() */
7828 MATCH_LIST, /* matchlist() */
7829 MATCH_POS /* matchstrpos() */
7830} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831
7832 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007833find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834{
7835 char_u *str = NULL;
7836 long len = 0;
7837 char_u *expr = NULL;
7838 char_u *pat;
7839 regmatch_T regmatch;
7840 char_u patbuf[NUMBUFLEN];
7841 char_u strbuf[NUMBUFLEN];
7842 char_u *save_cpo;
7843 long start = 0;
7844 long nth = 1;
7845 colnr_T startcol = 0;
7846 int match = 0;
7847 list_T *l = NULL;
7848 listitem_T *li = NULL;
7849 long idx = 0;
7850 char_u *tofree = NULL;
7851
7852 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7853 save_cpo = p_cpo;
7854 p_cpo = (char_u *)"";
7855
7856 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007857 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007859 /* type MATCH_LIST: return empty list when there are no matches.
7860 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 if (rettv_list_alloc(rettv) == FAIL)
7862 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007863 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 && (list_append_string(rettv->vval.v_list,
7865 (char_u *)"", 0) == FAIL
7866 || list_append_number(rettv->vval.v_list,
7867 (varnumber_T)-1) == FAIL
7868 || list_append_number(rettv->vval.v_list,
7869 (varnumber_T)-1) == FAIL
7870 || list_append_number(rettv->vval.v_list,
7871 (varnumber_T)-1) == FAIL))
7872 {
7873 list_free(rettv->vval.v_list);
7874 rettv->vval.v_list = NULL;
7875 goto theend;
7876 }
7877 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007878 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007879 {
7880 rettv->v_type = VAR_STRING;
7881 rettv->vval.v_string = NULL;
7882 }
7883
7884 if (argvars[0].v_type == VAR_LIST)
7885 {
7886 if ((l = argvars[0].vval.v_list) == NULL)
7887 goto theend;
7888 li = l->lv_first;
7889 }
7890 else
7891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 len = (long)STRLEN(str);
7894 }
7895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007896 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 if (pat == NULL)
7898 goto theend;
7899
7900 if (argvars[2].v_type != VAR_UNKNOWN)
7901 {
7902 int error = FALSE;
7903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 if (error)
7906 goto theend;
7907 if (l != NULL)
7908 {
7909 li = list_find(l, start);
7910 if (li == NULL)
7911 goto theend;
7912 idx = l->lv_idx; /* use the cached index */
7913 }
7914 else
7915 {
7916 if (start < 0)
7917 start = 0;
7918 if (start > len)
7919 goto theend;
7920 /* When "count" argument is there ignore matches before "start",
7921 * otherwise skip part of the string. Differs when pattern is "^"
7922 * or "\<". */
7923 if (argvars[3].v_type != VAR_UNKNOWN)
7924 startcol = start;
7925 else
7926 {
7927 str += start;
7928 len -= start;
7929 }
7930 }
7931
7932 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007933 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 if (error)
7935 goto theend;
7936 }
7937
7938 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7939 if (regmatch.regprog != NULL)
7940 {
7941 regmatch.rm_ic = p_ic;
7942
7943 for (;;)
7944 {
7945 if (l != NULL)
7946 {
7947 if (li == NULL)
7948 {
7949 match = FALSE;
7950 break;
7951 }
7952 vim_free(tofree);
7953 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7954 if (str == NULL)
7955 break;
7956 }
7957
7958 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7959
7960 if (match && --nth <= 0)
7961 break;
7962 if (l == NULL && !match)
7963 break;
7964
7965 /* Advance to just after the match. */
7966 if (l != NULL)
7967 {
7968 li = li->li_next;
7969 ++idx;
7970 }
7971 else
7972 {
7973#ifdef FEAT_MBYTE
7974 startcol = (colnr_T)(regmatch.startp[0]
7975 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7976#else
7977 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7978#endif
7979 if (startcol > (colnr_T)len
7980 || str + startcol <= regmatch.startp[0])
7981 {
7982 match = FALSE;
7983 break;
7984 }
7985 }
7986 }
7987
7988 if (match)
7989 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007990 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 {
7992 listitem_T *li1 = rettv->vval.v_list->lv_first;
7993 listitem_T *li2 = li1->li_next;
7994 listitem_T *li3 = li2->li_next;
7995 listitem_T *li4 = li3->li_next;
7996
7997 vim_free(li1->li_tv.vval.v_string);
7998 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7999 (int)(regmatch.endp[0] - regmatch.startp[0]));
8000 li3->li_tv.vval.v_number =
8001 (varnumber_T)(regmatch.startp[0] - expr);
8002 li4->li_tv.vval.v_number =
8003 (varnumber_T)(regmatch.endp[0] - expr);
8004 if (l != NULL)
8005 li2->li_tv.vval.v_number = (varnumber_T)idx;
8006 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008007 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 {
8009 int i;
8010
8011 /* return list with matched string and submatches */
8012 for (i = 0; i < NSUBEXP; ++i)
8013 {
8014 if (regmatch.endp[i] == NULL)
8015 {
8016 if (list_append_string(rettv->vval.v_list,
8017 (char_u *)"", 0) == FAIL)
8018 break;
8019 }
8020 else if (list_append_string(rettv->vval.v_list,
8021 regmatch.startp[i],
8022 (int)(regmatch.endp[i] - regmatch.startp[i]))
8023 == FAIL)
8024 break;
8025 }
8026 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008027 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 {
8029 /* return matched string */
8030 if (l != NULL)
8031 copy_tv(&li->li_tv, rettv);
8032 else
8033 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8034 (int)(regmatch.endp[0] - regmatch.startp[0]));
8035 }
8036 else if (l != NULL)
8037 rettv->vval.v_number = idx;
8038 else
8039 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008040 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 rettv->vval.v_number =
8042 (varnumber_T)(regmatch.startp[0] - str);
8043 else
8044 rettv->vval.v_number =
8045 (varnumber_T)(regmatch.endp[0] - str);
8046 rettv->vval.v_number += (varnumber_T)(str - expr);
8047 }
8048 }
8049 vim_regfree(regmatch.regprog);
8050 }
8051
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008052theend:
8053 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 /* matchstrpos() without a list: drop the second item. */
8055 listitem_remove(rettv->vval.v_list,
8056 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 vim_free(tofree);
8058 p_cpo = save_cpo;
8059}
8060
8061/*
8062 * "match()" function
8063 */
8064 static void
8065f_match(typval_T *argvars, typval_T *rettv)
8066{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008067 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068}
8069
Bram Moolenaar95e51472018-07-28 16:55:56 +02008070#ifdef FEAT_SEARCH_EXTRA
8071 static int
8072matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8073{
8074 dictitem_T *di;
8075
8076 if (tv->v_type != VAR_DICT)
8077 {
8078 EMSG(_(e_dictreq));
8079 return FAIL;
8080 }
8081
8082 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008083 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008084 (char_u *)"conceal", FALSE);
8085
8086 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8087 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008088 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008089 if (*win == NULL)
8090 {
8091 EMSG(_("E957: Invalid window number"));
8092 return FAIL;
8093 }
8094 }
8095
8096 return OK;
8097}
8098#endif
8099
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100/*
8101 * "matchadd()" function
8102 */
8103 static void
8104f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8105{
8106#ifdef FEAT_SEARCH_EXTRA
8107 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8109 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 int prio = 10; /* default priority */
8111 int id = -1;
8112 int error = FALSE;
8113 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008114 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115
8116 rettv->vval.v_number = -1;
8117
8118 if (grp == NULL || pat == NULL)
8119 return;
8120 if (argvars[2].v_type != VAR_UNKNOWN)
8121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008122 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 if (argvars[3].v_type != VAR_UNKNOWN)
8124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008125 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008126 if (argvars[4].v_type != VAR_UNKNOWN
8127 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8128 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 }
8130 }
8131 if (error == TRUE)
8132 return;
8133 if (id >= 1 && id <= 3)
8134 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008135 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 return;
8137 }
8138
Bram Moolenaar95e51472018-07-28 16:55:56 +02008139 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 conceal_char);
8141#endif
8142}
8143
8144/*
8145 * "matchaddpos()" function
8146 */
8147 static void
8148f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8149{
8150#ifdef FEAT_SEARCH_EXTRA
8151 char_u buf[NUMBUFLEN];
8152 char_u *group;
8153 int prio = 10;
8154 int id = -1;
8155 int error = FALSE;
8156 list_T *l;
8157 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008158 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159
8160 rettv->vval.v_number = -1;
8161
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 if (group == NULL)
8164 return;
8165
8166 if (argvars[1].v_type != VAR_LIST)
8167 {
8168 EMSG2(_(e_listarg), "matchaddpos()");
8169 return;
8170 }
8171 l = argvars[1].vval.v_list;
8172 if (l == NULL)
8173 return;
8174
8175 if (argvars[2].v_type != VAR_UNKNOWN)
8176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008177 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 if (argvars[3].v_type != VAR_UNKNOWN)
8179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008180 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008181
8182 if (argvars[4].v_type != VAR_UNKNOWN
8183 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8184 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 }
8186 }
8187 if (error == TRUE)
8188 return;
8189
8190 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8191 if (id == 1 || id == 2)
8192 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008193 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194 return;
8195 }
8196
Bram Moolenaar95e51472018-07-28 16:55:56 +02008197 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 conceal_char);
8199#endif
8200}
8201
8202/*
8203 * "matcharg()" function
8204 */
8205 static void
8206f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8207{
8208 if (rettv_list_alloc(rettv) == OK)
8209 {
8210#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008211 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 matchitem_T *m;
8213
8214 if (id >= 1 && id <= 3)
8215 {
8216 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8217 {
8218 list_append_string(rettv->vval.v_list,
8219 syn_id2name(m->hlg_id), -1);
8220 list_append_string(rettv->vval.v_list, m->pattern, -1);
8221 }
8222 else
8223 {
8224 list_append_string(rettv->vval.v_list, NULL, -1);
8225 list_append_string(rettv->vval.v_list, NULL, -1);
8226 }
8227 }
8228#endif
8229 }
8230}
8231
8232/*
8233 * "matchdelete()" function
8234 */
8235 static void
8236f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8237{
8238#ifdef FEAT_SEARCH_EXTRA
8239 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008240 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241#endif
8242}
8243
8244/*
8245 * "matchend()" function
8246 */
8247 static void
8248f_matchend(typval_T *argvars, typval_T *rettv)
8249{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008250 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251}
8252
8253/*
8254 * "matchlist()" function
8255 */
8256 static void
8257f_matchlist(typval_T *argvars, typval_T *rettv)
8258{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008259 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260}
8261
8262/*
8263 * "matchstr()" function
8264 */
8265 static void
8266f_matchstr(typval_T *argvars, typval_T *rettv)
8267{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008268 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269}
8270
8271/*
8272 * "matchstrpos()" function
8273 */
8274 static void
8275f_matchstrpos(typval_T *argvars, typval_T *rettv)
8276{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008277 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278}
8279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 static void
8281max_min(typval_T *argvars, typval_T *rettv, int domax)
8282{
8283 varnumber_T n = 0;
8284 varnumber_T i;
8285 int error = FALSE;
8286
8287 if (argvars[0].v_type == VAR_LIST)
8288 {
8289 list_T *l;
8290 listitem_T *li;
8291
8292 l = argvars[0].vval.v_list;
8293 if (l != NULL)
8294 {
8295 li = l->lv_first;
8296 if (li != NULL)
8297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008298 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 for (;;)
8300 {
8301 li = li->li_next;
8302 if (li == NULL)
8303 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 if (domax ? i > n : i < n)
8306 n = i;
8307 }
8308 }
8309 }
8310 }
8311 else if (argvars[0].v_type == VAR_DICT)
8312 {
8313 dict_T *d;
8314 int first = TRUE;
8315 hashitem_T *hi;
8316 int todo;
8317
8318 d = argvars[0].vval.v_dict;
8319 if (d != NULL)
8320 {
8321 todo = (int)d->dv_hashtab.ht_used;
8322 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8323 {
8324 if (!HASHITEM_EMPTY(hi))
8325 {
8326 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (first)
8329 {
8330 n = i;
8331 first = FALSE;
8332 }
8333 else if (domax ? i > n : i < n)
8334 n = i;
8335 }
8336 }
8337 }
8338 }
8339 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008340 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 rettv->vval.v_number = error ? 0 : n;
8342}
8343
8344/*
8345 * "max()" function
8346 */
8347 static void
8348f_max(typval_T *argvars, typval_T *rettv)
8349{
8350 max_min(argvars, rettv, TRUE);
8351}
8352
8353/*
8354 * "min()" function
8355 */
8356 static void
8357f_min(typval_T *argvars, typval_T *rettv)
8358{
8359 max_min(argvars, rettv, FALSE);
8360}
8361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362/*
8363 * Create the directory in which "dir" is located, and higher levels when
8364 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008365 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 */
8367 static int
8368mkdir_recurse(char_u *dir, int prot)
8369{
8370 char_u *p;
8371 char_u *updir;
8372 int r = FAIL;
8373
8374 /* Get end of directory name in "dir".
8375 * We're done when it's "/" or "c:/". */
8376 p = gettail_sep(dir);
8377 if (p <= get_past_head(dir))
8378 return OK;
8379
8380 /* If the directory exists we're done. Otherwise: create it.*/
8381 updir = vim_strnsave(dir, (int)(p - dir));
8382 if (updir == NULL)
8383 return FAIL;
8384 if (mch_isdir(updir))
8385 r = OK;
8386 else if (mkdir_recurse(updir, prot) == OK)
8387 r = vim_mkdir_emsg(updir, prot);
8388 vim_free(updir);
8389 return r;
8390}
8391
8392#ifdef vim_mkdir
8393/*
8394 * "mkdir()" function
8395 */
8396 static void
8397f_mkdir(typval_T *argvars, typval_T *rettv)
8398{
8399 char_u *dir;
8400 char_u buf[NUMBUFLEN];
8401 int prot = 0755;
8402
8403 rettv->vval.v_number = FAIL;
8404 if (check_restricted() || check_secure())
8405 return;
8406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008409 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008411 if (*gettail(dir) == NUL)
8412 /* remove trailing slashes */
8413 *gettail_sep(dir) = NUL;
8414
8415 if (argvars[1].v_type != VAR_UNKNOWN)
8416 {
8417 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008420 if (prot == -1)
8421 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008423 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008424 {
8425 if (mch_isdir(dir))
8426 {
8427 /* With the "p" flag it's OK if the dir already exists. */
8428 rettv->vval.v_number = OK;
8429 return;
8430 }
8431 mkdir_recurse(dir, prot);
8432 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008434 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435}
8436#endif
8437
8438/*
8439 * "mode()" function
8440 */
8441 static void
8442f_mode(typval_T *argvars, typval_T *rettv)
8443{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008444 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445
Bram Moolenaar612cc382018-07-29 15:34:26 +02008446 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447
8448 if (time_for_testing == 93784)
8449 {
8450 /* Testing the two-character code. */
8451 buf[0] = 'x';
8452 buf[1] = '!';
8453 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008454#ifdef FEAT_TERMINAL
8455 else if (term_use_loop())
8456 buf[0] = 't';
8457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 else if (VIsual_active)
8459 {
8460 if (VIsual_select)
8461 buf[0] = VIsual_mode + 's' - 'v';
8462 else
8463 buf[0] = VIsual_mode;
8464 }
8465 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8466 || State == CONFIRM)
8467 {
8468 buf[0] = 'r';
8469 if (State == ASKMORE)
8470 buf[1] = 'm';
8471 else if (State == CONFIRM)
8472 buf[1] = '?';
8473 }
8474 else if (State == EXTERNCMD)
8475 buf[0] = '!';
8476 else if (State & INSERT)
8477 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 if (State & VREPLACE_FLAG)
8479 {
8480 buf[0] = 'R';
8481 buf[1] = 'v';
8482 }
8483 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008484 {
8485 if (State & REPLACE_FLAG)
8486 buf[0] = 'R';
8487 else
8488 buf[0] = 'i';
8489#ifdef FEAT_INS_EXPAND
8490 if (ins_compl_active())
8491 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008492 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008493 buf[1] = 'x';
8494#endif
8495 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008497 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 {
8499 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008500 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008502 else if (exmode_active == EXMODE_NORMAL)
8503 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
8505 else
8506 {
8507 buf[0] = 'n';
8508 if (finish_op)
8509 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008510 else if (restart_edit == 'I' || restart_edit == 'R'
8511 || restart_edit == 'V')
8512 {
8513 buf[1] = 'i';
8514 buf[2] = restart_edit;
8515 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 }
8517
8518 /* Clear out the minor mode when the argument is not a non-zero number or
8519 * non-empty string. */
8520 if (!non_zero_arg(&argvars[0]))
8521 buf[1] = NUL;
8522
8523 rettv->vval.v_string = vim_strsave(buf);
8524 rettv->v_type = VAR_STRING;
8525}
8526
8527#if defined(FEAT_MZSCHEME) || defined(PROTO)
8528/*
8529 * "mzeval()" function
8530 */
8531 static void
8532f_mzeval(typval_T *argvars, typval_T *rettv)
8533{
8534 char_u *str;
8535 char_u buf[NUMBUFLEN];
8536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008537 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 do_mzeval(str, rettv);
8539}
8540
8541 void
8542mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8543{
8544 typval_T argvars[3];
8545
8546 argvars[0].v_type = VAR_STRING;
8547 argvars[0].vval.v_string = name;
8548 copy_tv(args, &argvars[1]);
8549 argvars[2].v_type = VAR_UNKNOWN;
8550 f_call(argvars, rettv);
8551 clear_tv(&argvars[1]);
8552}
8553#endif
8554
8555/*
8556 * "nextnonblank()" function
8557 */
8558 static void
8559f_nextnonblank(typval_T *argvars, typval_T *rettv)
8560{
8561 linenr_T lnum;
8562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008563 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 {
8565 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8566 {
8567 lnum = 0;
8568 break;
8569 }
8570 if (*skipwhite(ml_get(lnum)) != NUL)
8571 break;
8572 }
8573 rettv->vval.v_number = lnum;
8574}
8575
8576/*
8577 * "nr2char()" function
8578 */
8579 static void
8580f_nr2char(typval_T *argvars, typval_T *rettv)
8581{
8582 char_u buf[NUMBUFLEN];
8583
8584#ifdef FEAT_MBYTE
8585 if (has_mbyte)
8586 {
8587 int utf8 = 0;
8588
8589 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008590 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008592 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008594 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 }
8596 else
8597#endif
8598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008599 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 buf[1] = NUL;
8601 }
8602 rettv->v_type = VAR_STRING;
8603 rettv->vval.v_string = vim_strsave(buf);
8604}
8605
8606/*
8607 * "or(expr, expr)" function
8608 */
8609 static void
8610f_or(typval_T *argvars, typval_T *rettv)
8611{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008612 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8613 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614}
8615
8616/*
8617 * "pathshorten()" function
8618 */
8619 static void
8620f_pathshorten(typval_T *argvars, typval_T *rettv)
8621{
8622 char_u *p;
8623
8624 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008625 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626 if (p == NULL)
8627 rettv->vval.v_string = NULL;
8628 else
8629 {
8630 p = vim_strsave(p);
8631 rettv->vval.v_string = p;
8632 if (p != NULL)
8633 shorten_dir(p);
8634 }
8635}
8636
8637#ifdef FEAT_PERL
8638/*
8639 * "perleval()" function
8640 */
8641 static void
8642f_perleval(typval_T *argvars, typval_T *rettv)
8643{
8644 char_u *str;
8645 char_u buf[NUMBUFLEN];
8646
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008647 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 do_perleval(str, rettv);
8649}
8650#endif
8651
8652#ifdef FEAT_FLOAT
8653/*
8654 * "pow()" function
8655 */
8656 static void
8657f_pow(typval_T *argvars, typval_T *rettv)
8658{
8659 float_T fx = 0.0, fy = 0.0;
8660
8661 rettv->v_type = VAR_FLOAT;
8662 if (get_float_arg(argvars, &fx) == OK
8663 && get_float_arg(&argvars[1], &fy) == OK)
8664 rettv->vval.v_float = pow(fx, fy);
8665 else
8666 rettv->vval.v_float = 0.0;
8667}
8668#endif
8669
8670/*
8671 * "prevnonblank()" function
8672 */
8673 static void
8674f_prevnonblank(typval_T *argvars, typval_T *rettv)
8675{
8676 linenr_T lnum;
8677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008678 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8680 lnum = 0;
8681 else
8682 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8683 --lnum;
8684 rettv->vval.v_number = lnum;
8685}
8686
8687/* This dummy va_list is here because:
8688 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8689 * - locally in the function results in a "used before set" warning
8690 * - using va_start() to initialize it gives "function with fixed args" error */
8691static va_list ap;
8692
8693/*
8694 * "printf()" function
8695 */
8696 static void
8697f_printf(typval_T *argvars, typval_T *rettv)
8698{
8699 char_u buf[NUMBUFLEN];
8700 int len;
8701 char_u *s;
8702 int saved_did_emsg = did_emsg;
8703 char *fmt;
8704
8705 rettv->v_type = VAR_STRING;
8706 rettv->vval.v_string = NULL;
8707
8708 /* Get the required length, allocate the buffer and do it for real. */
8709 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008710 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008711 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 if (!did_emsg)
8713 {
8714 s = alloc(len + 1);
8715 if (s != NULL)
8716 {
8717 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008718 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8719 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 }
8721 }
8722 did_emsg |= saved_did_emsg;
8723}
8724
Bram Moolenaarf2732452018-06-03 14:47:35 +02008725#ifdef FEAT_JOB_CHANNEL
8726/*
8727 * "prompt_setcallback({buffer}, {callback})" function
8728 */
8729 static void
8730f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8731{
8732 buf_T *buf;
8733 char_u *callback;
8734 partial_T *partial;
8735
8736 if (check_secure())
8737 return;
8738 buf = get_buf_tv(&argvars[0], FALSE);
8739 if (buf == NULL)
8740 return;
8741
8742 callback = get_callback(&argvars[1], &partial);
8743 if (callback == NULL)
8744 return;
8745
8746 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8747 if (partial == NULL)
8748 buf->b_prompt_callback = vim_strsave(callback);
8749 else
8750 /* pointer into the partial */
8751 buf->b_prompt_callback = callback;
8752 buf->b_prompt_partial = partial;
8753}
8754
8755/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008756 * "prompt_setinterrupt({buffer}, {callback})" function
8757 */
8758 static void
8759f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8760{
8761 buf_T *buf;
8762 char_u *callback;
8763 partial_T *partial;
8764
8765 if (check_secure())
8766 return;
8767 buf = get_buf_tv(&argvars[0], FALSE);
8768 if (buf == NULL)
8769 return;
8770
8771 callback = get_callback(&argvars[1], &partial);
8772 if (callback == NULL)
8773 return;
8774
8775 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8776 if (partial == NULL)
8777 buf->b_prompt_interrupt = vim_strsave(callback);
8778 else
8779 /* pointer into the partial */
8780 buf->b_prompt_interrupt = callback;
8781 buf->b_prompt_int_partial = partial;
8782}
8783
8784/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008785 * "prompt_setprompt({buffer}, {text})" function
8786 */
8787 static void
8788f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8789{
8790 buf_T *buf;
8791 char_u *text;
8792
8793 if (check_secure())
8794 return;
8795 buf = get_buf_tv(&argvars[0], FALSE);
8796 if (buf == NULL)
8797 return;
8798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008799 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008800 vim_free(buf->b_prompt_text);
8801 buf->b_prompt_text = vim_strsave(text);
8802}
8803#endif
8804
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805/*
8806 * "pumvisible()" function
8807 */
8808 static void
8809f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8810{
8811#ifdef FEAT_INS_EXPAND
8812 if (pum_visible())
8813 rettv->vval.v_number = 1;
8814#endif
8815}
8816
8817#ifdef FEAT_PYTHON3
8818/*
8819 * "py3eval()" function
8820 */
8821 static void
8822f_py3eval(typval_T *argvars, typval_T *rettv)
8823{
8824 char_u *str;
8825 char_u buf[NUMBUFLEN];
8826
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008827 if (p_pyx == 0)
8828 p_pyx = 3;
8829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 do_py3eval(str, rettv);
8832}
8833#endif
8834
8835#ifdef FEAT_PYTHON
8836/*
8837 * "pyeval()" function
8838 */
8839 static void
8840f_pyeval(typval_T *argvars, typval_T *rettv)
8841{
8842 char_u *str;
8843 char_u buf[NUMBUFLEN];
8844
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008845 if (p_pyx == 0)
8846 p_pyx = 2;
8847
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008848 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 do_pyeval(str, rettv);
8850}
8851#endif
8852
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008853#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8854/*
8855 * "pyxeval()" function
8856 */
8857 static void
8858f_pyxeval(typval_T *argvars, typval_T *rettv)
8859{
8860# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8861 init_pyxversion();
8862 if (p_pyx == 2)
8863 f_pyeval(argvars, rettv);
8864 else
8865 f_py3eval(argvars, rettv);
8866# elif defined(FEAT_PYTHON)
8867 f_pyeval(argvars, rettv);
8868# elif defined(FEAT_PYTHON3)
8869 f_py3eval(argvars, rettv);
8870# endif
8871}
8872#endif
8873
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874/*
8875 * "range()" function
8876 */
8877 static void
8878f_range(typval_T *argvars, typval_T *rettv)
8879{
8880 varnumber_T start;
8881 varnumber_T end;
8882 varnumber_T stride = 1;
8883 varnumber_T i;
8884 int error = FALSE;
8885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008886 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 if (argvars[1].v_type == VAR_UNKNOWN)
8888 {
8889 end = start - 1;
8890 start = 0;
8891 }
8892 else
8893 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008894 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008896 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 }
8898
8899 if (error)
8900 return; /* type error; errmsg already given */
8901 if (stride == 0)
8902 EMSG(_("E726: Stride is zero"));
8903 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8904 EMSG(_("E727: Start past end"));
8905 else
8906 {
8907 if (rettv_list_alloc(rettv) == OK)
8908 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8909 if (list_append_number(rettv->vval.v_list,
8910 (varnumber_T)i) == FAIL)
8911 break;
8912 }
8913}
8914
8915/*
8916 * "readfile()" function
8917 */
8918 static void
8919f_readfile(typval_T *argvars, typval_T *rettv)
8920{
8921 int binary = FALSE;
8922 int failed = FALSE;
8923 char_u *fname;
8924 FILE *fd;
8925 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8926 int io_size = sizeof(buf);
8927 int readlen; /* size of last fread() */
8928 char_u *prev = NULL; /* previously read bytes, if any */
8929 long prevlen = 0; /* length of data in prev */
8930 long prevsize = 0; /* size of prev buffer */
8931 long maxline = MAXLNUM;
8932 long cnt = 0;
8933 char_u *p; /* position in buf */
8934 char_u *start; /* start of current line */
8935
8936 if (argvars[1].v_type != VAR_UNKNOWN)
8937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008938 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 binary = TRUE;
8940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008941 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 }
8943
8944 if (rettv_list_alloc(rettv) == FAIL)
8945 return;
8946
8947 /* Always open the file in binary mode, library functions have a mind of
8948 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008949 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8951 {
8952 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8953 return;
8954 }
8955
8956 while (cnt < maxline || maxline < 0)
8957 {
8958 readlen = (int)fread(buf, 1, io_size, fd);
8959
8960 /* This for loop processes what was read, but is also entered at end
8961 * of file so that either:
8962 * - an incomplete line gets written
8963 * - a "binary" file gets an empty line at the end if it ends in a
8964 * newline. */
8965 for (p = buf, start = buf;
8966 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8967 ++p)
8968 {
8969 if (*p == '\n' || readlen <= 0)
8970 {
8971 listitem_T *li;
8972 char_u *s = NULL;
8973 long_u len = p - start;
8974
8975 /* Finished a line. Remove CRs before NL. */
8976 if (readlen > 0 && !binary)
8977 {
8978 while (len > 0 && start[len - 1] == '\r')
8979 --len;
8980 /* removal may cross back to the "prev" string */
8981 if (len == 0)
8982 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8983 --prevlen;
8984 }
8985 if (prevlen == 0)
8986 s = vim_strnsave(start, (int)len);
8987 else
8988 {
8989 /* Change "prev" buffer to be the right size. This way
8990 * the bytes are only copied once, and very long lines are
8991 * allocated only once. */
8992 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8993 {
8994 mch_memmove(s + prevlen, start, len);
8995 s[prevlen + len] = NUL;
8996 prev = NULL; /* the list will own the string */
8997 prevlen = prevsize = 0;
8998 }
8999 }
9000 if (s == NULL)
9001 {
9002 do_outofmem_msg((long_u) prevlen + len + 1);
9003 failed = TRUE;
9004 break;
9005 }
9006
9007 if ((li = listitem_alloc()) == NULL)
9008 {
9009 vim_free(s);
9010 failed = TRUE;
9011 break;
9012 }
9013 li->li_tv.v_type = VAR_STRING;
9014 li->li_tv.v_lock = 0;
9015 li->li_tv.vval.v_string = s;
9016 list_append(rettv->vval.v_list, li);
9017
9018 start = p + 1; /* step over newline */
9019 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9020 break;
9021 }
9022 else if (*p == NUL)
9023 *p = '\n';
9024#ifdef FEAT_MBYTE
9025 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9026 * when finding the BF and check the previous two bytes. */
9027 else if (*p == 0xbf && enc_utf8 && !binary)
9028 {
9029 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9030 * + 1, these may be in the "prev" string. */
9031 char_u back1 = p >= buf + 1 ? p[-1]
9032 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9033 char_u back2 = p >= buf + 2 ? p[-2]
9034 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9035 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9036
9037 if (back2 == 0xef && back1 == 0xbb)
9038 {
9039 char_u *dest = p - 2;
9040
9041 /* Usually a BOM is at the beginning of a file, and so at
9042 * the beginning of a line; then we can just step over it.
9043 */
9044 if (start == dest)
9045 start = p + 1;
9046 else
9047 {
9048 /* have to shuffle buf to close gap */
9049 int adjust_prevlen = 0;
9050
9051 if (dest < buf)
9052 {
9053 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9054 dest = buf;
9055 }
9056 if (readlen > p - buf + 1)
9057 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9058 readlen -= 3 - adjust_prevlen;
9059 prevlen -= adjust_prevlen;
9060 p = dest - 1;
9061 }
9062 }
9063 }
9064#endif
9065 } /* for */
9066
9067 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9068 break;
9069 if (start < p)
9070 {
9071 /* There's part of a line in buf, store it in "prev". */
9072 if (p - start + prevlen >= prevsize)
9073 {
9074 /* need bigger "prev" buffer */
9075 char_u *newprev;
9076
9077 /* A common use case is ordinary text files and "prev" gets a
9078 * fragment of a line, so the first allocation is made
9079 * small, to avoid repeatedly 'allocing' large and
9080 * 'reallocing' small. */
9081 if (prevsize == 0)
9082 prevsize = (long)(p - start);
9083 else
9084 {
9085 long grow50pc = (prevsize * 3) / 2;
9086 long growmin = (long)((p - start) * 2 + prevlen);
9087 prevsize = grow50pc > growmin ? grow50pc : growmin;
9088 }
9089 newprev = prev == NULL ? alloc(prevsize)
9090 : vim_realloc(prev, prevsize);
9091 if (newprev == NULL)
9092 {
9093 do_outofmem_msg((long_u)prevsize);
9094 failed = TRUE;
9095 break;
9096 }
9097 prev = newprev;
9098 }
9099 /* Add the line part to end of "prev". */
9100 mch_memmove(prev + prevlen, start, p - start);
9101 prevlen += (long)(p - start);
9102 }
9103 } /* while */
9104
9105 /*
9106 * For a negative line count use only the lines at the end of the file,
9107 * free the rest.
9108 */
9109 if (!failed && maxline < 0)
9110 while (cnt > -maxline)
9111 {
9112 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9113 --cnt;
9114 }
9115
9116 if (failed)
9117 {
9118 list_free(rettv->vval.v_list);
9119 /* readfile doc says an empty list is returned on error */
9120 rettv->vval.v_list = list_alloc();
9121 }
9122
9123 vim_free(prev);
9124 fclose(fd);
9125}
9126
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009127 static void
9128return_register(int regname, typval_T *rettv)
9129{
9130 char_u buf[2] = {0, 0};
9131
9132 buf[0] = (char_u)regname;
9133 rettv->v_type = VAR_STRING;
9134 rettv->vval.v_string = vim_strsave(buf);
9135}
9136
9137/*
9138 * "reg_executing()" function
9139 */
9140 static void
9141f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9142{
9143 return_register(reg_executing, rettv);
9144}
9145
9146/*
9147 * "reg_recording()" function
9148 */
9149 static void
9150f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9151{
9152 return_register(reg_recording, rettv);
9153}
9154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156/*
9157 * Convert a List to proftime_T.
9158 * Return FAIL when there is something wrong.
9159 */
9160 static int
9161list2proftime(typval_T *arg, proftime_T *tm)
9162{
9163 long n1, n2;
9164 int error = FALSE;
9165
9166 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9167 || arg->vval.v_list->lv_len != 2)
9168 return FAIL;
9169 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9170 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9171# ifdef WIN3264
9172 tm->HighPart = n1;
9173 tm->LowPart = n2;
9174# else
9175 tm->tv_sec = n1;
9176 tm->tv_usec = n2;
9177# endif
9178 return error ? FAIL : OK;
9179}
9180#endif /* FEAT_RELTIME */
9181
9182/*
9183 * "reltime()" function
9184 */
9185 static void
9186f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9187{
9188#ifdef FEAT_RELTIME
9189 proftime_T res;
9190 proftime_T start;
9191
9192 if (argvars[0].v_type == VAR_UNKNOWN)
9193 {
9194 /* No arguments: get current time. */
9195 profile_start(&res);
9196 }
9197 else if (argvars[1].v_type == VAR_UNKNOWN)
9198 {
9199 if (list2proftime(&argvars[0], &res) == FAIL)
9200 return;
9201 profile_end(&res);
9202 }
9203 else
9204 {
9205 /* Two arguments: compute the difference. */
9206 if (list2proftime(&argvars[0], &start) == FAIL
9207 || list2proftime(&argvars[1], &res) == FAIL)
9208 return;
9209 profile_sub(&res, &start);
9210 }
9211
9212 if (rettv_list_alloc(rettv) == OK)
9213 {
9214 long n1, n2;
9215
9216# ifdef WIN3264
9217 n1 = res.HighPart;
9218 n2 = res.LowPart;
9219# else
9220 n1 = res.tv_sec;
9221 n2 = res.tv_usec;
9222# endif
9223 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9224 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9225 }
9226#endif
9227}
9228
9229#ifdef FEAT_FLOAT
9230/*
9231 * "reltimefloat()" function
9232 */
9233 static void
9234f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9235{
9236# ifdef FEAT_RELTIME
9237 proftime_T tm;
9238# endif
9239
9240 rettv->v_type = VAR_FLOAT;
9241 rettv->vval.v_float = 0;
9242# ifdef FEAT_RELTIME
9243 if (list2proftime(&argvars[0], &tm) == OK)
9244 rettv->vval.v_float = profile_float(&tm);
9245# endif
9246}
9247#endif
9248
9249/*
9250 * "reltimestr()" function
9251 */
9252 static void
9253f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9254{
9255#ifdef FEAT_RELTIME
9256 proftime_T tm;
9257#endif
9258
9259 rettv->v_type = VAR_STRING;
9260 rettv->vval.v_string = NULL;
9261#ifdef FEAT_RELTIME
9262 if (list2proftime(&argvars[0], &tm) == OK)
9263 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9264#endif
9265}
9266
9267#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 static void
9269make_connection(void)
9270{
9271 if (X_DISPLAY == NULL
9272# ifdef FEAT_GUI
9273 && !gui.in_use
9274# endif
9275 )
9276 {
9277 x_force_connect = TRUE;
9278 setup_term_clip();
9279 x_force_connect = FALSE;
9280 }
9281}
9282
9283 static int
9284check_connection(void)
9285{
9286 make_connection();
9287 if (X_DISPLAY == NULL)
9288 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009289 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290 return FAIL;
9291 }
9292 return OK;
9293}
9294#endif
9295
9296#ifdef FEAT_CLIENTSERVER
9297 static void
9298remote_common(typval_T *argvars, typval_T *rettv, int expr)
9299{
9300 char_u *server_name;
9301 char_u *keys;
9302 char_u *r = NULL;
9303 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009304 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305# ifdef WIN32
9306 HWND w;
9307# else
9308 Window w;
9309# endif
9310
9311 if (check_restricted() || check_secure())
9312 return;
9313
9314# ifdef FEAT_X11
9315 if (check_connection() == FAIL)
9316 return;
9317# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009318 if (argvars[2].v_type != VAR_UNKNOWN
9319 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009320 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 if (server_name == NULL)
9324 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009325 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009327 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009329 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9330 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331# endif
9332 {
9333 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009334 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009336 vim_free(r);
9337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 else
9339 EMSG2(_("E241: Unable to send to %s"), server_name);
9340 return;
9341 }
9342
9343 rettv->vval.v_string = r;
9344
9345 if (argvars[2].v_type != VAR_UNKNOWN)
9346 {
9347 dictitem_T v;
9348 char_u str[30];
9349 char_u *idvar;
9350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009351 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009352 if (idvar != NULL && *idvar != NUL)
9353 {
9354 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9355 v.di_tv.v_type = VAR_STRING;
9356 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009358 vim_free(v.di_tv.vval.v_string);
9359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360 }
9361}
9362#endif
9363
9364/*
9365 * "remote_expr()" function
9366 */
9367 static void
9368f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9369{
9370 rettv->v_type = VAR_STRING;
9371 rettv->vval.v_string = NULL;
9372#ifdef FEAT_CLIENTSERVER
9373 remote_common(argvars, rettv, TRUE);
9374#endif
9375}
9376
9377/*
9378 * "remote_foreground()" function
9379 */
9380 static void
9381f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9382{
9383#ifdef FEAT_CLIENTSERVER
9384# ifdef WIN32
9385 /* On Win32 it's done in this application. */
9386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009387 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388
9389 if (server_name != NULL)
9390 serverForeground(server_name);
9391 }
9392# else
9393 /* Send a foreground() expression to the server. */
9394 argvars[1].v_type = VAR_STRING;
9395 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9396 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009397 rettv->v_type = VAR_STRING;
9398 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009399 remote_common(argvars, rettv, TRUE);
9400 vim_free(argvars[1].vval.v_string);
9401# endif
9402#endif
9403}
9404
9405 static void
9406f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9407{
9408#ifdef FEAT_CLIENTSERVER
9409 dictitem_T v;
9410 char_u *s = NULL;
9411# ifdef WIN32
9412 long_u n = 0;
9413# endif
9414 char_u *serverid;
9415
9416 if (check_restricted() || check_secure())
9417 {
9418 rettv->vval.v_number = -1;
9419 return;
9420 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009421 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 if (serverid == NULL)
9423 {
9424 rettv->vval.v_number = -1;
9425 return; /* type error; errmsg already given */
9426 }
9427# ifdef WIN32
9428 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9429 if (n == 0)
9430 rettv->vval.v_number = -1;
9431 else
9432 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009433 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 rettv->vval.v_number = (s != NULL);
9435 }
9436# else
9437 if (check_connection() == FAIL)
9438 return;
9439
9440 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9441 serverStrToWin(serverid), &s);
9442# endif
9443
9444 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9445 {
9446 char_u *retvar;
9447
9448 v.di_tv.v_type = VAR_STRING;
9449 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009450 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (retvar != NULL)
9452 set_var(retvar, &v.di_tv, FALSE);
9453 vim_free(v.di_tv.vval.v_string);
9454 }
9455#else
9456 rettv->vval.v_number = -1;
9457#endif
9458}
9459
9460 static void
9461f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9462{
9463 char_u *r = NULL;
9464
9465#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009466 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467
9468 if (serverid != NULL && !check_restricted() && !check_secure())
9469 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009470 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009471# ifdef WIN32
9472 /* The server's HWND is encoded in the 'id' parameter */
9473 long_u n = 0;
9474# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009475
9476 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009478
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9481 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009482 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 if (r == NULL)
9484# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009485 if (check_connection() == FAIL
9486 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9487 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488# endif
9489 EMSG(_("E277: Unable to read a server reply"));
9490 }
9491#endif
9492 rettv->v_type = VAR_STRING;
9493 rettv->vval.v_string = r;
9494}
9495
9496/*
9497 * "remote_send()" function
9498 */
9499 static void
9500f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9501{
9502 rettv->v_type = VAR_STRING;
9503 rettv->vval.v_string = NULL;
9504#ifdef FEAT_CLIENTSERVER
9505 remote_common(argvars, rettv, FALSE);
9506#endif
9507}
9508
9509/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009510 * "remote_startserver()" function
9511 */
9512 static void
9513f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9514{
9515#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009516 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009517
9518 if (server == NULL)
9519 return; /* type error; errmsg already given */
9520 if (serverName != NULL)
9521 EMSG(_("E941: already started a server"));
9522 else
9523 {
9524# ifdef FEAT_X11
9525 if (check_connection() == OK)
9526 serverRegisterName(X_DISPLAY, server);
9527# else
9528 serverSetName(server);
9529# endif
9530 }
9531#else
9532 EMSG(_("E942: +clientserver feature not available"));
9533#endif
9534}
9535
9536/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009537 * "remove()" function
9538 */
9539 static void
9540f_remove(typval_T *argvars, typval_T *rettv)
9541{
9542 list_T *l;
9543 listitem_T *item, *item2;
9544 listitem_T *li;
9545 long idx;
9546 long end;
9547 char_u *key;
9548 dict_T *d;
9549 dictitem_T *di;
9550 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9551
9552 if (argvars[0].v_type == VAR_DICT)
9553 {
9554 if (argvars[2].v_type != VAR_UNKNOWN)
9555 EMSG2(_(e_toomanyarg), "remove()");
9556 else if ((d = argvars[0].vval.v_dict) != NULL
9557 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009559 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 if (key != NULL)
9561 {
9562 di = dict_find(d, key, -1);
9563 if (di == NULL)
9564 EMSG2(_(e_dictkey), key);
9565 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9566 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9567 {
9568 *rettv = di->di_tv;
9569 init_tv(&di->di_tv);
9570 dictitem_remove(d, di);
9571 }
9572 }
9573 }
9574 }
9575 else if (argvars[0].v_type != VAR_LIST)
9576 EMSG2(_(e_listdictarg), "remove()");
9577 else if ((l = argvars[0].vval.v_list) != NULL
9578 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9579 {
9580 int error = FALSE;
9581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009582 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009583 if (error)
9584 ; /* type error: do nothing, errmsg already given */
9585 else if ((item = list_find(l, idx)) == NULL)
9586 EMSGN(_(e_listidx), idx);
9587 else
9588 {
9589 if (argvars[2].v_type == VAR_UNKNOWN)
9590 {
9591 /* Remove one item, return its value. */
9592 vimlist_remove(l, item, item);
9593 *rettv = item->li_tv;
9594 vim_free(item);
9595 }
9596 else
9597 {
9598 /* Remove range of items, return list with values. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009599 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600 if (error)
9601 ; /* type error: do nothing */
9602 else if ((item2 = list_find(l, end)) == NULL)
9603 EMSGN(_(e_listidx), end);
9604 else
9605 {
9606 int cnt = 0;
9607
9608 for (li = item; li != NULL; li = li->li_next)
9609 {
9610 ++cnt;
9611 if (li == item2)
9612 break;
9613 }
9614 if (li == NULL) /* didn't find "item2" after "item" */
9615 EMSG(_(e_invrange));
9616 else
9617 {
9618 vimlist_remove(l, item, item2);
9619 if (rettv_list_alloc(rettv) == OK)
9620 {
9621 l = rettv->vval.v_list;
9622 l->lv_first = item;
9623 l->lv_last = item2;
9624 item->li_prev = NULL;
9625 item2->li_next = NULL;
9626 l->lv_len = cnt;
9627 }
9628 }
9629 }
9630 }
9631 }
9632 }
9633}
9634
9635/*
9636 * "rename({from}, {to})" function
9637 */
9638 static void
9639f_rename(typval_T *argvars, typval_T *rettv)
9640{
9641 char_u buf[NUMBUFLEN];
9642
9643 if (check_restricted() || check_secure())
9644 rettv->vval.v_number = -1;
9645 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009646 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9647 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648}
9649
9650/*
9651 * "repeat()" function
9652 */
9653 static void
9654f_repeat(typval_T *argvars, typval_T *rettv)
9655{
9656 char_u *p;
9657 int n;
9658 int slen;
9659 int len;
9660 char_u *r;
9661 int i;
9662
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009663 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664 if (argvars[0].v_type == VAR_LIST)
9665 {
9666 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9667 while (n-- > 0)
9668 if (list_extend(rettv->vval.v_list,
9669 argvars[0].vval.v_list, NULL) == FAIL)
9670 break;
9671 }
9672 else
9673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009674 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675 rettv->v_type = VAR_STRING;
9676 rettv->vval.v_string = NULL;
9677
9678 slen = (int)STRLEN(p);
9679 len = slen * n;
9680 if (len <= 0)
9681 return;
9682
9683 r = alloc(len + 1);
9684 if (r != NULL)
9685 {
9686 for (i = 0; i < n; i++)
9687 mch_memmove(r + i * slen, p, (size_t)slen);
9688 r[len] = NUL;
9689 }
9690
9691 rettv->vval.v_string = r;
9692 }
9693}
9694
9695/*
9696 * "resolve()" function
9697 */
9698 static void
9699f_resolve(typval_T *argvars, typval_T *rettv)
9700{
9701 char_u *p;
9702#ifdef HAVE_READLINK
9703 char_u *buf = NULL;
9704#endif
9705
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009706 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707#ifdef FEAT_SHORTCUT
9708 {
9709 char_u *v = NULL;
9710
9711 v = mch_resolve_shortcut(p);
9712 if (v != NULL)
9713 rettv->vval.v_string = v;
9714 else
9715 rettv->vval.v_string = vim_strsave(p);
9716 }
9717#else
9718# ifdef HAVE_READLINK
9719 {
9720 char_u *cpy;
9721 int len;
9722 char_u *remain = NULL;
9723 char_u *q;
9724 int is_relative_to_current = FALSE;
9725 int has_trailing_pathsep = FALSE;
9726 int limit = 100;
9727
9728 p = vim_strsave(p);
9729
9730 if (p[0] == '.' && (vim_ispathsep(p[1])
9731 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9732 is_relative_to_current = TRUE;
9733
9734 len = STRLEN(p);
9735 if (len > 0 && after_pathsep(p, p + len))
9736 {
9737 has_trailing_pathsep = TRUE;
9738 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9739 }
9740
9741 q = getnextcomp(p);
9742 if (*q != NUL)
9743 {
9744 /* Separate the first path component in "p", and keep the
9745 * remainder (beginning with the path separator). */
9746 remain = vim_strsave(q - 1);
9747 q[-1] = NUL;
9748 }
9749
9750 buf = alloc(MAXPATHL + 1);
9751 if (buf == NULL)
9752 goto fail;
9753
9754 for (;;)
9755 {
9756 for (;;)
9757 {
9758 len = readlink((char *)p, (char *)buf, MAXPATHL);
9759 if (len <= 0)
9760 break;
9761 buf[len] = NUL;
9762
9763 if (limit-- == 0)
9764 {
9765 vim_free(p);
9766 vim_free(remain);
9767 EMSG(_("E655: Too many symbolic links (cycle?)"));
9768 rettv->vval.v_string = NULL;
9769 goto fail;
9770 }
9771
9772 /* Ensure that the result will have a trailing path separator
9773 * if the argument has one. */
9774 if (remain == NULL && has_trailing_pathsep)
9775 add_pathsep(buf);
9776
9777 /* Separate the first path component in the link value and
9778 * concatenate the remainders. */
9779 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9780 if (*q != NUL)
9781 {
9782 if (remain == NULL)
9783 remain = vim_strsave(q - 1);
9784 else
9785 {
9786 cpy = concat_str(q - 1, remain);
9787 if (cpy != NULL)
9788 {
9789 vim_free(remain);
9790 remain = cpy;
9791 }
9792 }
9793 q[-1] = NUL;
9794 }
9795
9796 q = gettail(p);
9797 if (q > p && *q == NUL)
9798 {
9799 /* Ignore trailing path separator. */
9800 q[-1] = NUL;
9801 q = gettail(p);
9802 }
9803 if (q > p && !mch_isFullName(buf))
9804 {
9805 /* symlink is relative to directory of argument */
9806 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9807 if (cpy != NULL)
9808 {
9809 STRCPY(cpy, p);
9810 STRCPY(gettail(cpy), buf);
9811 vim_free(p);
9812 p = cpy;
9813 }
9814 }
9815 else
9816 {
9817 vim_free(p);
9818 p = vim_strsave(buf);
9819 }
9820 }
9821
9822 if (remain == NULL)
9823 break;
9824
9825 /* Append the first path component of "remain" to "p". */
9826 q = getnextcomp(remain + 1);
9827 len = q - remain - (*q != NUL);
9828 cpy = vim_strnsave(p, STRLEN(p) + len);
9829 if (cpy != NULL)
9830 {
9831 STRNCAT(cpy, remain, len);
9832 vim_free(p);
9833 p = cpy;
9834 }
9835 /* Shorten "remain". */
9836 if (*q != NUL)
9837 STRMOVE(remain, q - 1);
9838 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009839 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 }
9841
9842 /* If the result is a relative path name, make it explicitly relative to
9843 * the current directory if and only if the argument had this form. */
9844 if (!vim_ispathsep(*p))
9845 {
9846 if (is_relative_to_current
9847 && *p != NUL
9848 && !(p[0] == '.'
9849 && (p[1] == NUL
9850 || vim_ispathsep(p[1])
9851 || (p[1] == '.'
9852 && (p[2] == NUL
9853 || vim_ispathsep(p[2]))))))
9854 {
9855 /* Prepend "./". */
9856 cpy = concat_str((char_u *)"./", p);
9857 if (cpy != NULL)
9858 {
9859 vim_free(p);
9860 p = cpy;
9861 }
9862 }
9863 else if (!is_relative_to_current)
9864 {
9865 /* Strip leading "./". */
9866 q = p;
9867 while (q[0] == '.' && vim_ispathsep(q[1]))
9868 q += 2;
9869 if (q > p)
9870 STRMOVE(p, p + 2);
9871 }
9872 }
9873
9874 /* Ensure that the result will have no trailing path separator
9875 * if the argument had none. But keep "/" or "//". */
9876 if (!has_trailing_pathsep)
9877 {
9878 q = p + STRLEN(p);
9879 if (after_pathsep(p, q))
9880 *gettail_sep(p) = NUL;
9881 }
9882
9883 rettv->vval.v_string = p;
9884 }
9885# else
9886 rettv->vval.v_string = vim_strsave(p);
9887# endif
9888#endif
9889
9890 simplify_filename(rettv->vval.v_string);
9891
9892#ifdef HAVE_READLINK
9893fail:
9894 vim_free(buf);
9895#endif
9896 rettv->v_type = VAR_STRING;
9897}
9898
9899/*
9900 * "reverse({list})" function
9901 */
9902 static void
9903f_reverse(typval_T *argvars, typval_T *rettv)
9904{
9905 list_T *l;
9906 listitem_T *li, *ni;
9907
9908 if (argvars[0].v_type != VAR_LIST)
9909 EMSG2(_(e_listarg), "reverse()");
9910 else if ((l = argvars[0].vval.v_list) != NULL
9911 && !tv_check_lock(l->lv_lock,
9912 (char_u *)N_("reverse() argument"), TRUE))
9913 {
9914 li = l->lv_last;
9915 l->lv_first = l->lv_last = NULL;
9916 l->lv_len = 0;
9917 while (li != NULL)
9918 {
9919 ni = li->li_prev;
9920 list_append(l, li);
9921 li = ni;
9922 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009923 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 l->lv_idx = l->lv_len - l->lv_idx - 1;
9925 }
9926}
9927
9928#define SP_NOMOVE 0x01 /* don't move cursor */
9929#define SP_REPEAT 0x02 /* repeat to find outer pair */
9930#define SP_RETCOUNT 0x04 /* return matchcount */
9931#define SP_SETPCMARK 0x08 /* set previous context mark */
9932#define SP_START 0x10 /* accept match at start position */
9933#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9934#define SP_END 0x40 /* leave cursor at end of match */
9935#define SP_COLUMN 0x80 /* start at cursor column */
9936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937/*
9938 * Get flags for a search function.
9939 * Possibly sets "p_ws".
9940 * Returns BACKWARD, FORWARD or zero (for an error).
9941 */
9942 static int
9943get_search_arg(typval_T *varp, int *flagsp)
9944{
9945 int dir = FORWARD;
9946 char_u *flags;
9947 char_u nbuf[NUMBUFLEN];
9948 int mask;
9949
9950 if (varp->v_type != VAR_UNKNOWN)
9951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009952 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 if (flags == NULL)
9954 return 0; /* type error; errmsg already given */
9955 while (*flags != NUL)
9956 {
9957 switch (*flags)
9958 {
9959 case 'b': dir = BACKWARD; break;
9960 case 'w': p_ws = TRUE; break;
9961 case 'W': p_ws = FALSE; break;
9962 default: mask = 0;
9963 if (flagsp != NULL)
9964 switch (*flags)
9965 {
9966 case 'c': mask = SP_START; break;
9967 case 'e': mask = SP_END; break;
9968 case 'm': mask = SP_RETCOUNT; break;
9969 case 'n': mask = SP_NOMOVE; break;
9970 case 'p': mask = SP_SUBPAT; break;
9971 case 'r': mask = SP_REPEAT; break;
9972 case 's': mask = SP_SETPCMARK; break;
9973 case 'z': mask = SP_COLUMN; break;
9974 }
9975 if (mask == 0)
9976 {
9977 EMSG2(_(e_invarg2), flags);
9978 dir = 0;
9979 }
9980 else
9981 *flagsp |= mask;
9982 }
9983 if (dir == 0)
9984 break;
9985 ++flags;
9986 }
9987 }
9988 return dir;
9989}
9990
9991/*
9992 * Shared by search() and searchpos() functions.
9993 */
9994 static int
9995search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9996{
9997 int flags;
9998 char_u *pat;
9999 pos_T pos;
10000 pos_T save_cursor;
10001 int save_p_ws = p_ws;
10002 int dir;
10003 int retval = 0; /* default: FAIL */
10004 long lnum_stop = 0;
10005 proftime_T tm;
10006#ifdef FEAT_RELTIME
10007 long time_limit = 0;
10008#endif
10009 int options = SEARCH_KEEP;
10010 int subpatnum;
10011
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010012 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010013 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10014 if (dir == 0)
10015 goto theend;
10016 flags = *flagsp;
10017 if (flags & SP_START)
10018 options |= SEARCH_START;
10019 if (flags & SP_END)
10020 options |= SEARCH_END;
10021 if (flags & SP_COLUMN)
10022 options |= SEARCH_COL;
10023
10024 /* Optional arguments: line number to stop searching and timeout. */
10025 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010027 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028 if (lnum_stop < 0)
10029 goto theend;
10030#ifdef FEAT_RELTIME
10031 if (argvars[3].v_type != VAR_UNKNOWN)
10032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010033 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 if (time_limit < 0)
10035 goto theend;
10036 }
10037#endif
10038 }
10039
10040#ifdef FEAT_RELTIME
10041 /* Set the time limit, if there is one. */
10042 profile_setlimit(time_limit, &tm);
10043#endif
10044
10045 /*
10046 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10047 * Check to make sure only those flags are set.
10048 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10049 * flags cannot be set. Check for that condition also.
10050 */
10051 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10052 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10053 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010054 EMSG2(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 goto theend;
10056 }
10057
10058 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010059 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010060 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010061 if (subpatnum != FAIL)
10062 {
10063 if (flags & SP_SUBPAT)
10064 retval = subpatnum;
10065 else
10066 retval = pos.lnum;
10067 if (flags & SP_SETPCMARK)
10068 setpcmark();
10069 curwin->w_cursor = pos;
10070 if (match_pos != NULL)
10071 {
10072 /* Store the match cursor position */
10073 match_pos->lnum = pos.lnum;
10074 match_pos->col = pos.col + 1;
10075 }
10076 /* "/$" will put the cursor after the end of the line, may need to
10077 * correct that here */
10078 check_cursor();
10079 }
10080
10081 /* If 'n' flag is used: restore cursor position. */
10082 if (flags & SP_NOMOVE)
10083 curwin->w_cursor = save_cursor;
10084 else
10085 curwin->w_set_curswant = TRUE;
10086theend:
10087 p_ws = save_p_ws;
10088
10089 return retval;
10090}
10091
10092#ifdef FEAT_FLOAT
10093
10094/*
10095 * round() is not in C90, use ceil() or floor() instead.
10096 */
10097 float_T
10098vim_round(float_T f)
10099{
10100 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10101}
10102
10103/*
10104 * "round({float})" function
10105 */
10106 static void
10107f_round(typval_T *argvars, typval_T *rettv)
10108{
10109 float_T f = 0.0;
10110
10111 rettv->v_type = VAR_FLOAT;
10112 if (get_float_arg(argvars, &f) == OK)
10113 rettv->vval.v_float = vim_round(f);
10114 else
10115 rettv->vval.v_float = 0.0;
10116}
10117#endif
10118
10119/*
10120 * "screenattr()" function
10121 */
10122 static void
10123f_screenattr(typval_T *argvars, typval_T *rettv)
10124{
10125 int row;
10126 int col;
10127 int c;
10128
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010129 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10130 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 if (row < 0 || row >= screen_Rows
10132 || col < 0 || col >= screen_Columns)
10133 c = -1;
10134 else
10135 c = ScreenAttrs[LineOffset[row] + col];
10136 rettv->vval.v_number = c;
10137}
10138
10139/*
10140 * "screenchar()" function
10141 */
10142 static void
10143f_screenchar(typval_T *argvars, typval_T *rettv)
10144{
10145 int row;
10146 int col;
10147 int off;
10148 int c;
10149
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010150 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10151 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 if (row < 0 || row >= screen_Rows
10153 || col < 0 || col >= screen_Columns)
10154 c = -1;
10155 else
10156 {
10157 off = LineOffset[row] + col;
10158#ifdef FEAT_MBYTE
10159 if (enc_utf8 && ScreenLinesUC[off] != 0)
10160 c = ScreenLinesUC[off];
10161 else
10162#endif
10163 c = ScreenLines[off];
10164 }
10165 rettv->vval.v_number = c;
10166}
10167
10168/*
10169 * "screencol()" function
10170 *
10171 * First column is 1 to be consistent with virtcol().
10172 */
10173 static void
10174f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10175{
10176 rettv->vval.v_number = screen_screencol() + 1;
10177}
10178
10179/*
10180 * "screenrow()" function
10181 */
10182 static void
10183f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10184{
10185 rettv->vval.v_number = screen_screenrow() + 1;
10186}
10187
10188/*
10189 * "search()" function
10190 */
10191 static void
10192f_search(typval_T *argvars, typval_T *rettv)
10193{
10194 int flags = 0;
10195
10196 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10197}
10198
10199/*
10200 * "searchdecl()" function
10201 */
10202 static void
10203f_searchdecl(typval_T *argvars, typval_T *rettv)
10204{
10205 int locally = 1;
10206 int thisblock = 0;
10207 int error = FALSE;
10208 char_u *name;
10209
10210 rettv->vval.v_number = 1; /* default: FAIL */
10211
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010212 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010213 if (argvars[1].v_type != VAR_UNKNOWN)
10214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010215 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010216 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010217 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010218 }
10219 if (!error && name != NULL)
10220 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10221 locally, thisblock, SEARCH_KEEP) == FAIL;
10222}
10223
10224/*
10225 * Used by searchpair() and searchpairpos()
10226 */
10227 static int
10228searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10229{
10230 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010231 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 int save_p_ws = p_ws;
10233 int dir;
10234 int flags = 0;
10235 char_u nbuf1[NUMBUFLEN];
10236 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237 int retval = 0; /* default: FAIL */
10238 long lnum_stop = 0;
10239 long time_limit = 0;
10240
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010241 /* Get the three pattern arguments: start, middle, end. Will result in an
10242 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010243 spat = tv_get_string_chk(&argvars[0]);
10244 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10245 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 if (spat == NULL || mpat == NULL || epat == NULL)
10247 goto theend; /* type error */
10248
10249 /* Handle the optional fourth argument: flags */
10250 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10251 if (dir == 0)
10252 goto theend;
10253
10254 /* Don't accept SP_END or SP_SUBPAT.
10255 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10256 */
10257 if ((flags & (SP_END | SP_SUBPAT)) != 0
10258 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10259 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010260 EMSG2(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010261 goto theend;
10262 }
10263
10264 /* Using 'r' implies 'W', otherwise it doesn't work. */
10265 if (flags & SP_REPEAT)
10266 p_ws = FALSE;
10267
10268 /* Optional fifth argument: skip expression */
10269 if (argvars[3].v_type == VAR_UNKNOWN
10270 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010271 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 else
10273 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010274 skip = &argvars[4];
10275 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10276 && skip->v_type != VAR_STRING)
10277 {
10278 /* Type error */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010279 EMSG2(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010280 goto theend;
10281 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 if (argvars[5].v_type != VAR_UNKNOWN)
10283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010284 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010287 EMSG2(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010289 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010290#ifdef FEAT_RELTIME
10291 if (argvars[6].v_type != VAR_UNKNOWN)
10292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010293 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010296 EMSG2(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 }
10300#endif
10301 }
10302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303
10304 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10305 match_pos, lnum_stop, time_limit);
10306
10307theend:
10308 p_ws = save_p_ws;
10309
10310 return retval;
10311}
10312
10313/*
10314 * "searchpair()" function
10315 */
10316 static void
10317f_searchpair(typval_T *argvars, typval_T *rettv)
10318{
10319 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10320}
10321
10322/*
10323 * "searchpairpos()" function
10324 */
10325 static void
10326f_searchpairpos(typval_T *argvars, typval_T *rettv)
10327{
10328 pos_T match_pos;
10329 int lnum = 0;
10330 int col = 0;
10331
10332 if (rettv_list_alloc(rettv) == FAIL)
10333 return;
10334
10335 if (searchpair_cmn(argvars, &match_pos) > 0)
10336 {
10337 lnum = match_pos.lnum;
10338 col = match_pos.col;
10339 }
10340
10341 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10342 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10343}
10344
10345/*
10346 * Search for a start/middle/end thing.
10347 * Used by searchpair(), see its documentation for the details.
10348 * Returns 0 or -1 for no match,
10349 */
10350 long
10351do_searchpair(
10352 char_u *spat, /* start pattern */
10353 char_u *mpat, /* middle pattern */
10354 char_u *epat, /* end pattern */
10355 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010356 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 int flags, /* SP_SETPCMARK and other SP_ values */
10358 pos_T *match_pos,
10359 linenr_T lnum_stop, /* stop at this line if not zero */
10360 long time_limit UNUSED) /* stop after this many msec */
10361{
10362 char_u *save_cpo;
10363 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10364 long retval = 0;
10365 pos_T pos;
10366 pos_T firstpos;
10367 pos_T foundpos;
10368 pos_T save_cursor;
10369 pos_T save_pos;
10370 int n;
10371 int r;
10372 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010373 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010374 int err;
10375 int options = SEARCH_KEEP;
10376 proftime_T tm;
10377
10378 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10379 save_cpo = p_cpo;
10380 p_cpo = empty_option;
10381
10382#ifdef FEAT_RELTIME
10383 /* Set the time limit, if there is one. */
10384 profile_setlimit(time_limit, &tm);
10385#endif
10386
10387 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10388 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010389 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10390 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010391 if (pat2 == NULL || pat3 == NULL)
10392 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010393 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010394 if (*mpat == NUL)
10395 STRCPY(pat3, pat2);
10396 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010397 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 spat, epat, mpat);
10399 if (flags & SP_START)
10400 options |= SEARCH_START;
10401
Bram Moolenaar48570482017-10-30 21:48:41 +010010402 if (skip != NULL)
10403 {
10404 /* Empty string means to not use the skip expression. */
10405 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10406 use_skip = skip->vval.v_string != NULL
10407 && *skip->vval.v_string != NUL;
10408 }
10409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 save_cursor = curwin->w_cursor;
10411 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010412 CLEAR_POS(&firstpos);
10413 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 pat = pat3;
10415 for (;;)
10416 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010417 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010418 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010419 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010420 /* didn't find it or found the first match again: FAIL */
10421 break;
10422
10423 if (firstpos.lnum == 0)
10424 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010425 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010426 {
10427 /* Found the same position again. Can happen with a pattern that
10428 * has "\zs" at the end and searching backwards. Advance one
10429 * character and try again. */
10430 if (dir == BACKWARD)
10431 decl(&pos);
10432 else
10433 incl(&pos);
10434 }
10435 foundpos = pos;
10436
10437 /* clear the start flag to avoid getting stuck here */
10438 options &= ~SEARCH_START;
10439
10440 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010441 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010442 {
10443 save_pos = curwin->w_cursor;
10444 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010445 err = FALSE;
10446 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010447 curwin->w_cursor = save_pos;
10448 if (err)
10449 {
10450 /* Evaluating {skip} caused an error, break here. */
10451 curwin->w_cursor = save_cursor;
10452 retval = -1;
10453 break;
10454 }
10455 if (r)
10456 continue;
10457 }
10458
10459 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10460 {
10461 /* Found end when searching backwards or start when searching
10462 * forward: nested pair. */
10463 ++nest;
10464 pat = pat2; /* nested, don't search for middle */
10465 }
10466 else
10467 {
10468 /* Found end when searching forward or start when searching
10469 * backward: end of (nested) pair; or found middle in outer pair. */
10470 if (--nest == 1)
10471 pat = pat3; /* outer level, search for middle */
10472 }
10473
10474 if (nest == 0)
10475 {
10476 /* Found the match: return matchcount or line number. */
10477 if (flags & SP_RETCOUNT)
10478 ++retval;
10479 else
10480 retval = pos.lnum;
10481 if (flags & SP_SETPCMARK)
10482 setpcmark();
10483 curwin->w_cursor = pos;
10484 if (!(flags & SP_REPEAT))
10485 break;
10486 nest = 1; /* search for next unmatched */
10487 }
10488 }
10489
10490 if (match_pos != NULL)
10491 {
10492 /* Store the match cursor position */
10493 match_pos->lnum = curwin->w_cursor.lnum;
10494 match_pos->col = curwin->w_cursor.col + 1;
10495 }
10496
10497 /* If 'n' flag is used or search failed: restore cursor position. */
10498 if ((flags & SP_NOMOVE) || retval == 0)
10499 curwin->w_cursor = save_cursor;
10500
10501theend:
10502 vim_free(pat2);
10503 vim_free(pat3);
10504 if (p_cpo == empty_option)
10505 p_cpo = save_cpo;
10506 else
10507 /* Darn, evaluating the {skip} expression changed the value. */
10508 free_string_option(save_cpo);
10509
10510 return retval;
10511}
10512
10513/*
10514 * "searchpos()" function
10515 */
10516 static void
10517f_searchpos(typval_T *argvars, typval_T *rettv)
10518{
10519 pos_T match_pos;
10520 int lnum = 0;
10521 int col = 0;
10522 int n;
10523 int flags = 0;
10524
10525 if (rettv_list_alloc(rettv) == FAIL)
10526 return;
10527
10528 n = search_cmn(argvars, &match_pos, &flags);
10529 if (n > 0)
10530 {
10531 lnum = match_pos.lnum;
10532 col = match_pos.col;
10533 }
10534
10535 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10536 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10537 if (flags & SP_SUBPAT)
10538 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10539}
10540
10541 static void
10542f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10543{
10544#ifdef FEAT_CLIENTSERVER
10545 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010546 char_u *server = tv_get_string_chk(&argvars[0]);
10547 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010548
10549 rettv->vval.v_number = -1;
10550 if (server == NULL || reply == NULL)
10551 return;
10552 if (check_restricted() || check_secure())
10553 return;
10554# ifdef FEAT_X11
10555 if (check_connection() == FAIL)
10556 return;
10557# endif
10558
10559 if (serverSendReply(server, reply) < 0)
10560 {
10561 EMSG(_("E258: Unable to send to client"));
10562 return;
10563 }
10564 rettv->vval.v_number = 0;
10565#else
10566 rettv->vval.v_number = -1;
10567#endif
10568}
10569
10570 static void
10571f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10572{
10573 char_u *r = NULL;
10574
10575#ifdef FEAT_CLIENTSERVER
10576# ifdef WIN32
10577 r = serverGetVimNames();
10578# else
10579 make_connection();
10580 if (X_DISPLAY != NULL)
10581 r = serverGetVimNames(X_DISPLAY);
10582# endif
10583#endif
10584 rettv->v_type = VAR_STRING;
10585 rettv->vval.v_string = r;
10586}
10587
10588/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010589 * "setbufline()" function
10590 */
10591 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010592f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010593{
10594 linenr_T lnum;
10595 buf_T *buf;
10596
10597 buf = get_buf_tv(&argvars[0], FALSE);
10598 if (buf == NULL)
10599 rettv->vval.v_number = 1; /* FAIL */
10600 else
10601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010602 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010603 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010604 }
10605}
10606
10607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010608 * "setbufvar()" function
10609 */
10610 static void
10611f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10612{
10613 buf_T *buf;
10614 char_u *varname, *bufvarname;
10615 typval_T *varp;
10616 char_u nbuf[NUMBUFLEN];
10617
10618 if (check_restricted() || check_secure())
10619 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010620 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10621 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 buf = get_buf_tv(&argvars[0], FALSE);
10623 varp = &argvars[2];
10624
10625 if (buf != NULL && varname != NULL && varp != NULL)
10626 {
10627 if (*varname == '&')
10628 {
10629 long numval;
10630 char_u *strval;
10631 int error = FALSE;
10632 aco_save_T aco;
10633
10634 /* set curbuf to be our buf, temporarily */
10635 aucmd_prepbuf(&aco, buf);
10636
10637 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010638 numval = (long)tv_get_number_chk(varp, &error);
10639 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 if (!error && strval != NULL)
10641 set_option_value(varname, numval, strval, OPT_LOCAL);
10642
10643 /* reset notion of buffer */
10644 aucmd_restbuf(&aco);
10645 }
10646 else
10647 {
10648 buf_T *save_curbuf = curbuf;
10649
10650 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10651 if (bufvarname != NULL)
10652 {
10653 curbuf = buf;
10654 STRCPY(bufvarname, "b:");
10655 STRCPY(bufvarname + 2, varname);
10656 set_var(bufvarname, varp, TRUE);
10657 vim_free(bufvarname);
10658 curbuf = save_curbuf;
10659 }
10660 }
10661 }
10662}
10663
10664 static void
10665f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10666{
10667 dict_T *d;
10668 dictitem_T *di;
10669 char_u *csearch;
10670
10671 if (argvars[0].v_type != VAR_DICT)
10672 {
10673 EMSG(_(e_dictreq));
10674 return;
10675 }
10676
10677 if ((d = argvars[0].vval.v_dict) != NULL)
10678 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010679 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 if (csearch != NULL)
10681 {
10682#ifdef FEAT_MBYTE
10683 if (enc_utf8)
10684 {
10685 int pcc[MAX_MCO];
10686 int c = utfc_ptr2char(csearch, pcc);
10687
10688 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10689 }
10690 else
10691#endif
10692 set_last_csearch(PTR2CHAR(csearch),
10693 csearch, MB_PTR2LEN(csearch));
10694 }
10695
10696 di = dict_find(d, (char_u *)"forward", -1);
10697 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010698 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010699 ? FORWARD : BACKWARD);
10700
10701 di = dict_find(d, (char_u *)"until", -1);
10702 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010703 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010704 }
10705}
10706
10707/*
10708 * "setcmdpos()" function
10709 */
10710 static void
10711f_setcmdpos(typval_T *argvars, typval_T *rettv)
10712{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010713 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010714
10715 if (pos >= 0)
10716 rettv->vval.v_number = set_cmdline_pos(pos);
10717}
10718
10719/*
10720 * "setfperm({fname}, {mode})" function
10721 */
10722 static void
10723f_setfperm(typval_T *argvars, typval_T *rettv)
10724{
10725 char_u *fname;
10726 char_u modebuf[NUMBUFLEN];
10727 char_u *mode_str;
10728 int i;
10729 int mask;
10730 int mode = 0;
10731
10732 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010733 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734 if (fname == NULL)
10735 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010736 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 if (mode_str == NULL)
10738 return;
10739 if (STRLEN(mode_str) != 9)
10740 {
10741 EMSG2(_(e_invarg2), mode_str);
10742 return;
10743 }
10744
10745 mask = 1;
10746 for (i = 8; i >= 0; --i)
10747 {
10748 if (mode_str[i] != '-')
10749 mode |= mask;
10750 mask = mask << 1;
10751 }
10752 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10753}
10754
10755/*
10756 * "setline()" function
10757 */
10758 static void
10759f_setline(typval_T *argvars, typval_T *rettv)
10760{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010761 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762
Bram Moolenaarca851592018-06-06 21:04:07 +020010763 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764}
10765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766/*
10767 * Used by "setqflist()" and "setloclist()" functions
10768 */
10769 static void
10770set_qf_ll_list(
10771 win_T *wp UNUSED,
10772 typval_T *list_arg UNUSED,
10773 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010774 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010775 typval_T *rettv)
10776{
10777#ifdef FEAT_QUICKFIX
10778 static char *e_invact = N_("E927: Invalid action: '%s'");
10779 char_u *act;
10780 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010781 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782#endif
10783
10784 rettv->vval.v_number = -1;
10785
10786#ifdef FEAT_QUICKFIX
10787 if (list_arg->v_type != VAR_LIST)
10788 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010789 else if (recursive != 0)
10790 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010791 else
10792 {
10793 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010794 dict_T *d = NULL;
10795 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796
10797 if (action_arg->v_type == VAR_STRING)
10798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010799 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010800 if (act == NULL)
10801 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010802 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10803 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 action = *act;
10805 else
10806 EMSG2(_(e_invact), act);
10807 }
10808 else if (action_arg->v_type == VAR_UNKNOWN)
10809 action = ' ';
10810 else
10811 EMSG(_(e_stringreq));
10812
Bram Moolenaard823fa92016-08-12 16:29:27 +020010813 if (action_arg->v_type != VAR_UNKNOWN
10814 && what_arg->v_type != VAR_UNKNOWN)
10815 {
10816 if (what_arg->v_type == VAR_DICT)
10817 d = what_arg->vval.v_dict;
10818 else
10819 {
10820 EMSG(_(e_dictreq));
10821 valid_dict = FALSE;
10822 }
10823 }
10824
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010825 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010826 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010827 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10828 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010829 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010830 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010831 }
10832#endif
10833}
10834
10835/*
10836 * "setloclist()" function
10837 */
10838 static void
10839f_setloclist(typval_T *argvars, typval_T *rettv)
10840{
10841 win_T *win;
10842
10843 rettv->vval.v_number = -1;
10844
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010845 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010846 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010847 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848}
10849
10850/*
10851 * "setmatches()" function
10852 */
10853 static void
10854f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10855{
10856#ifdef FEAT_SEARCH_EXTRA
10857 list_T *l;
10858 listitem_T *li;
10859 dict_T *d;
10860 list_T *s = NULL;
10861
10862 rettv->vval.v_number = -1;
10863 if (argvars[0].v_type != VAR_LIST)
10864 {
10865 EMSG(_(e_listreq));
10866 return;
10867 }
10868 if ((l = argvars[0].vval.v_list) != NULL)
10869 {
10870
10871 /* To some extent make sure that we are dealing with a list from
10872 * "getmatches()". */
10873 li = l->lv_first;
10874 while (li != NULL)
10875 {
10876 if (li->li_tv.v_type != VAR_DICT
10877 || (d = li->li_tv.vval.v_dict) == NULL)
10878 {
10879 EMSG(_(e_invarg));
10880 return;
10881 }
10882 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10883 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10884 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10885 && dict_find(d, (char_u *)"priority", -1) != NULL
10886 && dict_find(d, (char_u *)"id", -1) != NULL))
10887 {
10888 EMSG(_(e_invarg));
10889 return;
10890 }
10891 li = li->li_next;
10892 }
10893
10894 clear_matches(curwin);
10895 li = l->lv_first;
10896 while (li != NULL)
10897 {
10898 int i = 0;
10899 char_u buf[5];
10900 dictitem_T *di;
10901 char_u *group;
10902 int priority;
10903 int id;
10904 char_u *conceal;
10905
10906 d = li->li_tv.vval.v_dict;
10907 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10908 {
10909 if (s == NULL)
10910 {
10911 s = list_alloc();
10912 if (s == NULL)
10913 return;
10914 }
10915
10916 /* match from matchaddpos() */
10917 for (i = 1; i < 9; i++)
10918 {
10919 sprintf((char *)buf, (char *)"pos%d", i);
10920 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10921 {
10922 if (di->di_tv.v_type != VAR_LIST)
10923 return;
10924
10925 list_append_tv(s, &di->di_tv);
10926 s->lv_refcount++;
10927 }
10928 else
10929 break;
10930 }
10931 }
10932
Bram Moolenaar8f667172018-12-14 15:38:31 +010010933 group = dict_get_string(d, (char_u *)"group", TRUE);
10934 priority = (int)dict_get_number(d, (char_u *)"priority");
10935 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010936 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010010937 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010938 : NULL;
10939 if (i == 0)
10940 {
10941 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010010942 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010943 priority, id, NULL, conceal);
10944 }
10945 else
10946 {
10947 match_add(curwin, group, NULL, priority, id, s, conceal);
10948 list_unref(s);
10949 s = NULL;
10950 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010951 vim_free(group);
10952 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953
10954 li = li->li_next;
10955 }
10956 rettv->vval.v_number = 0;
10957 }
10958#endif
10959}
10960
10961/*
10962 * "setpos()" function
10963 */
10964 static void
10965f_setpos(typval_T *argvars, typval_T *rettv)
10966{
10967 pos_T pos;
10968 int fnum;
10969 char_u *name;
10970 colnr_T curswant = -1;
10971
10972 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010973 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010974 if (name != NULL)
10975 {
10976 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10977 {
10978 if (--pos.col < 0)
10979 pos.col = 0;
10980 if (name[0] == '.' && name[1] == NUL)
10981 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010982 /* set cursor; "fnum" is ignored */
10983 curwin->w_cursor = pos;
10984 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010985 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010986 curwin->w_curswant = curswant - 1;
10987 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010988 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010989 check_cursor();
10990 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991 }
10992 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10993 {
10994 /* set mark */
10995 if (setmark_pos(name[1], &pos, fnum) == OK)
10996 rettv->vval.v_number = 0;
10997 }
10998 else
10999 EMSG(_(e_invarg));
11000 }
11001 }
11002}
11003
11004/*
11005 * "setqflist()" function
11006 */
11007 static void
11008f_setqflist(typval_T *argvars, typval_T *rettv)
11009{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011010 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011}
11012
11013/*
11014 * "setreg()" function
11015 */
11016 static void
11017f_setreg(typval_T *argvars, typval_T *rettv)
11018{
11019 int regname;
11020 char_u *strregname;
11021 char_u *stropt;
11022 char_u *strval;
11023 int append;
11024 char_u yank_type;
11025 long block_len;
11026
11027 block_len = -1;
11028 yank_type = MAUTO;
11029 append = FALSE;
11030
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011031 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 rettv->vval.v_number = 1; /* FAIL is default */
11033
11034 if (strregname == NULL)
11035 return; /* type error; errmsg already given */
11036 regname = *strregname;
11037 if (regname == 0 || regname == '@')
11038 regname = '"';
11039
11040 if (argvars[2].v_type != VAR_UNKNOWN)
11041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011042 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 if (stropt == NULL)
11044 return; /* type error */
11045 for (; *stropt != NUL; ++stropt)
11046 switch (*stropt)
11047 {
11048 case 'a': case 'A': /* append */
11049 append = TRUE;
11050 break;
11051 case 'v': case 'c': /* character-wise selection */
11052 yank_type = MCHAR;
11053 break;
11054 case 'V': case 'l': /* line-wise selection */
11055 yank_type = MLINE;
11056 break;
11057 case 'b': case Ctrl_V: /* block-wise selection */
11058 yank_type = MBLOCK;
11059 if (VIM_ISDIGIT(stropt[1]))
11060 {
11061 ++stropt;
11062 block_len = getdigits(&stropt) - 1;
11063 --stropt;
11064 }
11065 break;
11066 }
11067 }
11068
11069 if (argvars[1].v_type == VAR_LIST)
11070 {
11071 char_u **lstval;
11072 char_u **allocval;
11073 char_u buf[NUMBUFLEN];
11074 char_u **curval;
11075 char_u **curallocval;
11076 list_T *ll = argvars[1].vval.v_list;
11077 listitem_T *li;
11078 int len;
11079
11080 /* If the list is NULL handle like an empty list. */
11081 len = ll == NULL ? 0 : ll->lv_len;
11082
11083 /* First half: use for pointers to result lines; second half: use for
11084 * pointers to allocated copies. */
11085 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11086 if (lstval == NULL)
11087 return;
11088 curval = lstval;
11089 allocval = lstval + len + 2;
11090 curallocval = allocval;
11091
11092 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11093 li = li->li_next)
11094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011095 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096 if (strval == NULL)
11097 goto free_lstval;
11098 if (strval == buf)
11099 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011100 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 * overwrite the string. */
11102 strval = vim_strsave(buf);
11103 if (strval == NULL)
11104 goto free_lstval;
11105 *curallocval++ = strval;
11106 }
11107 *curval++ = strval;
11108 }
11109 *curval++ = NULL;
11110
11111 write_reg_contents_lst(regname, lstval, -1,
11112 append, yank_type, block_len);
11113free_lstval:
11114 while (curallocval > allocval)
11115 vim_free(*--curallocval);
11116 vim_free(lstval);
11117 }
11118 else
11119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011120 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 if (strval == NULL)
11122 return;
11123 write_reg_contents_ex(regname, strval, -1,
11124 append, yank_type, block_len);
11125 }
11126 rettv->vval.v_number = 0;
11127}
11128
11129/*
11130 * "settabvar()" function
11131 */
11132 static void
11133f_settabvar(typval_T *argvars, typval_T *rettv)
11134{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 tabpage_T *save_curtab;
11136 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137 char_u *varname, *tabvarname;
11138 typval_T *varp;
11139
11140 rettv->vval.v_number = 0;
11141
11142 if (check_restricted() || check_secure())
11143 return;
11144
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011145 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11146 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011147 varp = &argvars[2];
11148
Bram Moolenaar4033c552017-09-16 20:54:51 +020011149 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151 save_curtab = curtab;
11152 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153
11154 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11155 if (tabvarname != NULL)
11156 {
11157 STRCPY(tabvarname, "t:");
11158 STRCPY(tabvarname + 2, varname);
11159 set_var(tabvarname, varp, TRUE);
11160 vim_free(tabvarname);
11161 }
11162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011163 /* Restore current tabpage */
11164 if (valid_tabpage(save_curtab))
11165 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 }
11167}
11168
11169/*
11170 * "settabwinvar()" function
11171 */
11172 static void
11173f_settabwinvar(typval_T *argvars, typval_T *rettv)
11174{
11175 setwinvar(argvars, rettv, 1);
11176}
11177
11178/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011179 * "settagstack()" function
11180 */
11181 static void
11182f_settagstack(typval_T *argvars, typval_T *rettv)
11183{
11184 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11185 win_T *wp;
11186 dict_T *d;
11187 int action = 'r';
11188
11189 rettv->vval.v_number = -1;
11190
11191 // first argument: window number or id
11192 wp = find_win_by_nr_or_id(&argvars[0]);
11193 if (wp == NULL)
11194 return;
11195
11196 // second argument: dict with items to set in the tag stack
11197 if (argvars[1].v_type != VAR_DICT)
11198 {
11199 EMSG(_(e_dictreq));
11200 return;
11201 }
11202 d = argvars[1].vval.v_dict;
11203 if (d == NULL)
11204 return;
11205
11206 // third argument: action - 'a' for append and 'r' for replace.
11207 // default is to replace the stack.
11208 if (argvars[2].v_type == VAR_UNKNOWN)
11209 action = 'r';
11210 else if (argvars[2].v_type == VAR_STRING)
11211 {
11212 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011213 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011214 if (actstr == NULL)
11215 return;
11216 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11217 action = *actstr;
11218 else
11219 {
11220 EMSG2(_(e_invact2), actstr);
11221 return;
11222 }
11223 }
11224 else
11225 {
11226 EMSG(_(e_stringreq));
11227 return;
11228 }
11229
11230 if (set_tagstack(wp, d, action) == OK)
11231 rettv->vval.v_number = 0;
11232}
11233
11234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 * "setwinvar()" function
11236 */
11237 static void
11238f_setwinvar(typval_T *argvars, typval_T *rettv)
11239{
11240 setwinvar(argvars, rettv, 0);
11241}
11242
11243#ifdef FEAT_CRYPT
11244/*
11245 * "sha256({string})" function
11246 */
11247 static void
11248f_sha256(typval_T *argvars, typval_T *rettv)
11249{
11250 char_u *p;
11251
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011252 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011253 rettv->vval.v_string = vim_strsave(
11254 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11255 rettv->v_type = VAR_STRING;
11256}
11257#endif /* FEAT_CRYPT */
11258
11259/*
11260 * "shellescape({string})" function
11261 */
11262 static void
11263f_shellescape(typval_T *argvars, typval_T *rettv)
11264{
Bram Moolenaar20615522017-06-05 18:46:26 +020011265 int do_special = non_zero_arg(&argvars[1]);
11266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011267 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011268 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011269 rettv->v_type = VAR_STRING;
11270}
11271
11272/*
11273 * shiftwidth() function
11274 */
11275 static void
11276f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11277{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011278 rettv->vval.v_number = 0;
11279
11280 if (argvars[0].v_type != VAR_UNKNOWN)
11281 {
11282 long col;
11283
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011284 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011285 if (col < 0)
11286 return; // type error; errmsg already given
11287#ifdef FEAT_VARTABS
11288 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11289 return;
11290#endif
11291 }
11292
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 rettv->vval.v_number = get_sw_value(curbuf);
11294}
11295
Bram Moolenaar162b7142018-12-21 15:17:36 +010011296#ifdef FEAT_SIGNS
11297/*
11298 * "sign_define()" function
11299 */
11300 static void
11301f_sign_define(typval_T *argvars, typval_T *rettv)
11302{
11303 char_u *name;
11304 dict_T *dict;
11305 char_u *icon = NULL;
11306 char_u *linehl = NULL;
11307 char_u *text = NULL;
11308 char_u *texthl = NULL;
11309
11310 rettv->vval.v_number = -1;
11311
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011312 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011313 if (name == NULL)
11314 return;
11315
11316 if (argvars[1].v_type != VAR_UNKNOWN)
11317 {
11318 if (argvars[1].v_type != VAR_DICT)
11319 {
11320 EMSG(_(e_dictreq));
11321 return;
11322 }
11323
11324 // sign attributes
11325 dict = argvars[1].vval.v_dict;
11326 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11327 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11328 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11329 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11330 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11331 text = dict_get_string(dict, (char_u *)"text", TRUE);
11332 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11333 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11334 }
11335
11336 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11337 rettv->vval.v_number = 0;
11338
11339 vim_free(icon);
11340 vim_free(linehl);
11341 vim_free(text);
11342 vim_free(texthl);
11343}
11344
11345/*
11346 * "sign_getdefined()" function
11347 */
11348 static void
11349f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11350{
11351 char_u *name = NULL;
11352
11353 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11354 return;
11355
11356 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011357 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011358
11359 sign_getlist(name, rettv->vval.v_list);
11360}
11361
11362/*
11363 * "sign_getplaced()" function
11364 */
11365 static void
11366f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11367{
11368 buf_T *buf = NULL;
11369 dict_T *dict;
11370 dictitem_T *di;
11371 linenr_T lnum = 0;
11372 int sign_id = 0;
11373 char_u *group = NULL;
11374 int notanum = FALSE;
11375
11376 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11377 return;
11378
11379 if (argvars[0].v_type != VAR_UNKNOWN)
11380 {
11381 // get signs placed in this buffer
11382 buf = find_buffer(&argvars[0]);
11383 if (buf == NULL)
11384 {
11385 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011386 tv_get_string(&argvars[0]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011387 return;
11388 }
11389
11390 if (argvars[1].v_type != VAR_UNKNOWN)
11391 {
11392 if (argvars[1].v_type != VAR_DICT ||
11393 ((dict = argvars[1].vval.v_dict) == NULL))
11394 {
11395 EMSG(_(e_dictreq));
11396 return;
11397 }
11398 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11399 {
11400 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011401 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011402 if (notanum)
11403 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011404 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011405 }
11406 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11407 {
11408 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011409 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011410 if (notanum)
11411 return;
11412 }
11413 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011415 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011416 if (group == NULL)
11417 return;
11418 }
11419 }
11420 }
11421
11422 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11423}
11424
11425/*
11426 * "sign_place()" function
11427 */
11428 static void
11429f_sign_place(typval_T *argvars, typval_T *rettv)
11430{
11431 int sign_id;
11432 char_u *group = NULL;
11433 char_u *sign_name;
11434 buf_T *buf;
11435 dict_T *dict;
11436 dictitem_T *di;
11437 linenr_T lnum = 0;
11438 int prio = SIGN_DEF_PRIO;
11439 int notanum = FALSE;
11440
11441 rettv->vval.v_number = -1;
11442
11443 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011444 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011445 if (notanum)
11446 return;
11447 if (sign_id < 0)
11448 {
11449 EMSG(_(e_invarg));
11450 return;
11451 }
11452
11453 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011454 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011455 if (group == NULL)
11456 return;
11457 if (group[0] == '\0')
11458 group = NULL; // global sign group
11459 else
11460 {
11461 group = vim_strsave(group);
11462 if (group == NULL)
11463 return;
11464 }
11465
11466 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011467 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011468 if (sign_name == NULL)
11469 goto cleanup;
11470
11471 // Buffer to place the sign
11472 buf = find_buffer(&argvars[3]);
11473 if (buf == NULL)
11474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011475 EMSG2(_("E158: Invalid buffer name: %s"), tv_get_string(&argvars[2]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011476 goto cleanup;
11477 }
11478
11479 if (argvars[4].v_type != VAR_UNKNOWN)
11480 {
11481 if (argvars[4].v_type != VAR_DICT ||
11482 ((dict = argvars[4].vval.v_dict) == NULL))
11483 {
11484 EMSG(_(e_dictreq));
11485 goto cleanup;
11486 }
11487
11488 // Line number where the sign is to be placed
11489 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11490 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011491 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011492 if (notanum)
11493 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011494 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011495 }
11496 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11497 {
11498 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011500 if (notanum)
11501 goto cleanup;
11502 }
11503 }
11504
11505 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11506 rettv->vval.v_number = sign_id;
11507
11508cleanup:
11509 vim_free(group);
11510}
11511
11512/*
11513 * "sign_undefine()" function
11514 */
11515 static void
11516f_sign_undefine(typval_T *argvars, typval_T *rettv)
11517{
11518 char_u *name;
11519
11520 rettv->vval.v_number = -1;
11521
11522 if (argvars[0].v_type == VAR_UNKNOWN)
11523 {
11524 // Free all the signs
11525 free_signs();
11526 rettv->vval.v_number = 0;
11527 }
11528 else
11529 {
11530 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011531 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011532 if (name == NULL)
11533 return;
11534
11535 if (sign_undefine_by_name(name) == OK)
11536 rettv->vval.v_number = 0;
11537 }
11538}
11539
11540/*
11541 * "sign_unplace()" function
11542 */
11543 static void
11544f_sign_unplace(typval_T *argvars, typval_T *rettv)
11545{
11546 dict_T *dict;
11547 dictitem_T *di;
11548 int sign_id = 0;
11549 buf_T *buf = NULL;
11550 char_u *group = NULL;
11551
11552 rettv->vval.v_number = -1;
11553
11554 if (argvars[0].v_type != VAR_STRING)
11555 {
11556 EMSG(_(e_invarg));
11557 return;
11558 }
11559
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011560 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011561 if (group[0] == '\0')
11562 group = NULL; // global sign group
11563 else
11564 {
11565 group = vim_strsave(group);
11566 if (group == NULL)
11567 return;
11568 }
11569
11570 if (argvars[1].v_type != VAR_UNKNOWN)
11571 {
11572 if (argvars[1].v_type != VAR_DICT)
11573 {
11574 EMSG(_(e_dictreq));
11575 return;
11576 }
11577 dict = argvars[1].vval.v_dict;
11578
11579 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11580 {
11581 buf = find_buffer(&di->di_tv);
11582 if (buf == NULL)
11583 {
11584 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011585 tv_get_string(&di->di_tv));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011586 return;
11587 }
11588 }
11589 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11590 sign_id = dict_get_number(dict, (char_u *)"id");
11591 }
11592
11593 if (buf == NULL)
11594 {
11595 // Delete the sign in all the buffers
11596 FOR_ALL_BUFFERS(buf)
11597 if (sign_unplace(sign_id, group, buf) == OK)
11598 rettv->vval.v_number = 0;
11599 }
11600 else
11601 {
11602 if (sign_unplace(sign_id, group, buf) == OK)
11603 rettv->vval.v_number = 0;
11604 }
11605 vim_free(group);
11606}
11607#endif
11608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011609/*
11610 * "simplify()" function
11611 */
11612 static void
11613f_simplify(typval_T *argvars, typval_T *rettv)
11614{
11615 char_u *p;
11616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011617 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 rettv->vval.v_string = vim_strsave(p);
11619 simplify_filename(rettv->vval.v_string); /* simplify in place */
11620 rettv->v_type = VAR_STRING;
11621}
11622
11623#ifdef FEAT_FLOAT
11624/*
11625 * "sin()" function
11626 */
11627 static void
11628f_sin(typval_T *argvars, typval_T *rettv)
11629{
11630 float_T f = 0.0;
11631
11632 rettv->v_type = VAR_FLOAT;
11633 if (get_float_arg(argvars, &f) == OK)
11634 rettv->vval.v_float = sin(f);
11635 else
11636 rettv->vval.v_float = 0.0;
11637}
11638
11639/*
11640 * "sinh()" function
11641 */
11642 static void
11643f_sinh(typval_T *argvars, typval_T *rettv)
11644{
11645 float_T f = 0.0;
11646
11647 rettv->v_type = VAR_FLOAT;
11648 if (get_float_arg(argvars, &f) == OK)
11649 rettv->vval.v_float = sinh(f);
11650 else
11651 rettv->vval.v_float = 0.0;
11652}
11653#endif
11654
11655static int
11656#ifdef __BORLANDC__
11657 _RTLENTRYF
11658#endif
11659 item_compare(const void *s1, const void *s2);
11660static int
11661#ifdef __BORLANDC__
11662 _RTLENTRYF
11663#endif
11664 item_compare2(const void *s1, const void *s2);
11665
11666/* struct used in the array that's given to qsort() */
11667typedef struct
11668{
11669 listitem_T *item;
11670 int idx;
11671} sortItem_T;
11672
11673/* struct storing information about current sort */
11674typedef struct
11675{
11676 int item_compare_ic;
11677 int item_compare_numeric;
11678 int item_compare_numbers;
11679#ifdef FEAT_FLOAT
11680 int item_compare_float;
11681#endif
11682 char_u *item_compare_func;
11683 partial_T *item_compare_partial;
11684 dict_T *item_compare_selfdict;
11685 int item_compare_func_err;
11686 int item_compare_keep_zero;
11687} sortinfo_T;
11688static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011689#define ITEM_COMPARE_FAIL 999
11690
11691/*
11692 * Compare functions for f_sort() and f_uniq() below.
11693 */
11694 static int
11695#ifdef __BORLANDC__
11696_RTLENTRYF
11697#endif
11698item_compare(const void *s1, const void *s2)
11699{
11700 sortItem_T *si1, *si2;
11701 typval_T *tv1, *tv2;
11702 char_u *p1, *p2;
11703 char_u *tofree1 = NULL, *tofree2 = NULL;
11704 int res;
11705 char_u numbuf1[NUMBUFLEN];
11706 char_u numbuf2[NUMBUFLEN];
11707
11708 si1 = (sortItem_T *)s1;
11709 si2 = (sortItem_T *)s2;
11710 tv1 = &si1->item->li_tv;
11711 tv2 = &si2->item->li_tv;
11712
11713 if (sortinfo->item_compare_numbers)
11714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011715 varnumber_T v1 = tv_get_number(tv1);
11716 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717
11718 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11719 }
11720
11721#ifdef FEAT_FLOAT
11722 if (sortinfo->item_compare_float)
11723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011724 float_T v1 = tv_get_float(tv1);
11725 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726
11727 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11728 }
11729#endif
11730
11731 /* tv2string() puts quotes around a string and allocates memory. Don't do
11732 * that for string variables. Use a single quote when comparing with a
11733 * non-string to do what the docs promise. */
11734 if (tv1->v_type == VAR_STRING)
11735 {
11736 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11737 p1 = (char_u *)"'";
11738 else
11739 p1 = tv1->vval.v_string;
11740 }
11741 else
11742 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11743 if (tv2->v_type == VAR_STRING)
11744 {
11745 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11746 p2 = (char_u *)"'";
11747 else
11748 p2 = tv2->vval.v_string;
11749 }
11750 else
11751 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11752 if (p1 == NULL)
11753 p1 = (char_u *)"";
11754 if (p2 == NULL)
11755 p2 = (char_u *)"";
11756 if (!sortinfo->item_compare_numeric)
11757 {
11758 if (sortinfo->item_compare_ic)
11759 res = STRICMP(p1, p2);
11760 else
11761 res = STRCMP(p1, p2);
11762 }
11763 else
11764 {
11765 double n1, n2;
11766 n1 = strtod((char *)p1, (char **)&p1);
11767 n2 = strtod((char *)p2, (char **)&p2);
11768 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11769 }
11770
11771 /* When the result would be zero, compare the item indexes. Makes the
11772 * sort stable. */
11773 if (res == 0 && !sortinfo->item_compare_keep_zero)
11774 res = si1->idx > si2->idx ? 1 : -1;
11775
11776 vim_free(tofree1);
11777 vim_free(tofree2);
11778 return res;
11779}
11780
11781 static int
11782#ifdef __BORLANDC__
11783_RTLENTRYF
11784#endif
11785item_compare2(const void *s1, const void *s2)
11786{
11787 sortItem_T *si1, *si2;
11788 int res;
11789 typval_T rettv;
11790 typval_T argv[3];
11791 int dummy;
11792 char_u *func_name;
11793 partial_T *partial = sortinfo->item_compare_partial;
11794
11795 /* shortcut after failure in previous call; compare all items equal */
11796 if (sortinfo->item_compare_func_err)
11797 return 0;
11798
11799 si1 = (sortItem_T *)s1;
11800 si2 = (sortItem_T *)s2;
11801
11802 if (partial == NULL)
11803 func_name = sortinfo->item_compare_func;
11804 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011805 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806
11807 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11808 * in the copy without changing the original list items. */
11809 copy_tv(&si1->item->li_tv, &argv[0]);
11810 copy_tv(&si2->item->li_tv, &argv[1]);
11811
11812 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11813 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011814 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815 partial, sortinfo->item_compare_selfdict);
11816 clear_tv(&argv[0]);
11817 clear_tv(&argv[1]);
11818
11819 if (res == FAIL)
11820 res = ITEM_COMPARE_FAIL;
11821 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011822 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011823 if (sortinfo->item_compare_func_err)
11824 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11825 clear_tv(&rettv);
11826
11827 /* When the result would be zero, compare the pointers themselves. Makes
11828 * the sort stable. */
11829 if (res == 0 && !sortinfo->item_compare_keep_zero)
11830 res = si1->idx > si2->idx ? 1 : -1;
11831
11832 return res;
11833}
11834
11835/*
11836 * "sort({list})" function
11837 */
11838 static void
11839do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11840{
11841 list_T *l;
11842 listitem_T *li;
11843 sortItem_T *ptrs;
11844 sortinfo_T *old_sortinfo;
11845 sortinfo_T info;
11846 long len;
11847 long i;
11848
11849 /* Pointer to current info struct used in compare function. Save and
11850 * restore the current one for nested calls. */
11851 old_sortinfo = sortinfo;
11852 sortinfo = &info;
11853
11854 if (argvars[0].v_type != VAR_LIST)
11855 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11856 else
11857 {
11858 l = argvars[0].vval.v_list;
11859 if (l == NULL || tv_check_lock(l->lv_lock,
11860 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11861 TRUE))
11862 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011863 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011864
11865 len = list_len(l);
11866 if (len <= 1)
11867 goto theend; /* short list sorts pretty quickly */
11868
11869 info.item_compare_ic = FALSE;
11870 info.item_compare_numeric = FALSE;
11871 info.item_compare_numbers = FALSE;
11872#ifdef FEAT_FLOAT
11873 info.item_compare_float = FALSE;
11874#endif
11875 info.item_compare_func = NULL;
11876 info.item_compare_partial = NULL;
11877 info.item_compare_selfdict = NULL;
11878 if (argvars[1].v_type != VAR_UNKNOWN)
11879 {
11880 /* optional second argument: {func} */
11881 if (argvars[1].v_type == VAR_FUNC)
11882 info.item_compare_func = argvars[1].vval.v_string;
11883 else if (argvars[1].v_type == VAR_PARTIAL)
11884 info.item_compare_partial = argvars[1].vval.v_partial;
11885 else
11886 {
11887 int error = FALSE;
11888
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011889 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011890 if (error)
11891 goto theend; /* type error; errmsg already given */
11892 if (i == 1)
11893 info.item_compare_ic = TRUE;
11894 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011895 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 else if (i != 0)
11897 {
11898 EMSG(_(e_invarg));
11899 goto theend;
11900 }
11901 if (info.item_compare_func != NULL)
11902 {
11903 if (*info.item_compare_func == NUL)
11904 {
11905 /* empty string means default sort */
11906 info.item_compare_func = NULL;
11907 }
11908 else if (STRCMP(info.item_compare_func, "n") == 0)
11909 {
11910 info.item_compare_func = NULL;
11911 info.item_compare_numeric = TRUE;
11912 }
11913 else if (STRCMP(info.item_compare_func, "N") == 0)
11914 {
11915 info.item_compare_func = NULL;
11916 info.item_compare_numbers = TRUE;
11917 }
11918#ifdef FEAT_FLOAT
11919 else if (STRCMP(info.item_compare_func, "f") == 0)
11920 {
11921 info.item_compare_func = NULL;
11922 info.item_compare_float = TRUE;
11923 }
11924#endif
11925 else if (STRCMP(info.item_compare_func, "i") == 0)
11926 {
11927 info.item_compare_func = NULL;
11928 info.item_compare_ic = TRUE;
11929 }
11930 }
11931 }
11932
11933 if (argvars[2].v_type != VAR_UNKNOWN)
11934 {
11935 /* optional third argument: {dict} */
11936 if (argvars[2].v_type != VAR_DICT)
11937 {
11938 EMSG(_(e_dictreq));
11939 goto theend;
11940 }
11941 info.item_compare_selfdict = argvars[2].vval.v_dict;
11942 }
11943 }
11944
11945 /* Make an array with each entry pointing to an item in the List. */
11946 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11947 if (ptrs == NULL)
11948 goto theend;
11949
11950 i = 0;
11951 if (sort)
11952 {
11953 /* sort(): ptrs will be the list to sort */
11954 for (li = l->lv_first; li != NULL; li = li->li_next)
11955 {
11956 ptrs[i].item = li;
11957 ptrs[i].idx = i;
11958 ++i;
11959 }
11960
11961 info.item_compare_func_err = FALSE;
11962 info.item_compare_keep_zero = FALSE;
11963 /* test the compare function */
11964 if ((info.item_compare_func != NULL
11965 || info.item_compare_partial != NULL)
11966 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11967 == ITEM_COMPARE_FAIL)
11968 EMSG(_("E702: Sort compare function failed"));
11969 else
11970 {
11971 /* Sort the array with item pointers. */
11972 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11973 info.item_compare_func == NULL
11974 && info.item_compare_partial == NULL
11975 ? item_compare : item_compare2);
11976
11977 if (!info.item_compare_func_err)
11978 {
11979 /* Clear the List and append the items in sorted order. */
11980 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11981 l->lv_len = 0;
11982 for (i = 0; i < len; ++i)
11983 list_append(l, ptrs[i].item);
11984 }
11985 }
11986 }
11987 else
11988 {
11989 int (*item_compare_func_ptr)(const void *, const void *);
11990
11991 /* f_uniq(): ptrs will be a stack of items to remove */
11992 info.item_compare_func_err = FALSE;
11993 info.item_compare_keep_zero = TRUE;
11994 item_compare_func_ptr = info.item_compare_func != NULL
11995 || info.item_compare_partial != NULL
11996 ? item_compare2 : item_compare;
11997
11998 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11999 li = li->li_next)
12000 {
12001 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12002 == 0)
12003 ptrs[i++].item = li;
12004 if (info.item_compare_func_err)
12005 {
12006 EMSG(_("E882: Uniq compare function failed"));
12007 break;
12008 }
12009 }
12010
12011 if (!info.item_compare_func_err)
12012 {
12013 while (--i >= 0)
12014 {
12015 li = ptrs[i].item->li_next;
12016 ptrs[i].item->li_next = li->li_next;
12017 if (li->li_next != NULL)
12018 li->li_next->li_prev = ptrs[i].item;
12019 else
12020 l->lv_last = ptrs[i].item;
12021 list_fix_watch(l, li);
12022 listitem_free(li);
12023 l->lv_len--;
12024 }
12025 }
12026 }
12027
12028 vim_free(ptrs);
12029 }
12030theend:
12031 sortinfo = old_sortinfo;
12032}
12033
12034/*
12035 * "sort({list})" function
12036 */
12037 static void
12038f_sort(typval_T *argvars, typval_T *rettv)
12039{
12040 do_sort_uniq(argvars, rettv, TRUE);
12041}
12042
12043/*
12044 * "uniq({list})" function
12045 */
12046 static void
12047f_uniq(typval_T *argvars, typval_T *rettv)
12048{
12049 do_sort_uniq(argvars, rettv, FALSE);
12050}
12051
12052/*
12053 * "soundfold({word})" function
12054 */
12055 static void
12056f_soundfold(typval_T *argvars, typval_T *rettv)
12057{
12058 char_u *s;
12059
12060 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012061 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012062#ifdef FEAT_SPELL
12063 rettv->vval.v_string = eval_soundfold(s);
12064#else
12065 rettv->vval.v_string = vim_strsave(s);
12066#endif
12067}
12068
12069/*
12070 * "spellbadword()" function
12071 */
12072 static void
12073f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12074{
12075 char_u *word = (char_u *)"";
12076 hlf_T attr = HLF_COUNT;
12077 int len = 0;
12078
12079 if (rettv_list_alloc(rettv) == FAIL)
12080 return;
12081
12082#ifdef FEAT_SPELL
12083 if (argvars[0].v_type == VAR_UNKNOWN)
12084 {
12085 /* Find the start and length of the badly spelled word. */
12086 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12087 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012088 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012089 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012090 curwin->w_set_curswant = TRUE;
12091 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012092 }
12093 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012095 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012096 int capcol = -1;
12097
12098 if (str != NULL)
12099 {
12100 /* Check the argument for spelling. */
12101 while (*str != NUL)
12102 {
12103 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12104 if (attr != HLF_COUNT)
12105 {
12106 word = str;
12107 break;
12108 }
12109 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012110 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012111 }
12112 }
12113 }
12114#endif
12115
12116 list_append_string(rettv->vval.v_list, word, len);
12117 list_append_string(rettv->vval.v_list, (char_u *)(
12118 attr == HLF_SPB ? "bad" :
12119 attr == HLF_SPR ? "rare" :
12120 attr == HLF_SPL ? "local" :
12121 attr == HLF_SPC ? "caps" :
12122 ""), -1);
12123}
12124
12125/*
12126 * "spellsuggest()" function
12127 */
12128 static void
12129f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12130{
12131#ifdef FEAT_SPELL
12132 char_u *str;
12133 int typeerr = FALSE;
12134 int maxcount;
12135 garray_T ga;
12136 int i;
12137 listitem_T *li;
12138 int need_capital = FALSE;
12139#endif
12140
12141 if (rettv_list_alloc(rettv) == FAIL)
12142 return;
12143
12144#ifdef FEAT_SPELL
12145 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012147 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012148 if (argvars[1].v_type != VAR_UNKNOWN)
12149 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012150 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012151 if (maxcount <= 0)
12152 return;
12153 if (argvars[2].v_type != VAR_UNKNOWN)
12154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012155 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012156 if (typeerr)
12157 return;
12158 }
12159 }
12160 else
12161 maxcount = 25;
12162
12163 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12164
12165 for (i = 0; i < ga.ga_len; ++i)
12166 {
12167 str = ((char_u **)ga.ga_data)[i];
12168
12169 li = listitem_alloc();
12170 if (li == NULL)
12171 vim_free(str);
12172 else
12173 {
12174 li->li_tv.v_type = VAR_STRING;
12175 li->li_tv.v_lock = 0;
12176 li->li_tv.vval.v_string = str;
12177 list_append(rettv->vval.v_list, li);
12178 }
12179 }
12180 ga_clear(&ga);
12181 }
12182#endif
12183}
12184
12185 static void
12186f_split(typval_T *argvars, typval_T *rettv)
12187{
12188 char_u *str;
12189 char_u *end;
12190 char_u *pat = NULL;
12191 regmatch_T regmatch;
12192 char_u patbuf[NUMBUFLEN];
12193 char_u *save_cpo;
12194 int match;
12195 colnr_T col = 0;
12196 int keepempty = FALSE;
12197 int typeerr = FALSE;
12198
12199 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12200 save_cpo = p_cpo;
12201 p_cpo = (char_u *)"";
12202
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012203 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012204 if (argvars[1].v_type != VAR_UNKNOWN)
12205 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012206 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012207 if (pat == NULL)
12208 typeerr = TRUE;
12209 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012210 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012211 }
12212 if (pat == NULL || *pat == NUL)
12213 pat = (char_u *)"[\\x01- ]\\+";
12214
12215 if (rettv_list_alloc(rettv) == FAIL)
12216 return;
12217 if (typeerr)
12218 return;
12219
12220 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12221 if (regmatch.regprog != NULL)
12222 {
12223 regmatch.rm_ic = FALSE;
12224 while (*str != NUL || keepempty)
12225 {
12226 if (*str == NUL)
12227 match = FALSE; /* empty item at the end */
12228 else
12229 match = vim_regexec_nl(&regmatch, str, col);
12230 if (match)
12231 end = regmatch.startp[0];
12232 else
12233 end = str + STRLEN(str);
12234 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12235 && *str != NUL && match && end < regmatch.endp[0]))
12236 {
12237 if (list_append_string(rettv->vval.v_list, str,
12238 (int)(end - str)) == FAIL)
12239 break;
12240 }
12241 if (!match)
12242 break;
12243 /* Advance to just after the match. */
12244 if (regmatch.endp[0] > str)
12245 col = 0;
12246 else
12247 {
12248 /* Don't get stuck at the same match. */
12249#ifdef FEAT_MBYTE
12250 col = (*mb_ptr2len)(regmatch.endp[0]);
12251#else
12252 col = 1;
12253#endif
12254 }
12255 str = regmatch.endp[0];
12256 }
12257
12258 vim_regfree(regmatch.regprog);
12259 }
12260
12261 p_cpo = save_cpo;
12262}
12263
12264#ifdef FEAT_FLOAT
12265/*
12266 * "sqrt()" function
12267 */
12268 static void
12269f_sqrt(typval_T *argvars, typval_T *rettv)
12270{
12271 float_T f = 0.0;
12272
12273 rettv->v_type = VAR_FLOAT;
12274 if (get_float_arg(argvars, &f) == OK)
12275 rettv->vval.v_float = sqrt(f);
12276 else
12277 rettv->vval.v_float = 0.0;
12278}
12279
12280/*
12281 * "str2float()" function
12282 */
12283 static void
12284f_str2float(typval_T *argvars, typval_T *rettv)
12285{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012286 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012287 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288
Bram Moolenaar08243d22017-01-10 16:12:29 +010012289 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 p = skipwhite(p + 1);
12291 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012292 if (isneg)
12293 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294 rettv->v_type = VAR_FLOAT;
12295}
12296#endif
12297
12298/*
12299 * "str2nr()" function
12300 */
12301 static void
12302f_str2nr(typval_T *argvars, typval_T *rettv)
12303{
12304 int base = 10;
12305 char_u *p;
12306 varnumber_T n;
12307 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012308 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309
12310 if (argvars[1].v_type != VAR_UNKNOWN)
12311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012312 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313 if (base != 2 && base != 8 && base != 10 && base != 16)
12314 {
12315 EMSG(_(e_invarg));
12316 return;
12317 }
12318 }
12319
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012320 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012321 isneg = (*p == '-');
12322 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012323 p = skipwhite(p + 1);
12324 switch (base)
12325 {
12326 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12327 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12328 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12329 default: what = 0;
12330 }
12331 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012332 if (isneg)
12333 rettv->vval.v_number = -n;
12334 else
12335 rettv->vval.v_number = n;
12336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337}
12338
12339#ifdef HAVE_STRFTIME
12340/*
12341 * "strftime({format}[, {time}])" function
12342 */
12343 static void
12344f_strftime(typval_T *argvars, typval_T *rettv)
12345{
12346 char_u result_buf[256];
12347 struct tm *curtime;
12348 time_t seconds;
12349 char_u *p;
12350
12351 rettv->v_type = VAR_STRING;
12352
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012353 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354 if (argvars[1].v_type == VAR_UNKNOWN)
12355 seconds = time(NULL);
12356 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012357 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012358 curtime = localtime(&seconds);
12359 /* MSVC returns NULL for an invalid value of seconds. */
12360 if (curtime == NULL)
12361 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12362 else
12363 {
12364# ifdef FEAT_MBYTE
12365 vimconv_T conv;
12366 char_u *enc;
12367
12368 conv.vc_type = CONV_NONE;
12369 enc = enc_locale();
12370 convert_setup(&conv, p_enc, enc);
12371 if (conv.vc_type != CONV_NONE)
12372 p = string_convert(&conv, p, NULL);
12373# endif
12374 if (p != NULL)
12375 (void)strftime((char *)result_buf, sizeof(result_buf),
12376 (char *)p, curtime);
12377 else
12378 result_buf[0] = NUL;
12379
12380# ifdef FEAT_MBYTE
12381 if (conv.vc_type != CONV_NONE)
12382 vim_free(p);
12383 convert_setup(&conv, enc, p_enc);
12384 if (conv.vc_type != CONV_NONE)
12385 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12386 else
12387# endif
12388 rettv->vval.v_string = vim_strsave(result_buf);
12389
12390# ifdef FEAT_MBYTE
12391 /* Release conversion descriptors */
12392 convert_setup(&conv, NULL, NULL);
12393 vim_free(enc);
12394# endif
12395 }
12396}
12397#endif
12398
12399/*
12400 * "strgetchar()" function
12401 */
12402 static void
12403f_strgetchar(typval_T *argvars, typval_T *rettv)
12404{
12405 char_u *str;
12406 int len;
12407 int error = FALSE;
12408 int charidx;
12409
12410 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012411 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012412 if (str == NULL)
12413 return;
12414 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012415 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012416 if (error)
12417 return;
12418#ifdef FEAT_MBYTE
12419 {
12420 int byteidx = 0;
12421
12422 while (charidx >= 0 && byteidx < len)
12423 {
12424 if (charidx == 0)
12425 {
12426 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12427 break;
12428 }
12429 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012430 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431 }
12432 }
12433#else
12434 if (charidx < len)
12435 rettv->vval.v_number = str[charidx];
12436#endif
12437}
12438
12439/*
12440 * "stridx()" function
12441 */
12442 static void
12443f_stridx(typval_T *argvars, typval_T *rettv)
12444{
12445 char_u buf[NUMBUFLEN];
12446 char_u *needle;
12447 char_u *haystack;
12448 char_u *save_haystack;
12449 char_u *pos;
12450 int start_idx;
12451
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012452 needle = tv_get_string_chk(&argvars[1]);
12453 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 rettv->vval.v_number = -1;
12455 if (needle == NULL || haystack == NULL)
12456 return; /* type error; errmsg already given */
12457
12458 if (argvars[2].v_type != VAR_UNKNOWN)
12459 {
12460 int error = FALSE;
12461
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012462 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463 if (error || start_idx >= (int)STRLEN(haystack))
12464 return;
12465 if (start_idx >= 0)
12466 haystack += start_idx;
12467 }
12468
12469 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12470 if (pos != NULL)
12471 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12472}
12473
12474/*
12475 * "string()" function
12476 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012477 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478f_string(typval_T *argvars, typval_T *rettv)
12479{
12480 char_u *tofree;
12481 char_u numbuf[NUMBUFLEN];
12482
12483 rettv->v_type = VAR_STRING;
12484 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12485 get_copyID());
12486 /* Make a copy if we have a value but it's not in allocated memory. */
12487 if (rettv->vval.v_string != NULL && tofree == NULL)
12488 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12489}
12490
12491/*
12492 * "strlen()" function
12493 */
12494 static void
12495f_strlen(typval_T *argvars, typval_T *rettv)
12496{
12497 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012498 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012499}
12500
12501/*
12502 * "strchars()" function
12503 */
12504 static void
12505f_strchars(typval_T *argvars, typval_T *rettv)
12506{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012507 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508 int skipcc = 0;
12509#ifdef FEAT_MBYTE
12510 varnumber_T len = 0;
12511 int (*func_mb_ptr2char_adv)(char_u **pp);
12512#endif
12513
12514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012515 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516 if (skipcc < 0 || skipcc > 1)
12517 EMSG(_(e_invarg));
12518 else
12519 {
12520#ifdef FEAT_MBYTE
12521 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12522 while (*s != NUL)
12523 {
12524 func_mb_ptr2char_adv(&s);
12525 ++len;
12526 }
12527 rettv->vval.v_number = len;
12528#else
12529 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12530#endif
12531 }
12532}
12533
12534/*
12535 * "strdisplaywidth()" function
12536 */
12537 static void
12538f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12539{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012540 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012541 int col = 0;
12542
12543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012544 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545
12546 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12547}
12548
12549/*
12550 * "strwidth()" function
12551 */
12552 static void
12553f_strwidth(typval_T *argvars, typval_T *rettv)
12554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012555 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556
12557 rettv->vval.v_number = (varnumber_T)(
12558#ifdef FEAT_MBYTE
12559 mb_string2cells(s, -1)
12560#else
12561 STRLEN(s)
12562#endif
12563 );
12564}
12565
12566/*
12567 * "strcharpart()" function
12568 */
12569 static void
12570f_strcharpart(typval_T *argvars, typval_T *rettv)
12571{
12572#ifdef FEAT_MBYTE
12573 char_u *p;
12574 int nchar;
12575 int nbyte = 0;
12576 int charlen;
12577 int len = 0;
12578 int slen;
12579 int error = FALSE;
12580
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012581 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582 slen = (int)STRLEN(p);
12583
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012584 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 if (!error)
12586 {
12587 if (nchar > 0)
12588 while (nchar > 0 && nbyte < slen)
12589 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012590 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591 --nchar;
12592 }
12593 else
12594 nbyte = nchar;
12595 if (argvars[2].v_type != VAR_UNKNOWN)
12596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012597 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598 while (charlen > 0 && nbyte + len < slen)
12599 {
12600 int off = nbyte + len;
12601
12602 if (off < 0)
12603 len += 1;
12604 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012605 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012606 --charlen;
12607 }
12608 }
12609 else
12610 len = slen - nbyte; /* default: all bytes that are available. */
12611 }
12612
12613 /*
12614 * Only return the overlap between the specified part and the actual
12615 * string.
12616 */
12617 if (nbyte < 0)
12618 {
12619 len += nbyte;
12620 nbyte = 0;
12621 }
12622 else if (nbyte > slen)
12623 nbyte = slen;
12624 if (len < 0)
12625 len = 0;
12626 else if (nbyte + len > slen)
12627 len = slen - nbyte;
12628
12629 rettv->v_type = VAR_STRING;
12630 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12631#else
12632 f_strpart(argvars, rettv);
12633#endif
12634}
12635
12636/*
12637 * "strpart()" function
12638 */
12639 static void
12640f_strpart(typval_T *argvars, typval_T *rettv)
12641{
12642 char_u *p;
12643 int n;
12644 int len;
12645 int slen;
12646 int error = FALSE;
12647
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012648 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 slen = (int)STRLEN(p);
12650
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012651 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012652 if (error)
12653 len = 0;
12654 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012655 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012656 else
12657 len = slen - n; /* default len: all bytes that are available. */
12658
12659 /*
12660 * Only return the overlap between the specified part and the actual
12661 * string.
12662 */
12663 if (n < 0)
12664 {
12665 len += n;
12666 n = 0;
12667 }
12668 else if (n > slen)
12669 n = slen;
12670 if (len < 0)
12671 len = 0;
12672 else if (n + len > slen)
12673 len = slen - n;
12674
12675 rettv->v_type = VAR_STRING;
12676 rettv->vval.v_string = vim_strnsave(p + n, len);
12677}
12678
12679/*
12680 * "strridx()" function
12681 */
12682 static void
12683f_strridx(typval_T *argvars, typval_T *rettv)
12684{
12685 char_u buf[NUMBUFLEN];
12686 char_u *needle;
12687 char_u *haystack;
12688 char_u *rest;
12689 char_u *lastmatch = NULL;
12690 int haystack_len, end_idx;
12691
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012692 needle = tv_get_string_chk(&argvars[1]);
12693 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694
12695 rettv->vval.v_number = -1;
12696 if (needle == NULL || haystack == NULL)
12697 return; /* type error; errmsg already given */
12698
12699 haystack_len = (int)STRLEN(haystack);
12700 if (argvars[2].v_type != VAR_UNKNOWN)
12701 {
12702 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012703 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704 if (end_idx < 0)
12705 return; /* can never find a match */
12706 }
12707 else
12708 end_idx = haystack_len;
12709
12710 if (*needle == NUL)
12711 {
12712 /* Empty string matches past the end. */
12713 lastmatch = haystack + end_idx;
12714 }
12715 else
12716 {
12717 for (rest = haystack; *rest != '\0'; ++rest)
12718 {
12719 rest = (char_u *)strstr((char *)rest, (char *)needle);
12720 if (rest == NULL || rest > haystack + end_idx)
12721 break;
12722 lastmatch = rest;
12723 }
12724 }
12725
12726 if (lastmatch == NULL)
12727 rettv->vval.v_number = -1;
12728 else
12729 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12730}
12731
12732/*
12733 * "strtrans()" function
12734 */
12735 static void
12736f_strtrans(typval_T *argvars, typval_T *rettv)
12737{
12738 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012739 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740}
12741
12742/*
12743 * "submatch()" function
12744 */
12745 static void
12746f_submatch(typval_T *argvars, typval_T *rettv)
12747{
12748 int error = FALSE;
12749 int no;
12750 int retList = 0;
12751
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012752 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 if (error)
12754 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012755 if (no < 0 || no >= NSUBEXP)
12756 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012757 EMSGN(_("E935: invalid submatch number: %d"), no);
12758 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012760 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012761 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 if (error)
12763 return;
12764
12765 if (retList == 0)
12766 {
12767 rettv->v_type = VAR_STRING;
12768 rettv->vval.v_string = reg_submatch(no);
12769 }
12770 else
12771 {
12772 rettv->v_type = VAR_LIST;
12773 rettv->vval.v_list = reg_submatch_list(no);
12774 }
12775}
12776
12777/*
12778 * "substitute()" function
12779 */
12780 static void
12781f_substitute(typval_T *argvars, typval_T *rettv)
12782{
12783 char_u patbuf[NUMBUFLEN];
12784 char_u subbuf[NUMBUFLEN];
12785 char_u flagsbuf[NUMBUFLEN];
12786
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012787 char_u *str = tv_get_string_chk(&argvars[0]);
12788 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012789 char_u *sub = NULL;
12790 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012793 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12794 expr = &argvars[2];
12795 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012796 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012798 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012799 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12800 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801 rettv->vval.v_string = NULL;
12802 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012803 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804}
12805
12806/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012807 * "swapinfo(swap_filename)" function
12808 */
12809 static void
12810f_swapinfo(typval_T *argvars, typval_T *rettv)
12811{
12812 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012813 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012814}
12815
12816/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012817 * "swapname(expr)" function
12818 */
12819 static void
12820f_swapname(typval_T *argvars, typval_T *rettv)
12821{
12822 buf_T *buf;
12823
12824 rettv->v_type = VAR_STRING;
12825 buf = get_buf_tv(&argvars[0], FALSE);
12826 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12827 || buf->b_ml.ml_mfp->mf_fname == NULL)
12828 rettv->vval.v_string = NULL;
12829 else
12830 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12831}
12832
12833/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834 * "synID(lnum, col, trans)" function
12835 */
12836 static void
12837f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12838{
12839 int id = 0;
12840#ifdef FEAT_SYN_HL
12841 linenr_T lnum;
12842 colnr_T col;
12843 int trans;
12844 int transerr = FALSE;
12845
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012846 lnum = tv_get_lnum(argvars); /* -1 on type error */
12847 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12848 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849
12850 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12851 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12852 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12853#endif
12854
12855 rettv->vval.v_number = id;
12856}
12857
12858/*
12859 * "synIDattr(id, what [, mode])" function
12860 */
12861 static void
12862f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12863{
12864 char_u *p = NULL;
12865#ifdef FEAT_SYN_HL
12866 int id;
12867 char_u *what;
12868 char_u *mode;
12869 char_u modebuf[NUMBUFLEN];
12870 int modec;
12871
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012872 id = (int)tv_get_number(&argvars[0]);
12873 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874 if (argvars[2].v_type != VAR_UNKNOWN)
12875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 modec = TOLOWER_ASC(mode[0]);
12878 if (modec != 't' && modec != 'c' && modec != 'g')
12879 modec = 0; /* replace invalid with current */
12880 }
12881 else
12882 {
12883#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12884 if (USE_24BIT)
12885 modec = 'g';
12886 else
12887#endif
12888 if (t_colors > 1)
12889 modec = 'c';
12890 else
12891 modec = 't';
12892 }
12893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 switch (TOLOWER_ASC(what[0]))
12895 {
12896 case 'b':
12897 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12898 p = highlight_color(id, what, modec);
12899 else /* bold */
12900 p = highlight_has_attr(id, HL_BOLD, modec);
12901 break;
12902
12903 case 'f': /* fg[#] or font */
12904 p = highlight_color(id, what, modec);
12905 break;
12906
12907 case 'i':
12908 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12909 p = highlight_has_attr(id, HL_INVERSE, modec);
12910 else /* italic */
12911 p = highlight_has_attr(id, HL_ITALIC, modec);
12912 break;
12913
12914 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012915 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 break;
12917
12918 case 'r': /* reverse */
12919 p = highlight_has_attr(id, HL_INVERSE, modec);
12920 break;
12921
12922 case 's':
12923 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12924 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012925 /* strikeout */
12926 else if (TOLOWER_ASC(what[1]) == 't' &&
12927 TOLOWER_ASC(what[2]) == 'r')
12928 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012929 else /* standout */
12930 p = highlight_has_attr(id, HL_STANDOUT, modec);
12931 break;
12932
12933 case 'u':
12934 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12935 /* underline */
12936 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12937 else
12938 /* undercurl */
12939 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12940 break;
12941 }
12942
12943 if (p != NULL)
12944 p = vim_strsave(p);
12945#endif
12946 rettv->v_type = VAR_STRING;
12947 rettv->vval.v_string = p;
12948}
12949
12950/*
12951 * "synIDtrans(id)" function
12952 */
12953 static void
12954f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12955{
12956 int id;
12957
12958#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012959 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960
12961 if (id > 0)
12962 id = syn_get_final_id(id);
12963 else
12964#endif
12965 id = 0;
12966
12967 rettv->vval.v_number = id;
12968}
12969
12970/*
12971 * "synconcealed(lnum, col)" function
12972 */
12973 static void
12974f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12975{
12976#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12977 linenr_T lnum;
12978 colnr_T col;
12979 int syntax_flags = 0;
12980 int cchar;
12981 int matchid = 0;
12982 char_u str[NUMBUFLEN];
12983#endif
12984
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012985 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012986
12987#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012988 lnum = tv_get_lnum(argvars); /* -1 on type error */
12989 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990
12991 vim_memset(str, NUL, sizeof(str));
12992
12993 if (rettv_list_alloc(rettv) != FAIL)
12994 {
12995 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12996 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12997 && curwin->w_p_cole > 0)
12998 {
12999 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13000 syntax_flags = get_syntax_info(&matchid);
13001
13002 /* get the conceal character */
13003 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13004 {
13005 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013006 if (cchar == NUL && curwin->w_p_cole == 1)
13007 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 if (cchar != NUL)
13009 {
13010# ifdef FEAT_MBYTE
13011 if (has_mbyte)
13012 (*mb_char2bytes)(cchar, str);
13013 else
13014# endif
13015 str[0] = cchar;
13016 }
13017 }
13018 }
13019
13020 list_append_number(rettv->vval.v_list,
13021 (syntax_flags & HL_CONCEAL) != 0);
13022 /* -1 to auto-determine strlen */
13023 list_append_string(rettv->vval.v_list, str, -1);
13024 list_append_number(rettv->vval.v_list, matchid);
13025 }
13026#endif
13027}
13028
13029/*
13030 * "synstack(lnum, col)" function
13031 */
13032 static void
13033f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13034{
13035#ifdef FEAT_SYN_HL
13036 linenr_T lnum;
13037 colnr_T col;
13038 int i;
13039 int id;
13040#endif
13041
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013042 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043
13044#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013045 lnum = tv_get_lnum(argvars); /* -1 on type error */
13046 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047
13048 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13049 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13050 && rettv_list_alloc(rettv) != FAIL)
13051 {
13052 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13053 for (i = 0; ; ++i)
13054 {
13055 id = syn_get_stack_item(i);
13056 if (id < 0)
13057 break;
13058 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13059 break;
13060 }
13061 }
13062#endif
13063}
13064
13065 static void
13066get_cmd_output_as_rettv(
13067 typval_T *argvars,
13068 typval_T *rettv,
13069 int retlist)
13070{
13071 char_u *res = NULL;
13072 char_u *p;
13073 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074 int err = FALSE;
13075 FILE *fd;
13076 list_T *list = NULL;
13077 int flags = SHELL_SILENT;
13078
13079 rettv->v_type = VAR_STRING;
13080 rettv->vval.v_string = NULL;
13081 if (check_restricted() || check_secure())
13082 goto errret;
13083
13084 if (argvars[1].v_type != VAR_UNKNOWN)
13085 {
13086 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013087 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 * command.
13089 */
13090 if ((infile = vim_tempname('i', TRUE)) == NULL)
13091 {
13092 EMSG(_(e_notmp));
13093 goto errret;
13094 }
13095
13096 fd = mch_fopen((char *)infile, WRITEBIN);
13097 if (fd == NULL)
13098 {
13099 EMSG2(_(e_notopen), infile);
13100 goto errret;
13101 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013102 if (argvars[1].v_type == VAR_NUMBER)
13103 {
13104 linenr_T lnum;
13105 buf_T *buf;
13106
13107 buf = buflist_findnr(argvars[1].vval.v_number);
13108 if (buf == NULL)
13109 {
13110 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013111 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013112 goto errret;
13113 }
13114
13115 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13116 {
13117 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13118 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13119 {
13120 err = TRUE;
13121 break;
13122 }
13123 if (putc(NL, fd) == EOF)
13124 {
13125 err = TRUE;
13126 break;
13127 }
13128 }
13129 }
13130 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 {
13132 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13133 err = TRUE;
13134 }
13135 else
13136 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013137 size_t len;
13138 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013140 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 if (p == NULL)
13142 {
13143 fclose(fd);
13144 goto errret; /* type error; errmsg already given */
13145 }
13146 len = STRLEN(p);
13147 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13148 err = TRUE;
13149 }
13150 if (fclose(fd) != 0)
13151 err = TRUE;
13152 if (err)
13153 {
13154 EMSG(_("E677: Error writing temp file"));
13155 goto errret;
13156 }
13157 }
13158
13159 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13160 * echoes typeahead, that messes up the display. */
13161 if (!msg_silent)
13162 flags += SHELL_COOKED;
13163
13164 if (retlist)
13165 {
13166 int len;
13167 listitem_T *li;
13168 char_u *s = NULL;
13169 char_u *start;
13170 char_u *end;
13171 int i;
13172
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013173 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 if (res == NULL)
13175 goto errret;
13176
13177 list = list_alloc();
13178 if (list == NULL)
13179 goto errret;
13180
13181 for (i = 0; i < len; ++i)
13182 {
13183 start = res + i;
13184 while (i < len && res[i] != NL)
13185 ++i;
13186 end = res + i;
13187
13188 s = alloc((unsigned)(end - start + 1));
13189 if (s == NULL)
13190 goto errret;
13191
13192 for (p = s; start < end; ++p, ++start)
13193 *p = *start == NUL ? NL : *start;
13194 *p = NUL;
13195
13196 li = listitem_alloc();
13197 if (li == NULL)
13198 {
13199 vim_free(s);
13200 goto errret;
13201 }
13202 li->li_tv.v_type = VAR_STRING;
13203 li->li_tv.v_lock = 0;
13204 li->li_tv.vval.v_string = s;
13205 list_append(list, li);
13206 }
13207
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013208 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 list = NULL;
13210 }
13211 else
13212 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013213 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214#ifdef USE_CR
13215 /* translate <CR> into <NL> */
13216 if (res != NULL)
13217 {
13218 char_u *s;
13219
13220 for (s = res; *s; ++s)
13221 {
13222 if (*s == CAR)
13223 *s = NL;
13224 }
13225 }
13226#else
13227# ifdef USE_CRNL
13228 /* translate <CR><NL> into <NL> */
13229 if (res != NULL)
13230 {
13231 char_u *s, *d;
13232
13233 d = res;
13234 for (s = res; *s; ++s)
13235 {
13236 if (s[0] == CAR && s[1] == NL)
13237 ++s;
13238 *d++ = *s;
13239 }
13240 *d = NUL;
13241 }
13242# endif
13243#endif
13244 rettv->vval.v_string = res;
13245 res = NULL;
13246 }
13247
13248errret:
13249 if (infile != NULL)
13250 {
13251 mch_remove(infile);
13252 vim_free(infile);
13253 }
13254 if (res != NULL)
13255 vim_free(res);
13256 if (list != NULL)
13257 list_free(list);
13258}
13259
13260/*
13261 * "system()" function
13262 */
13263 static void
13264f_system(typval_T *argvars, typval_T *rettv)
13265{
13266 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13267}
13268
13269/*
13270 * "systemlist()" function
13271 */
13272 static void
13273f_systemlist(typval_T *argvars, typval_T *rettv)
13274{
13275 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13276}
13277
13278/*
13279 * "tabpagebuflist()" function
13280 */
13281 static void
13282f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13283{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 tabpage_T *tp;
13285 win_T *wp = NULL;
13286
13287 if (argvars[0].v_type == VAR_UNKNOWN)
13288 wp = firstwin;
13289 else
13290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013291 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292 if (tp != NULL)
13293 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13294 }
13295 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13296 {
13297 for (; wp != NULL; wp = wp->w_next)
13298 if (list_append_number(rettv->vval.v_list,
13299 wp->w_buffer->b_fnum) == FAIL)
13300 break;
13301 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302}
13303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304/*
13305 * "tabpagenr()" function
13306 */
13307 static void
13308f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13309{
13310 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311 char_u *arg;
13312
13313 if (argvars[0].v_type != VAR_UNKNOWN)
13314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013315 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316 nr = 0;
13317 if (arg != NULL)
13318 {
13319 if (STRCMP(arg, "$") == 0)
13320 nr = tabpage_index(NULL) - 1;
13321 else
13322 EMSG2(_(e_invexpr2), arg);
13323 }
13324 }
13325 else
13326 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 rettv->vval.v_number = nr;
13328}
13329
13330
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331/*
13332 * Common code for tabpagewinnr() and winnr().
13333 */
13334 static int
13335get_winnr(tabpage_T *tp, typval_T *argvar)
13336{
13337 win_T *twin;
13338 int nr = 1;
13339 win_T *wp;
13340 char_u *arg;
13341
13342 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13343 if (argvar->v_type != VAR_UNKNOWN)
13344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013345 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346 if (arg == NULL)
13347 nr = 0; /* type error; errmsg already given */
13348 else if (STRCMP(arg, "$") == 0)
13349 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13350 else if (STRCMP(arg, "#") == 0)
13351 {
13352 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13353 if (twin == NULL)
13354 nr = 0;
13355 }
13356 else
13357 {
13358 EMSG2(_(e_invexpr2), arg);
13359 nr = 0;
13360 }
13361 }
13362
13363 if (nr > 0)
13364 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13365 wp != twin; wp = wp->w_next)
13366 {
13367 if (wp == NULL)
13368 {
13369 /* didn't find it in this tabpage */
13370 nr = 0;
13371 break;
13372 }
13373 ++nr;
13374 }
13375 return nr;
13376}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377
13378/*
13379 * "tabpagewinnr()" function
13380 */
13381 static void
13382f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13383{
13384 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013385 tabpage_T *tp;
13386
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013387 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 if (tp == NULL)
13389 nr = 0;
13390 else
13391 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013392 rettv->vval.v_number = nr;
13393}
13394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013395/*
13396 * "tagfiles()" function
13397 */
13398 static void
13399f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13400{
13401 char_u *fname;
13402 tagname_T tn;
13403 int first;
13404
13405 if (rettv_list_alloc(rettv) == FAIL)
13406 return;
13407 fname = alloc(MAXPATHL);
13408 if (fname == NULL)
13409 return;
13410
13411 for (first = TRUE; ; first = FALSE)
13412 if (get_tagfname(&tn, first, fname) == FAIL
13413 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13414 break;
13415 tagname_free(&tn);
13416 vim_free(fname);
13417}
13418
13419/*
13420 * "taglist()" function
13421 */
13422 static void
13423f_taglist(typval_T *argvars, typval_T *rettv)
13424{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013425 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 char_u *tag_pattern;
13427
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013428 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429
13430 rettv->vval.v_number = FALSE;
13431 if (*tag_pattern == NUL)
13432 return;
13433
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013434 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013435 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013437 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013438}
13439
13440/*
13441 * "tempname()" function
13442 */
13443 static void
13444f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13445{
13446 static int x = 'A';
13447
13448 rettv->v_type = VAR_STRING;
13449 rettv->vval.v_string = vim_tempname(x, FALSE);
13450
13451 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13452 * names. Skip 'I' and 'O', they are used for shell redirection. */
13453 do
13454 {
13455 if (x == 'Z')
13456 x = '0';
13457 else if (x == '9')
13458 x = 'A';
13459 else
13460 {
13461#ifdef EBCDIC
13462 if (x == 'I')
13463 x = 'J';
13464 else if (x == 'R')
13465 x = 'S';
13466 else
13467#endif
13468 ++x;
13469 }
13470 } while (x == 'I' || x == 'O');
13471}
13472
13473#ifdef FEAT_FLOAT
13474/*
13475 * "tan()" function
13476 */
13477 static void
13478f_tan(typval_T *argvars, typval_T *rettv)
13479{
13480 float_T f = 0.0;
13481
13482 rettv->v_type = VAR_FLOAT;
13483 if (get_float_arg(argvars, &f) == OK)
13484 rettv->vval.v_float = tan(f);
13485 else
13486 rettv->vval.v_float = 0.0;
13487}
13488
13489/*
13490 * "tanh()" function
13491 */
13492 static void
13493f_tanh(typval_T *argvars, typval_T *rettv)
13494{
13495 float_T f = 0.0;
13496
13497 rettv->v_type = VAR_FLOAT;
13498 if (get_float_arg(argvars, &f) == OK)
13499 rettv->vval.v_float = tanh(f);
13500 else
13501 rettv->vval.v_float = 0.0;
13502}
13503#endif
13504
13505/*
13506 * "test_alloc_fail(id, countdown, repeat)" function
13507 */
13508 static void
13509f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13510{
13511 if (argvars[0].v_type != VAR_NUMBER
13512 || argvars[0].vval.v_number <= 0
13513 || argvars[1].v_type != VAR_NUMBER
13514 || argvars[1].vval.v_number < 0
13515 || argvars[2].v_type != VAR_NUMBER)
13516 EMSG(_(e_invarg));
13517 else
13518 {
13519 alloc_fail_id = argvars[0].vval.v_number;
13520 if (alloc_fail_id >= aid_last)
13521 EMSG(_(e_invarg));
13522 alloc_fail_countdown = argvars[1].vval.v_number;
13523 alloc_fail_repeat = argvars[2].vval.v_number;
13524 did_outofmem_msg = FALSE;
13525 }
13526}
13527
13528/*
13529 * "test_autochdir()"
13530 */
13531 static void
13532f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13533{
13534#if defined(FEAT_AUTOCHDIR)
13535 test_autochdir = TRUE;
13536#endif
13537}
13538
13539/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013540 * "test_feedinput()"
13541 */
13542 static void
13543f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13544{
13545#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013546 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013547
13548 if (val != NULL)
13549 {
13550 trash_input_buf();
13551 add_to_input_buf_csi(val, (int)STRLEN(val));
13552 }
13553#endif
13554}
13555
13556/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013557 * "test_option_not_set({name})" function
13558 */
13559 static void
13560f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13561{
13562 char_u *name = (char_u *)"";
13563
13564 if (argvars[0].v_type != VAR_STRING)
13565 EMSG(_(e_invarg));
13566 else
13567 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013568 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013569 if (reset_option_was_set(name) == FAIL)
13570 EMSG2(_(e_invarg2), name);
13571 }
13572}
13573
13574/*
13575 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013576 */
13577 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013578f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013580 char_u *name = (char_u *)"";
13581 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013582 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013583
13584 if (argvars[0].v_type != VAR_STRING
13585 || (argvars[1].v_type) != VAR_NUMBER)
13586 EMSG(_(e_invarg));
13587 else
13588 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013589 name = tv_get_string_chk(&argvars[0]);
13590 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013591
13592 if (STRCMP(name, (char_u *)"redraw") == 0)
13593 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013594 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13595 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013596 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13597 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013598 else if (STRCMP(name, (char_u *)"starting") == 0)
13599 {
13600 if (val)
13601 {
13602 if (save_starting < 0)
13603 save_starting = starting;
13604 starting = 0;
13605 }
13606 else
13607 {
13608 starting = save_starting;
13609 save_starting = -1;
13610 }
13611 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013612 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13613 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013614 else if (STRCMP(name, (char_u *)"ALL") == 0)
13615 {
13616 disable_char_avail_for_testing = FALSE;
13617 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013618 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013619 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013620 if (save_starting >= 0)
13621 {
13622 starting = save_starting;
13623 save_starting = -1;
13624 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013625 }
13626 else
13627 EMSG2(_(e_invarg2), name);
13628 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013629}
13630
13631/*
13632 * "test_garbagecollect_now()" function
13633 */
13634 static void
13635f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13636{
13637 /* This is dangerous, any Lists and Dicts used internally may be freed
13638 * while still in use. */
13639 garbage_collect(TRUE);
13640}
13641
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013642/*
13643 * "test_ignore_error()" function
13644 */
13645 static void
13646f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13647{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013648 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013649}
13650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013651#ifdef FEAT_JOB_CHANNEL
13652 static void
13653f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13654{
13655 rettv->v_type = VAR_CHANNEL;
13656 rettv->vval.v_channel = NULL;
13657}
13658#endif
13659
13660 static void
13661f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13662{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013663 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013664}
13665
13666#ifdef FEAT_JOB_CHANNEL
13667 static void
13668f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13669{
13670 rettv->v_type = VAR_JOB;
13671 rettv->vval.v_job = NULL;
13672}
13673#endif
13674
13675 static void
13676f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13677{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013678 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679}
13680
13681 static void
13682f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13683{
13684 rettv->v_type = VAR_PARTIAL;
13685 rettv->vval.v_partial = NULL;
13686}
13687
13688 static void
13689f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13690{
13691 rettv->v_type = VAR_STRING;
13692 rettv->vval.v_string = NULL;
13693}
13694
Bram Moolenaarab186732018-09-14 21:27:06 +020013695#ifdef FEAT_GUI
13696 static void
13697f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13698{
13699 char_u *which;
13700 long value;
13701 int dragging;
13702 scrollbar_T *sb = NULL;
13703
13704 if (argvars[0].v_type != VAR_STRING
13705 || (argvars[1].v_type) != VAR_NUMBER
13706 || (argvars[2].v_type) != VAR_NUMBER)
13707 {
13708 EMSG(_(e_invarg));
13709 return;
13710 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013711 which = tv_get_string(&argvars[0]);
13712 value = tv_get_number(&argvars[1]);
13713 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013714
13715 if (STRCMP(which, "left") == 0)
13716 sb = &curwin->w_scrollbars[SBAR_LEFT];
13717 else if (STRCMP(which, "right") == 0)
13718 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13719 else if (STRCMP(which, "hor") == 0)
13720 sb = &gui.bottom_sbar;
13721 if (sb == NULL)
13722 {
13723 EMSG2(_(e_invarg2), which);
13724 return;
13725 }
13726 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013727# ifndef USE_ON_FLY_SCROLL
13728 // need to loop through normal_cmd() to handle the scroll events
13729 exec_normal(FALSE, TRUE, FALSE);
13730# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013731}
13732#endif
13733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734 static void
13735f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13736{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013737 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013738}
13739
13740#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13741/*
13742 * Get a callback from "arg". It can be a Funcref or a function name.
13743 * When "arg" is zero return an empty string.
13744 * Return NULL for an invalid argument.
13745 */
13746 char_u *
13747get_callback(typval_T *arg, partial_T **pp)
13748{
13749 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13750 {
13751 *pp = arg->vval.v_partial;
13752 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013753 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013754 }
13755 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013756 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013757 {
13758 func_ref(arg->vval.v_string);
13759 return arg->vval.v_string;
13760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013761 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13762 return (char_u *)"";
13763 EMSG(_("E921: Invalid callback argument"));
13764 return NULL;
13765}
13766
13767/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013768 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013769 */
13770 void
13771free_callback(char_u *callback, partial_T *partial)
13772{
13773 if (partial != NULL)
13774 partial_unref(partial);
13775 else if (callback != NULL)
13776 {
13777 func_unref(callback);
13778 vim_free(callback);
13779 }
13780}
13781#endif
13782
13783#ifdef FEAT_TIMERS
13784/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013785 * "timer_info([timer])" function
13786 */
13787 static void
13788f_timer_info(typval_T *argvars, typval_T *rettv)
13789{
13790 timer_T *timer = NULL;
13791
13792 if (rettv_list_alloc(rettv) != OK)
13793 return;
13794 if (argvars[0].v_type != VAR_UNKNOWN)
13795 {
13796 if (argvars[0].v_type != VAR_NUMBER)
13797 EMSG(_(e_number_exp));
13798 else
13799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013800 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013801 if (timer != NULL)
13802 add_timer_info(rettv, timer);
13803 }
13804 }
13805 else
13806 add_timer_info_all(rettv);
13807}
13808
13809/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013810 * "timer_pause(timer, paused)" function
13811 */
13812 static void
13813f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13814{
13815 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013816 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013817
13818 if (argvars[0].v_type != VAR_NUMBER)
13819 EMSG(_(e_number_exp));
13820 else
13821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013822 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013823 if (timer != NULL)
13824 timer->tr_paused = paused;
13825 }
13826}
13827
13828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 * "timer_start(time, callback [, options])" function
13830 */
13831 static void
13832f_timer_start(typval_T *argvars, typval_T *rettv)
13833{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013834 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013835 timer_T *timer;
13836 int repeat = 0;
13837 char_u *callback;
13838 dict_T *dict;
13839 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013840
Bram Moolenaar75537a92016-09-05 22:45:28 +020013841 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013842 if (check_secure())
13843 return;
13844 if (argvars[2].v_type != VAR_UNKNOWN)
13845 {
13846 if (argvars[2].v_type != VAR_DICT
13847 || (dict = argvars[2].vval.v_dict) == NULL)
13848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013849 EMSG2(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013850 return;
13851 }
13852 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013853 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013854 }
13855
Bram Moolenaar75537a92016-09-05 22:45:28 +020013856 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013858 return;
13859
13860 timer = create_timer(msec, repeat);
13861 if (timer == NULL)
13862 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863 else
13864 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013865 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013866 timer->tr_callback = vim_strsave(callback);
13867 else
13868 /* pointer into the partial */
13869 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013870 timer->tr_partial = partial;
13871 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872 }
13873}
13874
13875/*
13876 * "timer_stop(timer)" function
13877 */
13878 static void
13879f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13880{
13881 timer_T *timer;
13882
13883 if (argvars[0].v_type != VAR_NUMBER)
13884 {
13885 EMSG(_(e_number_exp));
13886 return;
13887 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013888 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889 if (timer != NULL)
13890 stop_timer(timer);
13891}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013892
13893/*
13894 * "timer_stopall()" function
13895 */
13896 static void
13897f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13898{
13899 stop_all_timers();
13900}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013901#endif
13902
13903/*
13904 * "tolower(string)" function
13905 */
13906 static void
13907f_tolower(typval_T *argvars, typval_T *rettv)
13908{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013909 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013910 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911}
13912
13913/*
13914 * "toupper(string)" function
13915 */
13916 static void
13917f_toupper(typval_T *argvars, typval_T *rettv)
13918{
13919 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013920 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013921}
13922
13923/*
13924 * "tr(string, fromstr, tostr)" function
13925 */
13926 static void
13927f_tr(typval_T *argvars, typval_T *rettv)
13928{
13929 char_u *in_str;
13930 char_u *fromstr;
13931 char_u *tostr;
13932 char_u *p;
13933#ifdef FEAT_MBYTE
13934 int inlen;
13935 int fromlen;
13936 int tolen;
13937 int idx;
13938 char_u *cpstr;
13939 int cplen;
13940 int first = TRUE;
13941#endif
13942 char_u buf[NUMBUFLEN];
13943 char_u buf2[NUMBUFLEN];
13944 garray_T ga;
13945
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013946 in_str = tv_get_string(&argvars[0]);
13947 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13948 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013949
13950 /* Default return value: empty string. */
13951 rettv->v_type = VAR_STRING;
13952 rettv->vval.v_string = NULL;
13953 if (fromstr == NULL || tostr == NULL)
13954 return; /* type error; errmsg already given */
13955 ga_init2(&ga, (int)sizeof(char), 80);
13956
13957#ifdef FEAT_MBYTE
13958 if (!has_mbyte)
13959#endif
13960 /* not multi-byte: fromstr and tostr must be the same length */
13961 if (STRLEN(fromstr) != STRLEN(tostr))
13962 {
13963#ifdef FEAT_MBYTE
13964error:
13965#endif
13966 EMSG2(_(e_invarg2), fromstr);
13967 ga_clear(&ga);
13968 return;
13969 }
13970
13971 /* fromstr and tostr have to contain the same number of chars */
13972 while (*in_str != NUL)
13973 {
13974#ifdef FEAT_MBYTE
13975 if (has_mbyte)
13976 {
13977 inlen = (*mb_ptr2len)(in_str);
13978 cpstr = in_str;
13979 cplen = inlen;
13980 idx = 0;
13981 for (p = fromstr; *p != NUL; p += fromlen)
13982 {
13983 fromlen = (*mb_ptr2len)(p);
13984 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13985 {
13986 for (p = tostr; *p != NUL; p += tolen)
13987 {
13988 tolen = (*mb_ptr2len)(p);
13989 if (idx-- == 0)
13990 {
13991 cplen = tolen;
13992 cpstr = p;
13993 break;
13994 }
13995 }
13996 if (*p == NUL) /* tostr is shorter than fromstr */
13997 goto error;
13998 break;
13999 }
14000 ++idx;
14001 }
14002
14003 if (first && cpstr == in_str)
14004 {
14005 /* Check that fromstr and tostr have the same number of
14006 * (multi-byte) characters. Done only once when a character
14007 * of in_str doesn't appear in fromstr. */
14008 first = FALSE;
14009 for (p = tostr; *p != NUL; p += tolen)
14010 {
14011 tolen = (*mb_ptr2len)(p);
14012 --idx;
14013 }
14014 if (idx != 0)
14015 goto error;
14016 }
14017
14018 (void)ga_grow(&ga, cplen);
14019 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14020 ga.ga_len += cplen;
14021
14022 in_str += inlen;
14023 }
14024 else
14025#endif
14026 {
14027 /* When not using multi-byte chars we can do it faster. */
14028 p = vim_strchr(fromstr, *in_str);
14029 if (p != NULL)
14030 ga_append(&ga, tostr[p - fromstr]);
14031 else
14032 ga_append(&ga, *in_str);
14033 ++in_str;
14034 }
14035 }
14036
14037 /* add a terminating NUL */
14038 (void)ga_grow(&ga, 1);
14039 ga_append(&ga, NUL);
14040
14041 rettv->vval.v_string = ga.ga_data;
14042}
14043
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014044/*
14045 * "trim({expr})" function
14046 */
14047 static void
14048f_trim(typval_T *argvars, typval_T *rettv)
14049{
14050 char_u buf1[NUMBUFLEN];
14051 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014052 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014053 char_u *mask = NULL;
14054 char_u *tail;
14055 char_u *prev;
14056 char_u *p;
14057 int c1;
14058
14059 rettv->v_type = VAR_STRING;
14060 if (head == NULL)
14061 {
14062 rettv->vval.v_string = NULL;
14063 return;
14064 }
14065
14066 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014067 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014068
14069 while (*head != NUL)
14070 {
14071 c1 = PTR2CHAR(head);
14072 if (mask == NULL)
14073 {
14074 if (c1 > ' ' && c1 != 0xa0)
14075 break;
14076 }
14077 else
14078 {
14079 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14080 if (c1 == PTR2CHAR(p))
14081 break;
14082 if (*p == NUL)
14083 break;
14084 }
14085 MB_PTR_ADV(head);
14086 }
14087
14088 for (tail = head + STRLEN(head); tail > head; tail = prev)
14089 {
14090 prev = tail;
14091 MB_PTR_BACK(head, prev);
14092 c1 = PTR2CHAR(prev);
14093 if (mask == NULL)
14094 {
14095 if (c1 > ' ' && c1 != 0xa0)
14096 break;
14097 }
14098 else
14099 {
14100 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14101 if (c1 == PTR2CHAR(p))
14102 break;
14103 if (*p == NUL)
14104 break;
14105 }
14106 }
14107 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14108}
14109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014110#ifdef FEAT_FLOAT
14111/*
14112 * "trunc({float})" function
14113 */
14114 static void
14115f_trunc(typval_T *argvars, typval_T *rettv)
14116{
14117 float_T f = 0.0;
14118
14119 rettv->v_type = VAR_FLOAT;
14120 if (get_float_arg(argvars, &f) == OK)
14121 /* trunc() is not in C90, use floor() or ceil() instead. */
14122 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14123 else
14124 rettv->vval.v_float = 0.0;
14125}
14126#endif
14127
14128/*
14129 * "type(expr)" function
14130 */
14131 static void
14132f_type(typval_T *argvars, typval_T *rettv)
14133{
14134 int n = -1;
14135
14136 switch (argvars[0].v_type)
14137 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014138 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14139 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014140 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014141 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14142 case VAR_LIST: n = VAR_TYPE_LIST; break;
14143 case VAR_DICT: n = VAR_TYPE_DICT; break;
14144 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 case VAR_SPECIAL:
14146 if (argvars[0].vval.v_number == VVAL_FALSE
14147 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014148 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014150 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014152 case VAR_JOB: n = VAR_TYPE_JOB; break;
14153 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014154 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014155 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014156 n = -1;
14157 break;
14158 }
14159 rettv->vval.v_number = n;
14160}
14161
14162/*
14163 * "undofile(name)" function
14164 */
14165 static void
14166f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14167{
14168 rettv->v_type = VAR_STRING;
14169#ifdef FEAT_PERSISTENT_UNDO
14170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014171 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014172
14173 if (*fname == NUL)
14174 {
14175 /* If there is no file name there will be no undo file. */
14176 rettv->vval.v_string = NULL;
14177 }
14178 else
14179 {
14180 char_u *ffname = FullName_save(fname, FALSE);
14181
14182 if (ffname != NULL)
14183 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14184 vim_free(ffname);
14185 }
14186 }
14187#else
14188 rettv->vval.v_string = NULL;
14189#endif
14190}
14191
14192/*
14193 * "undotree()" function
14194 */
14195 static void
14196f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14197{
14198 if (rettv_dict_alloc(rettv) == OK)
14199 {
14200 dict_T *dict = rettv->vval.v_dict;
14201 list_T *list;
14202
Bram Moolenaare0be1672018-07-08 16:50:37 +020014203 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14204 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14205 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14206 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14207 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14208 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014209
14210 list = list_alloc();
14211 if (list != NULL)
14212 {
14213 u_eval_tree(curbuf->b_u_oldhead, list);
14214 dict_add_list(dict, "entries", list);
14215 }
14216 }
14217}
14218
14219/*
14220 * "values(dict)" function
14221 */
14222 static void
14223f_values(typval_T *argvars, typval_T *rettv)
14224{
14225 dict_list(argvars, rettv, 1);
14226}
14227
14228/*
14229 * "virtcol(string)" function
14230 */
14231 static void
14232f_virtcol(typval_T *argvars, typval_T *rettv)
14233{
14234 colnr_T vcol = 0;
14235 pos_T *fp;
14236 int fnum = curbuf->b_fnum;
14237
14238 fp = var2fpos(&argvars[0], FALSE, &fnum);
14239 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14240 && fnum == curbuf->b_fnum)
14241 {
14242 getvvcol(curwin, fp, NULL, NULL, &vcol);
14243 ++vcol;
14244 }
14245
14246 rettv->vval.v_number = vcol;
14247}
14248
14249/*
14250 * "visualmode()" function
14251 */
14252 static void
14253f_visualmode(typval_T *argvars, typval_T *rettv)
14254{
14255 char_u str[2];
14256
14257 rettv->v_type = VAR_STRING;
14258 str[0] = curbuf->b_visual_mode_eval;
14259 str[1] = NUL;
14260 rettv->vval.v_string = vim_strsave(str);
14261
14262 /* A non-zero number or non-empty string argument: reset mode. */
14263 if (non_zero_arg(&argvars[0]))
14264 curbuf->b_visual_mode_eval = NUL;
14265}
14266
14267/*
14268 * "wildmenumode()" function
14269 */
14270 static void
14271f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14272{
14273#ifdef FEAT_WILDMENU
14274 if (wild_menu_showing)
14275 rettv->vval.v_number = 1;
14276#endif
14277}
14278
14279/*
14280 * "winbufnr(nr)" function
14281 */
14282 static void
14283f_winbufnr(typval_T *argvars, typval_T *rettv)
14284{
14285 win_T *wp;
14286
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014287 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014288 if (wp == NULL)
14289 rettv->vval.v_number = -1;
14290 else
14291 rettv->vval.v_number = wp->w_buffer->b_fnum;
14292}
14293
14294/*
14295 * "wincol()" function
14296 */
14297 static void
14298f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14299{
14300 validate_cursor();
14301 rettv->vval.v_number = curwin->w_wcol + 1;
14302}
14303
14304/*
14305 * "winheight(nr)" function
14306 */
14307 static void
14308f_winheight(typval_T *argvars, typval_T *rettv)
14309{
14310 win_T *wp;
14311
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014312 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014313 if (wp == NULL)
14314 rettv->vval.v_number = -1;
14315 else
14316 rettv->vval.v_number = wp->w_height;
14317}
14318
14319/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014320 * "winlayout()" function
14321 */
14322 static void
14323f_winlayout(typval_T *argvars, typval_T *rettv)
14324{
14325 tabpage_T *tp;
14326
14327 if (rettv_list_alloc(rettv) != OK)
14328 return;
14329
14330 if (argvars[0].v_type == VAR_UNKNOWN)
14331 tp = curtab;
14332 else
14333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014334 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014335 if (tp == NULL)
14336 return;
14337 }
14338
14339 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14340}
14341
14342/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014343 * "winline()" function
14344 */
14345 static void
14346f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14347{
14348 validate_cursor();
14349 rettv->vval.v_number = curwin->w_wrow + 1;
14350}
14351
14352/*
14353 * "winnr()" function
14354 */
14355 static void
14356f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14357{
14358 int nr = 1;
14359
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014360 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014361 rettv->vval.v_number = nr;
14362}
14363
14364/*
14365 * "winrestcmd()" function
14366 */
14367 static void
14368f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14369{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014370 win_T *wp;
14371 int winnr = 1;
14372 garray_T ga;
14373 char_u buf[50];
14374
14375 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014376 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014377 {
14378 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14379 ga_concat(&ga, buf);
14380 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14381 ga_concat(&ga, buf);
14382 ++winnr;
14383 }
14384 ga_append(&ga, NUL);
14385
14386 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014387 rettv->v_type = VAR_STRING;
14388}
14389
14390/*
14391 * "winrestview()" function
14392 */
14393 static void
14394f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14395{
14396 dict_T *dict;
14397
14398 if (argvars[0].v_type != VAR_DICT
14399 || (dict = argvars[0].vval.v_dict) == NULL)
14400 EMSG(_(e_invarg));
14401 else
14402 {
14403 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014404 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014405 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014406 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407#ifdef FEAT_VIRTUALEDIT
14408 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014409 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014410#endif
14411 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14412 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014413 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014414 curwin->w_set_curswant = FALSE;
14415 }
14416
14417 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014418 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014419#ifdef FEAT_DIFF
14420 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014421 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014422#endif
14423 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014424 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014426 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014427
14428 check_cursor();
14429 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014430 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014431 changed_window_setting();
14432
14433 if (curwin->w_topline <= 0)
14434 curwin->w_topline = 1;
14435 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14436 curwin->w_topline = curbuf->b_ml.ml_line_count;
14437#ifdef FEAT_DIFF
14438 check_topfill(curwin, TRUE);
14439#endif
14440 }
14441}
14442
14443/*
14444 * "winsaveview()" function
14445 */
14446 static void
14447f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14448{
14449 dict_T *dict;
14450
14451 if (rettv_dict_alloc(rettv) == FAIL)
14452 return;
14453 dict = rettv->vval.v_dict;
14454
Bram Moolenaare0be1672018-07-08 16:50:37 +020014455 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14456 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014457#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014458 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014459#endif
14460 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014461 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014462
Bram Moolenaare0be1672018-07-08 16:50:37 +020014463 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014464#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014465 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014466#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014467 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14468 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014469}
14470
14471/*
14472 * "winwidth(nr)" function
14473 */
14474 static void
14475f_winwidth(typval_T *argvars, typval_T *rettv)
14476{
14477 win_T *wp;
14478
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014479 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014480 if (wp == NULL)
14481 rettv->vval.v_number = -1;
14482 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014483 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014484}
14485
14486/*
14487 * "wordcount()" function
14488 */
14489 static void
14490f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14491{
14492 if (rettv_dict_alloc(rettv) == FAIL)
14493 return;
14494 cursor_pos_info(rettv->vval.v_dict);
14495}
14496
14497/*
14498 * "writefile()" function
14499 */
14500 static void
14501f_writefile(typval_T *argvars, typval_T *rettv)
14502{
14503 int binary = FALSE;
14504 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014505#ifdef HAVE_FSYNC
14506 int do_fsync = p_fs;
14507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014508 char_u *fname;
14509 FILE *fd;
14510 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014511 listitem_T *li;
14512 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014513
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014514 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014515 if (check_restricted() || check_secure())
14516 return;
14517
14518 if (argvars[0].v_type != VAR_LIST)
14519 {
14520 EMSG2(_(e_listarg), "writefile()");
14521 return;
14522 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014523 list = argvars[0].vval.v_list;
14524 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014525 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014526 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014527 if (tv_get_string_chk(&li->li_tv) == NULL)
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014528 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014529
14530 if (argvars[2].v_type != VAR_UNKNOWN)
14531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014532 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014533
14534 if (arg2 == NULL)
14535 return;
14536 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014537 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014538 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014540#ifdef HAVE_FSYNC
14541 if (vim_strchr(arg2, 's') != NULL)
14542 do_fsync = TRUE;
14543 else if (vim_strchr(arg2, 'S') != NULL)
14544 do_fsync = FALSE;
14545#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014546 }
14547
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014548 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014549 if (fname == NULL)
14550 return;
14551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014552 /* Always open the file in binary mode, library functions have a mind of
14553 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014554 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14555 append ? APPENDBIN : WRITEBIN)) == NULL)
14556 {
14557 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14558 ret = -1;
14559 }
14560 else
14561 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014562 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014563 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014564#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014565 else if (do_fsync)
14566 /* Ignore the error, the user wouldn't know what to do about it.
14567 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014568 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014570 fclose(fd);
14571 }
14572
14573 rettv->vval.v_number = ret;
14574}
14575
14576/*
14577 * "xor(expr, expr)" function
14578 */
14579 static void
14580f_xor(typval_T *argvars, typval_T *rettv)
14581{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014582 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14583 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584}
14585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014586#endif /* FEAT_EVAL */