blob: d3bacad7785b49ba0ba621c49b27be94fa8a0183 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020032static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033
34#ifdef FEAT_FLOAT
35static void f_abs(typval_T *argvars, typval_T *rettv);
36static void f_acos(typval_T *argvars, typval_T *rettv);
37#endif
38static void f_add(typval_T *argvars, typval_T *rettv);
39static void f_and(typval_T *argvars, typval_T *rettv);
40static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020041static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042static void f_argc(typval_T *argvars, typval_T *rettv);
43static void f_argidx(typval_T *argvars, typval_T *rettv);
44static void f_arglistid(typval_T *argvars, typval_T *rettv);
45static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010046static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010048static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
99static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
103static void f_ch_status(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_changenr(typval_T *argvars, typval_T *rettv);
106static void f_char2nr(typval_T *argvars, typval_T *rettv);
107static void f_cindent(typval_T *argvars, typval_T *rettv);
108static void f_clearmatches(typval_T *argvars, typval_T *rettv);
109static void f_col(typval_T *argvars, typval_T *rettv);
110#if defined(FEAT_INS_EXPAND)
111static void f_complete(typval_T *argvars, typval_T *rettv);
112static void f_complete_add(typval_T *argvars, typval_T *rettv);
113static void f_complete_check(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_confirm(typval_T *argvars, typval_T *rettv);
116static void f_copy(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_cos(typval_T *argvars, typval_T *rettv);
119static void f_cosh(typval_T *argvars, typval_T *rettv);
120#endif
121static void f_count(typval_T *argvars, typval_T *rettv);
122static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
123static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200124#ifdef WIN3264
125static void f_debugbreak(typval_T *argvars, typval_T *rettv);
126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_deepcopy(typval_T *argvars, typval_T *rettv);
128static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200129static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200130static void f_did_filetype(typval_T *argvars, typval_T *rettv);
131static void f_diff_filler(typval_T *argvars, typval_T *rettv);
132static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
133static void f_empty(typval_T *argvars, typval_T *rettv);
134static void f_escape(typval_T *argvars, typval_T *rettv);
135static void f_eval(typval_T *argvars, typval_T *rettv);
136static void f_eventhandler(typval_T *argvars, typval_T *rettv);
137static void f_executable(typval_T *argvars, typval_T *rettv);
138static void f_execute(typval_T *argvars, typval_T *rettv);
139static void f_exepath(typval_T *argvars, typval_T *rettv);
140static void f_exists(typval_T *argvars, typval_T *rettv);
141#ifdef FEAT_FLOAT
142static void f_exp(typval_T *argvars, typval_T *rettv);
143#endif
144static void f_expand(typval_T *argvars, typval_T *rettv);
145static void f_extend(typval_T *argvars, typval_T *rettv);
146static void f_feedkeys(typval_T *argvars, typval_T *rettv);
147static void f_filereadable(typval_T *argvars, typval_T *rettv);
148static void f_filewritable(typval_T *argvars, typval_T *rettv);
149static void f_filter(typval_T *argvars, typval_T *rettv);
150static void f_finddir(typval_T *argvars, typval_T *rettv);
151static void f_findfile(typval_T *argvars, typval_T *rettv);
152#ifdef FEAT_FLOAT
153static void f_float2nr(typval_T *argvars, typval_T *rettv);
154static void f_floor(typval_T *argvars, typval_T *rettv);
155static void f_fmod(typval_T *argvars, typval_T *rettv);
156#endif
157static void f_fnameescape(typval_T *argvars, typval_T *rettv);
158static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
159static void f_foldclosed(typval_T *argvars, typval_T *rettv);
160static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
161static void f_foldlevel(typval_T *argvars, typval_T *rettv);
162static void f_foldtext(typval_T *argvars, typval_T *rettv);
163static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
164static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200165static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_function(typval_T *argvars, typval_T *rettv);
167static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
168static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200169static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getbufline(typval_T *argvars, typval_T *rettv);
171static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100172static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getchar(typval_T *argvars, typval_T *rettv);
174static void f_getcharmod(typval_T *argvars, typval_T *rettv);
175static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
176static void f_getcmdline(typval_T *argvars, typval_T *rettv);
177#if defined(FEAT_CMDL_COMPL)
178static void f_getcompletion(typval_T *argvars, typval_T *rettv);
179#endif
180static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
181static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
182static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
183static void f_getcwd(typval_T *argvars, typval_T *rettv);
184static void f_getfontname(typval_T *argvars, typval_T *rettv);
185static void f_getfperm(typval_T *argvars, typval_T *rettv);
186static void f_getfsize(typval_T *argvars, typval_T *rettv);
187static void f_getftime(typval_T *argvars, typval_T *rettv);
188static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100189static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200191static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getmatches(typval_T *argvars, typval_T *rettv);
193static void f_getpid(typval_T *argvars, typval_T *rettv);
194static void f_getcurpos(typval_T *argvars, typval_T *rettv);
195static void f_getpos(typval_T *argvars, typval_T *rettv);
196static void f_getqflist(typval_T *argvars, typval_T *rettv);
197static void f_getreg(typval_T *argvars, typval_T *rettv);
198static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_gettabvar(typval_T *argvars, typval_T *rettv);
201static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100202static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100204static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_getwinposx(typval_T *argvars, typval_T *rettv);
206static void f_getwinposy(typval_T *argvars, typval_T *rettv);
207static void f_getwinvar(typval_T *argvars, typval_T *rettv);
208static void f_glob(typval_T *argvars, typval_T *rettv);
209static void f_globpath(typval_T *argvars, typval_T *rettv);
210static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
211static void f_has(typval_T *argvars, typval_T *rettv);
212static void f_has_key(typval_T *argvars, typval_T *rettv);
213static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
214static void f_hasmapto(typval_T *argvars, typval_T *rettv);
215static void f_histadd(typval_T *argvars, typval_T *rettv);
216static void f_histdel(typval_T *argvars, typval_T *rettv);
217static void f_histget(typval_T *argvars, typval_T *rettv);
218static void f_histnr(typval_T *argvars, typval_T *rettv);
219static void f_hlID(typval_T *argvars, typval_T *rettv);
220static void f_hlexists(typval_T *argvars, typval_T *rettv);
221static void f_hostname(typval_T *argvars, typval_T *rettv);
222static void f_iconv(typval_T *argvars, typval_T *rettv);
223static void f_indent(typval_T *argvars, typval_T *rettv);
224static void f_index(typval_T *argvars, typval_T *rettv);
225static void f_input(typval_T *argvars, typval_T *rettv);
226static void f_inputdialog(typval_T *argvars, typval_T *rettv);
227static void f_inputlist(typval_T *argvars, typval_T *rettv);
228static void f_inputrestore(typval_T *argvars, typval_T *rettv);
229static void f_inputsave(typval_T *argvars, typval_T *rettv);
230static void f_inputsecret(typval_T *argvars, typval_T *rettv);
231static void f_insert(typval_T *argvars, typval_T *rettv);
232static void f_invert(typval_T *argvars, typval_T *rettv);
233static void f_isdirectory(typval_T *argvars, typval_T *rettv);
234static void f_islocked(typval_T *argvars, typval_T *rettv);
235#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
236static void f_isnan(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_items(typval_T *argvars, typval_T *rettv);
239#ifdef FEAT_JOB_CHANNEL
240static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
241static void f_job_info(typval_T *argvars, typval_T *rettv);
242static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
243static void f_job_start(typval_T *argvars, typval_T *rettv);
244static void f_job_stop(typval_T *argvars, typval_T *rettv);
245static void f_job_status(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_join(typval_T *argvars, typval_T *rettv);
248static void f_js_decode(typval_T *argvars, typval_T *rettv);
249static void f_js_encode(typval_T *argvars, typval_T *rettv);
250static void f_json_decode(typval_T *argvars, typval_T *rettv);
251static void f_json_encode(typval_T *argvars, typval_T *rettv);
252static void f_keys(typval_T *argvars, typval_T *rettv);
253static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
254static void f_len(typval_T *argvars, typval_T *rettv);
255static void f_libcall(typval_T *argvars, typval_T *rettv);
256static void f_libcallnr(typval_T *argvars, typval_T *rettv);
257static void f_line(typval_T *argvars, typval_T *rettv);
258static void f_line2byte(typval_T *argvars, typval_T *rettv);
259static void f_lispindent(typval_T *argvars, typval_T *rettv);
260static void f_localtime(typval_T *argvars, typval_T *rettv);
261#ifdef FEAT_FLOAT
262static void f_log(typval_T *argvars, typval_T *rettv);
263static void f_log10(typval_T *argvars, typval_T *rettv);
264#endif
265#ifdef FEAT_LUA
266static void f_luaeval(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_map(typval_T *argvars, typval_T *rettv);
269static void f_maparg(typval_T *argvars, typval_T *rettv);
270static void f_mapcheck(typval_T *argvars, typval_T *rettv);
271static void f_match(typval_T *argvars, typval_T *rettv);
272static void f_matchadd(typval_T *argvars, typval_T *rettv);
273static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
274static void f_matcharg(typval_T *argvars, typval_T *rettv);
275static void f_matchdelete(typval_T *argvars, typval_T *rettv);
276static void f_matchend(typval_T *argvars, typval_T *rettv);
277static void f_matchlist(typval_T *argvars, typval_T *rettv);
278static void f_matchstr(typval_T *argvars, typval_T *rettv);
279static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
280static void f_max(typval_T *argvars, typval_T *rettv);
281static void f_min(typval_T *argvars, typval_T *rettv);
282#ifdef vim_mkdir
283static void f_mkdir(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_mode(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_MZSCHEME
287static void f_mzeval(typval_T *argvars, typval_T *rettv);
288#endif
289static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
290static void f_nr2char(typval_T *argvars, typval_T *rettv);
291static void f_or(typval_T *argvars, typval_T *rettv);
292static void f_pathshorten(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PERL
294static void f_perleval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_FLOAT
297static void f_pow(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
300static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200301#ifdef FEAT_JOB_CHANNEL
302static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200303static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200304static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_pumvisible(typval_T *argvars, typval_T *rettv);
307#ifdef FEAT_PYTHON3
308static void f_py3eval(typval_T *argvars, typval_T *rettv);
309#endif
310#ifdef FEAT_PYTHON
311static void f_pyeval(typval_T *argvars, typval_T *rettv);
312#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100313#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
314static void f_pyxeval(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_range(typval_T *argvars, typval_T *rettv);
317static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200318static void f_reg_executing(typval_T *argvars, typval_T *rettv);
319static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_reltime(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_reltimestr(typval_T *argvars, typval_T *rettv);
325static void f_remote_expr(typval_T *argvars, typval_T *rettv);
326static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
327static void f_remote_peek(typval_T *argvars, typval_T *rettv);
328static void f_remote_read(typval_T *argvars, typval_T *rettv);
329static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100330static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_remove(typval_T *argvars, typval_T *rettv);
332static void f_rename(typval_T *argvars, typval_T *rettv);
333static void f_repeat(typval_T *argvars, typval_T *rettv);
334static void f_resolve(typval_T *argvars, typval_T *rettv);
335static void f_reverse(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_FLOAT
337static void f_round(typval_T *argvars, typval_T *rettv);
338#endif
339static void f_screenattr(typval_T *argvars, typval_T *rettv);
340static void f_screenchar(typval_T *argvars, typval_T *rettv);
341static void f_screencol(typval_T *argvars, typval_T *rettv);
342static void f_screenrow(typval_T *argvars, typval_T *rettv);
343static void f_search(typval_T *argvars, typval_T *rettv);
344static void f_searchdecl(typval_T *argvars, typval_T *rettv);
345static void f_searchpair(typval_T *argvars, typval_T *rettv);
346static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
347static void f_searchpos(typval_T *argvars, typval_T *rettv);
348static void f_server2client(typval_T *argvars, typval_T *rettv);
349static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200350static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_setbufvar(typval_T *argvars, typval_T *rettv);
352static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
353static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
354static void f_setfperm(typval_T *argvars, typval_T *rettv);
355static void f_setline(typval_T *argvars, typval_T *rettv);
356static void f_setloclist(typval_T *argvars, typval_T *rettv);
357static void f_setmatches(typval_T *argvars, typval_T *rettv);
358static void f_setpos(typval_T *argvars, typval_T *rettv);
359static void f_setqflist(typval_T *argvars, typval_T *rettv);
360static void f_setreg(typval_T *argvars, typval_T *rettv);
361static void f_settabvar(typval_T *argvars, typval_T *rettv);
362static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100363static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200401static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200402static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_synID(typval_T *argvars, typval_T *rettv);
404static void f_synIDattr(typval_T *argvars, typval_T *rettv);
405static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
406static void f_synstack(typval_T *argvars, typval_T *rettv);
407static void f_synconcealed(typval_T *argvars, typval_T *rettv);
408static void f_system(typval_T *argvars, typval_T *rettv);
409static void f_systemlist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
411static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
412static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
413static void f_taglist(typval_T *argvars, typval_T *rettv);
414static void f_tagfiles(typval_T *argvars, typval_T *rettv);
415static void f_tempname(typval_T *argvars, typval_T *rettv);
416static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
417static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200418static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200419static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100420static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100422static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423#ifdef FEAT_JOB_CHANNEL
424static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_JOB_CHANNEL
428static void f_test_null_job(typval_T *argvars, typval_T *rettv);
429#endif
430static void f_test_null_list(typval_T *argvars, typval_T *rettv);
431static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
432static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200433#ifdef FEAT_GUI
434static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436static void f_test_settime(typval_T *argvars, typval_T *rettv);
437#ifdef FEAT_FLOAT
438static void f_tan(typval_T *argvars, typval_T *rettv);
439static void f_tanh(typval_T *argvars, typval_T *rettv);
440#endif
441#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200442static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200443static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444static void f_timer_start(typval_T *argvars, typval_T *rettv);
445static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200446static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#endif
448static void f_tolower(typval_T *argvars, typval_T *rettv);
449static void f_toupper(typval_T *argvars, typval_T *rettv);
450static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100451static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_FLOAT
453static void f_trunc(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_type(typval_T *argvars, typval_T *rettv);
456static void f_undofile(typval_T *argvars, typval_T *rettv);
457static void f_undotree(typval_T *argvars, typval_T *rettv);
458static void f_uniq(typval_T *argvars, typval_T *rettv);
459static void f_values(typval_T *argvars, typval_T *rettv);
460static void f_virtcol(typval_T *argvars, typval_T *rettv);
461static void f_visualmode(typval_T *argvars, typval_T *rettv);
462static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
463static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
464static void f_win_getid(typval_T *argvars, typval_T *rettv);
465static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
466static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
467static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100468static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_winbufnr(typval_T *argvars, typval_T *rettv);
470static void f_wincol(typval_T *argvars, typval_T *rettv);
471static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200472static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473static void f_winline(typval_T *argvars, typval_T *rettv);
474static void f_winnr(typval_T *argvars, typval_T *rettv);
475static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
476static void f_winrestview(typval_T *argvars, typval_T *rettv);
477static void f_winsaveview(typval_T *argvars, typval_T *rettv);
478static void f_winwidth(typval_T *argvars, typval_T *rettv);
479static void f_writefile(typval_T *argvars, typval_T *rettv);
480static void f_wordcount(typval_T *argvars, typval_T *rettv);
481static void f_xor(typval_T *argvars, typval_T *rettv);
482
483/*
484 * Array with names and number of arguments of all internal functions
485 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
486 */
487static struct fst
488{
489 char *f_name; /* function name */
490 char f_min_argc; /* minimal number of arguments */
491 char f_max_argc; /* maximal number of arguments */
492 void (*f_func)(typval_T *args, typval_T *rvar);
493 /* implementation of function */
494} functions[] =
495{
496#ifdef FEAT_FLOAT
497 {"abs", 1, 1, f_abs},
498 {"acos", 1, 1, f_acos}, /* WJMc */
499#endif
500 {"add", 2, 2, f_add},
501 {"and", 2, 2, f_and},
502 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200503 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200504 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"argidx", 0, 0, f_argidx},
506 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200507 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508#ifdef FEAT_FLOAT
509 {"asin", 1, 1, f_asin}, /* WJMc */
510#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100511 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100513 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200515 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100517 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_match", 2, 3, f_assert_match},
519 {"assert_notequal", 2, 3, f_assert_notequal},
520 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100521 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522 {"assert_true", 1, 2, f_assert_true},
523#ifdef FEAT_FLOAT
524 {"atan", 1, 1, f_atan},
525 {"atan2", 2, 2, f_atan2},
526#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100527#ifdef FEAT_BEVAL
528 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100529# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100530 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100531# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100532#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"browse", 4, 4, f_browse},
534 {"browsedir", 2, 2, f_browsedir},
535 {"bufexists", 1, 1, f_bufexists},
536 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
537 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
538 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
539 {"buflisted", 1, 1, f_buflisted},
540 {"bufloaded", 1, 1, f_bufloaded},
541 {"bufname", 1, 1, f_bufname},
542 {"bufnr", 1, 2, f_bufnr},
543 {"bufwinid", 1, 1, f_bufwinid},
544 {"bufwinnr", 1, 1, f_bufwinnr},
545 {"byte2line", 1, 1, f_byte2line},
546 {"byteidx", 2, 2, f_byteidx},
547 {"byteidxcomp", 2, 2, f_byteidxcomp},
548 {"call", 2, 3, f_call},
549#ifdef FEAT_FLOAT
550 {"ceil", 1, 1, f_ceil},
551#endif
552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100553 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200555 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
557 {"ch_evalraw", 2, 3, f_ch_evalraw},
558 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
559 {"ch_getjob", 1, 1, f_ch_getjob},
560 {"ch_info", 1, 1, f_ch_info},
561 {"ch_log", 1, 2, f_ch_log},
562 {"ch_logfile", 1, 2, f_ch_logfile},
563 {"ch_open", 1, 2, f_ch_open},
564 {"ch_read", 1, 2, f_ch_read},
565 {"ch_readraw", 1, 2, f_ch_readraw},
566 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
567 {"ch_sendraw", 2, 3, f_ch_sendraw},
568 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200569 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200570#endif
571 {"changenr", 0, 0, f_changenr},
572 {"char2nr", 1, 2, f_char2nr},
573 {"cindent", 1, 1, f_cindent},
574 {"clearmatches", 0, 0, f_clearmatches},
575 {"col", 1, 1, f_col},
576#if defined(FEAT_INS_EXPAND)
577 {"complete", 2, 2, f_complete},
578 {"complete_add", 1, 1, f_complete_add},
579 {"complete_check", 0, 0, f_complete_check},
580#endif
581 {"confirm", 1, 4, f_confirm},
582 {"copy", 1, 1, f_copy},
583#ifdef FEAT_FLOAT
584 {"cos", 1, 1, f_cos},
585 {"cosh", 1, 1, f_cosh},
586#endif
587 {"count", 2, 4, f_count},
588 {"cscope_connection",0,3, f_cscope_connection},
589 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200590#ifdef WIN3264
591 {"debugbreak", 1, 1, f_debugbreak},
592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"deepcopy", 1, 2, f_deepcopy},
594 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200595 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"did_filetype", 0, 0, f_did_filetype},
597 {"diff_filler", 1, 1, f_diff_filler},
598 {"diff_hlID", 2, 2, f_diff_hlID},
599 {"empty", 1, 1, f_empty},
600 {"escape", 2, 2, f_escape},
601 {"eval", 1, 1, f_eval},
602 {"eventhandler", 0, 0, f_eventhandler},
603 {"executable", 1, 1, f_executable},
604 {"execute", 1, 2, f_execute},
605 {"exepath", 1, 1, f_exepath},
606 {"exists", 1, 1, f_exists},
607#ifdef FEAT_FLOAT
608 {"exp", 1, 1, f_exp},
609#endif
610 {"expand", 1, 3, f_expand},
611 {"extend", 2, 3, f_extend},
612 {"feedkeys", 1, 2, f_feedkeys},
613 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
614 {"filereadable", 1, 1, f_filereadable},
615 {"filewritable", 1, 1, f_filewritable},
616 {"filter", 2, 2, f_filter},
617 {"finddir", 1, 3, f_finddir},
618 {"findfile", 1, 3, f_findfile},
619#ifdef FEAT_FLOAT
620 {"float2nr", 1, 1, f_float2nr},
621 {"floor", 1, 1, f_floor},
622 {"fmod", 2, 2, f_fmod},
623#endif
624 {"fnameescape", 1, 1, f_fnameescape},
625 {"fnamemodify", 2, 2, f_fnamemodify},
626 {"foldclosed", 1, 1, f_foldclosed},
627 {"foldclosedend", 1, 1, f_foldclosedend},
628 {"foldlevel", 1, 1, f_foldlevel},
629 {"foldtext", 0, 0, f_foldtext},
630 {"foldtextresult", 1, 1, f_foldtextresult},
631 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"function", 1, 3, f_function},
634 {"garbagecollect", 0, 1, f_garbagecollect},
635 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200636 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"getbufline", 2, 3, f_getbufline},
638 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100639 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getchar", 0, 1, f_getchar},
641 {"getcharmod", 0, 0, f_getcharmod},
642 {"getcharsearch", 0, 0, f_getcharsearch},
643 {"getcmdline", 0, 0, f_getcmdline},
644 {"getcmdpos", 0, 0, f_getcmdpos},
645 {"getcmdtype", 0, 0, f_getcmdtype},
646 {"getcmdwintype", 0, 0, f_getcmdwintype},
647#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200648 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649#endif
650 {"getcurpos", 0, 0, f_getcurpos},
651 {"getcwd", 0, 2, f_getcwd},
652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getmatches", 0, 0, f_getmatches},
661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
703 {"islocked", 1, 1, f_islocked},
704#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
705 {"isnan", 1, 1, f_isnan},
706#endif
707 {"items", 1, 1, f_items},
708#ifdef FEAT_JOB_CHANNEL
709 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200710 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200711 {"job_setoptions", 2, 2, f_job_setoptions},
712 {"job_start", 1, 2, f_job_start},
713 {"job_status", 1, 1, f_job_status},
714 {"job_stop", 1, 2, f_job_stop},
715#endif
716 {"join", 1, 2, f_join},
717 {"js_decode", 1, 1, f_js_decode},
718 {"js_encode", 1, 1, f_js_encode},
719 {"json_decode", 1, 1, f_json_decode},
720 {"json_encode", 1, 1, f_json_encode},
721 {"keys", 1, 1, f_keys},
722 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
723 {"len", 1, 1, f_len},
724 {"libcall", 3, 3, f_libcall},
725 {"libcallnr", 3, 3, f_libcallnr},
726 {"line", 1, 1, f_line},
727 {"line2byte", 1, 1, f_line2byte},
728 {"lispindent", 1, 1, f_lispindent},
729 {"localtime", 0, 0, f_localtime},
730#ifdef FEAT_FLOAT
731 {"log", 1, 1, f_log},
732 {"log10", 1, 1, f_log10},
733#endif
734#ifdef FEAT_LUA
735 {"luaeval", 1, 2, f_luaeval},
736#endif
737 {"map", 2, 2, f_map},
738 {"maparg", 1, 4, f_maparg},
739 {"mapcheck", 1, 3, f_mapcheck},
740 {"match", 2, 4, f_match},
741 {"matchadd", 2, 5, f_matchadd},
742 {"matchaddpos", 2, 5, f_matchaddpos},
743 {"matcharg", 1, 1, f_matcharg},
744 {"matchdelete", 1, 1, f_matchdelete},
745 {"matchend", 2, 4, f_matchend},
746 {"matchlist", 2, 4, f_matchlist},
747 {"matchstr", 2, 4, f_matchstr},
748 {"matchstrpos", 2, 4, f_matchstrpos},
749 {"max", 1, 1, f_max},
750 {"min", 1, 1, f_min},
751#ifdef vim_mkdir
752 {"mkdir", 1, 3, f_mkdir},
753#endif
754 {"mode", 0, 1, f_mode},
755#ifdef FEAT_MZSCHEME
756 {"mzeval", 1, 1, f_mzeval},
757#endif
758 {"nextnonblank", 1, 1, f_nextnonblank},
759 {"nr2char", 1, 2, f_nr2char},
760 {"or", 2, 2, f_or},
761 {"pathshorten", 1, 1, f_pathshorten},
762#ifdef FEAT_PERL
763 {"perleval", 1, 1, f_perleval},
764#endif
765#ifdef FEAT_FLOAT
766 {"pow", 2, 2, f_pow},
767#endif
768 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100769 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200770#ifdef FEAT_JOB_CHANNEL
771 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200772 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200773 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
774#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"pumvisible", 0, 0, f_pumvisible},
776#ifdef FEAT_PYTHON3
777 {"py3eval", 1, 1, f_py3eval},
778#endif
779#ifdef FEAT_PYTHON
780 {"pyeval", 1, 1, f_pyeval},
781#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100782#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
783 {"pyxeval", 1, 1, f_pyxeval},
784#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"range", 1, 3, f_range},
786 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200787 {"reg_executing", 0, 0, f_reg_executing},
788 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"reltime", 0, 2, f_reltime},
790#ifdef FEAT_FLOAT
791 {"reltimefloat", 1, 1, f_reltimefloat},
792#endif
793 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100794 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200795 {"remote_foreground", 1, 1, f_remote_foreground},
796 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100797 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100799 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"remove", 2, 3, f_remove},
801 {"rename", 2, 2, f_rename},
802 {"repeat", 2, 2, f_repeat},
803 {"resolve", 1, 1, f_resolve},
804 {"reverse", 1, 1, f_reverse},
805#ifdef FEAT_FLOAT
806 {"round", 1, 1, f_round},
807#endif
808 {"screenattr", 2, 2, f_screenattr},
809 {"screenchar", 2, 2, f_screenchar},
810 {"screencol", 0, 0, f_screencol},
811 {"screenrow", 0, 0, f_screenrow},
812 {"search", 1, 4, f_search},
813 {"searchdecl", 1, 3, f_searchdecl},
814 {"searchpair", 3, 7, f_searchpair},
815 {"searchpairpos", 3, 7, f_searchpairpos},
816 {"searchpos", 1, 4, f_searchpos},
817 {"server2client", 2, 2, f_server2client},
818 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200819 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"setbufvar", 3, 3, f_setbufvar},
821 {"setcharsearch", 1, 1, f_setcharsearch},
822 {"setcmdpos", 1, 1, f_setcmdpos},
823 {"setfperm", 2, 2, f_setfperm},
824 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200825 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"setmatches", 1, 1, f_setmatches},
827 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200828 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829 {"setreg", 2, 3, f_setreg},
830 {"settabvar", 3, 3, f_settabvar},
831 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100832 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"setwinvar", 3, 3, f_setwinvar},
834#ifdef FEAT_CRYPT
835 {"sha256", 1, 1, f_sha256},
836#endif
837 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100838 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"simplify", 1, 1, f_simplify},
840#ifdef FEAT_FLOAT
841 {"sin", 1, 1, f_sin},
842 {"sinh", 1, 1, f_sinh},
843#endif
844 {"sort", 1, 3, f_sort},
845 {"soundfold", 1, 1, f_soundfold},
846 {"spellbadword", 0, 1, f_spellbadword},
847 {"spellsuggest", 1, 3, f_spellsuggest},
848 {"split", 1, 3, f_split},
849#ifdef FEAT_FLOAT
850 {"sqrt", 1, 1, f_sqrt},
851 {"str2float", 1, 1, f_str2float},
852#endif
853 {"str2nr", 1, 2, f_str2nr},
854 {"strcharpart", 2, 3, f_strcharpart},
855 {"strchars", 1, 2, f_strchars},
856 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
857#ifdef HAVE_STRFTIME
858 {"strftime", 1, 2, f_strftime},
859#endif
860 {"strgetchar", 2, 2, f_strgetchar},
861 {"stridx", 2, 3, f_stridx},
862 {"string", 1, 1, f_string},
863 {"strlen", 1, 1, f_strlen},
864 {"strpart", 2, 3, f_strpart},
865 {"strridx", 2, 3, f_strridx},
866 {"strtrans", 1, 1, f_strtrans},
867 {"strwidth", 1, 1, f_strwidth},
868 {"submatch", 1, 2, f_submatch},
869 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200870 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200871 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"synID", 3, 3, f_synID},
873 {"synIDattr", 2, 3, f_synIDattr},
874 {"synIDtrans", 1, 1, f_synIDtrans},
875 {"synconcealed", 2, 2, f_synconcealed},
876 {"synstack", 2, 2, f_synstack},
877 {"system", 1, 2, f_system},
878 {"systemlist", 1, 2, f_systemlist},
879 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
880 {"tabpagenr", 0, 1, f_tabpagenr},
881 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
882 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100883 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884#ifdef FEAT_FLOAT
885 {"tan", 1, 1, f_tan},
886 {"tanh", 1, 1, f_tanh},
887#endif
888 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200889#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100890 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
891 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100892 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200893 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200894# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
895 {"term_getansicolors", 1, 1, f_term_getansicolors},
896# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200898 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200899 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200900 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200901 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200903 {"term_getstatus", 1, 1, f_term_getstatus},
904 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200905 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200907 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200909# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
910 {"term_setansicolors", 2, 2, f_term_setansicolors},
911# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100912 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100913 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200914 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200916 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
919 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200920 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100922 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923#ifdef FEAT_JOB_CHANNEL
924 {"test_null_channel", 0, 0, f_test_null_channel},
925#endif
926 {"test_null_dict", 0, 0, f_test_null_dict},
927#ifdef FEAT_JOB_CHANNEL
928 {"test_null_job", 0, 0, f_test_null_job},
929#endif
930 {"test_null_list", 0, 0, f_test_null_list},
931 {"test_null_partial", 0, 0, f_test_null_partial},
932 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200933 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100934 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200935#ifdef FEAT_GUI
936 {"test_scrollbar", 3, 3, f_test_scrollbar},
937#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938 {"test_settime", 1, 1, f_test_settime},
939#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200940 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200941 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200942 {"timer_start", 2, 3, f_timer_start},
943 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200944 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945#endif
946 {"tolower", 1, 1, f_tolower},
947 {"toupper", 1, 1, f_toupper},
948 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100949 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950#ifdef FEAT_FLOAT
951 {"trunc", 1, 1, f_trunc},
952#endif
953 {"type", 1, 1, f_type},
954 {"undofile", 1, 1, f_undofile},
955 {"undotree", 0, 0, f_undotree},
956 {"uniq", 1, 3, f_uniq},
957 {"values", 1, 1, f_values},
958 {"virtcol", 1, 1, f_virtcol},
959 {"visualmode", 0, 1, f_visualmode},
960 {"wildmenumode", 0, 0, f_wildmenumode},
961 {"win_findbuf", 1, 1, f_win_findbuf},
962 {"win_getid", 0, 2, f_win_getid},
963 {"win_gotoid", 1, 1, f_win_gotoid},
964 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
965 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100966 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200967 {"winbufnr", 1, 1, f_winbufnr},
968 {"wincol", 0, 0, f_wincol},
969 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200970 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"winline", 0, 0, f_winline},
972 {"winnr", 0, 1, f_winnr},
973 {"winrestcmd", 0, 0, f_winrestcmd},
974 {"winrestview", 1, 1, f_winrestview},
975 {"winsaveview", 0, 0, f_winsaveview},
976 {"winwidth", 1, 1, f_winwidth},
977 {"wordcount", 0, 0, f_wordcount},
978 {"writefile", 2, 3, f_writefile},
979 {"xor", 2, 2, f_xor},
980};
981
982#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
983
984/*
985 * Function given to ExpandGeneric() to obtain the list of internal
986 * or user defined function names.
987 */
988 char_u *
989get_function_name(expand_T *xp, int idx)
990{
991 static int intidx = -1;
992 char_u *name;
993
994 if (idx == 0)
995 intidx = -1;
996 if (intidx < 0)
997 {
998 name = get_user_func_name(xp, idx);
999 if (name != NULL)
1000 return name;
1001 }
1002 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1003 {
1004 STRCPY(IObuff, functions[intidx].f_name);
1005 STRCAT(IObuff, "(");
1006 if (functions[intidx].f_max_argc == 0)
1007 STRCAT(IObuff, ")");
1008 return IObuff;
1009 }
1010
1011 return NULL;
1012}
1013
1014/*
1015 * Function given to ExpandGeneric() to obtain the list of internal or
1016 * user defined variable or function names.
1017 */
1018 char_u *
1019get_expr_name(expand_T *xp, int idx)
1020{
1021 static int intidx = -1;
1022 char_u *name;
1023
1024 if (idx == 0)
1025 intidx = -1;
1026 if (intidx < 0)
1027 {
1028 name = get_function_name(xp, idx);
1029 if (name != NULL)
1030 return name;
1031 }
1032 return get_user_var_name(xp, ++intidx);
1033}
1034
1035#endif /* FEAT_CMDL_COMPL */
1036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037/*
1038 * Find internal function in table above.
1039 * Return index, or -1 if not found
1040 */
1041 int
1042find_internal_func(
1043 char_u *name) /* name of the function */
1044{
1045 int first = 0;
1046 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1047 int cmp;
1048 int x;
1049
1050 /*
1051 * Find the function name in the table. Binary search.
1052 */
1053 while (first <= last)
1054 {
1055 x = first + ((unsigned)(last - first) >> 1);
1056 cmp = STRCMP(name, functions[x].f_name);
1057 if (cmp < 0)
1058 last = x - 1;
1059 else if (cmp > 0)
1060 first = x + 1;
1061 else
1062 return x;
1063 }
1064 return -1;
1065}
1066
1067 int
1068call_internal_func(
1069 char_u *name,
1070 int argcount,
1071 typval_T *argvars,
1072 typval_T *rettv)
1073{
1074 int i;
1075
1076 i = find_internal_func(name);
1077 if (i < 0)
1078 return ERROR_UNKNOWN;
1079 if (argcount < functions[i].f_min_argc)
1080 return ERROR_TOOFEW;
1081 if (argcount > functions[i].f_max_argc)
1082 return ERROR_TOOMANY;
1083 argvars[argcount].v_type = VAR_UNKNOWN;
1084 functions[i].f_func(argvars, rettv);
1085 return ERROR_NONE;
1086}
1087
1088/*
1089 * Return TRUE for a non-zero Number and a non-empty String.
1090 */
1091 static int
1092non_zero_arg(typval_T *argvars)
1093{
1094 return ((argvars[0].v_type == VAR_NUMBER
1095 && argvars[0].vval.v_number != 0)
1096 || (argvars[0].v_type == VAR_SPECIAL
1097 && argvars[0].vval.v_number == VVAL_TRUE)
1098 || (argvars[0].v_type == VAR_STRING
1099 && argvars[0].vval.v_string != NULL
1100 && *argvars[0].vval.v_string != NUL));
1101}
1102
1103/*
1104 * Get the lnum from the first argument.
1105 * Also accepts ".", "$", etc., but that only works for the current buffer.
1106 * Returns -1 on error.
1107 */
1108 static linenr_T
1109get_tv_lnum(typval_T *argvars)
1110{
1111 typval_T rettv;
1112 linenr_T lnum;
1113
1114 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1115 if (lnum == 0) /* no valid number, try using line() */
1116 {
1117 rettv.v_type = VAR_NUMBER;
1118 f_line(argvars, &rettv);
1119 lnum = (linenr_T)rettv.vval.v_number;
1120 clear_tv(&rettv);
1121 }
1122 return lnum;
1123}
1124
1125#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126/*
1127 * Get the float value of "argvars[0]" into "f".
1128 * Returns FAIL when the argument is not a Number or Float.
1129 */
1130 static int
1131get_float_arg(typval_T *argvars, float_T *f)
1132{
1133 if (argvars[0].v_type == VAR_FLOAT)
1134 {
1135 *f = argvars[0].vval.v_float;
1136 return OK;
1137 }
1138 if (argvars[0].v_type == VAR_NUMBER)
1139 {
1140 *f = (float_T)argvars[0].vval.v_number;
1141 return OK;
1142 }
1143 EMSG(_("E808: Number or Float required"));
1144 return FAIL;
1145}
1146
1147/*
1148 * "abs(expr)" function
1149 */
1150 static void
1151f_abs(typval_T *argvars, typval_T *rettv)
1152{
1153 if (argvars[0].v_type == VAR_FLOAT)
1154 {
1155 rettv->v_type = VAR_FLOAT;
1156 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1157 }
1158 else
1159 {
1160 varnumber_T n;
1161 int error = FALSE;
1162
1163 n = get_tv_number_chk(&argvars[0], &error);
1164 if (error)
1165 rettv->vval.v_number = -1;
1166 else if (n > 0)
1167 rettv->vval.v_number = n;
1168 else
1169 rettv->vval.v_number = -n;
1170 }
1171}
1172
1173/*
1174 * "acos()" function
1175 */
1176 static void
1177f_acos(typval_T *argvars, typval_T *rettv)
1178{
1179 float_T f = 0.0;
1180
1181 rettv->v_type = VAR_FLOAT;
1182 if (get_float_arg(argvars, &f) == OK)
1183 rettv->vval.v_float = acos(f);
1184 else
1185 rettv->vval.v_float = 0.0;
1186}
1187#endif
1188
1189/*
1190 * "add(list, item)" function
1191 */
1192 static void
1193f_add(typval_T *argvars, typval_T *rettv)
1194{
1195 list_T *l;
1196
1197 rettv->vval.v_number = 1; /* Default: Failed */
1198 if (argvars[0].v_type == VAR_LIST)
1199 {
1200 if ((l = argvars[0].vval.v_list) != NULL
1201 && !tv_check_lock(l->lv_lock,
1202 (char_u *)N_("add() argument"), TRUE)
1203 && list_append_tv(l, &argvars[1]) == OK)
1204 copy_tv(&argvars[0], rettv);
1205 }
1206 else
1207 EMSG(_(e_listreq));
1208}
1209
1210/*
1211 * "and(expr, expr)" function
1212 */
1213 static void
1214f_and(typval_T *argvars, typval_T *rettv)
1215{
1216 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1217 & get_tv_number_chk(&argvars[1], NULL);
1218}
1219
1220/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001221 * Get the lnum from the first argument.
1222 * Also accepts "$", then "buf" is used.
1223 * Returns 0 on error.
1224 */
1225 static linenr_T
1226get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1227{
1228 if (argvars[0].v_type == VAR_STRING
1229 && argvars[0].vval.v_string != NULL
1230 && argvars[0].vval.v_string[0] == '$'
1231 && buf != NULL)
1232 return buf->b_ml.ml_line_count;
1233 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1234}
1235
1236/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001237 * If there is a window for "curbuf", make it the current window.
1238 */
1239 static void
1240find_win_for_curbuf(void)
1241{
1242 wininfo_T *wip;
1243
1244 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1245 {
1246 if (wip->wi_win != NULL)
1247 {
1248 curwin = wip->wi_win;
1249 break;
1250 }
1251 }
1252}
1253
1254/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001255 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256 */
1257 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001258set_buffer_lines(
1259 buf_T *buf,
1260 linenr_T lnum_arg,
1261 int append,
1262 typval_T *lines,
1263 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264{
Bram Moolenaarca851592018-06-06 21:04:07 +02001265 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1266 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 list_T *l = NULL;
1268 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 linenr_T append_lnum;
1271 buf_T *curbuf_save = NULL;
1272 win_T *curwin_save = NULL;
1273 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274
Bram Moolenaarca851592018-06-06 21:04:07 +02001275 /* When using the current buffer ml_mfp will be set if needed. Useful when
1276 * setline() is used on startup. For other buffers the buffer must be
1277 * loaded. */
1278 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001279 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001280 rettv->vval.v_number = 1; /* FAIL */
1281 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 }
1283
Bram Moolenaarca851592018-06-06 21:04:07 +02001284 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001286 curbuf_save = curbuf;
1287 curwin_save = curwin;
1288 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001289 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001290 }
1291
1292 if (append)
1293 // appendbufline() uses the line number below which we insert
1294 append_lnum = lnum - 1;
1295 else
1296 // setbufline() uses the line number above which we insert, we only
1297 // append if it's below the last line
1298 append_lnum = curbuf->b_ml.ml_line_count;
1299
1300 if (lines->v_type == VAR_LIST)
1301 {
1302 l = lines->vval.v_list;
1303 li = l->lv_first;
1304 }
1305 else
1306 line = get_tv_string_chk(lines);
1307
1308 /* default result is zero == OK */
1309 for (;;)
1310 {
1311 if (l != NULL)
1312 {
1313 /* list argument, get next string */
1314 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 li = li->li_next;
1318 }
1319
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 rettv->vval.v_number = 1; /* FAIL */
1321 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1322 break;
1323
1324 /* When coming here from Insert mode, sync undo, so that this can be
1325 * undone separately from what was previously inserted. */
1326 if (u_sync_once == 2)
1327 {
1328 u_sync_once = 1; /* notify that u_sync() was called */
1329 u_sync(TRUE);
1330 }
1331
1332 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1333 {
1334 /* existing line, replace it */
1335 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1336 {
1337 changed_bytes(lnum, 0);
1338 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1339 check_cursor_col();
1340 rettv->vval.v_number = 0; /* OK */
1341 }
1342 }
1343 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1344 {
1345 /* append the line */
1346 ++added;
1347 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1348 rettv->vval.v_number = 0; /* OK */
1349 }
1350
1351 if (l == NULL) /* only one string argument */
1352 break;
1353 ++lnum;
1354 }
1355
1356 if (added > 0)
1357 {
1358 win_T *wp;
1359 tabpage_T *tp;
1360
1361 appended_lines_mark(append_lnum, added);
1362 FOR_ALL_TAB_WINDOWS(tp, wp)
1363 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1364 wp->w_cursor.lnum += added;
1365 check_cursor_col();
1366
Bram Moolenaarf2732452018-06-03 14:47:35 +02001367#ifdef FEAT_JOB_CHANNEL
1368 if (bt_prompt(curbuf) && (State & INSERT))
1369 // show the line with the prompt
1370 update_topline();
1371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001373
1374 if (!is_curbuf)
1375 {
1376 curbuf = curbuf_save;
1377 curwin = curwin_save;
1378 }
1379}
1380
1381/*
1382 * "append(lnum, string/list)" function
1383 */
1384 static void
1385f_append(typval_T *argvars, typval_T *rettv)
1386{
1387 linenr_T lnum = get_tv_lnum(&argvars[0]);
1388
1389 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1390}
1391
1392/*
1393 * "appendbufline(buf, lnum, string/list)" function
1394 */
1395 static void
1396f_appendbufline(typval_T *argvars, typval_T *rettv)
1397{
1398 linenr_T lnum;
1399 buf_T *buf;
1400
1401 buf = get_buf_tv(&argvars[0], FALSE);
1402 if (buf == NULL)
1403 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001405 {
1406 lnum = get_tv_lnum_buf(&argvars[1], buf);
1407 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1408 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001409}
1410
1411/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001412 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 */
1414 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001415f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001416{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001417 win_T *wp;
1418
1419 if (argvars[0].v_type == VAR_UNKNOWN)
1420 // use the current window
1421 rettv->vval.v_number = ARGCOUNT;
1422 else if (argvars[0].v_type == VAR_NUMBER
1423 && get_tv_number(&argvars[0]) == -1)
1424 // use the global argument list
1425 rettv->vval.v_number = GARGCOUNT;
1426 else
1427 {
1428 // use the argument list of the specified window
1429 wp = find_win_by_nr_or_id(&argvars[0]);
1430 if (wp != NULL)
1431 rettv->vval.v_number = WARGCOUNT(wp);
1432 else
1433 rettv->vval.v_number = -1;
1434 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435}
1436
1437/*
1438 * "argidx()" function
1439 */
1440 static void
1441f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1442{
1443 rettv->vval.v_number = curwin->w_arg_idx;
1444}
1445
1446/*
1447 * "arglistid()" function
1448 */
1449 static void
1450f_arglistid(typval_T *argvars, typval_T *rettv)
1451{
1452 win_T *wp;
1453
1454 rettv->vval.v_number = -1;
1455 wp = find_tabwin(&argvars[0], &argvars[1]);
1456 if (wp != NULL)
1457 rettv->vval.v_number = wp->w_alist->id;
1458}
1459
1460/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001461 * Get the argument list for a given window
1462 */
1463 static void
1464get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1465{
1466 int idx;
1467
1468 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1469 for (idx = 0; idx < argcount; ++idx)
1470 list_append_string(rettv->vval.v_list,
1471 alist_name(&arglist[idx]), -1);
1472}
1473
1474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001475 * "argv(nr)" function
1476 */
1477 static void
1478f_argv(typval_T *argvars, typval_T *rettv)
1479{
1480 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001481 aentry_T *arglist = NULL;
1482 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001483
1484 if (argvars[0].v_type != VAR_UNKNOWN)
1485 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001486 if (argvars[1].v_type == VAR_UNKNOWN)
1487 {
1488 arglist = ARGLIST;
1489 argcount = ARGCOUNT;
1490 }
1491 else if (argvars[1].v_type == VAR_NUMBER
1492 && get_tv_number(&argvars[1]) == -1)
1493 {
1494 arglist = GARGLIST;
1495 argcount = GARGCOUNT;
1496 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001497 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 {
1499 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1500
1501 if (wp != NULL)
1502 {
1503 /* Use the argument list of the specified window */
1504 arglist = WARGLIST(wp);
1505 argcount = WARGCOUNT(wp);
1506 }
1507 }
1508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 rettv->vval.v_string = NULL;
1511 idx = get_tv_number_chk(&argvars[0], NULL);
1512 if (arglist != NULL && idx >= 0 && idx < argcount)
1513 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1514 else if (idx == -1)
1515 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001517 else
1518 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519}
1520
1521/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001522 * "assert_beeps(cmd [, error])" function
1523 */
1524 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001525f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001526{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001527 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001528}
1529
1530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531 * "assert_equal(expected, actual[, msg])" function
1532 */
1533 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001534f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001536 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537}
1538
1539/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001540 * "assert_equalfile(fname-one, fname-two)" function
1541 */
1542 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001543f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001544{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001545 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001546}
1547
1548/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 * "assert_notequal(expected, actual[, msg])" function
1550 */
1551 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001552f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001553{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001554 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001555}
1556
1557/*
1558 * "assert_exception(string[, msg])" function
1559 */
1560 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001561f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001562{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001563 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564}
1565
1566/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001567 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 */
1569 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001570f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001572 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573}
1574
1575/*
1576 * "assert_false(actual[, msg])" function
1577 */
1578 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001579f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001581 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582}
1583
1584/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001585 * "assert_inrange(lower, upper[, msg])" function
1586 */
1587 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001588f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001589{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001590 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001591}
1592
1593/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 * "assert_match(pattern, actual[, msg])" function
1595 */
1596 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001597f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001599 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600}
1601
1602/*
1603 * "assert_notmatch(pattern, actual[, msg])" function
1604 */
1605 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001606f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001608 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609}
1610
1611/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001612 * "assert_report(msg)" function
1613 */
1614 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001616{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001618}
1619
1620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 * "assert_true(actual[, msg])" function
1622 */
1623 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627}
1628
1629#ifdef FEAT_FLOAT
1630/*
1631 * "asin()" function
1632 */
1633 static void
1634f_asin(typval_T *argvars, typval_T *rettv)
1635{
1636 float_T f = 0.0;
1637
1638 rettv->v_type = VAR_FLOAT;
1639 if (get_float_arg(argvars, &f) == OK)
1640 rettv->vval.v_float = asin(f);
1641 else
1642 rettv->vval.v_float = 0.0;
1643}
1644
1645/*
1646 * "atan()" function
1647 */
1648 static void
1649f_atan(typval_T *argvars, typval_T *rettv)
1650{
1651 float_T f = 0.0;
1652
1653 rettv->v_type = VAR_FLOAT;
1654 if (get_float_arg(argvars, &f) == OK)
1655 rettv->vval.v_float = atan(f);
1656 else
1657 rettv->vval.v_float = 0.0;
1658}
1659
1660/*
1661 * "atan2()" function
1662 */
1663 static void
1664f_atan2(typval_T *argvars, typval_T *rettv)
1665{
1666 float_T fx = 0.0, fy = 0.0;
1667
1668 rettv->v_type = VAR_FLOAT;
1669 if (get_float_arg(argvars, &fx) == OK
1670 && get_float_arg(&argvars[1], &fy) == OK)
1671 rettv->vval.v_float = atan2(fx, fy);
1672 else
1673 rettv->vval.v_float = 0.0;
1674}
1675#endif
1676
1677/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001678 * "balloon_show()" function
1679 */
1680#ifdef FEAT_BEVAL
1681 static void
1682f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1683{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001684 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001685 {
1686 if (argvars[0].v_type == VAR_LIST
1687# ifdef FEAT_GUI
1688 && !gui.in_use
1689# endif
1690 )
1691 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1692 else
1693 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1694 }
1695}
1696
Bram Moolenaar669a8282017-11-19 20:13:05 +01001697# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001698 static void
1699f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1700{
1701 if (rettv_list_alloc(rettv) == OK)
1702 {
1703 char_u *msg = get_tv_string_chk(&argvars[0]);
1704
1705 if (msg != NULL)
1706 {
1707 pumitem_T *array;
1708 int size = split_message(msg, &array);
1709 int i;
1710
1711 /* Skip the first and last item, they are always empty. */
1712 for (i = 1; i < size - 1; ++i)
1713 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001714 while (size > 0)
1715 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001716 vim_free(array);
1717 }
1718 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001719}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001720# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001721#endif
1722
1723/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 * "browse(save, title, initdir, default)" function
1725 */
1726 static void
1727f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1728{
1729#ifdef FEAT_BROWSE
1730 int save;
1731 char_u *title;
1732 char_u *initdir;
1733 char_u *defname;
1734 char_u buf[NUMBUFLEN];
1735 char_u buf2[NUMBUFLEN];
1736 int error = FALSE;
1737
1738 save = (int)get_tv_number_chk(&argvars[0], &error);
1739 title = get_tv_string_chk(&argvars[1]);
1740 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1741 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1742
1743 if (error || title == NULL || initdir == NULL || defname == NULL)
1744 rettv->vval.v_string = NULL;
1745 else
1746 rettv->vval.v_string =
1747 do_browse(save ? BROWSE_SAVE : 0,
1748 title, defname, NULL, initdir, NULL, curbuf);
1749#else
1750 rettv->vval.v_string = NULL;
1751#endif
1752 rettv->v_type = VAR_STRING;
1753}
1754
1755/*
1756 * "browsedir(title, initdir)" function
1757 */
1758 static void
1759f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1760{
1761#ifdef FEAT_BROWSE
1762 char_u *title;
1763 char_u *initdir;
1764 char_u buf[NUMBUFLEN];
1765
1766 title = get_tv_string_chk(&argvars[0]);
1767 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1768
1769 if (title == NULL || initdir == NULL)
1770 rettv->vval.v_string = NULL;
1771 else
1772 rettv->vval.v_string = do_browse(BROWSE_DIR,
1773 title, NULL, NULL, initdir, NULL, curbuf);
1774#else
1775 rettv->vval.v_string = NULL;
1776#endif
1777 rettv->v_type = VAR_STRING;
1778}
1779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780/*
1781 * Find a buffer by number or exact name.
1782 */
1783 static buf_T *
1784find_buffer(typval_T *avar)
1785{
1786 buf_T *buf = NULL;
1787
1788 if (avar->v_type == VAR_NUMBER)
1789 buf = buflist_findnr((int)avar->vval.v_number);
1790 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1791 {
1792 buf = buflist_findname_exp(avar->vval.v_string);
1793 if (buf == NULL)
1794 {
1795 /* No full path name match, try a match with a URL or a "nofile"
1796 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001797 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001798 if (buf->b_fname != NULL
1799 && (path_with_url(buf->b_fname)
1800#ifdef FEAT_QUICKFIX
1801 || bt_nofile(buf)
1802#endif
1803 )
1804 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1805 break;
1806 }
1807 }
1808 return buf;
1809}
1810
1811/*
1812 * "bufexists(expr)" function
1813 */
1814 static void
1815f_bufexists(typval_T *argvars, typval_T *rettv)
1816{
1817 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1818}
1819
1820/*
1821 * "buflisted(expr)" function
1822 */
1823 static void
1824f_buflisted(typval_T *argvars, typval_T *rettv)
1825{
1826 buf_T *buf;
1827
1828 buf = find_buffer(&argvars[0]);
1829 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1830}
1831
1832/*
1833 * "bufloaded(expr)" function
1834 */
1835 static void
1836f_bufloaded(typval_T *argvars, typval_T *rettv)
1837{
1838 buf_T *buf;
1839
1840 buf = find_buffer(&argvars[0]);
1841 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1842}
1843
1844 buf_T *
1845buflist_find_by_name(char_u *name, int curtab_only)
1846{
1847 int save_magic;
1848 char_u *save_cpo;
1849 buf_T *buf;
1850
1851 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1852 save_magic = p_magic;
1853 p_magic = TRUE;
1854 save_cpo = p_cpo;
1855 p_cpo = (char_u *)"";
1856
1857 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1858 TRUE, FALSE, curtab_only));
1859
1860 p_magic = save_magic;
1861 p_cpo = save_cpo;
1862 return buf;
1863}
1864
1865/*
1866 * Get buffer by number or pattern.
1867 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001868 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869get_buf_tv(typval_T *tv, int curtab_only)
1870{
1871 char_u *name = tv->vval.v_string;
1872 buf_T *buf;
1873
1874 if (tv->v_type == VAR_NUMBER)
1875 return buflist_findnr((int)tv->vval.v_number);
1876 if (tv->v_type != VAR_STRING)
1877 return NULL;
1878 if (name == NULL || *name == NUL)
1879 return curbuf;
1880 if (name[0] == '$' && name[1] == NUL)
1881 return lastbuf;
1882
1883 buf = buflist_find_by_name(name, curtab_only);
1884
1885 /* If not found, try expanding the name, like done for bufexists(). */
1886 if (buf == NULL)
1887 buf = find_buffer(tv);
1888
1889 return buf;
1890}
1891
1892/*
1893 * "bufname(expr)" function
1894 */
1895 static void
1896f_bufname(typval_T *argvars, typval_T *rettv)
1897{
1898 buf_T *buf;
1899
1900 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1901 ++emsg_off;
1902 buf = get_buf_tv(&argvars[0], FALSE);
1903 rettv->v_type = VAR_STRING;
1904 if (buf != NULL && buf->b_fname != NULL)
1905 rettv->vval.v_string = vim_strsave(buf->b_fname);
1906 else
1907 rettv->vval.v_string = NULL;
1908 --emsg_off;
1909}
1910
1911/*
1912 * "bufnr(expr)" function
1913 */
1914 static void
1915f_bufnr(typval_T *argvars, typval_T *rettv)
1916{
1917 buf_T *buf;
1918 int error = FALSE;
1919 char_u *name;
1920
1921 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1922 ++emsg_off;
1923 buf = get_buf_tv(&argvars[0], FALSE);
1924 --emsg_off;
1925
1926 /* If the buffer isn't found and the second argument is not zero create a
1927 * new buffer. */
1928 if (buf == NULL
1929 && argvars[1].v_type != VAR_UNKNOWN
1930 && get_tv_number_chk(&argvars[1], &error) != 0
1931 && !error
1932 && (name = get_tv_string_chk(&argvars[0])) != NULL
1933 && !error)
1934 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1935
1936 if (buf != NULL)
1937 rettv->vval.v_number = buf->b_fnum;
1938 else
1939 rettv->vval.v_number = -1;
1940}
1941
1942 static void
1943buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1944{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 win_T *wp;
1946 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947 buf_T *buf;
1948
1949 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1950 ++emsg_off;
1951 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001952 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 {
1954 ++winnr;
1955 if (wp->w_buffer == buf)
1956 break;
1957 }
1958 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 --emsg_off;
1960}
1961
1962/*
1963 * "bufwinid(nr)" function
1964 */
1965 static void
1966f_bufwinid(typval_T *argvars, typval_T *rettv)
1967{
1968 buf_win_common(argvars, rettv, FALSE);
1969}
1970
1971/*
1972 * "bufwinnr(nr)" function
1973 */
1974 static void
1975f_bufwinnr(typval_T *argvars, typval_T *rettv)
1976{
1977 buf_win_common(argvars, rettv, TRUE);
1978}
1979
1980/*
1981 * "byte2line(byte)" function
1982 */
1983 static void
1984f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1985{
1986#ifndef FEAT_BYTEOFF
1987 rettv->vval.v_number = -1;
1988#else
1989 long boff = 0;
1990
1991 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1992 if (boff < 0)
1993 rettv->vval.v_number = -1;
1994 else
1995 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1996 (linenr_T)0, &boff);
1997#endif
1998}
1999
2000 static void
2001byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2002{
2003#ifdef FEAT_MBYTE
2004 char_u *t;
2005#endif
2006 char_u *str;
2007 varnumber_T idx;
2008
2009 str = get_tv_string_chk(&argvars[0]);
2010 idx = get_tv_number_chk(&argvars[1], NULL);
2011 rettv->vval.v_number = -1;
2012 if (str == NULL || idx < 0)
2013 return;
2014
2015#ifdef FEAT_MBYTE
2016 t = str;
2017 for ( ; idx > 0; idx--)
2018 {
2019 if (*t == NUL) /* EOL reached */
2020 return;
2021 if (enc_utf8 && comp)
2022 t += utf_ptr2len(t);
2023 else
2024 t += (*mb_ptr2len)(t);
2025 }
2026 rettv->vval.v_number = (varnumber_T)(t - str);
2027#else
2028 if ((size_t)idx <= STRLEN(str))
2029 rettv->vval.v_number = idx;
2030#endif
2031}
2032
2033/*
2034 * "byteidx()" function
2035 */
2036 static void
2037f_byteidx(typval_T *argvars, typval_T *rettv)
2038{
2039 byteidx(argvars, rettv, FALSE);
2040}
2041
2042/*
2043 * "byteidxcomp()" function
2044 */
2045 static void
2046f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2047{
2048 byteidx(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "call(func, arglist [, dict])" function
2053 */
2054 static void
2055f_call(typval_T *argvars, typval_T *rettv)
2056{
2057 char_u *func;
2058 partial_T *partial = NULL;
2059 dict_T *selfdict = NULL;
2060
2061 if (argvars[1].v_type != VAR_LIST)
2062 {
2063 EMSG(_(e_listreq));
2064 return;
2065 }
2066 if (argvars[1].vval.v_list == NULL)
2067 return;
2068
2069 if (argvars[0].v_type == VAR_FUNC)
2070 func = argvars[0].vval.v_string;
2071 else if (argvars[0].v_type == VAR_PARTIAL)
2072 {
2073 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002074 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 }
2076 else
2077 func = get_tv_string(&argvars[0]);
2078 if (*func == NUL)
2079 return; /* type error or empty name */
2080
2081 if (argvars[2].v_type != VAR_UNKNOWN)
2082 {
2083 if (argvars[2].v_type != VAR_DICT)
2084 {
2085 EMSG(_(e_dictreq));
2086 return;
2087 }
2088 selfdict = argvars[2].vval.v_dict;
2089 }
2090
2091 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2092}
2093
2094#ifdef FEAT_FLOAT
2095/*
2096 * "ceil({float})" function
2097 */
2098 static void
2099f_ceil(typval_T *argvars, typval_T *rettv)
2100{
2101 float_T f = 0.0;
2102
2103 rettv->v_type = VAR_FLOAT;
2104 if (get_float_arg(argvars, &f) == OK)
2105 rettv->vval.v_float = ceil(f);
2106 else
2107 rettv->vval.v_float = 0.0;
2108}
2109#endif
2110
2111#ifdef FEAT_JOB_CHANNEL
2112/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002113 * "ch_canread()" function
2114 */
2115 static void
2116f_ch_canread(typval_T *argvars, typval_T *rettv)
2117{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002118 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002119
2120 rettv->vval.v_number = 0;
2121 if (channel != NULL)
2122 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2123 || channel_has_readahead(channel, PART_OUT)
2124 || channel_has_readahead(channel, PART_ERR);
2125}
2126
2127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 * "ch_close()" function
2129 */
2130 static void
2131f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2132{
2133 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2134
2135 if (channel != NULL)
2136 {
2137 channel_close(channel, FALSE);
2138 channel_clear(channel);
2139 }
2140}
2141
2142/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002143 * "ch_close()" function
2144 */
2145 static void
2146f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2147{
2148 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2149
2150 if (channel != NULL)
2151 channel_close_in(channel);
2152}
2153
2154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 * "ch_getbufnr()" function
2156 */
2157 static void
2158f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2159{
2160 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2161
2162 rettv->vval.v_number = -1;
2163 if (channel != NULL)
2164 {
2165 char_u *what = get_tv_string(&argvars[1]);
2166 int part;
2167
2168 if (STRCMP(what, "err") == 0)
2169 part = PART_ERR;
2170 else if (STRCMP(what, "out") == 0)
2171 part = PART_OUT;
2172 else if (STRCMP(what, "in") == 0)
2173 part = PART_IN;
2174 else
2175 part = PART_SOCK;
2176 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2177 rettv->vval.v_number =
2178 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2179 }
2180}
2181
2182/*
2183 * "ch_getjob()" function
2184 */
2185 static void
2186f_ch_getjob(typval_T *argvars, typval_T *rettv)
2187{
2188 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2189
2190 if (channel != NULL)
2191 {
2192 rettv->v_type = VAR_JOB;
2193 rettv->vval.v_job = channel->ch_job;
2194 if (channel->ch_job != NULL)
2195 ++channel->ch_job->jv_refcount;
2196 }
2197}
2198
2199/*
2200 * "ch_info()" function
2201 */
2202 static void
2203f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2204{
2205 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2206
2207 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2208 channel_info(channel, rettv->vval.v_dict);
2209}
2210
2211/*
2212 * "ch_log()" function
2213 */
2214 static void
2215f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2216{
2217 char_u *msg = get_tv_string(&argvars[0]);
2218 channel_T *channel = NULL;
2219
2220 if (argvars[1].v_type != VAR_UNKNOWN)
2221 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2222
Bram Moolenaard5359b22018-04-05 22:44:39 +02002223 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224}
2225
2226/*
2227 * "ch_logfile()" function
2228 */
2229 static void
2230f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2231{
2232 char_u *fname;
2233 char_u *opt = (char_u *)"";
2234 char_u buf[NUMBUFLEN];
2235
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002236 /* Don't open a file in restricted mode. */
2237 if (check_restricted() || check_secure())
2238 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239 fname = get_tv_string(&argvars[0]);
2240 if (argvars[1].v_type == VAR_STRING)
2241 opt = get_tv_string_buf(&argvars[1], buf);
2242 ch_logfile(fname, opt);
2243}
2244
2245/*
2246 * "ch_open()" function
2247 */
2248 static void
2249f_ch_open(typval_T *argvars, typval_T *rettv)
2250{
2251 rettv->v_type = VAR_CHANNEL;
2252 if (check_restricted() || check_secure())
2253 return;
2254 rettv->vval.v_channel = channel_open_func(argvars);
2255}
2256
2257/*
2258 * "ch_read()" function
2259 */
2260 static void
2261f_ch_read(typval_T *argvars, typval_T *rettv)
2262{
2263 common_channel_read(argvars, rettv, FALSE);
2264}
2265
2266/*
2267 * "ch_readraw()" function
2268 */
2269 static void
2270f_ch_readraw(typval_T *argvars, typval_T *rettv)
2271{
2272 common_channel_read(argvars, rettv, TRUE);
2273}
2274
2275/*
2276 * "ch_evalexpr()" function
2277 */
2278 static void
2279f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2280{
2281 ch_expr_common(argvars, rettv, TRUE);
2282}
2283
2284/*
2285 * "ch_sendexpr()" function
2286 */
2287 static void
2288f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2289{
2290 ch_expr_common(argvars, rettv, FALSE);
2291}
2292
2293/*
2294 * "ch_evalraw()" function
2295 */
2296 static void
2297f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2298{
2299 ch_raw_common(argvars, rettv, TRUE);
2300}
2301
2302/*
2303 * "ch_sendraw()" function
2304 */
2305 static void
2306f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2307{
2308 ch_raw_common(argvars, rettv, FALSE);
2309}
2310
2311/*
2312 * "ch_setoptions()" function
2313 */
2314 static void
2315f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2316{
2317 channel_T *channel;
2318 jobopt_T opt;
2319
2320 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2321 if (channel == NULL)
2322 return;
2323 clear_job_options(&opt);
2324 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002325 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 channel_set_options(channel, &opt);
2327 free_job_options(&opt);
2328}
2329
2330/*
2331 * "ch_status()" function
2332 */
2333 static void
2334f_ch_status(typval_T *argvars, typval_T *rettv)
2335{
2336 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002337 jobopt_T opt;
2338 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339
2340 /* return an empty string by default */
2341 rettv->v_type = VAR_STRING;
2342 rettv->vval.v_string = NULL;
2343
2344 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002345
2346 if (argvars[1].v_type != VAR_UNKNOWN)
2347 {
2348 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002349 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002350 && (opt.jo_set & JO_PART))
2351 part = opt.jo_part;
2352 }
2353
2354 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355}
2356#endif
2357
2358/*
2359 * "changenr()" function
2360 */
2361 static void
2362f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2363{
2364 rettv->vval.v_number = curbuf->b_u_seq_cur;
2365}
2366
2367/*
2368 * "char2nr(string)" function
2369 */
2370 static void
2371f_char2nr(typval_T *argvars, typval_T *rettv)
2372{
2373#ifdef FEAT_MBYTE
2374 if (has_mbyte)
2375 {
2376 int utf8 = 0;
2377
2378 if (argvars[1].v_type != VAR_UNKNOWN)
2379 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2380
2381 if (utf8)
2382 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2383 else
2384 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2385 }
2386 else
2387#endif
2388 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2389}
2390
2391/*
2392 * "cindent(lnum)" function
2393 */
2394 static void
2395f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2396{
2397#ifdef FEAT_CINDENT
2398 pos_T pos;
2399 linenr_T lnum;
2400
2401 pos = curwin->w_cursor;
2402 lnum = get_tv_lnum(argvars);
2403 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2404 {
2405 curwin->w_cursor.lnum = lnum;
2406 rettv->vval.v_number = get_c_indent();
2407 curwin->w_cursor = pos;
2408 }
2409 else
2410#endif
2411 rettv->vval.v_number = -1;
2412}
2413
2414/*
2415 * "clearmatches()" function
2416 */
2417 static void
2418f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2419{
2420#ifdef FEAT_SEARCH_EXTRA
2421 clear_matches(curwin);
2422#endif
2423}
2424
2425/*
2426 * "col(string)" function
2427 */
2428 static void
2429f_col(typval_T *argvars, typval_T *rettv)
2430{
2431 colnr_T col = 0;
2432 pos_T *fp;
2433 int fnum = curbuf->b_fnum;
2434
2435 fp = var2fpos(&argvars[0], FALSE, &fnum);
2436 if (fp != NULL && fnum == curbuf->b_fnum)
2437 {
2438 if (fp->col == MAXCOL)
2439 {
2440 /* '> can be MAXCOL, get the length of the line then */
2441 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2442 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2443 else
2444 col = MAXCOL;
2445 }
2446 else
2447 {
2448 col = fp->col + 1;
2449#ifdef FEAT_VIRTUALEDIT
2450 /* col(".") when the cursor is on the NUL at the end of the line
2451 * because of "coladd" can be seen as an extra column. */
2452 if (virtual_active() && fp == &curwin->w_cursor)
2453 {
2454 char_u *p = ml_get_cursor();
2455
2456 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2457 curwin->w_virtcol - curwin->w_cursor.coladd))
2458 {
2459# ifdef FEAT_MBYTE
2460 int l;
2461
2462 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2463 col += l;
2464# else
2465 if (*p != NUL && p[1] == NUL)
2466 ++col;
2467# endif
2468 }
2469 }
2470#endif
2471 }
2472 }
2473 rettv->vval.v_number = col;
2474}
2475
2476#if defined(FEAT_INS_EXPAND)
2477/*
2478 * "complete()" function
2479 */
2480 static void
2481f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2482{
2483 int startcol;
2484
2485 if ((State & INSERT) == 0)
2486 {
2487 EMSG(_("E785: complete() can only be used in Insert mode"));
2488 return;
2489 }
2490
2491 /* Check for undo allowed here, because if something was already inserted
2492 * the line was already saved for undo and this check isn't done. */
2493 if (!undo_allowed())
2494 return;
2495
2496 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2497 {
2498 EMSG(_(e_invarg));
2499 return;
2500 }
2501
2502 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2503 if (startcol <= 0)
2504 return;
2505
2506 set_completion(startcol - 1, argvars[1].vval.v_list);
2507}
2508
2509/*
2510 * "complete_add()" function
2511 */
2512 static void
2513f_complete_add(typval_T *argvars, typval_T *rettv)
2514{
2515 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2516}
2517
2518/*
2519 * "complete_check()" function
2520 */
2521 static void
2522f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2523{
2524 int saved = RedrawingDisabled;
2525
2526 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002527 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 rettv->vval.v_number = compl_interrupted;
2529 RedrawingDisabled = saved;
2530}
2531#endif
2532
2533/*
2534 * "confirm(message, buttons[, default [, type]])" function
2535 */
2536 static void
2537f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2538{
2539#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2540 char_u *message;
2541 char_u *buttons = NULL;
2542 char_u buf[NUMBUFLEN];
2543 char_u buf2[NUMBUFLEN];
2544 int def = 1;
2545 int type = VIM_GENERIC;
2546 char_u *typestr;
2547 int error = FALSE;
2548
2549 message = get_tv_string_chk(&argvars[0]);
2550 if (message == NULL)
2551 error = TRUE;
2552 if (argvars[1].v_type != VAR_UNKNOWN)
2553 {
2554 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2555 if (buttons == NULL)
2556 error = TRUE;
2557 if (argvars[2].v_type != VAR_UNKNOWN)
2558 {
2559 def = (int)get_tv_number_chk(&argvars[2], &error);
2560 if (argvars[3].v_type != VAR_UNKNOWN)
2561 {
2562 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2563 if (typestr == NULL)
2564 error = TRUE;
2565 else
2566 {
2567 switch (TOUPPER_ASC(*typestr))
2568 {
2569 case 'E': type = VIM_ERROR; break;
2570 case 'Q': type = VIM_QUESTION; break;
2571 case 'I': type = VIM_INFO; break;
2572 case 'W': type = VIM_WARNING; break;
2573 case 'G': type = VIM_GENERIC; break;
2574 }
2575 }
2576 }
2577 }
2578 }
2579
2580 if (buttons == NULL || *buttons == NUL)
2581 buttons = (char_u *)_("&Ok");
2582
2583 if (!error)
2584 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2585 def, NULL, FALSE);
2586#endif
2587}
2588
2589/*
2590 * "copy()" function
2591 */
2592 static void
2593f_copy(typval_T *argvars, typval_T *rettv)
2594{
2595 item_copy(&argvars[0], rettv, FALSE, 0);
2596}
2597
2598#ifdef FEAT_FLOAT
2599/*
2600 * "cos()" function
2601 */
2602 static void
2603f_cos(typval_T *argvars, typval_T *rettv)
2604{
2605 float_T f = 0.0;
2606
2607 rettv->v_type = VAR_FLOAT;
2608 if (get_float_arg(argvars, &f) == OK)
2609 rettv->vval.v_float = cos(f);
2610 else
2611 rettv->vval.v_float = 0.0;
2612}
2613
2614/*
2615 * "cosh()" function
2616 */
2617 static void
2618f_cosh(typval_T *argvars, typval_T *rettv)
2619{
2620 float_T f = 0.0;
2621
2622 rettv->v_type = VAR_FLOAT;
2623 if (get_float_arg(argvars, &f) == OK)
2624 rettv->vval.v_float = cosh(f);
2625 else
2626 rettv->vval.v_float = 0.0;
2627}
2628#endif
2629
2630/*
2631 * "count()" function
2632 */
2633 static void
2634f_count(typval_T *argvars, typval_T *rettv)
2635{
2636 long n = 0;
2637 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002638 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639
Bram Moolenaar9966b212017-07-28 16:46:57 +02002640 if (argvars[2].v_type != VAR_UNKNOWN)
2641 ic = (int)get_tv_number_chk(&argvars[2], &error);
2642
2643 if (argvars[0].v_type == VAR_STRING)
2644 {
2645 char_u *expr = get_tv_string_chk(&argvars[1]);
2646 char_u *p = argvars[0].vval.v_string;
2647 char_u *next;
2648
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002649 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002650 {
2651 if (ic)
2652 {
2653 size_t len = STRLEN(expr);
2654
2655 while (*p != NUL)
2656 {
2657 if (MB_STRNICMP(p, expr, len) == 0)
2658 {
2659 ++n;
2660 p += len;
2661 }
2662 else
2663 MB_PTR_ADV(p);
2664 }
2665 }
2666 else
2667 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2668 != NULL)
2669 {
2670 ++n;
2671 p = next + STRLEN(expr);
2672 }
2673 }
2674
2675 }
2676 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 {
2678 listitem_T *li;
2679 list_T *l;
2680 long idx;
2681
2682 if ((l = argvars[0].vval.v_list) != NULL)
2683 {
2684 li = l->lv_first;
2685 if (argvars[2].v_type != VAR_UNKNOWN)
2686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (argvars[3].v_type != VAR_UNKNOWN)
2688 {
2689 idx = (long)get_tv_number_chk(&argvars[3], &error);
2690 if (!error)
2691 {
2692 li = list_find(l, idx);
2693 if (li == NULL)
2694 EMSGN(_(e_listidx), idx);
2695 }
2696 }
2697 if (error)
2698 li = NULL;
2699 }
2700
2701 for ( ; li != NULL; li = li->li_next)
2702 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2703 ++n;
2704 }
2705 }
2706 else if (argvars[0].v_type == VAR_DICT)
2707 {
2708 int todo;
2709 dict_T *d;
2710 hashitem_T *hi;
2711
2712 if ((d = argvars[0].vval.v_dict) != NULL)
2713 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 if (argvars[2].v_type != VAR_UNKNOWN)
2715 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (argvars[3].v_type != VAR_UNKNOWN)
2717 EMSG(_(e_invarg));
2718 }
2719
2720 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2721 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2722 {
2723 if (!HASHITEM_EMPTY(hi))
2724 {
2725 --todo;
2726 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2727 ++n;
2728 }
2729 }
2730 }
2731 }
2732 else
2733 EMSG2(_(e_listdictarg), "count()");
2734 rettv->vval.v_number = n;
2735}
2736
2737/*
2738 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2739 *
2740 * Checks the existence of a cscope connection.
2741 */
2742 static void
2743f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2744{
2745#ifdef FEAT_CSCOPE
2746 int num = 0;
2747 char_u *dbpath = NULL;
2748 char_u *prepend = NULL;
2749 char_u buf[NUMBUFLEN];
2750
2751 if (argvars[0].v_type != VAR_UNKNOWN
2752 && argvars[1].v_type != VAR_UNKNOWN)
2753 {
2754 num = (int)get_tv_number(&argvars[0]);
2755 dbpath = get_tv_string(&argvars[1]);
2756 if (argvars[2].v_type != VAR_UNKNOWN)
2757 prepend = get_tv_string_buf(&argvars[2], buf);
2758 }
2759
2760 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2761#endif
2762}
2763
2764/*
2765 * "cursor(lnum, col)" function, or
2766 * "cursor(list)"
2767 *
2768 * Moves the cursor to the specified line and column.
2769 * Returns 0 when the position could be set, -1 otherwise.
2770 */
2771 static void
2772f_cursor(typval_T *argvars, typval_T *rettv)
2773{
2774 long line, col;
2775#ifdef FEAT_VIRTUALEDIT
2776 long coladd = 0;
2777#endif
2778 int set_curswant = TRUE;
2779
2780 rettv->vval.v_number = -1;
2781 if (argvars[1].v_type == VAR_UNKNOWN)
2782 {
2783 pos_T pos;
2784 colnr_T curswant = -1;
2785
2786 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2787 {
2788 EMSG(_(e_invarg));
2789 return;
2790 }
2791 line = pos.lnum;
2792 col = pos.col;
2793#ifdef FEAT_VIRTUALEDIT
2794 coladd = pos.coladd;
2795#endif
2796 if (curswant >= 0)
2797 {
2798 curwin->w_curswant = curswant - 1;
2799 set_curswant = FALSE;
2800 }
2801 }
2802 else
2803 {
2804 line = get_tv_lnum(argvars);
2805 col = (long)get_tv_number_chk(&argvars[1], NULL);
2806#ifdef FEAT_VIRTUALEDIT
2807 if (argvars[2].v_type != VAR_UNKNOWN)
2808 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2809#endif
2810 }
2811 if (line < 0 || col < 0
2812#ifdef FEAT_VIRTUALEDIT
2813 || coladd < 0
2814#endif
2815 )
2816 return; /* type error; errmsg already given */
2817 if (line > 0)
2818 curwin->w_cursor.lnum = line;
2819 if (col > 0)
2820 curwin->w_cursor.col = col - 1;
2821#ifdef FEAT_VIRTUALEDIT
2822 curwin->w_cursor.coladd = coladd;
2823#endif
2824
2825 /* Make sure the cursor is in a valid position. */
2826 check_cursor();
2827#ifdef FEAT_MBYTE
2828 /* Correct cursor for multi-byte character. */
2829 if (has_mbyte)
2830 mb_adjust_cursor();
2831#endif
2832
2833 curwin->w_set_curswant = set_curswant;
2834 rettv->vval.v_number = 0;
2835}
2836
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002837#ifdef WIN3264
2838/*
2839 * "debugbreak()" function
2840 */
2841 static void
2842f_debugbreak(typval_T *argvars, typval_T *rettv)
2843{
2844 int pid;
2845
2846 rettv->vval.v_number = FAIL;
2847 pid = (int)get_tv_number(&argvars[0]);
2848 if (pid == 0)
2849 EMSG(_(e_invarg));
2850 else
2851 {
2852 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2853
2854 if (hProcess != NULL)
2855 {
2856 DebugBreakProcess(hProcess);
2857 CloseHandle(hProcess);
2858 rettv->vval.v_number = OK;
2859 }
2860 }
2861}
2862#endif
2863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864/*
2865 * "deepcopy()" function
2866 */
2867 static void
2868f_deepcopy(typval_T *argvars, typval_T *rettv)
2869{
2870 int noref = 0;
2871 int copyID;
2872
2873 if (argvars[1].v_type != VAR_UNKNOWN)
2874 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2875 if (noref < 0 || noref > 1)
2876 EMSG(_(e_invarg));
2877 else
2878 {
2879 copyID = get_copyID();
2880 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2881 }
2882}
2883
2884/*
2885 * "delete()" function
2886 */
2887 static void
2888f_delete(typval_T *argvars, typval_T *rettv)
2889{
2890 char_u nbuf[NUMBUFLEN];
2891 char_u *name;
2892 char_u *flags;
2893
2894 rettv->vval.v_number = -1;
2895 if (check_restricted() || check_secure())
2896 return;
2897
2898 name = get_tv_string(&argvars[0]);
2899 if (name == NULL || *name == NUL)
2900 {
2901 EMSG(_(e_invarg));
2902 return;
2903 }
2904
2905 if (argvars[1].v_type != VAR_UNKNOWN)
2906 flags = get_tv_string_buf(&argvars[1], nbuf);
2907 else
2908 flags = (char_u *)"";
2909
2910 if (*flags == NUL)
2911 /* delete a file */
2912 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2913 else if (STRCMP(flags, "d") == 0)
2914 /* delete an empty directory */
2915 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2916 else if (STRCMP(flags, "rf") == 0)
2917 /* delete a directory recursively */
2918 rettv->vval.v_number = delete_recursive(name);
2919 else
2920 EMSG2(_(e_invexpr2), flags);
2921}
2922
2923/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002924 * "deletebufline()" function
2925 */
2926 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002927f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002928{
2929 buf_T *buf;
2930 linenr_T first, last;
2931 linenr_T lnum;
2932 long count;
2933 int is_curbuf;
2934 buf_T *curbuf_save = NULL;
2935 win_T *curwin_save = NULL;
2936 tabpage_T *tp;
2937 win_T *wp;
2938
2939 buf = get_buf_tv(&argvars[0], FALSE);
2940 if (buf == NULL)
2941 {
2942 rettv->vval.v_number = 1; /* FAIL */
2943 return;
2944 }
2945 is_curbuf = buf == curbuf;
2946
2947 first = get_tv_lnum_buf(&argvars[1], buf);
2948 if (argvars[2].v_type != VAR_UNKNOWN)
2949 last = get_tv_lnum_buf(&argvars[2], buf);
2950 else
2951 last = first;
2952
2953 if (buf->b_ml.ml_mfp == NULL || first < 1
2954 || first > buf->b_ml.ml_line_count || last < first)
2955 {
2956 rettv->vval.v_number = 1; /* FAIL */
2957 return;
2958 }
2959
2960 if (!is_curbuf)
2961 {
2962 curbuf_save = curbuf;
2963 curwin_save = curwin;
2964 curbuf = buf;
2965 find_win_for_curbuf();
2966 }
2967 if (last > curbuf->b_ml.ml_line_count)
2968 last = curbuf->b_ml.ml_line_count;
2969 count = last - first + 1;
2970
2971 // When coming here from Insert mode, sync undo, so that this can be
2972 // undone separately from what was previously inserted.
2973 if (u_sync_once == 2)
2974 {
2975 u_sync_once = 1; // notify that u_sync() was called
2976 u_sync(TRUE);
2977 }
2978
2979 if (u_save(first - 1, last + 1) == FAIL)
2980 {
2981 rettv->vval.v_number = 1; /* FAIL */
2982 return;
2983 }
2984
2985 for (lnum = first; lnum <= last; ++lnum)
2986 ml_delete(first, TRUE);
2987
2988 FOR_ALL_TAB_WINDOWS(tp, wp)
2989 if (wp->w_buffer == buf)
2990 {
2991 if (wp->w_cursor.lnum > last)
2992 wp->w_cursor.lnum -= count;
2993 else if (wp->w_cursor.lnum> first)
2994 wp->w_cursor.lnum = first;
2995 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2996 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2997 }
2998 check_cursor_col();
2999 deleted_lines_mark(first, count);
3000
3001 if (!is_curbuf)
3002 {
3003 curbuf = curbuf_save;
3004 curwin = curwin_save;
3005 }
3006}
3007
3008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 * "did_filetype()" function
3010 */
3011 static void
3012f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3013{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015}
3016
3017/*
3018 * "diff_filler()" function
3019 */
3020 static void
3021f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3022{
3023#ifdef FEAT_DIFF
3024 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
3025#endif
3026}
3027
3028/*
3029 * "diff_hlID()" function
3030 */
3031 static void
3032f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3033{
3034#ifdef FEAT_DIFF
3035 linenr_T lnum = get_tv_lnum(argvars);
3036 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003037 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 static int fnum = 0;
3039 static int change_start = 0;
3040 static int change_end = 0;
3041 static hlf_T hlID = (hlf_T)0;
3042 int filler_lines;
3043 int col;
3044
3045 if (lnum < 0) /* ignore type error in {lnum} arg */
3046 lnum = 0;
3047 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003048 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 || fnum != curbuf->b_fnum)
3050 {
3051 /* New line, buffer, change: need to get the values. */
3052 filler_lines = diff_check(curwin, lnum);
3053 if (filler_lines < 0)
3054 {
3055 if (filler_lines == -1)
3056 {
3057 change_start = MAXCOL;
3058 change_end = -1;
3059 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3060 hlID = HLF_ADD; /* added line */
3061 else
3062 hlID = HLF_CHD; /* changed line */
3063 }
3064 else
3065 hlID = HLF_ADD; /* added line */
3066 }
3067 else
3068 hlID = (hlf_T)0;
3069 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003070 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071 fnum = curbuf->b_fnum;
3072 }
3073
3074 if (hlID == HLF_CHD || hlID == HLF_TXD)
3075 {
3076 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3077 if (col >= change_start && col <= change_end)
3078 hlID = HLF_TXD; /* changed text */
3079 else
3080 hlID = HLF_CHD; /* changed line */
3081 }
3082 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3083#endif
3084}
3085
3086/*
3087 * "empty({expr})" function
3088 */
3089 static void
3090f_empty(typval_T *argvars, typval_T *rettv)
3091{
3092 int n = FALSE;
3093
3094 switch (argvars[0].v_type)
3095 {
3096 case VAR_STRING:
3097 case VAR_FUNC:
3098 n = argvars[0].vval.v_string == NULL
3099 || *argvars[0].vval.v_string == NUL;
3100 break;
3101 case VAR_PARTIAL:
3102 n = FALSE;
3103 break;
3104 case VAR_NUMBER:
3105 n = argvars[0].vval.v_number == 0;
3106 break;
3107 case VAR_FLOAT:
3108#ifdef FEAT_FLOAT
3109 n = argvars[0].vval.v_float == 0.0;
3110 break;
3111#endif
3112 case VAR_LIST:
3113 n = argvars[0].vval.v_list == NULL
3114 || argvars[0].vval.v_list->lv_first == NULL;
3115 break;
3116 case VAR_DICT:
3117 n = argvars[0].vval.v_dict == NULL
3118 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3119 break;
3120 case VAR_SPECIAL:
3121 n = argvars[0].vval.v_number != VVAL_TRUE;
3122 break;
3123
3124 case VAR_JOB:
3125#ifdef FEAT_JOB_CHANNEL
3126 n = argvars[0].vval.v_job == NULL
3127 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3128 break;
3129#endif
3130 case VAR_CHANNEL:
3131#ifdef FEAT_JOB_CHANNEL
3132 n = argvars[0].vval.v_channel == NULL
3133 || !channel_is_open(argvars[0].vval.v_channel);
3134 break;
3135#endif
3136 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003137 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 n = TRUE;
3139 break;
3140 }
3141
3142 rettv->vval.v_number = n;
3143}
3144
3145/*
3146 * "escape({string}, {chars})" function
3147 */
3148 static void
3149f_escape(typval_T *argvars, typval_T *rettv)
3150{
3151 char_u buf[NUMBUFLEN];
3152
3153 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3154 get_tv_string_buf(&argvars[1], buf));
3155 rettv->v_type = VAR_STRING;
3156}
3157
3158/*
3159 * "eval()" function
3160 */
3161 static void
3162f_eval(typval_T *argvars, typval_T *rettv)
3163{
3164 char_u *s, *p;
3165
3166 s = get_tv_string_chk(&argvars[0]);
3167 if (s != NULL)
3168 s = skipwhite(s);
3169
3170 p = s;
3171 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3172 {
3173 if (p != NULL && !aborting())
3174 EMSG2(_(e_invexpr2), p);
3175 need_clr_eos = FALSE;
3176 rettv->v_type = VAR_NUMBER;
3177 rettv->vval.v_number = 0;
3178 }
3179 else if (*s != NUL)
3180 EMSG(_(e_trailing));
3181}
3182
3183/*
3184 * "eventhandler()" function
3185 */
3186 static void
3187f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3188{
3189 rettv->vval.v_number = vgetc_busy;
3190}
3191
3192/*
3193 * "executable()" function
3194 */
3195 static void
3196f_executable(typval_T *argvars, typval_T *rettv)
3197{
3198 char_u *name = get_tv_string(&argvars[0]);
3199
3200 /* Check in $PATH and also check directly if there is a directory name. */
3201 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3202 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3203}
3204
3205static garray_T redir_execute_ga;
3206
3207/*
3208 * Append "value[value_len]" to the execute() output.
3209 */
3210 void
3211execute_redir_str(char_u *value, int value_len)
3212{
3213 int len;
3214
3215 if (value_len == -1)
3216 len = (int)STRLEN(value); /* Append the entire string */
3217 else
3218 len = value_len; /* Append only "value_len" characters */
3219 if (ga_grow(&redir_execute_ga, len) == OK)
3220 {
3221 mch_memmove((char *)redir_execute_ga.ga_data
3222 + redir_execute_ga.ga_len, value, len);
3223 redir_execute_ga.ga_len += len;
3224 }
3225}
3226
3227/*
3228 * Get next line from a list.
3229 * Called by do_cmdline() to get the next line.
3230 * Returns allocated string, or NULL for end of function.
3231 */
3232
3233 static char_u *
3234get_list_line(
3235 int c UNUSED,
3236 void *cookie,
3237 int indent UNUSED)
3238{
3239 listitem_T **p = (listitem_T **)cookie;
3240 listitem_T *item = *p;
3241 char_u buf[NUMBUFLEN];
3242 char_u *s;
3243
3244 if (item == NULL)
3245 return NULL;
3246 s = get_tv_string_buf_chk(&item->li_tv, buf);
3247 *p = item->li_next;
3248 return s == NULL ? NULL : vim_strsave(s);
3249}
3250
3251/*
3252 * "execute()" function
3253 */
3254 static void
3255f_execute(typval_T *argvars, typval_T *rettv)
3256{
3257 char_u *cmd = NULL;
3258 list_T *list = NULL;
3259 int save_msg_silent = msg_silent;
3260 int save_emsg_silent = emsg_silent;
3261 int save_emsg_noredir = emsg_noredir;
3262 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003263 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 garray_T save_ga;
3265
3266 rettv->vval.v_string = NULL;
3267 rettv->v_type = VAR_STRING;
3268
3269 if (argvars[0].v_type == VAR_LIST)
3270 {
3271 list = argvars[0].vval.v_list;
3272 if (list == NULL || list->lv_first == NULL)
3273 /* empty list, no commands, empty output */
3274 return;
3275 ++list->lv_refcount;
3276 }
3277 else
3278 {
3279 cmd = get_tv_string_chk(&argvars[0]);
3280 if (cmd == NULL)
3281 return;
3282 }
3283
3284 if (argvars[1].v_type != VAR_UNKNOWN)
3285 {
3286 char_u buf[NUMBUFLEN];
3287 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3288
3289 if (s == NULL)
3290 return;
3291 if (STRNCMP(s, "silent", 6) == 0)
3292 ++msg_silent;
3293 if (STRCMP(s, "silent!") == 0)
3294 {
3295 emsg_silent = TRUE;
3296 emsg_noredir = TRUE;
3297 }
3298 }
3299 else
3300 ++msg_silent;
3301
3302 if (redir_execute)
3303 save_ga = redir_execute_ga;
3304 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3305 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003306 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307
3308 if (cmd != NULL)
3309 do_cmdline_cmd(cmd);
3310 else
3311 {
3312 listitem_T *item = list->lv_first;
3313
3314 do_cmdline(NULL, get_list_line, (void *)&item,
3315 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3316 --list->lv_refcount;
3317 }
3318
Bram Moolenaard297f352017-01-29 20:31:21 +01003319 /* Need to append a NUL to the result. */
3320 if (ga_grow(&redir_execute_ga, 1) == OK)
3321 {
3322 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3323 rettv->vval.v_string = redir_execute_ga.ga_data;
3324 }
3325 else
3326 {
3327 ga_clear(&redir_execute_ga);
3328 rettv->vval.v_string = NULL;
3329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 msg_silent = save_msg_silent;
3331 emsg_silent = save_emsg_silent;
3332 emsg_noredir = save_emsg_noredir;
3333
3334 redir_execute = save_redir_execute;
3335 if (redir_execute)
3336 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003337 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338
3339 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3340 * line. Put it back in the first column. */
3341 msg_col = 0;
3342}
3343
3344/*
3345 * "exepath()" function
3346 */
3347 static void
3348f_exepath(typval_T *argvars, typval_T *rettv)
3349{
3350 char_u *p = NULL;
3351
3352 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3353 rettv->v_type = VAR_STRING;
3354 rettv->vval.v_string = p;
3355}
3356
3357/*
3358 * "exists()" function
3359 */
3360 static void
3361f_exists(typval_T *argvars, typval_T *rettv)
3362{
3363 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365
3366 p = get_tv_string(&argvars[0]);
3367 if (*p == '$') /* environment variable */
3368 {
3369 /* first try "normal" environment variables (fast) */
3370 if (mch_getenv(p + 1) != NULL)
3371 n = TRUE;
3372 else
3373 {
3374 /* try expanding things like $VIM and ${HOME} */
3375 p = expand_env_save(p);
3376 if (p != NULL && *p != '$')
3377 n = TRUE;
3378 vim_free(p);
3379 }
3380 }
3381 else if (*p == '&' || *p == '+') /* option */
3382 {
3383 n = (get_option_tv(&p, NULL, TRUE) == OK);
3384 if (*skipwhite(p) != NUL)
3385 n = FALSE; /* trailing garbage */
3386 }
3387 else if (*p == '*') /* internal or user defined function */
3388 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003389 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 }
3391 else if (*p == ':')
3392 {
3393 n = cmd_exists(p + 1);
3394 }
3395 else if (*p == '#')
3396 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 if (p[1] == '#')
3398 n = autocmd_supported(p + 2);
3399 else
3400 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 }
3402 else /* internal variable */
3403 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003404 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 }
3406
3407 rettv->vval.v_number = n;
3408}
3409
3410#ifdef FEAT_FLOAT
3411/*
3412 * "exp()" function
3413 */
3414 static void
3415f_exp(typval_T *argvars, typval_T *rettv)
3416{
3417 float_T f = 0.0;
3418
3419 rettv->v_type = VAR_FLOAT;
3420 if (get_float_arg(argvars, &f) == OK)
3421 rettv->vval.v_float = exp(f);
3422 else
3423 rettv->vval.v_float = 0.0;
3424}
3425#endif
3426
3427/*
3428 * "expand()" function
3429 */
3430 static void
3431f_expand(typval_T *argvars, typval_T *rettv)
3432{
3433 char_u *s;
3434 int len;
3435 char_u *errormsg;
3436 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3437 expand_T xpc;
3438 int error = FALSE;
3439 char_u *result;
3440
3441 rettv->v_type = VAR_STRING;
3442 if (argvars[1].v_type != VAR_UNKNOWN
3443 && argvars[2].v_type != VAR_UNKNOWN
3444 && get_tv_number_chk(&argvars[2], &error)
3445 && !error)
3446 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003447 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 }
3449
3450 s = get_tv_string(&argvars[0]);
3451 if (*s == '%' || *s == '#' || *s == '<')
3452 {
3453 ++emsg_off;
3454 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3455 --emsg_off;
3456 if (rettv->v_type == VAR_LIST)
3457 {
3458 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3459 list_append_string(rettv->vval.v_list, result, -1);
3460 else
3461 vim_free(result);
3462 }
3463 else
3464 rettv->vval.v_string = result;
3465 }
3466 else
3467 {
3468 /* When the optional second argument is non-zero, don't remove matches
3469 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3470 if (argvars[1].v_type != VAR_UNKNOWN
3471 && get_tv_number_chk(&argvars[1], &error))
3472 options |= WILD_KEEP_ALL;
3473 if (!error)
3474 {
3475 ExpandInit(&xpc);
3476 xpc.xp_context = EXPAND_FILES;
3477 if (p_wic)
3478 options += WILD_ICASE;
3479 if (rettv->v_type == VAR_STRING)
3480 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3481 options, WILD_ALL);
3482 else if (rettv_list_alloc(rettv) != FAIL)
3483 {
3484 int i;
3485
3486 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3487 for (i = 0; i < xpc.xp_numfiles; i++)
3488 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3489 ExpandCleanup(&xpc);
3490 }
3491 }
3492 else
3493 rettv->vval.v_string = NULL;
3494 }
3495}
3496
3497/*
3498 * "extend(list, list [, idx])" function
3499 * "extend(dict, dict [, action])" function
3500 */
3501 static void
3502f_extend(typval_T *argvars, typval_T *rettv)
3503{
3504 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3505
3506 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3507 {
3508 list_T *l1, *l2;
3509 listitem_T *item;
3510 long before;
3511 int error = FALSE;
3512
3513 l1 = argvars[0].vval.v_list;
3514 l2 = argvars[1].vval.v_list;
3515 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3516 && l2 != NULL)
3517 {
3518 if (argvars[2].v_type != VAR_UNKNOWN)
3519 {
3520 before = (long)get_tv_number_chk(&argvars[2], &error);
3521 if (error)
3522 return; /* type error; errmsg already given */
3523
3524 if (before == l1->lv_len)
3525 item = NULL;
3526 else
3527 {
3528 item = list_find(l1, before);
3529 if (item == NULL)
3530 {
3531 EMSGN(_(e_listidx), before);
3532 return;
3533 }
3534 }
3535 }
3536 else
3537 item = NULL;
3538 list_extend(l1, l2, item);
3539
3540 copy_tv(&argvars[0], rettv);
3541 }
3542 }
3543 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3544 {
3545 dict_T *d1, *d2;
3546 char_u *action;
3547 int i;
3548
3549 d1 = argvars[0].vval.v_dict;
3550 d2 = argvars[1].vval.v_dict;
3551 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3552 && d2 != NULL)
3553 {
3554 /* Check the third argument. */
3555 if (argvars[2].v_type != VAR_UNKNOWN)
3556 {
3557 static char *(av[]) = {"keep", "force", "error"};
3558
3559 action = get_tv_string_chk(&argvars[2]);
3560 if (action == NULL)
3561 return; /* type error; errmsg already given */
3562 for (i = 0; i < 3; ++i)
3563 if (STRCMP(action, av[i]) == 0)
3564 break;
3565 if (i == 3)
3566 {
3567 EMSG2(_(e_invarg2), action);
3568 return;
3569 }
3570 }
3571 else
3572 action = (char_u *)"force";
3573
3574 dict_extend(d1, d2, action);
3575
3576 copy_tv(&argvars[0], rettv);
3577 }
3578 }
3579 else
3580 EMSG2(_(e_listdictarg), "extend()");
3581}
3582
3583/*
3584 * "feedkeys()" function
3585 */
3586 static void
3587f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3588{
3589 int remap = TRUE;
3590 int insert = FALSE;
3591 char_u *keys, *flags;
3592 char_u nbuf[NUMBUFLEN];
3593 int typed = FALSE;
3594 int execute = FALSE;
3595 int dangerous = FALSE;
3596 char_u *keys_esc;
3597
3598 /* This is not allowed in the sandbox. If the commands would still be
3599 * executed in the sandbox it would be OK, but it probably happens later,
3600 * when "sandbox" is no longer set. */
3601 if (check_secure())
3602 return;
3603
3604 keys = get_tv_string(&argvars[0]);
3605
3606 if (argvars[1].v_type != VAR_UNKNOWN)
3607 {
3608 flags = get_tv_string_buf(&argvars[1], nbuf);
3609 for ( ; *flags != NUL; ++flags)
3610 {
3611 switch (*flags)
3612 {
3613 case 'n': remap = FALSE; break;
3614 case 'm': remap = TRUE; break;
3615 case 't': typed = TRUE; break;
3616 case 'i': insert = TRUE; break;
3617 case 'x': execute = TRUE; break;
3618 case '!': dangerous = TRUE; break;
3619 }
3620 }
3621 }
3622
3623 if (*keys != NUL || execute)
3624 {
3625 /* Need to escape K_SPECIAL and CSI before putting the string in the
3626 * typeahead buffer. */
3627 keys_esc = vim_strsave_escape_csi(keys);
3628 if (keys_esc != NULL)
3629 {
3630 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3631 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3632 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003633 if (vgetc_busy
3634#ifdef FEAT_TIMERS
3635 || timer_busy
3636#endif
3637 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638 typebuf_was_filled = TRUE;
3639 if (execute)
3640 {
3641 int save_msg_scroll = msg_scroll;
3642
3643 /* Avoid a 1 second delay when the keys start Insert mode. */
3644 msg_scroll = FALSE;
3645
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003646 if (!dangerous)
3647 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003648 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003649 if (!dangerous)
3650 --ex_normal_busy;
3651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 msg_scroll |= save_msg_scroll;
3653 }
3654 }
3655 }
3656}
3657
3658/*
3659 * "filereadable()" function
3660 */
3661 static void
3662f_filereadable(typval_T *argvars, typval_T *rettv)
3663{
3664 int fd;
3665 char_u *p;
3666 int n;
3667
3668#ifndef O_NONBLOCK
3669# define O_NONBLOCK 0
3670#endif
3671 p = get_tv_string(&argvars[0]);
3672 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3673 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3674 {
3675 n = TRUE;
3676 close(fd);
3677 }
3678 else
3679 n = FALSE;
3680
3681 rettv->vval.v_number = n;
3682}
3683
3684/*
3685 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3686 * rights to write into.
3687 */
3688 static void
3689f_filewritable(typval_T *argvars, typval_T *rettv)
3690{
3691 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3692}
3693
3694 static void
3695findfilendir(
3696 typval_T *argvars UNUSED,
3697 typval_T *rettv,
3698 int find_what UNUSED)
3699{
3700#ifdef FEAT_SEARCHPATH
3701 char_u *fname;
3702 char_u *fresult = NULL;
3703 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3704 char_u *p;
3705 char_u pathbuf[NUMBUFLEN];
3706 int count = 1;
3707 int first = TRUE;
3708 int error = FALSE;
3709#endif
3710
3711 rettv->vval.v_string = NULL;
3712 rettv->v_type = VAR_STRING;
3713
3714#ifdef FEAT_SEARCHPATH
3715 fname = get_tv_string(&argvars[0]);
3716
3717 if (argvars[1].v_type != VAR_UNKNOWN)
3718 {
3719 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3720 if (p == NULL)
3721 error = TRUE;
3722 else
3723 {
3724 if (*p != NUL)
3725 path = p;
3726
3727 if (argvars[2].v_type != VAR_UNKNOWN)
3728 count = (int)get_tv_number_chk(&argvars[2], &error);
3729 }
3730 }
3731
3732 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3733 error = TRUE;
3734
3735 if (*fname != NUL && !error)
3736 {
3737 do
3738 {
3739 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3740 vim_free(fresult);
3741 fresult = find_file_in_path_option(first ? fname : NULL,
3742 first ? (int)STRLEN(fname) : 0,
3743 0, first, path,
3744 find_what,
3745 curbuf->b_ffname,
3746 find_what == FINDFILE_DIR
3747 ? (char_u *)"" : curbuf->b_p_sua);
3748 first = FALSE;
3749
3750 if (fresult != NULL && rettv->v_type == VAR_LIST)
3751 list_append_string(rettv->vval.v_list, fresult, -1);
3752
3753 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3754 }
3755
3756 if (rettv->v_type == VAR_STRING)
3757 rettv->vval.v_string = fresult;
3758#endif
3759}
3760
3761/*
3762 * "filter()" function
3763 */
3764 static void
3765f_filter(typval_T *argvars, typval_T *rettv)
3766{
3767 filter_map(argvars, rettv, FALSE);
3768}
3769
3770/*
3771 * "finddir({fname}[, {path}[, {count}]])" function
3772 */
3773 static void
3774f_finddir(typval_T *argvars, typval_T *rettv)
3775{
3776 findfilendir(argvars, rettv, FINDFILE_DIR);
3777}
3778
3779/*
3780 * "findfile({fname}[, {path}[, {count}]])" function
3781 */
3782 static void
3783f_findfile(typval_T *argvars, typval_T *rettv)
3784{
3785 findfilendir(argvars, rettv, FINDFILE_FILE);
3786}
3787
3788#ifdef FEAT_FLOAT
3789/*
3790 * "float2nr({float})" function
3791 */
3792 static void
3793f_float2nr(typval_T *argvars, typval_T *rettv)
3794{
3795 float_T f = 0.0;
3796
3797 if (get_float_arg(argvars, &f) == OK)
3798 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003799 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003800 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003801 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003802 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003803 else
3804 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 }
3806}
3807
3808/*
3809 * "floor({float})" function
3810 */
3811 static void
3812f_floor(typval_T *argvars, typval_T *rettv)
3813{
3814 float_T f = 0.0;
3815
3816 rettv->v_type = VAR_FLOAT;
3817 if (get_float_arg(argvars, &f) == OK)
3818 rettv->vval.v_float = floor(f);
3819 else
3820 rettv->vval.v_float = 0.0;
3821}
3822
3823/*
3824 * "fmod()" function
3825 */
3826 static void
3827f_fmod(typval_T *argvars, typval_T *rettv)
3828{
3829 float_T fx = 0.0, fy = 0.0;
3830
3831 rettv->v_type = VAR_FLOAT;
3832 if (get_float_arg(argvars, &fx) == OK
3833 && get_float_arg(&argvars[1], &fy) == OK)
3834 rettv->vval.v_float = fmod(fx, fy);
3835 else
3836 rettv->vval.v_float = 0.0;
3837}
3838#endif
3839
3840/*
3841 * "fnameescape({string})" function
3842 */
3843 static void
3844f_fnameescape(typval_T *argvars, typval_T *rettv)
3845{
3846 rettv->vval.v_string = vim_strsave_fnameescape(
3847 get_tv_string(&argvars[0]), FALSE);
3848 rettv->v_type = VAR_STRING;
3849}
3850
3851/*
3852 * "fnamemodify({fname}, {mods})" function
3853 */
3854 static void
3855f_fnamemodify(typval_T *argvars, typval_T *rettv)
3856{
3857 char_u *fname;
3858 char_u *mods;
3859 int usedlen = 0;
3860 int len;
3861 char_u *fbuf = NULL;
3862 char_u buf[NUMBUFLEN];
3863
3864 fname = get_tv_string_chk(&argvars[0]);
3865 mods = get_tv_string_buf_chk(&argvars[1], buf);
3866 if (fname == NULL || mods == NULL)
3867 fname = NULL;
3868 else
3869 {
3870 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003871 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003872 }
3873
3874 rettv->v_type = VAR_STRING;
3875 if (fname == NULL)
3876 rettv->vval.v_string = NULL;
3877 else
3878 rettv->vval.v_string = vim_strnsave(fname, len);
3879 vim_free(fbuf);
3880}
3881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882/*
3883 * "foldclosed()" function
3884 */
3885 static void
3886foldclosed_both(
3887 typval_T *argvars UNUSED,
3888 typval_T *rettv,
3889 int end UNUSED)
3890{
3891#ifdef FEAT_FOLDING
3892 linenr_T lnum;
3893 linenr_T first, last;
3894
3895 lnum = get_tv_lnum(argvars);
3896 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3897 {
3898 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3899 {
3900 if (end)
3901 rettv->vval.v_number = (varnumber_T)last;
3902 else
3903 rettv->vval.v_number = (varnumber_T)first;
3904 return;
3905 }
3906 }
3907#endif
3908 rettv->vval.v_number = -1;
3909}
3910
3911/*
3912 * "foldclosed()" function
3913 */
3914 static void
3915f_foldclosed(typval_T *argvars, typval_T *rettv)
3916{
3917 foldclosed_both(argvars, rettv, FALSE);
3918}
3919
3920/*
3921 * "foldclosedend()" function
3922 */
3923 static void
3924f_foldclosedend(typval_T *argvars, typval_T *rettv)
3925{
3926 foldclosed_both(argvars, rettv, TRUE);
3927}
3928
3929/*
3930 * "foldlevel()" function
3931 */
3932 static void
3933f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3934{
3935#ifdef FEAT_FOLDING
3936 linenr_T lnum;
3937
3938 lnum = get_tv_lnum(argvars);
3939 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3940 rettv->vval.v_number = foldLevel(lnum);
3941#endif
3942}
3943
3944/*
3945 * "foldtext()" function
3946 */
3947 static void
3948f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3949{
3950#ifdef FEAT_FOLDING
3951 linenr_T foldstart;
3952 linenr_T foldend;
3953 char_u *dashes;
3954 linenr_T lnum;
3955 char_u *s;
3956 char_u *r;
3957 int len;
3958 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003959 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003960#endif
3961
3962 rettv->v_type = VAR_STRING;
3963 rettv->vval.v_string = NULL;
3964#ifdef FEAT_FOLDING
3965 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3966 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3967 dashes = get_vim_var_str(VV_FOLDDASHES);
3968 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3969 && dashes != NULL)
3970 {
3971 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003972 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 if (!linewhite(lnum))
3974 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975
3976 /* Find interesting text in this line. */
3977 s = skipwhite(ml_get(lnum));
3978 /* skip C comment-start */
3979 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3980 {
3981 s = skipwhite(s + 2);
3982 if (*skipwhite(s) == NUL
3983 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3984 {
3985 s = skipwhite(ml_get(lnum + 1));
3986 if (*s == '*')
3987 s = skipwhite(s + 1);
3988 }
3989 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003990 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003991 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003992 r = alloc((unsigned)(STRLEN(txt)
3993 + STRLEN(dashes) /* for %s */
3994 + 20 /* for %3ld */
3995 + STRLEN(s))); /* concatenated */
3996 if (r != NULL)
3997 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003998 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003999 len = (int)STRLEN(r);
4000 STRCAT(r, s);
4001 /* remove 'foldmarker' and 'commentstring' */
4002 foldtext_cleanup(r + len);
4003 rettv->vval.v_string = r;
4004 }
4005 }
4006#endif
4007}
4008
4009/*
4010 * "foldtextresult(lnum)" function
4011 */
4012 static void
4013f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4014{
4015#ifdef FEAT_FOLDING
4016 linenr_T lnum;
4017 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004018 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 foldinfo_T foldinfo;
4020 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004021 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022#endif
4023
4024 rettv->v_type = VAR_STRING;
4025 rettv->vval.v_string = NULL;
4026#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004027 if (entered)
4028 return; /* reject recursive use */
4029 entered = TRUE;
4030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 lnum = get_tv_lnum(argvars);
4032 /* treat illegal types and illegal string values for {lnum} the same */
4033 if (lnum < 0)
4034 lnum = 0;
4035 fold_count = foldedCount(curwin, lnum, &foldinfo);
4036 if (fold_count > 0)
4037 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004038 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4039 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040 if (text == buf)
4041 text = vim_strsave(text);
4042 rettv->vval.v_string = text;
4043 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004044
4045 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046#endif
4047}
4048
4049/*
4050 * "foreground()" function
4051 */
4052 static void
4053f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4054{
4055#ifdef FEAT_GUI
4056 if (gui.in_use)
4057 gui_mch_set_foreground();
4058#else
4059# ifdef WIN32
4060 win32_set_foreground();
4061# endif
4062#endif
4063}
4064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004065 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004066common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067{
4068 char_u *s;
4069 char_u *name;
4070 int use_string = FALSE;
4071 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004072 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073
4074 if (argvars[0].v_type == VAR_FUNC)
4075 {
4076 /* function(MyFunc, [arg], dict) */
4077 s = argvars[0].vval.v_string;
4078 }
4079 else if (argvars[0].v_type == VAR_PARTIAL
4080 && argvars[0].vval.v_partial != NULL)
4081 {
4082 /* function(dict.MyFunc, [arg]) */
4083 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004084 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004085 }
4086 else
4087 {
4088 /* function('MyFunc', [arg], dict) */
4089 s = get_tv_string(&argvars[0]);
4090 use_string = TRUE;
4091 }
4092
Bram Moolenaar843b8842016-08-21 14:36:15 +02004093 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004094 {
4095 name = s;
4096 trans_name = trans_function_name(&name, FALSE,
4097 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4098 if (*name != NUL)
4099 s = NULL;
4100 }
4101
Bram Moolenaar843b8842016-08-21 14:36:15 +02004102 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4103 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004104 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004106 else if (trans_name != NULL && (is_funcref
4107 ? find_func(trans_name) == NULL
4108 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 EMSG2(_("E700: Unknown function: %s"), s);
4110 else
4111 {
4112 int dict_idx = 0;
4113 int arg_idx = 0;
4114 list_T *list = NULL;
4115
4116 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4117 {
4118 char sid_buf[25];
4119 int off = *s == 's' ? 2 : 5;
4120
4121 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4122 * also be called from another script. Using trans_function_name()
4123 * would also work, but some plugins depend on the name being
4124 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004125 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4127 if (name != NULL)
4128 {
4129 STRCPY(name, sid_buf);
4130 STRCAT(name, s + off);
4131 }
4132 }
4133 else
4134 name = vim_strsave(s);
4135
4136 if (argvars[1].v_type != VAR_UNKNOWN)
4137 {
4138 if (argvars[2].v_type != VAR_UNKNOWN)
4139 {
4140 /* function(name, [args], dict) */
4141 arg_idx = 1;
4142 dict_idx = 2;
4143 }
4144 else if (argvars[1].v_type == VAR_DICT)
4145 /* function(name, dict) */
4146 dict_idx = 1;
4147 else
4148 /* function(name, [args]) */
4149 arg_idx = 1;
4150 if (dict_idx > 0)
4151 {
4152 if (argvars[dict_idx].v_type != VAR_DICT)
4153 {
4154 EMSG(_("E922: expected a dict"));
4155 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157 }
4158 if (argvars[dict_idx].vval.v_dict == NULL)
4159 dict_idx = 0;
4160 }
4161 if (arg_idx > 0)
4162 {
4163 if (argvars[arg_idx].v_type != VAR_LIST)
4164 {
4165 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4166 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004167 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004168 }
4169 list = argvars[arg_idx].vval.v_list;
4170 if (list == NULL || list->lv_len == 0)
4171 arg_idx = 0;
4172 }
4173 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004174 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004175 {
4176 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4177
4178 /* result is a VAR_PARTIAL */
4179 if (pt == NULL)
4180 vim_free(name);
4181 else
4182 {
4183 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4184 {
4185 listitem_T *li;
4186 int i = 0;
4187 int arg_len = 0;
4188 int lv_len = 0;
4189
4190 if (arg_pt != NULL)
4191 arg_len = arg_pt->pt_argc;
4192 if (list != NULL)
4193 lv_len = list->lv_len;
4194 pt->pt_argc = arg_len + lv_len;
4195 pt->pt_argv = (typval_T *)alloc(
4196 sizeof(typval_T) * pt->pt_argc);
4197 if (pt->pt_argv == NULL)
4198 {
4199 vim_free(pt);
4200 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004201 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 for (i = 0; i < arg_len; i++)
4204 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4205 if (lv_len > 0)
4206 for (li = list->lv_first; li != NULL;
4207 li = li->li_next)
4208 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004209 }
4210
4211 /* For "function(dict.func, [], dict)" and "func" is a partial
4212 * use "dict". That is backwards compatible. */
4213 if (dict_idx > 0)
4214 {
4215 /* The dict is bound explicitly, pt_auto is FALSE. */
4216 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4217 ++pt->pt_dict->dv_refcount;
4218 }
4219 else if (arg_pt != NULL)
4220 {
4221 /* If the dict was bound automatically the result is also
4222 * bound automatically. */
4223 pt->pt_dict = arg_pt->pt_dict;
4224 pt->pt_auto = arg_pt->pt_auto;
4225 if (pt->pt_dict != NULL)
4226 ++pt->pt_dict->dv_refcount;
4227 }
4228
4229 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004230 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4231 {
4232 pt->pt_func = arg_pt->pt_func;
4233 func_ptr_ref(pt->pt_func);
4234 vim_free(name);
4235 }
4236 else if (is_funcref)
4237 {
4238 pt->pt_func = find_func(trans_name);
4239 func_ptr_ref(pt->pt_func);
4240 vim_free(name);
4241 }
4242 else
4243 {
4244 pt->pt_name = name;
4245 func_ref(name);
4246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 }
4248 rettv->v_type = VAR_PARTIAL;
4249 rettv->vval.v_partial = pt;
4250 }
4251 else
4252 {
4253 /* result is a VAR_FUNC */
4254 rettv->v_type = VAR_FUNC;
4255 rettv->vval.v_string = name;
4256 func_ref(name);
4257 }
4258 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004259theend:
4260 vim_free(trans_name);
4261}
4262
4263/*
4264 * "funcref()" function
4265 */
4266 static void
4267f_funcref(typval_T *argvars, typval_T *rettv)
4268{
4269 common_function(argvars, rettv, TRUE);
4270}
4271
4272/*
4273 * "function()" function
4274 */
4275 static void
4276f_function(typval_T *argvars, typval_T *rettv)
4277{
4278 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279}
4280
4281/*
4282 * "garbagecollect()" function
4283 */
4284 static void
4285f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4286{
4287 /* This is postponed until we are back at the toplevel, because we may be
4288 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4289 want_garbage_collect = TRUE;
4290
4291 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4292 garbage_collect_at_exit = TRUE;
4293}
4294
4295/*
4296 * "get()" function
4297 */
4298 static void
4299f_get(typval_T *argvars, typval_T *rettv)
4300{
4301 listitem_T *li;
4302 list_T *l;
4303 dictitem_T *di;
4304 dict_T *d;
4305 typval_T *tv = NULL;
4306
4307 if (argvars[0].v_type == VAR_LIST)
4308 {
4309 if ((l = argvars[0].vval.v_list) != NULL)
4310 {
4311 int error = FALSE;
4312
4313 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4314 if (!error && li != NULL)
4315 tv = &li->li_tv;
4316 }
4317 }
4318 else if (argvars[0].v_type == VAR_DICT)
4319 {
4320 if ((d = argvars[0].vval.v_dict) != NULL)
4321 {
4322 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4323 if (di != NULL)
4324 tv = &di->di_tv;
4325 }
4326 }
4327 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4328 {
4329 partial_T *pt;
4330 partial_T fref_pt;
4331
4332 if (argvars[0].v_type == VAR_PARTIAL)
4333 pt = argvars[0].vval.v_partial;
4334 else
4335 {
4336 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4337 fref_pt.pt_name = argvars[0].vval.v_string;
4338 pt = &fref_pt;
4339 }
4340
4341 if (pt != NULL)
4342 {
4343 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004344 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345
4346 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4347 {
4348 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004349 n = partial_name(pt);
4350 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004351 rettv->vval.v_string = NULL;
4352 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004353 {
4354 rettv->vval.v_string = vim_strsave(n);
4355 if (rettv->v_type == VAR_FUNC)
4356 func_ref(rettv->vval.v_string);
4357 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358 }
4359 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004360 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 else if (STRCMP(what, "args") == 0)
4362 {
4363 rettv->v_type = VAR_LIST;
4364 if (rettv_list_alloc(rettv) == OK)
4365 {
4366 int i;
4367
4368 for (i = 0; i < pt->pt_argc; ++i)
4369 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4370 }
4371 }
4372 else
4373 EMSG2(_(e_invarg2), what);
4374 return;
4375 }
4376 }
4377 else
4378 EMSG2(_(e_listdictarg), "get()");
4379
4380 if (tv == NULL)
4381 {
4382 if (argvars[2].v_type != VAR_UNKNOWN)
4383 copy_tv(&argvars[2], rettv);
4384 }
4385 else
4386 copy_tv(tv, rettv);
4387}
4388
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004389#ifdef FEAT_SIGNS
4390/*
4391 * Returns information about signs placed in a buffer as list of dicts.
4392 */
4393 static void
4394get_buffer_signs(buf_T *buf, list_T *l)
4395{
4396 signlist_T *sign;
4397
4398 for (sign = buf->b_signlist; sign; sign = sign->next)
4399 {
4400 dict_T *d = dict_alloc();
4401
4402 if (d != NULL)
4403 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004404 dict_add_number(d, "id", sign->id);
4405 dict_add_number(d, "lnum", sign->lnum);
4406 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004407
4408 list_append_dict(l, d);
4409 }
4410 }
4411}
4412#endif
4413
4414/*
4415 * Returns buffer options, variables and other attributes in a dictionary.
4416 */
4417 static dict_T *
4418get_buffer_info(buf_T *buf)
4419{
4420 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004421 tabpage_T *tp;
4422 win_T *wp;
4423 list_T *windows;
4424
4425 dict = dict_alloc();
4426 if (dict == NULL)
4427 return NULL;
4428
Bram Moolenaare0be1672018-07-08 16:50:37 +02004429 dict_add_number(dict, "bufnr", buf->b_fnum);
4430 dict_add_string(dict, "name", buf->b_ffname);
4431 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4432 : buflist_findlnum(buf));
4433 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4434 dict_add_number(dict, "listed", buf->b_p_bl);
4435 dict_add_number(dict, "changed", bufIsChanged(buf));
4436 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4437 dict_add_number(dict, "hidden",
4438 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004439
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004440 /* Get a reference to buffer variables */
4441 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004442
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004443 /* List of windows displaying this buffer */
4444 windows = list_alloc();
4445 if (windows != NULL)
4446 {
4447 FOR_ALL_TAB_WINDOWS(tp, wp)
4448 if (wp->w_buffer == buf)
4449 list_append_number(windows, (varnumber_T)wp->w_id);
4450 dict_add_list(dict, "windows", windows);
4451 }
4452
4453#ifdef FEAT_SIGNS
4454 if (buf->b_signlist != NULL)
4455 {
4456 /* List of signs placed in this buffer */
4457 list_T *signs = list_alloc();
4458 if (signs != NULL)
4459 {
4460 get_buffer_signs(buf, signs);
4461 dict_add_list(dict, "signs", signs);
4462 }
4463 }
4464#endif
4465
4466 return dict;
4467}
4468
4469/*
4470 * "getbufinfo()" function
4471 */
4472 static void
4473f_getbufinfo(typval_T *argvars, typval_T *rettv)
4474{
4475 buf_T *buf = NULL;
4476 buf_T *argbuf = NULL;
4477 dict_T *d;
4478 int filtered = FALSE;
4479 int sel_buflisted = FALSE;
4480 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004481 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004482
4483 if (rettv_list_alloc(rettv) != OK)
4484 return;
4485
4486 /* List of all the buffers or selected buffers */
4487 if (argvars[0].v_type == VAR_DICT)
4488 {
4489 dict_T *sel_d = argvars[0].vval.v_dict;
4490
4491 if (sel_d != NULL)
4492 {
4493 dictitem_T *di;
4494
4495 filtered = TRUE;
4496
4497 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4498 if (di != NULL && get_tv_number(&di->di_tv))
4499 sel_buflisted = TRUE;
4500
4501 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4502 if (di != NULL && get_tv_number(&di->di_tv))
4503 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004504
4505 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4506 if (di != NULL && get_tv_number(&di->di_tv))
4507 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004508 }
4509 }
4510 else if (argvars[0].v_type != VAR_UNKNOWN)
4511 {
4512 /* Information about one buffer. Argument specifies the buffer */
4513 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4514 ++emsg_off;
4515 argbuf = get_buf_tv(&argvars[0], FALSE);
4516 --emsg_off;
4517 if (argbuf == NULL)
4518 return;
4519 }
4520
4521 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004522 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004523 {
4524 if (argbuf != NULL && argbuf != buf)
4525 continue;
4526 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004527 || (sel_buflisted && !buf->b_p_bl)
4528 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004529 continue;
4530
4531 d = get_buffer_info(buf);
4532 if (d != NULL)
4533 list_append_dict(rettv->vval.v_list, d);
4534 if (argbuf != NULL)
4535 return;
4536 }
4537}
4538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539/*
4540 * Get line or list of lines from buffer "buf" into "rettv".
4541 * Return a range (from start to end) of lines in rettv from the specified
4542 * buffer.
4543 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4544 */
4545 static void
4546get_buffer_lines(
4547 buf_T *buf,
4548 linenr_T start,
4549 linenr_T end,
4550 int retlist,
4551 typval_T *rettv)
4552{
4553 char_u *p;
4554
4555 rettv->v_type = VAR_STRING;
4556 rettv->vval.v_string = NULL;
4557 if (retlist && rettv_list_alloc(rettv) == FAIL)
4558 return;
4559
4560 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4561 return;
4562
4563 if (!retlist)
4564 {
4565 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4566 p = ml_get_buf(buf, start, FALSE);
4567 else
4568 p = (char_u *)"";
4569 rettv->vval.v_string = vim_strsave(p);
4570 }
4571 else
4572 {
4573 if (end < start)
4574 return;
4575
4576 if (start < 1)
4577 start = 1;
4578 if (end > buf->b_ml.ml_line_count)
4579 end = buf->b_ml.ml_line_count;
4580 while (start <= end)
4581 if (list_append_string(rettv->vval.v_list,
4582 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4583 break;
4584 }
4585}
4586
4587/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588 * "getbufline()" function
4589 */
4590 static void
4591f_getbufline(typval_T *argvars, typval_T *rettv)
4592{
4593 linenr_T lnum;
4594 linenr_T end;
4595 buf_T *buf;
4596
4597 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4598 ++emsg_off;
4599 buf = get_buf_tv(&argvars[0], FALSE);
4600 --emsg_off;
4601
4602 lnum = get_tv_lnum_buf(&argvars[1], buf);
4603 if (argvars[2].v_type == VAR_UNKNOWN)
4604 end = lnum;
4605 else
4606 end = get_tv_lnum_buf(&argvars[2], buf);
4607
4608 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4609}
4610
4611/*
4612 * "getbufvar()" function
4613 */
4614 static void
4615f_getbufvar(typval_T *argvars, typval_T *rettv)
4616{
4617 buf_T *buf;
4618 buf_T *save_curbuf;
4619 char_u *varname;
4620 dictitem_T *v;
4621 int done = FALSE;
4622
4623 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4624 varname = get_tv_string_chk(&argvars[1]);
4625 ++emsg_off;
4626 buf = get_buf_tv(&argvars[0], FALSE);
4627
4628 rettv->v_type = VAR_STRING;
4629 rettv->vval.v_string = NULL;
4630
4631 if (buf != NULL && varname != NULL)
4632 {
4633 /* set curbuf to be our buf, temporarily */
4634 save_curbuf = curbuf;
4635 curbuf = buf;
4636
Bram Moolenaar30567352016-08-27 21:25:44 +02004637 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004639 if (varname[1] == NUL)
4640 {
4641 /* get all buffer-local options in a dict */
4642 dict_T *opts = get_winbuf_options(TRUE);
4643
4644 if (opts != NULL)
4645 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004646 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004647 done = TRUE;
4648 }
4649 }
4650 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4651 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652 done = TRUE;
4653 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654 else
4655 {
4656 /* Look up the variable. */
4657 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4658 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4659 'b', varname, FALSE);
4660 if (v != NULL)
4661 {
4662 copy_tv(&v->di_tv, rettv);
4663 done = TRUE;
4664 }
4665 }
4666
4667 /* restore previous notion of curbuf */
4668 curbuf = save_curbuf;
4669 }
4670
4671 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4672 /* use the default value */
4673 copy_tv(&argvars[2], rettv);
4674
4675 --emsg_off;
4676}
4677
4678/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004679 * "getchangelist()" function
4680 */
4681 static void
4682f_getchangelist(typval_T *argvars, typval_T *rettv)
4683{
4684#ifdef FEAT_JUMPLIST
4685 buf_T *buf;
4686 int i;
4687 list_T *l;
4688 dict_T *d;
4689#endif
4690
4691 if (rettv_list_alloc(rettv) != OK)
4692 return;
4693
4694#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004695 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4696 ++emsg_off;
4697 buf = get_buf_tv(&argvars[0], FALSE);
4698 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004699 if (buf == NULL)
4700 return;
4701
4702 l = list_alloc();
4703 if (l == NULL)
4704 return;
4705
4706 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4707 return;
4708 /*
4709 * The current window change list index tracks only the position in the
4710 * current buffer change list. For other buffers, use the change list
4711 * length as the current index.
4712 */
4713 list_append_number(rettv->vval.v_list,
4714 (varnumber_T)((buf == curwin->w_buffer)
4715 ? curwin->w_changelistidx : buf->b_changelistlen));
4716
4717 for (i = 0; i < buf->b_changelistlen; ++i)
4718 {
4719 if (buf->b_changelist[i].lnum == 0)
4720 continue;
4721 if ((d = dict_alloc()) == NULL)
4722 return;
4723 if (list_append_dict(l, d) == FAIL)
4724 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004725 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4726 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004727# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004728 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004729# endif
4730 }
4731#endif
4732}
4733/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004734 * "getchar()" function
4735 */
4736 static void
4737f_getchar(typval_T *argvars, typval_T *rettv)
4738{
4739 varnumber_T n;
4740 int error = FALSE;
4741
Bram Moolenaar84d93902018-09-11 20:10:20 +02004742#ifdef MESSAGE_QUEUE
4743 // vpeekc() used to check for messages, but that caused problems, invoking
4744 // a callback where it was not expected. Some plugins use getchar(1) in a
4745 // loop to await a message, therefore make sure we check for messages here.
4746 parse_queued_messages();
4747#endif
4748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 /* Position the cursor. Needed after a message that ends in a space. */
4750 windgoto(msg_row, msg_col);
4751
4752 ++no_mapping;
4753 ++allow_keys;
4754 for (;;)
4755 {
4756 if (argvars[0].v_type == VAR_UNKNOWN)
4757 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004758 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4760 /* getchar(1): only check if char avail */
4761 n = vpeekc_any();
4762 else if (error || vpeekc_any() == NUL)
4763 /* illegal argument or getchar(0) and no char avail: return zero */
4764 n = 0;
4765 else
4766 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004767 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768
4769 if (n == K_IGNORE)
4770 continue;
4771 break;
4772 }
4773 --no_mapping;
4774 --allow_keys;
4775
4776 set_vim_var_nr(VV_MOUSE_WIN, 0);
4777 set_vim_var_nr(VV_MOUSE_WINID, 0);
4778 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4779 set_vim_var_nr(VV_MOUSE_COL, 0);
4780
4781 rettv->vval.v_number = n;
4782 if (IS_SPECIAL(n) || mod_mask != 0)
4783 {
4784 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4785 int i = 0;
4786
4787 /* Turn a special key into three bytes, plus modifier. */
4788 if (mod_mask != 0)
4789 {
4790 temp[i++] = K_SPECIAL;
4791 temp[i++] = KS_MODIFIER;
4792 temp[i++] = mod_mask;
4793 }
4794 if (IS_SPECIAL(n))
4795 {
4796 temp[i++] = K_SPECIAL;
4797 temp[i++] = K_SECOND(n);
4798 temp[i++] = K_THIRD(n);
4799 }
4800#ifdef FEAT_MBYTE
4801 else if (has_mbyte)
4802 i += (*mb_char2bytes)(n, temp + i);
4803#endif
4804 else
4805 temp[i++] = n;
4806 temp[i++] = NUL;
4807 rettv->v_type = VAR_STRING;
4808 rettv->vval.v_string = vim_strsave(temp);
4809
4810#ifdef FEAT_MOUSE
4811 if (is_mouse_key(n))
4812 {
4813 int row = mouse_row;
4814 int col = mouse_col;
4815 win_T *win;
4816 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818 int winnr = 1;
4819
4820 if (row >= 0 && col >= 0)
4821 {
4822 /* Find the window at the mouse coordinates and compute the
4823 * text position. */
4824 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004825 if (win == NULL)
4826 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004827 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828 for (wp = firstwin; wp != win; wp = wp->w_next)
4829 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4831 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4832 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4833 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4834 }
4835 }
4836#endif
4837 }
4838}
4839
4840/*
4841 * "getcharmod()" function
4842 */
4843 static void
4844f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4845{
4846 rettv->vval.v_number = mod_mask;
4847}
4848
4849/*
4850 * "getcharsearch()" function
4851 */
4852 static void
4853f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4854{
4855 if (rettv_dict_alloc(rettv) != FAIL)
4856 {
4857 dict_T *dict = rettv->vval.v_dict;
4858
Bram Moolenaare0be1672018-07-08 16:50:37 +02004859 dict_add_string(dict, "char", last_csearch());
4860 dict_add_number(dict, "forward", last_csearch_forward());
4861 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862 }
4863}
4864
4865/*
4866 * "getcmdline()" function
4867 */
4868 static void
4869f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4870{
4871 rettv->v_type = VAR_STRING;
4872 rettv->vval.v_string = get_cmdline_str();
4873}
4874
4875/*
4876 * "getcmdpos()" function
4877 */
4878 static void
4879f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4880{
4881 rettv->vval.v_number = get_cmdline_pos() + 1;
4882}
4883
4884/*
4885 * "getcmdtype()" function
4886 */
4887 static void
4888f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4889{
4890 rettv->v_type = VAR_STRING;
4891 rettv->vval.v_string = alloc(2);
4892 if (rettv->vval.v_string != NULL)
4893 {
4894 rettv->vval.v_string[0] = get_cmdline_type();
4895 rettv->vval.v_string[1] = NUL;
4896 }
4897}
4898
4899/*
4900 * "getcmdwintype()" function
4901 */
4902 static void
4903f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4904{
4905 rettv->v_type = VAR_STRING;
4906 rettv->vval.v_string = NULL;
4907#ifdef FEAT_CMDWIN
4908 rettv->vval.v_string = alloc(2);
4909 if (rettv->vval.v_string != NULL)
4910 {
4911 rettv->vval.v_string[0] = cmdwin_type;
4912 rettv->vval.v_string[1] = NUL;
4913 }
4914#endif
4915}
4916
4917#if defined(FEAT_CMDL_COMPL)
4918/*
4919 * "getcompletion()" function
4920 */
4921 static void
4922f_getcompletion(typval_T *argvars, typval_T *rettv)
4923{
4924 char_u *pat;
4925 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004926 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004927 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4928 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004930 if (argvars[2].v_type != VAR_UNKNOWN)
4931 filtered = get_tv_number_chk(&argvars[2], NULL);
4932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 if (p_wic)
4934 options |= WILD_ICASE;
4935
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004936 /* For filtered results, 'wildignore' is used */
4937 if (!filtered)
4938 options |= WILD_KEEP_ALL;
4939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 ExpandInit(&xpc);
4941 xpc.xp_pattern = get_tv_string(&argvars[0]);
4942 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4943 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4944 if (xpc.xp_context == EXPAND_NOTHING)
4945 {
4946 if (argvars[1].v_type == VAR_STRING)
4947 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4948 else
4949 EMSG(_(e_invarg));
4950 return;
4951 }
4952
4953# if defined(FEAT_MENU)
4954 if (xpc.xp_context == EXPAND_MENUS)
4955 {
4956 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4957 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4958 }
4959# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004960#ifdef FEAT_CSCOPE
4961 if (xpc.xp_context == EXPAND_CSCOPE)
4962 {
4963 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4964 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4965 }
4966#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004967#ifdef FEAT_SIGNS
4968 if (xpc.xp_context == EXPAND_SIGN)
4969 {
4970 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4971 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4972 }
4973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004974
4975 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4976 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4977 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004978 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004979
4980 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4981
4982 for (i = 0; i < xpc.xp_numfiles; i++)
4983 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4984 }
4985 vim_free(pat);
4986 ExpandCleanup(&xpc);
4987}
4988#endif
4989
4990/*
4991 * "getcwd()" function
4992 */
4993 static void
4994f_getcwd(typval_T *argvars, typval_T *rettv)
4995{
4996 win_T *wp = NULL;
4997 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004998 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999
5000 rettv->v_type = VAR_STRING;
5001 rettv->vval.v_string = NULL;
5002
Bram Moolenaar54591292018-02-09 20:53:59 +01005003 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5004 global = TRUE;
5005 else
5006 wp = find_tabwin(&argvars[0], &argvars[1]);
5007
5008 if (wp != NULL && wp->w_localdir != NULL)
5009 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5010 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005012 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 rettv->vval.v_string = vim_strsave(globaldir);
5014 else
5015 {
5016 cwd = alloc(MAXPATHL);
5017 if (cwd != NULL)
5018 {
5019 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5020 rettv->vval.v_string = vim_strsave(cwd);
5021 vim_free(cwd);
5022 }
5023 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005025#ifdef BACKSLASH_IN_FILENAME
5026 if (rettv->vval.v_string != NULL)
5027 slash_adjust(rettv->vval.v_string);
5028#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029}
5030
5031/*
5032 * "getfontname()" function
5033 */
5034 static void
5035f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5036{
5037 rettv->v_type = VAR_STRING;
5038 rettv->vval.v_string = NULL;
5039#ifdef FEAT_GUI
5040 if (gui.in_use)
5041 {
5042 GuiFont font;
5043 char_u *name = NULL;
5044
5045 if (argvars[0].v_type == VAR_UNKNOWN)
5046 {
5047 /* Get the "Normal" font. Either the name saved by
5048 * hl_set_font_name() or from the font ID. */
5049 font = gui.norm_font;
5050 name = hl_get_font_name();
5051 }
5052 else
5053 {
5054 name = get_tv_string(&argvars[0]);
5055 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5056 return;
5057 font = gui_mch_get_font(name, FALSE);
5058 if (font == NOFONT)
5059 return; /* Invalid font name, return empty string. */
5060 }
5061 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5062 if (argvars[0].v_type != VAR_UNKNOWN)
5063 gui_mch_free_font(font);
5064 }
5065#endif
5066}
5067
5068/*
5069 * "getfperm({fname})" function
5070 */
5071 static void
5072f_getfperm(typval_T *argvars, typval_T *rettv)
5073{
5074 char_u *fname;
5075 stat_T st;
5076 char_u *perm = NULL;
5077 char_u flags[] = "rwx";
5078 int i;
5079
5080 fname = get_tv_string(&argvars[0]);
5081
5082 rettv->v_type = VAR_STRING;
5083 if (mch_stat((char *)fname, &st) >= 0)
5084 {
5085 perm = vim_strsave((char_u *)"---------");
5086 if (perm != NULL)
5087 {
5088 for (i = 0; i < 9; i++)
5089 {
5090 if (st.st_mode & (1 << (8 - i)))
5091 perm[i] = flags[i % 3];
5092 }
5093 }
5094 }
5095 rettv->vval.v_string = perm;
5096}
5097
5098/*
5099 * "getfsize({fname})" function
5100 */
5101 static void
5102f_getfsize(typval_T *argvars, typval_T *rettv)
5103{
5104 char_u *fname;
5105 stat_T st;
5106
5107 fname = get_tv_string(&argvars[0]);
5108
5109 rettv->v_type = VAR_NUMBER;
5110
5111 if (mch_stat((char *)fname, &st) >= 0)
5112 {
5113 if (mch_isdir(fname))
5114 rettv->vval.v_number = 0;
5115 else
5116 {
5117 rettv->vval.v_number = (varnumber_T)st.st_size;
5118
5119 /* non-perfect check for overflow */
5120 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5121 rettv->vval.v_number = -2;
5122 }
5123 }
5124 else
5125 rettv->vval.v_number = -1;
5126}
5127
5128/*
5129 * "getftime({fname})" function
5130 */
5131 static void
5132f_getftime(typval_T *argvars, typval_T *rettv)
5133{
5134 char_u *fname;
5135 stat_T st;
5136
5137 fname = get_tv_string(&argvars[0]);
5138
5139 if (mch_stat((char *)fname, &st) >= 0)
5140 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5141 else
5142 rettv->vval.v_number = -1;
5143}
5144
5145/*
5146 * "getftype({fname})" function
5147 */
5148 static void
5149f_getftype(typval_T *argvars, typval_T *rettv)
5150{
5151 char_u *fname;
5152 stat_T st;
5153 char_u *type = NULL;
5154 char *t;
5155
5156 fname = get_tv_string(&argvars[0]);
5157
5158 rettv->v_type = VAR_STRING;
5159 if (mch_lstat((char *)fname, &st) >= 0)
5160 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 if (S_ISREG(st.st_mode))
5162 t = "file";
5163 else if (S_ISDIR(st.st_mode))
5164 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 else if (S_ISLNK(st.st_mode))
5166 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 else if (S_ISBLK(st.st_mode))
5168 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 else if (S_ISCHR(st.st_mode))
5170 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 else if (S_ISFIFO(st.st_mode))
5172 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005174 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 else
5176 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 type = vim_strsave((char_u *)t);
5178 }
5179 rettv->vval.v_string = type;
5180}
5181
5182/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005183 * "getjumplist()" function
5184 */
5185 static void
5186f_getjumplist(typval_T *argvars, typval_T *rettv)
5187{
5188#ifdef FEAT_JUMPLIST
5189 win_T *wp;
5190 int i;
5191 list_T *l;
5192 dict_T *d;
5193#endif
5194
5195 if (rettv_list_alloc(rettv) != OK)
5196 return;
5197
5198#ifdef FEAT_JUMPLIST
5199 wp = find_tabwin(&argvars[0], &argvars[1]);
5200 if (wp == NULL)
5201 return;
5202
5203 l = list_alloc();
5204 if (l == NULL)
5205 return;
5206
5207 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5208 return;
5209 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5210
Bram Moolenaar48679742018-02-13 13:33:29 +01005211 cleanup_jumplist(wp, TRUE);
5212
Bram Moolenaar4f505882018-02-10 21:06:32 +01005213 for (i = 0; i < wp->w_jumplistlen; ++i)
5214 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005215 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5216 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005217 if ((d = dict_alloc()) == NULL)
5218 return;
5219 if (list_append_dict(l, d) == FAIL)
5220 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005221 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5222 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005223# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005224 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005225# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005226 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005227 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005228 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005229 }
5230#endif
5231}
5232
5233/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 * "getline(lnum, [end])" function
5235 */
5236 static void
5237f_getline(typval_T *argvars, typval_T *rettv)
5238{
5239 linenr_T lnum;
5240 linenr_T end;
5241 int retlist;
5242
5243 lnum = get_tv_lnum(argvars);
5244 if (argvars[1].v_type == VAR_UNKNOWN)
5245 {
5246 end = 0;
5247 retlist = FALSE;
5248 }
5249 else
5250 {
5251 end = get_tv_lnum(&argvars[1]);
5252 retlist = TRUE;
5253 }
5254
5255 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5256}
5257
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005258#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005259 static void
5260get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5261{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005262 if (what_arg->v_type == VAR_UNKNOWN)
5263 {
5264 if (rettv_list_alloc(rettv) == OK)
5265 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005266 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005267 }
5268 else
5269 {
5270 if (rettv_dict_alloc(rettv) == OK)
5271 if (is_qf || (wp != NULL))
5272 {
5273 if (what_arg->v_type == VAR_DICT)
5274 {
5275 dict_T *d = what_arg->vval.v_dict;
5276
5277 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005278 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005279 }
5280 else
5281 EMSG(_(e_dictreq));
5282 }
5283 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005284}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005285#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005286
5287/*
5288 * "getloclist()" function
5289 */
5290 static void
5291f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5292{
5293#ifdef FEAT_QUICKFIX
5294 win_T *wp;
5295
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005296 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005297 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5298#endif
5299}
5300
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301/*
5302 * "getmatches()" function
5303 */
5304 static void
5305f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5306{
5307#ifdef FEAT_SEARCH_EXTRA
5308 dict_T *dict;
5309 matchitem_T *cur = curwin->w_match_head;
5310 int i;
5311
5312 if (rettv_list_alloc(rettv) == OK)
5313 {
5314 while (cur != NULL)
5315 {
5316 dict = dict_alloc();
5317 if (dict == NULL)
5318 return;
5319 if (cur->match.regprog == NULL)
5320 {
5321 /* match added with matchaddpos() */
5322 for (i = 0; i < MAXPOSMATCH; ++i)
5323 {
5324 llpos_T *llpos;
5325 char buf[6];
5326 list_T *l;
5327
5328 llpos = &cur->pos.pos[i];
5329 if (llpos->lnum == 0)
5330 break;
5331 l = list_alloc();
5332 if (l == NULL)
5333 break;
5334 list_append_number(l, (varnumber_T)llpos->lnum);
5335 if (llpos->col > 0)
5336 {
5337 list_append_number(l, (varnumber_T)llpos->col);
5338 list_append_number(l, (varnumber_T)llpos->len);
5339 }
5340 sprintf(buf, "pos%d", i + 1);
5341 dict_add_list(dict, buf, l);
5342 }
5343 }
5344 else
5345 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005346 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005348 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5349 dict_add_number(dict, "priority", (long)cur->priority);
5350 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5352 if (cur->conceal_char)
5353 {
5354 char_u buf[MB_MAXBYTES + 1];
5355
5356 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 }
5359# endif
5360 list_append_dict(rettv->vval.v_list, dict);
5361 cur = cur->next;
5362 }
5363 }
5364#endif
5365}
5366
5367/*
5368 * "getpid()" function
5369 */
5370 static void
5371f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5372{
5373 rettv->vval.v_number = mch_get_pid();
5374}
5375
5376 static void
5377getpos_both(
5378 typval_T *argvars,
5379 typval_T *rettv,
5380 int getcurpos)
5381{
5382 pos_T *fp;
5383 list_T *l;
5384 int fnum = -1;
5385
5386 if (rettv_list_alloc(rettv) == OK)
5387 {
5388 l = rettv->vval.v_list;
5389 if (getcurpos)
5390 fp = &curwin->w_cursor;
5391 else
5392 fp = var2fpos(&argvars[0], TRUE, &fnum);
5393 if (fnum != -1)
5394 list_append_number(l, (varnumber_T)fnum);
5395 else
5396 list_append_number(l, (varnumber_T)0);
5397 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5398 : (varnumber_T)0);
5399 list_append_number(l, (fp != NULL)
5400 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5401 : (varnumber_T)0);
5402 list_append_number(l,
5403#ifdef FEAT_VIRTUALEDIT
5404 (fp != NULL) ? (varnumber_T)fp->coladd :
5405#endif
5406 (varnumber_T)0);
5407 if (getcurpos)
5408 {
5409 update_curswant();
5410 list_append_number(l, curwin->w_curswant == MAXCOL ?
5411 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5412 }
5413 }
5414 else
5415 rettv->vval.v_number = FALSE;
5416}
5417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418/*
5419 * "getcurpos()" function
5420 */
5421 static void
5422f_getcurpos(typval_T *argvars, typval_T *rettv)
5423{
5424 getpos_both(argvars, rettv, TRUE);
5425}
5426
5427/*
5428 * "getpos(string)" function
5429 */
5430 static void
5431f_getpos(typval_T *argvars, typval_T *rettv)
5432{
5433 getpos_both(argvars, rettv, FALSE);
5434}
5435
5436/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005437 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438 */
5439 static void
5440f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5441{
5442#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005443 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444#endif
5445}
5446
5447/*
5448 * "getreg()" function
5449 */
5450 static void
5451f_getreg(typval_T *argvars, typval_T *rettv)
5452{
5453 char_u *strregname;
5454 int regname;
5455 int arg2 = FALSE;
5456 int return_list = FALSE;
5457 int error = FALSE;
5458
5459 if (argvars[0].v_type != VAR_UNKNOWN)
5460 {
5461 strregname = get_tv_string_chk(&argvars[0]);
5462 error = strregname == NULL;
5463 if (argvars[1].v_type != VAR_UNKNOWN)
5464 {
5465 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5466 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5467 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5468 }
5469 }
5470 else
5471 strregname = get_vim_var_str(VV_REG);
5472
5473 if (error)
5474 return;
5475
5476 regname = (strregname == NULL ? '"' : *strregname);
5477 if (regname == 0)
5478 regname = '"';
5479
5480 if (return_list)
5481 {
5482 rettv->v_type = VAR_LIST;
5483 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5484 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5485 if (rettv->vval.v_list == NULL)
5486 (void)rettv_list_alloc(rettv);
5487 else
5488 ++rettv->vval.v_list->lv_refcount;
5489 }
5490 else
5491 {
5492 rettv->v_type = VAR_STRING;
5493 rettv->vval.v_string = get_reg_contents(regname,
5494 arg2 ? GREG_EXPR_SRC : 0);
5495 }
5496}
5497
5498/*
5499 * "getregtype()" function
5500 */
5501 static void
5502f_getregtype(typval_T *argvars, typval_T *rettv)
5503{
5504 char_u *strregname;
5505 int regname;
5506 char_u buf[NUMBUFLEN + 2];
5507 long reglen = 0;
5508
5509 if (argvars[0].v_type != VAR_UNKNOWN)
5510 {
5511 strregname = get_tv_string_chk(&argvars[0]);
5512 if (strregname == NULL) /* type error; errmsg already given */
5513 {
5514 rettv->v_type = VAR_STRING;
5515 rettv->vval.v_string = NULL;
5516 return;
5517 }
5518 }
5519 else
5520 /* Default to v:register */
5521 strregname = get_vim_var_str(VV_REG);
5522
5523 regname = (strregname == NULL ? '"' : *strregname);
5524 if (regname == 0)
5525 regname = '"';
5526
5527 buf[0] = NUL;
5528 buf[1] = NUL;
5529 switch (get_reg_type(regname, &reglen))
5530 {
5531 case MLINE: buf[0] = 'V'; break;
5532 case MCHAR: buf[0] = 'v'; break;
5533 case MBLOCK:
5534 buf[0] = Ctrl_V;
5535 sprintf((char *)buf + 1, "%ld", reglen + 1);
5536 break;
5537 }
5538 rettv->v_type = VAR_STRING;
5539 rettv->vval.v_string = vim_strsave(buf);
5540}
5541
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005542/*
5543 * Returns information (variables, options, etc.) about a tab page
5544 * as a dictionary.
5545 */
5546 static dict_T *
5547get_tabpage_info(tabpage_T *tp, int tp_idx)
5548{
5549 win_T *wp;
5550 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551 list_T *l;
5552
5553 dict = dict_alloc();
5554 if (dict == NULL)
5555 return NULL;
5556
Bram Moolenaare0be1672018-07-08 16:50:37 +02005557 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005558
5559 l = list_alloc();
5560 if (l != NULL)
5561 {
5562 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5563 wp; wp = wp->w_next)
5564 list_append_number(l, (varnumber_T)wp->w_id);
5565 dict_add_list(dict, "windows", l);
5566 }
5567
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005568 /* Make a reference to tabpage variables */
5569 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005570
5571 return dict;
5572}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573
5574/*
5575 * "gettabinfo()" function
5576 */
5577 static void
5578f_gettabinfo(typval_T *argvars, typval_T *rettv)
5579{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005580 tabpage_T *tp, *tparg = NULL;
5581 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005582 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005583
5584 if (rettv_list_alloc(rettv) != OK)
5585 return;
5586
5587 if (argvars[0].v_type != VAR_UNKNOWN)
5588 {
5589 /* Information about one tab page */
5590 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5591 if (tparg == NULL)
5592 return;
5593 }
5594
5595 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005596 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005597 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005598 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005599 if (tparg != NULL && tp != tparg)
5600 continue;
5601 d = get_tabpage_info(tp, tpnr);
5602 if (d != NULL)
5603 list_append_dict(rettv->vval.v_list, d);
5604 if (tparg != NULL)
5605 return;
5606 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005607}
5608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609/*
5610 * "gettabvar()" function
5611 */
5612 static void
5613f_gettabvar(typval_T *argvars, typval_T *rettv)
5614{
5615 win_T *oldcurwin;
5616 tabpage_T *tp, *oldtabpage;
5617 dictitem_T *v;
5618 char_u *varname;
5619 int done = FALSE;
5620
5621 rettv->v_type = VAR_STRING;
5622 rettv->vval.v_string = NULL;
5623
5624 varname = get_tv_string_chk(&argvars[1]);
5625 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5626 if (tp != NULL && varname != NULL)
5627 {
5628 /* Set tp to be our tabpage, temporarily. Also set the window to the
5629 * first window in the tabpage, otherwise the window is not valid. */
5630 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005631 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5632 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633 {
5634 /* look up the variable */
5635 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5636 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5637 if (v != NULL)
5638 {
5639 copy_tv(&v->di_tv, rettv);
5640 done = TRUE;
5641 }
5642 }
5643
5644 /* restore previous notion of curwin */
5645 restore_win(oldcurwin, oldtabpage, TRUE);
5646 }
5647
5648 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5649 copy_tv(&argvars[2], rettv);
5650}
5651
5652/*
5653 * "gettabwinvar()" function
5654 */
5655 static void
5656f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5657{
5658 getwinvar(argvars, rettv, 1);
5659}
5660
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005661/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005662 * "gettagstack()" function
5663 */
5664 static void
5665f_gettagstack(typval_T *argvars, typval_T *rettv)
5666{
5667 win_T *wp = curwin; // default is current window
5668
5669 if (rettv_dict_alloc(rettv) != OK)
5670 return;
5671
5672 if (argvars[0].v_type != VAR_UNKNOWN)
5673 {
5674 wp = find_win_by_nr_or_id(&argvars[0]);
5675 if (wp == NULL)
5676 return;
5677 }
5678
5679 get_tagstack(wp, rettv->vval.v_dict);
5680}
5681
5682/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005683 * Returns information about a window as a dictionary.
5684 */
5685 static dict_T *
5686get_win_info(win_T *wp, short tpnr, short winnr)
5687{
5688 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005689
5690 dict = dict_alloc();
5691 if (dict == NULL)
5692 return NULL;
5693
Bram Moolenaare0be1672018-07-08 16:50:37 +02005694 dict_add_number(dict, "tabnr", tpnr);
5695 dict_add_number(dict, "winnr", winnr);
5696 dict_add_number(dict, "winid", wp->w_id);
5697 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005698 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005699#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005700 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005701#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005702 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005703 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005704 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005705
Bram Moolenaar69905d12017-08-13 18:14:47 +02005706#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005707 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005708#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005709#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005710 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5711 dict_add_number(dict, "loclist",
5712 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005713#endif
5714
Bram Moolenaar30567352016-08-27 21:25:44 +02005715 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005716 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718 return dict;
5719}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720
5721/*
5722 * "getwininfo()" function
5723 */
5724 static void
5725f_getwininfo(typval_T *argvars, typval_T *rettv)
5726{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727 tabpage_T *tp;
5728 win_T *wp = NULL, *wparg = NULL;
5729 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005730 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005731
5732 if (rettv_list_alloc(rettv) != OK)
5733 return;
5734
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005735 if (argvars[0].v_type != VAR_UNKNOWN)
5736 {
5737 wparg = win_id2wp(argvars);
5738 if (wparg == NULL)
5739 return;
5740 }
5741
5742 /* Collect information about either all the windows across all the tab
5743 * pages or one particular window.
5744 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005745 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005746 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005747 tabnr++;
5748 winnr = 0;
5749 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005750 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005751 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752 if (wparg != NULL && wp != wparg)
5753 continue;
5754 d = get_win_info(wp, tabnr, winnr);
5755 if (d != NULL)
5756 list_append_dict(rettv->vval.v_list, d);
5757 if (wparg != NULL)
5758 /* found information about a specific window */
5759 return;
5760 }
5761 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005762}
5763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005765 * "win_findbuf()" function
5766 */
5767 static void
5768f_win_findbuf(typval_T *argvars, typval_T *rettv)
5769{
5770 if (rettv_list_alloc(rettv) != FAIL)
5771 win_findbuf(argvars, rettv->vval.v_list);
5772}
5773
5774/*
5775 * "win_getid()" function
5776 */
5777 static void
5778f_win_getid(typval_T *argvars, typval_T *rettv)
5779{
5780 rettv->vval.v_number = win_getid(argvars);
5781}
5782
5783/*
5784 * "win_gotoid()" function
5785 */
5786 static void
5787f_win_gotoid(typval_T *argvars, typval_T *rettv)
5788{
5789 rettv->vval.v_number = win_gotoid(argvars);
5790}
5791
5792/*
5793 * "win_id2tabwin()" function
5794 */
5795 static void
5796f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5797{
5798 if (rettv_list_alloc(rettv) != FAIL)
5799 win_id2tabwin(argvars, rettv->vval.v_list);
5800}
5801
5802/*
5803 * "win_id2win()" function
5804 */
5805 static void
5806f_win_id2win(typval_T *argvars, typval_T *rettv)
5807{
5808 rettv->vval.v_number = win_id2win(argvars);
5809}
5810
5811/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005812 * "win_screenpos()" function
5813 */
5814 static void
5815f_win_screenpos(typval_T *argvars, typval_T *rettv)
5816{
5817 win_T *wp;
5818
5819 if (rettv_list_alloc(rettv) == FAIL)
5820 return;
5821
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005822 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005823 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5824 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5825}
5826
5827/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005828 * "getwinpos({timeout})" function
5829 */
5830 static void
5831f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5832{
5833 int x = -1;
5834 int y = -1;
5835
5836 if (rettv_list_alloc(rettv) == FAIL)
5837 return;
5838#ifdef FEAT_GUI
5839 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005840 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005841# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5842 else
5843# endif
5844#endif
5845#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5846 {
5847 varnumber_T timeout = 100;
5848
5849 if (argvars[0].v_type != VAR_UNKNOWN)
5850 timeout = get_tv_number(&argvars[0]);
5851 term_get_winpos(&x, &y, timeout);
5852 }
5853#endif
5854 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5855 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5856}
5857
5858
5859/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005860 * "getwinposx()" function
5861 */
5862 static void
5863f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5864{
5865 rettv->vval.v_number = -1;
5866#ifdef FEAT_GUI
5867 if (gui.in_use)
5868 {
5869 int x, y;
5870
5871 if (gui_mch_get_winpos(&x, &y) == OK)
5872 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005873 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005874 }
5875#endif
5876#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5877 {
5878 int x, y;
5879
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005880 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005881 rettv->vval.v_number = x;
5882 }
5883#endif
5884}
5885
5886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887 * "getwinposy()" function
5888 */
5889 static void
5890f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5891{
5892 rettv->vval.v_number = -1;
5893#ifdef FEAT_GUI
5894 if (gui.in_use)
5895 {
5896 int x, y;
5897
5898 if (gui_mch_get_winpos(&x, &y) == OK)
5899 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005900 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901 }
5902#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005903#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5904 {
5905 int x, y;
5906
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005907 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005908 rettv->vval.v_number = y;
5909 }
5910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911}
5912
5913/*
5914 * "getwinvar()" function
5915 */
5916 static void
5917f_getwinvar(typval_T *argvars, typval_T *rettv)
5918{
5919 getwinvar(argvars, rettv, 0);
5920}
5921
5922/*
5923 * "glob()" function
5924 */
5925 static void
5926f_glob(typval_T *argvars, typval_T *rettv)
5927{
5928 int options = WILD_SILENT|WILD_USE_NL;
5929 expand_T xpc;
5930 int error = FALSE;
5931
5932 /* When the optional second argument is non-zero, don't remove matches
5933 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5934 rettv->v_type = VAR_STRING;
5935 if (argvars[1].v_type != VAR_UNKNOWN)
5936 {
5937 if (get_tv_number_chk(&argvars[1], &error))
5938 options |= WILD_KEEP_ALL;
5939 if (argvars[2].v_type != VAR_UNKNOWN)
5940 {
5941 if (get_tv_number_chk(&argvars[2], &error))
5942 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005943 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 }
5945 if (argvars[3].v_type != VAR_UNKNOWN
5946 && get_tv_number_chk(&argvars[3], &error))
5947 options |= WILD_ALLLINKS;
5948 }
5949 }
5950 if (!error)
5951 {
5952 ExpandInit(&xpc);
5953 xpc.xp_context = EXPAND_FILES;
5954 if (p_wic)
5955 options += WILD_ICASE;
5956 if (rettv->v_type == VAR_STRING)
5957 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5958 NULL, options, WILD_ALL);
5959 else if (rettv_list_alloc(rettv) != FAIL)
5960 {
5961 int i;
5962
5963 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5964 NULL, options, WILD_ALL_KEEP);
5965 for (i = 0; i < xpc.xp_numfiles; i++)
5966 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5967
5968 ExpandCleanup(&xpc);
5969 }
5970 }
5971 else
5972 rettv->vval.v_string = NULL;
5973}
5974
5975/*
5976 * "globpath()" function
5977 */
5978 static void
5979f_globpath(typval_T *argvars, typval_T *rettv)
5980{
5981 int flags = 0;
5982 char_u buf1[NUMBUFLEN];
5983 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5984 int error = FALSE;
5985 garray_T ga;
5986 int i;
5987
5988 /* When the optional second argument is non-zero, don't remove matches
5989 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5990 rettv->v_type = VAR_STRING;
5991 if (argvars[2].v_type != VAR_UNKNOWN)
5992 {
5993 if (get_tv_number_chk(&argvars[2], &error))
5994 flags |= WILD_KEEP_ALL;
5995 if (argvars[3].v_type != VAR_UNKNOWN)
5996 {
5997 if (get_tv_number_chk(&argvars[3], &error))
5998 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005999 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 }
6001 if (argvars[4].v_type != VAR_UNKNOWN
6002 && get_tv_number_chk(&argvars[4], &error))
6003 flags |= WILD_ALLLINKS;
6004 }
6005 }
6006 if (file != NULL && !error)
6007 {
6008 ga_init2(&ga, (int)sizeof(char_u *), 10);
6009 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
6010 if (rettv->v_type == VAR_STRING)
6011 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6012 else if (rettv_list_alloc(rettv) != FAIL)
6013 for (i = 0; i < ga.ga_len; ++i)
6014 list_append_string(rettv->vval.v_list,
6015 ((char_u **)(ga.ga_data))[i], -1);
6016 ga_clear_strings(&ga);
6017 }
6018 else
6019 rettv->vval.v_string = NULL;
6020}
6021
6022/*
6023 * "glob2regpat()" function
6024 */
6025 static void
6026f_glob2regpat(typval_T *argvars, typval_T *rettv)
6027{
6028 char_u *pat = get_tv_string_chk(&argvars[0]);
6029
6030 rettv->v_type = VAR_STRING;
6031 rettv->vval.v_string = (pat == NULL)
6032 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6033}
6034
6035/* for VIM_VERSION_ defines */
6036#include "version.h"
6037
6038/*
6039 * "has()" function
6040 */
6041 static void
6042f_has(typval_T *argvars, typval_T *rettv)
6043{
6044 int i;
6045 char_u *name;
6046 int n = FALSE;
6047 static char *(has_list[]) =
6048 {
6049#ifdef AMIGA
6050 "amiga",
6051# ifdef FEAT_ARP
6052 "arp",
6053# endif
6054#endif
6055#ifdef __BEOS__
6056 "beos",
6057#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006058#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006059 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6060 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006061# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006062 "macunix", /* Mac OS X, with the darwin feature */
6063 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006064# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006065#endif
6066#ifdef __QNX__
6067 "qnx",
6068#endif
6069#ifdef UNIX
6070 "unix",
6071#endif
6072#ifdef VMS
6073 "vms",
6074#endif
6075#ifdef WIN32
6076 "win32",
6077#endif
6078#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6079 "win32unix",
6080#endif
6081#if defined(WIN64) || defined(_WIN64)
6082 "win64",
6083#endif
6084#ifdef EBCDIC
6085 "ebcdic",
6086#endif
6087#ifndef CASE_INSENSITIVE_FILENAME
6088 "fname_case",
6089#endif
6090#ifdef HAVE_ACL
6091 "acl",
6092#endif
6093#ifdef FEAT_ARABIC
6094 "arabic",
6095#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006097#ifdef FEAT_AUTOCHDIR
6098 "autochdir",
6099#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006100#ifdef FEAT_AUTOSERVERNAME
6101 "autoservername",
6102#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006103#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006104 "balloon_eval",
6105# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6106 "balloon_multiline",
6107# endif
6108#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006109#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006110 "balloon_eval_term",
6111#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6113 "builtin_terms",
6114# ifdef ALL_BUILTIN_TCAPS
6115 "all_builtin_terms",
6116# endif
6117#endif
6118#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6119 || defined(FEAT_GUI_W32) \
6120 || defined(FEAT_GUI_MOTIF))
6121 "browsefilter",
6122#endif
6123#ifdef FEAT_BYTEOFF
6124 "byte_offset",
6125#endif
6126#ifdef FEAT_JOB_CHANNEL
6127 "channel",
6128#endif
6129#ifdef FEAT_CINDENT
6130 "cindent",
6131#endif
6132#ifdef FEAT_CLIENTSERVER
6133 "clientserver",
6134#endif
6135#ifdef FEAT_CLIPBOARD
6136 "clipboard",
6137#endif
6138#ifdef FEAT_CMDL_COMPL
6139 "cmdline_compl",
6140#endif
6141#ifdef FEAT_CMDHIST
6142 "cmdline_hist",
6143#endif
6144#ifdef FEAT_COMMENTS
6145 "comments",
6146#endif
6147#ifdef FEAT_CONCEAL
6148 "conceal",
6149#endif
6150#ifdef FEAT_CRYPT
6151 "cryptv",
6152 "crypt-blowfish",
6153 "crypt-blowfish2",
6154#endif
6155#ifdef FEAT_CSCOPE
6156 "cscope",
6157#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159#ifdef CURSOR_SHAPE
6160 "cursorshape",
6161#endif
6162#ifdef DEBUG
6163 "debug",
6164#endif
6165#ifdef FEAT_CON_DIALOG
6166 "dialog_con",
6167#endif
6168#ifdef FEAT_GUI_DIALOG
6169 "dialog_gui",
6170#endif
6171#ifdef FEAT_DIFF
6172 "diff",
6173#endif
6174#ifdef FEAT_DIGRAPHS
6175 "digraphs",
6176#endif
6177#ifdef FEAT_DIRECTX
6178 "directx",
6179#endif
6180#ifdef FEAT_DND
6181 "dnd",
6182#endif
6183#ifdef FEAT_EMACS_TAGS
6184 "emacs_tags",
6185#endif
6186 "eval", /* always present, of course! */
6187 "ex_extra", /* graduated feature */
6188#ifdef FEAT_SEARCH_EXTRA
6189 "extra_search",
6190#endif
6191#ifdef FEAT_FKMAP
6192 "farsi",
6193#endif
6194#ifdef FEAT_SEARCHPATH
6195 "file_in_path",
6196#endif
6197#ifdef FEAT_FILTERPIPE
6198 "filterpipe",
6199#endif
6200#ifdef FEAT_FIND_ID
6201 "find_in_path",
6202#endif
6203#ifdef FEAT_FLOAT
6204 "float",
6205#endif
6206#ifdef FEAT_FOLDING
6207 "folding",
6208#endif
6209#ifdef FEAT_FOOTER
6210 "footer",
6211#endif
6212#if !defined(USE_SYSTEM) && defined(UNIX)
6213 "fork",
6214#endif
6215#ifdef FEAT_GETTEXT
6216 "gettext",
6217#endif
6218#ifdef FEAT_GUI
6219 "gui",
6220#endif
6221#ifdef FEAT_GUI_ATHENA
6222# ifdef FEAT_GUI_NEXTAW
6223 "gui_neXtaw",
6224# else
6225 "gui_athena",
6226# endif
6227#endif
6228#ifdef FEAT_GUI_GTK
6229 "gui_gtk",
6230# ifdef USE_GTK3
6231 "gui_gtk3",
6232# else
6233 "gui_gtk2",
6234# endif
6235#endif
6236#ifdef FEAT_GUI_GNOME
6237 "gui_gnome",
6238#endif
6239#ifdef FEAT_GUI_MAC
6240 "gui_mac",
6241#endif
6242#ifdef FEAT_GUI_MOTIF
6243 "gui_motif",
6244#endif
6245#ifdef FEAT_GUI_PHOTON
6246 "gui_photon",
6247#endif
6248#ifdef FEAT_GUI_W32
6249 "gui_win32",
6250#endif
6251#ifdef FEAT_HANGULIN
6252 "hangul_input",
6253#endif
6254#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6255 "iconv",
6256#endif
6257#ifdef FEAT_INS_EXPAND
6258 "insert_expand",
6259#endif
6260#ifdef FEAT_JOB_CHANNEL
6261 "job",
6262#endif
6263#ifdef FEAT_JUMPLIST
6264 "jumplist",
6265#endif
6266#ifdef FEAT_KEYMAP
6267 "keymap",
6268#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006269 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270#ifdef FEAT_LANGMAP
6271 "langmap",
6272#endif
6273#ifdef FEAT_LIBCALL
6274 "libcall",
6275#endif
6276#ifdef FEAT_LINEBREAK
6277 "linebreak",
6278#endif
6279#ifdef FEAT_LISP
6280 "lispindent",
6281#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283#ifdef FEAT_LOCALMAP
6284 "localmap",
6285#endif
6286#ifdef FEAT_LUA
6287# ifndef DYNAMIC_LUA
6288 "lua",
6289# endif
6290#endif
6291#ifdef FEAT_MENU
6292 "menu",
6293#endif
6294#ifdef FEAT_SESSION
6295 "mksession",
6296#endif
6297#ifdef FEAT_MODIFY_FNAME
6298 "modify_fname",
6299#endif
6300#ifdef FEAT_MOUSE
6301 "mouse",
6302#endif
6303#ifdef FEAT_MOUSESHAPE
6304 "mouseshape",
6305#endif
6306#if defined(UNIX) || defined(VMS)
6307# ifdef FEAT_MOUSE_DEC
6308 "mouse_dec",
6309# endif
6310# ifdef FEAT_MOUSE_GPM
6311 "mouse_gpm",
6312# endif
6313# ifdef FEAT_MOUSE_JSB
6314 "mouse_jsbterm",
6315# endif
6316# ifdef FEAT_MOUSE_NET
6317 "mouse_netterm",
6318# endif
6319# ifdef FEAT_MOUSE_PTERM
6320 "mouse_pterm",
6321# endif
6322# ifdef FEAT_MOUSE_SGR
6323 "mouse_sgr",
6324# endif
6325# ifdef FEAT_SYSMOUSE
6326 "mouse_sysmouse",
6327# endif
6328# ifdef FEAT_MOUSE_URXVT
6329 "mouse_urxvt",
6330# endif
6331# ifdef FEAT_MOUSE_XTERM
6332 "mouse_xterm",
6333# endif
6334#endif
6335#ifdef FEAT_MBYTE
6336 "multi_byte",
6337#endif
6338#ifdef FEAT_MBYTE_IME
6339 "multi_byte_ime",
6340#endif
6341#ifdef FEAT_MULTI_LANG
6342 "multi_lang",
6343#endif
6344#ifdef FEAT_MZSCHEME
6345#ifndef DYNAMIC_MZSCHEME
6346 "mzscheme",
6347#endif
6348#endif
6349#ifdef FEAT_NUM64
6350 "num64",
6351#endif
6352#ifdef FEAT_OLE
6353 "ole",
6354#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006355#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358#ifdef FEAT_PATH_EXTRA
6359 "path_extra",
6360#endif
6361#ifdef FEAT_PERL
6362#ifndef DYNAMIC_PERL
6363 "perl",
6364#endif
6365#endif
6366#ifdef FEAT_PERSISTENT_UNDO
6367 "persistent_undo",
6368#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006369#if defined(FEAT_PYTHON)
6370 "python_compiled",
6371# if defined(DYNAMIC_PYTHON)
6372 "python_dynamic",
6373# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006375 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006376# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006378#if defined(FEAT_PYTHON3)
6379 "python3_compiled",
6380# if defined(DYNAMIC_PYTHON3)
6381 "python3_dynamic",
6382# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006384 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006385# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386#endif
6387#ifdef FEAT_POSTSCRIPT
6388 "postscript",
6389#endif
6390#ifdef FEAT_PRINTER
6391 "printer",
6392#endif
6393#ifdef FEAT_PROFILE
6394 "profile",
6395#endif
6396#ifdef FEAT_RELTIME
6397 "reltime",
6398#endif
6399#ifdef FEAT_QUICKFIX
6400 "quickfix",
6401#endif
6402#ifdef FEAT_RIGHTLEFT
6403 "rightleft",
6404#endif
6405#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6406 "ruby",
6407#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409#ifdef FEAT_CMDL_INFO
6410 "showcmd",
6411 "cmdline_info",
6412#endif
6413#ifdef FEAT_SIGNS
6414 "signs",
6415#endif
6416#ifdef FEAT_SMARTINDENT
6417 "smartindent",
6418#endif
6419#ifdef STARTUPTIME
6420 "startuptime",
6421#endif
6422#ifdef FEAT_STL_OPT
6423 "statusline",
6424#endif
6425#ifdef FEAT_SUN_WORKSHOP
6426 "sun_workshop",
6427#endif
6428#ifdef FEAT_NETBEANS_INTG
6429 "netbeans_intg",
6430#endif
6431#ifdef FEAT_SPELL
6432 "spell",
6433#endif
6434#ifdef FEAT_SYN_HL
6435 "syntax",
6436#endif
6437#if defined(USE_SYSTEM) || !defined(UNIX)
6438 "system",
6439#endif
6440#ifdef FEAT_TAG_BINS
6441 "tag_binary",
6442#endif
6443#ifdef FEAT_TAG_OLDSTATIC
6444 "tag_old_static",
6445#endif
6446#ifdef FEAT_TAG_ANYWHITE
6447 "tag_any_white",
6448#endif
6449#ifdef FEAT_TCL
6450# ifndef DYNAMIC_TCL
6451 "tcl",
6452# endif
6453#endif
6454#ifdef FEAT_TERMGUICOLORS
6455 "termguicolors",
6456#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006457#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006458 "terminal",
6459#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460#ifdef TERMINFO
6461 "terminfo",
6462#endif
6463#ifdef FEAT_TERMRESPONSE
6464 "termresponse",
6465#endif
6466#ifdef FEAT_TEXTOBJ
6467 "textobjects",
6468#endif
6469#ifdef HAVE_TGETENT
6470 "tgetent",
6471#endif
6472#ifdef FEAT_TIMERS
6473 "timers",
6474#endif
6475#ifdef FEAT_TITLE
6476 "title",
6477#endif
6478#ifdef FEAT_TOOLBAR
6479 "toolbar",
6480#endif
6481#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6482 "unnamedplus",
6483#endif
6484#ifdef FEAT_USR_CMDS
6485 "user-commands", /* was accidentally included in 5.4 */
6486 "user_commands",
6487#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006488#ifdef FEAT_VARTABS
6489 "vartabs",
6490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491#ifdef FEAT_VIMINFO
6492 "viminfo",
6493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495#ifdef FEAT_VIRTUALEDIT
6496 "virtualedit",
6497#endif
6498 "visual",
6499#ifdef FEAT_VISUALEXTRA
6500 "visualextra",
6501#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006502 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006503#ifdef FEAT_VTP
6504 "vtp",
6505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506#ifdef FEAT_WILDIGN
6507 "wildignore",
6508#endif
6509#ifdef FEAT_WILDMENU
6510 "wildmenu",
6511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef FEAT_WAK
6514 "winaltkeys",
6515#endif
6516#ifdef FEAT_WRITEBACKUP
6517 "writebackup",
6518#endif
6519#ifdef FEAT_XIM
6520 "xim",
6521#endif
6522#ifdef FEAT_XFONTSET
6523 "xfontset",
6524#endif
6525#ifdef FEAT_XPM_W32
6526 "xpm",
6527 "xpm_w32", /* for backward compatibility */
6528#else
6529# if defined(HAVE_XPM)
6530 "xpm",
6531# endif
6532#endif
6533#ifdef USE_XSMP
6534 "xsmp",
6535#endif
6536#ifdef USE_XSMP_INTERACT
6537 "xsmp_interact",
6538#endif
6539#ifdef FEAT_XCLIPBOARD
6540 "xterm_clipboard",
6541#endif
6542#ifdef FEAT_XTERM_SAVE
6543 "xterm_save",
6544#endif
6545#if defined(UNIX) && defined(FEAT_X11)
6546 "X11",
6547#endif
6548 NULL
6549 };
6550
6551 name = get_tv_string(&argvars[0]);
6552 for (i = 0; has_list[i] != NULL; ++i)
6553 if (STRICMP(name, has_list[i]) == 0)
6554 {
6555 n = TRUE;
6556 break;
6557 }
6558
6559 if (n == FALSE)
6560 {
6561 if (STRNICMP(name, "patch", 5) == 0)
6562 {
6563 if (name[5] == '-'
6564 && STRLEN(name) >= 11
6565 && vim_isdigit(name[6])
6566 && vim_isdigit(name[8])
6567 && vim_isdigit(name[10]))
6568 {
6569 int major = atoi((char *)name + 6);
6570 int minor = atoi((char *)name + 8);
6571
6572 /* Expect "patch-9.9.01234". */
6573 n = (major < VIM_VERSION_MAJOR
6574 || (major == VIM_VERSION_MAJOR
6575 && (minor < VIM_VERSION_MINOR
6576 || (minor == VIM_VERSION_MINOR
6577 && has_patch(atoi((char *)name + 10))))));
6578 }
6579 else
6580 n = has_patch(atoi((char *)name + 5));
6581 }
6582 else if (STRICMP(name, "vim_starting") == 0)
6583 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006584 else if (STRICMP(name, "ttyin") == 0)
6585 n = mch_input_isatty();
6586 else if (STRICMP(name, "ttyout") == 0)
6587 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588#ifdef FEAT_MBYTE
6589 else if (STRICMP(name, "multi_byte_encoding") == 0)
6590 n = has_mbyte;
6591#endif
6592#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6593 else if (STRICMP(name, "balloon_multiline") == 0)
6594 n = multiline_balloon_available();
6595#endif
6596#ifdef DYNAMIC_TCL
6597 else if (STRICMP(name, "tcl") == 0)
6598 n = tcl_enabled(FALSE);
6599#endif
6600#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6601 else if (STRICMP(name, "iconv") == 0)
6602 n = iconv_enabled(FALSE);
6603#endif
6604#ifdef DYNAMIC_LUA
6605 else if (STRICMP(name, "lua") == 0)
6606 n = lua_enabled(FALSE);
6607#endif
6608#ifdef DYNAMIC_MZSCHEME
6609 else if (STRICMP(name, "mzscheme") == 0)
6610 n = mzscheme_enabled(FALSE);
6611#endif
6612#ifdef DYNAMIC_RUBY
6613 else if (STRICMP(name, "ruby") == 0)
6614 n = ruby_enabled(FALSE);
6615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616#ifdef DYNAMIC_PYTHON
6617 else if (STRICMP(name, "python") == 0)
6618 n = python_enabled(FALSE);
6619#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620#ifdef DYNAMIC_PYTHON3
6621 else if (STRICMP(name, "python3") == 0)
6622 n = python3_enabled(FALSE);
6623#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006624#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6625 else if (STRICMP(name, "pythonx") == 0)
6626 {
6627# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6628 if (p_pyx == 0)
6629 n = python3_enabled(FALSE) || python_enabled(FALSE);
6630 else if (p_pyx == 3)
6631 n = python3_enabled(FALSE);
6632 else if (p_pyx == 2)
6633 n = python_enabled(FALSE);
6634# elif defined(DYNAMIC_PYTHON)
6635 n = python_enabled(FALSE);
6636# elif defined(DYNAMIC_PYTHON3)
6637 n = python3_enabled(FALSE);
6638# endif
6639 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640#endif
6641#ifdef DYNAMIC_PERL
6642 else if (STRICMP(name, "perl") == 0)
6643 n = perl_enabled(FALSE);
6644#endif
6645#ifdef FEAT_GUI
6646 else if (STRICMP(name, "gui_running") == 0)
6647 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648# ifdef FEAT_BROWSE
6649 else if (STRICMP(name, "browse") == 0)
6650 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6651# endif
6652#endif
6653#ifdef FEAT_SYN_HL
6654 else if (STRICMP(name, "syntax_items") == 0)
6655 n = syntax_present(curwin);
6656#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006657#ifdef FEAT_VTP
6658 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006659 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660#endif
6661#ifdef FEAT_NETBEANS_INTG
6662 else if (STRICMP(name, "netbeans_enabled") == 0)
6663 n = netbeans_active();
6664#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006665#if defined(FEAT_TERMINAL) && defined(WIN3264)
6666 else if (STRICMP(name, "terminal") == 0)
6667 n = terminal_enabled();
6668#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 }
6670
6671 rettv->vval.v_number = n;
6672}
6673
6674/*
6675 * "has_key()" function
6676 */
6677 static void
6678f_has_key(typval_T *argvars, typval_T *rettv)
6679{
6680 if (argvars[0].v_type != VAR_DICT)
6681 {
6682 EMSG(_(e_dictreq));
6683 return;
6684 }
6685 if (argvars[0].vval.v_dict == NULL)
6686 return;
6687
6688 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6689 get_tv_string(&argvars[1]), -1) != NULL;
6690}
6691
6692/*
6693 * "haslocaldir()" function
6694 */
6695 static void
6696f_haslocaldir(typval_T *argvars, typval_T *rettv)
6697{
6698 win_T *wp = NULL;
6699
6700 wp = find_tabwin(&argvars[0], &argvars[1]);
6701 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6702}
6703
6704/*
6705 * "hasmapto()" function
6706 */
6707 static void
6708f_hasmapto(typval_T *argvars, typval_T *rettv)
6709{
6710 char_u *name;
6711 char_u *mode;
6712 char_u buf[NUMBUFLEN];
6713 int abbr = FALSE;
6714
6715 name = get_tv_string(&argvars[0]);
6716 if (argvars[1].v_type == VAR_UNKNOWN)
6717 mode = (char_u *)"nvo";
6718 else
6719 {
6720 mode = get_tv_string_buf(&argvars[1], buf);
6721 if (argvars[2].v_type != VAR_UNKNOWN)
6722 abbr = (int)get_tv_number(&argvars[2]);
6723 }
6724
6725 if (map_to_exists(name, mode, abbr))
6726 rettv->vval.v_number = TRUE;
6727 else
6728 rettv->vval.v_number = FALSE;
6729}
6730
6731/*
6732 * "histadd()" function
6733 */
6734 static void
6735f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6736{
6737#ifdef FEAT_CMDHIST
6738 int histype;
6739 char_u *str;
6740 char_u buf[NUMBUFLEN];
6741#endif
6742
6743 rettv->vval.v_number = FALSE;
6744 if (check_restricted() || check_secure())
6745 return;
6746#ifdef FEAT_CMDHIST
6747 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6748 histype = str != NULL ? get_histtype(str) : -1;
6749 if (histype >= 0)
6750 {
6751 str = get_tv_string_buf(&argvars[1], buf);
6752 if (*str != NUL)
6753 {
6754 init_history();
6755 add_to_history(histype, str, FALSE, NUL);
6756 rettv->vval.v_number = TRUE;
6757 return;
6758 }
6759 }
6760#endif
6761}
6762
6763/*
6764 * "histdel()" function
6765 */
6766 static void
6767f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6768{
6769#ifdef FEAT_CMDHIST
6770 int n;
6771 char_u buf[NUMBUFLEN];
6772 char_u *str;
6773
6774 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6775 if (str == NULL)
6776 n = 0;
6777 else if (argvars[1].v_type == VAR_UNKNOWN)
6778 /* only one argument: clear entire history */
6779 n = clr_history(get_histtype(str));
6780 else if (argvars[1].v_type == VAR_NUMBER)
6781 /* index given: remove that entry */
6782 n = del_history_idx(get_histtype(str),
6783 (int)get_tv_number(&argvars[1]));
6784 else
6785 /* string given: remove all matching entries */
6786 n = del_history_entry(get_histtype(str),
6787 get_tv_string_buf(&argvars[1], buf));
6788 rettv->vval.v_number = n;
6789#endif
6790}
6791
6792/*
6793 * "histget()" function
6794 */
6795 static void
6796f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6797{
6798#ifdef FEAT_CMDHIST
6799 int type;
6800 int idx;
6801 char_u *str;
6802
6803 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6804 if (str == NULL)
6805 rettv->vval.v_string = NULL;
6806 else
6807 {
6808 type = get_histtype(str);
6809 if (argvars[1].v_type == VAR_UNKNOWN)
6810 idx = get_history_idx(type);
6811 else
6812 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6813 /* -1 on type error */
6814 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6815 }
6816#else
6817 rettv->vval.v_string = NULL;
6818#endif
6819 rettv->v_type = VAR_STRING;
6820}
6821
6822/*
6823 * "histnr()" function
6824 */
6825 static void
6826f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6827{
6828 int i;
6829
6830#ifdef FEAT_CMDHIST
6831 char_u *history = get_tv_string_chk(&argvars[0]);
6832
6833 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6834 if (i >= HIST_CMD && i < HIST_COUNT)
6835 i = get_history_idx(i);
6836 else
6837#endif
6838 i = -1;
6839 rettv->vval.v_number = i;
6840}
6841
6842/*
6843 * "highlightID(name)" function
6844 */
6845 static void
6846f_hlID(typval_T *argvars, typval_T *rettv)
6847{
6848 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6849}
6850
6851/*
6852 * "highlight_exists()" function
6853 */
6854 static void
6855f_hlexists(typval_T *argvars, typval_T *rettv)
6856{
6857 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6858}
6859
6860/*
6861 * "hostname()" function
6862 */
6863 static void
6864f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6865{
6866 char_u hostname[256];
6867
6868 mch_get_host_name(hostname, 256);
6869 rettv->v_type = VAR_STRING;
6870 rettv->vval.v_string = vim_strsave(hostname);
6871}
6872
6873/*
6874 * iconv() function
6875 */
6876 static void
6877f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6878{
6879#ifdef FEAT_MBYTE
6880 char_u buf1[NUMBUFLEN];
6881 char_u buf2[NUMBUFLEN];
6882 char_u *from, *to, *str;
6883 vimconv_T vimconv;
6884#endif
6885
6886 rettv->v_type = VAR_STRING;
6887 rettv->vval.v_string = NULL;
6888
6889#ifdef FEAT_MBYTE
6890 str = get_tv_string(&argvars[0]);
6891 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6892 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6893 vimconv.vc_type = CONV_NONE;
6894 convert_setup(&vimconv, from, to);
6895
6896 /* If the encodings are equal, no conversion needed. */
6897 if (vimconv.vc_type == CONV_NONE)
6898 rettv->vval.v_string = vim_strsave(str);
6899 else
6900 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6901
6902 convert_setup(&vimconv, NULL, NULL);
6903 vim_free(from);
6904 vim_free(to);
6905#endif
6906}
6907
6908/*
6909 * "indent()" function
6910 */
6911 static void
6912f_indent(typval_T *argvars, typval_T *rettv)
6913{
6914 linenr_T lnum;
6915
6916 lnum = get_tv_lnum(argvars);
6917 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6918 rettv->vval.v_number = get_indent_lnum(lnum);
6919 else
6920 rettv->vval.v_number = -1;
6921}
6922
6923/*
6924 * "index()" function
6925 */
6926 static void
6927f_index(typval_T *argvars, typval_T *rettv)
6928{
6929 list_T *l;
6930 listitem_T *item;
6931 long idx = 0;
6932 int ic = FALSE;
6933
6934 rettv->vval.v_number = -1;
6935 if (argvars[0].v_type != VAR_LIST)
6936 {
6937 EMSG(_(e_listreq));
6938 return;
6939 }
6940 l = argvars[0].vval.v_list;
6941 if (l != NULL)
6942 {
6943 item = l->lv_first;
6944 if (argvars[2].v_type != VAR_UNKNOWN)
6945 {
6946 int error = FALSE;
6947
6948 /* Start at specified item. Use the cached index that list_find()
6949 * sets, so that a negative number also works. */
6950 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6951 idx = l->lv_idx;
6952 if (argvars[3].v_type != VAR_UNKNOWN)
6953 ic = (int)get_tv_number_chk(&argvars[3], &error);
6954 if (error)
6955 item = NULL;
6956 }
6957
6958 for ( ; item != NULL; item = item->li_next, ++idx)
6959 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6960 {
6961 rettv->vval.v_number = idx;
6962 break;
6963 }
6964 }
6965}
6966
6967static int inputsecret_flag = 0;
6968
6969/*
6970 * "input()" function
6971 * Also handles inputsecret() when inputsecret is set.
6972 */
6973 static void
6974f_input(typval_T *argvars, typval_T *rettv)
6975{
6976 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6977}
6978
6979/*
6980 * "inputdialog()" function
6981 */
6982 static void
6983f_inputdialog(typval_T *argvars, typval_T *rettv)
6984{
6985#if defined(FEAT_GUI_TEXTDIALOG)
6986 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6987 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6988 {
6989 char_u *message;
6990 char_u buf[NUMBUFLEN];
6991 char_u *defstr = (char_u *)"";
6992
6993 message = get_tv_string_chk(&argvars[0]);
6994 if (argvars[1].v_type != VAR_UNKNOWN
6995 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6996 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6997 else
6998 IObuff[0] = NUL;
6999 if (message != NULL && defstr != NULL
7000 && do_dialog(VIM_QUESTION, NULL, message,
7001 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7002 rettv->vval.v_string = vim_strsave(IObuff);
7003 else
7004 {
7005 if (message != NULL && defstr != NULL
7006 && argvars[1].v_type != VAR_UNKNOWN
7007 && argvars[2].v_type != VAR_UNKNOWN)
7008 rettv->vval.v_string = vim_strsave(
7009 get_tv_string_buf(&argvars[2], buf));
7010 else
7011 rettv->vval.v_string = NULL;
7012 }
7013 rettv->v_type = VAR_STRING;
7014 }
7015 else
7016#endif
7017 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7018}
7019
7020/*
7021 * "inputlist()" function
7022 */
7023 static void
7024f_inputlist(typval_T *argvars, typval_T *rettv)
7025{
7026 listitem_T *li;
7027 int selected;
7028 int mouse_used;
7029
7030#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007031 /* While starting up, there is no place to enter text. When running tests
7032 * with --not-a-term we assume feedkeys() will be used. */
7033 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 return;
7035#endif
7036 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7037 {
7038 EMSG2(_(e_listarg), "inputlist()");
7039 return;
7040 }
7041
7042 msg_start();
7043 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7044 lines_left = Rows; /* avoid more prompt */
7045 msg_scroll = TRUE;
7046 msg_clr_eos();
7047
7048 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7049 {
7050 msg_puts(get_tv_string(&li->li_tv));
7051 msg_putchar('\n');
7052 }
7053
7054 /* Ask for choice. */
7055 selected = prompt_for_number(&mouse_used);
7056 if (mouse_used)
7057 selected -= lines_left;
7058
7059 rettv->vval.v_number = selected;
7060}
7061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7063
7064/*
7065 * "inputrestore()" function
7066 */
7067 static void
7068f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7069{
7070 if (ga_userinput.ga_len > 0)
7071 {
7072 --ga_userinput.ga_len;
7073 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7074 + ga_userinput.ga_len);
7075 /* default return is zero == OK */
7076 }
7077 else if (p_verbose > 1)
7078 {
7079 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7080 rettv->vval.v_number = 1; /* Failed */
7081 }
7082}
7083
7084/*
7085 * "inputsave()" function
7086 */
7087 static void
7088f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7089{
7090 /* Add an entry to the stack of typeahead storage. */
7091 if (ga_grow(&ga_userinput, 1) == OK)
7092 {
7093 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7094 + ga_userinput.ga_len);
7095 ++ga_userinput.ga_len;
7096 /* default return is zero == OK */
7097 }
7098 else
7099 rettv->vval.v_number = 1; /* Failed */
7100}
7101
7102/*
7103 * "inputsecret()" function
7104 */
7105 static void
7106f_inputsecret(typval_T *argvars, typval_T *rettv)
7107{
7108 ++cmdline_star;
7109 ++inputsecret_flag;
7110 f_input(argvars, rettv);
7111 --cmdline_star;
7112 --inputsecret_flag;
7113}
7114
7115/*
7116 * "insert()" function
7117 */
7118 static void
7119f_insert(typval_T *argvars, typval_T *rettv)
7120{
7121 long before = 0;
7122 listitem_T *item;
7123 list_T *l;
7124 int error = FALSE;
7125
7126 if (argvars[0].v_type != VAR_LIST)
7127 EMSG2(_(e_listarg), "insert()");
7128 else if ((l = argvars[0].vval.v_list) != NULL
7129 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7130 {
7131 if (argvars[2].v_type != VAR_UNKNOWN)
7132 before = (long)get_tv_number_chk(&argvars[2], &error);
7133 if (error)
7134 return; /* type error; errmsg already given */
7135
7136 if (before == l->lv_len)
7137 item = NULL;
7138 else
7139 {
7140 item = list_find(l, before);
7141 if (item == NULL)
7142 {
7143 EMSGN(_(e_listidx), before);
7144 l = NULL;
7145 }
7146 }
7147 if (l != NULL)
7148 {
7149 list_insert_tv(l, &argvars[1], item);
7150 copy_tv(&argvars[0], rettv);
7151 }
7152 }
7153}
7154
7155/*
7156 * "invert(expr)" function
7157 */
7158 static void
7159f_invert(typval_T *argvars, typval_T *rettv)
7160{
7161 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7162}
7163
7164/*
7165 * "isdirectory()" function
7166 */
7167 static void
7168f_isdirectory(typval_T *argvars, typval_T *rettv)
7169{
7170 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7171}
7172
7173/*
7174 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7175 * or it refers to a List or Dictionary that is locked.
7176 */
7177 static int
7178tv_islocked(typval_T *tv)
7179{
7180 return (tv->v_lock & VAR_LOCKED)
7181 || (tv->v_type == VAR_LIST
7182 && tv->vval.v_list != NULL
7183 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7184 || (tv->v_type == VAR_DICT
7185 && tv->vval.v_dict != NULL
7186 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7187}
7188
7189/*
7190 * "islocked()" function
7191 */
7192 static void
7193f_islocked(typval_T *argvars, typval_T *rettv)
7194{
7195 lval_T lv;
7196 char_u *end;
7197 dictitem_T *di;
7198
7199 rettv->vval.v_number = -1;
7200 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007201 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202 if (end != NULL && lv.ll_name != NULL)
7203 {
7204 if (*end != NUL)
7205 EMSG(_(e_trailing));
7206 else
7207 {
7208 if (lv.ll_tv == NULL)
7209 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007210 di = find_var(lv.ll_name, NULL, TRUE);
7211 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007212 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007213 /* Consider a variable locked when:
7214 * 1. the variable itself is locked
7215 * 2. the value of the variable is locked.
7216 * 3. the List or Dict value is locked.
7217 */
7218 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7219 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 }
7221 }
7222 else if (lv.ll_range)
7223 EMSG(_("E786: Range not allowed"));
7224 else if (lv.ll_newkey != NULL)
7225 EMSG2(_(e_dictkey), lv.ll_newkey);
7226 else if (lv.ll_list != NULL)
7227 /* List item. */
7228 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7229 else
7230 /* Dictionary item. */
7231 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7232 }
7233 }
7234
7235 clear_lval(&lv);
7236}
7237
7238#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7239/*
7240 * "isnan()" function
7241 */
7242 static void
7243f_isnan(typval_T *argvars, typval_T *rettv)
7244{
7245 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7246 && isnan(argvars[0].vval.v_float);
7247}
7248#endif
7249
7250/*
7251 * "items(dict)" function
7252 */
7253 static void
7254f_items(typval_T *argvars, typval_T *rettv)
7255{
7256 dict_list(argvars, rettv, 2);
7257}
7258
7259#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7260/*
7261 * Get the job from the argument.
7262 * Returns NULL if the job is invalid.
7263 */
7264 static job_T *
7265get_job_arg(typval_T *tv)
7266{
7267 job_T *job;
7268
7269 if (tv->v_type != VAR_JOB)
7270 {
7271 EMSG2(_(e_invarg2), get_tv_string(tv));
7272 return NULL;
7273 }
7274 job = tv->vval.v_job;
7275
7276 if (job == NULL)
7277 EMSG(_("E916: not a valid job"));
7278 return job;
7279}
7280
7281/*
7282 * "job_getchannel()" function
7283 */
7284 static void
7285f_job_getchannel(typval_T *argvars, typval_T *rettv)
7286{
7287 job_T *job = get_job_arg(&argvars[0]);
7288
7289 if (job != NULL)
7290 {
7291 rettv->v_type = VAR_CHANNEL;
7292 rettv->vval.v_channel = job->jv_channel;
7293 if (job->jv_channel != NULL)
7294 ++job->jv_channel->ch_refcount;
7295 }
7296}
7297
7298/*
7299 * "job_info()" function
7300 */
7301 static void
7302f_job_info(typval_T *argvars, typval_T *rettv)
7303{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007304 if (argvars[0].v_type != VAR_UNKNOWN)
7305 {
7306 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007308 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7309 job_info(job, rettv->vval.v_dict);
7310 }
7311 else if (rettv_list_alloc(rettv) == OK)
7312 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313}
7314
7315/*
7316 * "job_setoptions()" function
7317 */
7318 static void
7319f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7320{
7321 job_T *job = get_job_arg(&argvars[0]);
7322 jobopt_T opt;
7323
7324 if (job == NULL)
7325 return;
7326 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007327 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 job_set_options(job, &opt);
7329 free_job_options(&opt);
7330}
7331
7332/*
7333 * "job_start()" function
7334 */
7335 static void
7336f_job_start(typval_T *argvars, typval_T *rettv)
7337{
7338 rettv->v_type = VAR_JOB;
7339 if (check_restricted() || check_secure())
7340 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007341 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342}
7343
7344/*
7345 * "job_status()" function
7346 */
7347 static void
7348f_job_status(typval_T *argvars, typval_T *rettv)
7349{
7350 job_T *job = get_job_arg(&argvars[0]);
7351
7352 if (job != NULL)
7353 {
7354 rettv->v_type = VAR_STRING;
7355 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7356 }
7357}
7358
7359/*
7360 * "job_stop()" function
7361 */
7362 static void
7363f_job_stop(typval_T *argvars, typval_T *rettv)
7364{
7365 job_T *job = get_job_arg(&argvars[0]);
7366
7367 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007368 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369}
7370#endif
7371
7372/*
7373 * "join()" function
7374 */
7375 static void
7376f_join(typval_T *argvars, typval_T *rettv)
7377{
7378 garray_T ga;
7379 char_u *sep;
7380
7381 if (argvars[0].v_type != VAR_LIST)
7382 {
7383 EMSG(_(e_listreq));
7384 return;
7385 }
7386 if (argvars[0].vval.v_list == NULL)
7387 return;
7388 if (argvars[1].v_type == VAR_UNKNOWN)
7389 sep = (char_u *)" ";
7390 else
7391 sep = get_tv_string_chk(&argvars[1]);
7392
7393 rettv->v_type = VAR_STRING;
7394
7395 if (sep != NULL)
7396 {
7397 ga_init2(&ga, (int)sizeof(char), 80);
7398 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7399 ga_append(&ga, NUL);
7400 rettv->vval.v_string = (char_u *)ga.ga_data;
7401 }
7402 else
7403 rettv->vval.v_string = NULL;
7404}
7405
7406/*
7407 * "js_decode()" function
7408 */
7409 static void
7410f_js_decode(typval_T *argvars, typval_T *rettv)
7411{
7412 js_read_T reader;
7413
7414 reader.js_buf = get_tv_string(&argvars[0]);
7415 reader.js_fill = NULL;
7416 reader.js_used = 0;
7417 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7418 EMSG(_(e_invarg));
7419}
7420
7421/*
7422 * "js_encode()" function
7423 */
7424 static void
7425f_js_encode(typval_T *argvars, typval_T *rettv)
7426{
7427 rettv->v_type = VAR_STRING;
7428 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7429}
7430
7431/*
7432 * "json_decode()" function
7433 */
7434 static void
7435f_json_decode(typval_T *argvars, typval_T *rettv)
7436{
7437 js_read_T reader;
7438
7439 reader.js_buf = get_tv_string(&argvars[0]);
7440 reader.js_fill = NULL;
7441 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007442 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443}
7444
7445/*
7446 * "json_encode()" function
7447 */
7448 static void
7449f_json_encode(typval_T *argvars, typval_T *rettv)
7450{
7451 rettv->v_type = VAR_STRING;
7452 rettv->vval.v_string = json_encode(&argvars[0], 0);
7453}
7454
7455/*
7456 * "keys()" function
7457 */
7458 static void
7459f_keys(typval_T *argvars, typval_T *rettv)
7460{
7461 dict_list(argvars, rettv, 0);
7462}
7463
7464/*
7465 * "last_buffer_nr()" function.
7466 */
7467 static void
7468f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7469{
7470 int n = 0;
7471 buf_T *buf;
7472
Bram Moolenaar29323592016-07-24 22:04:11 +02007473 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 if (n < buf->b_fnum)
7475 n = buf->b_fnum;
7476
7477 rettv->vval.v_number = n;
7478}
7479
7480/*
7481 * "len()" function
7482 */
7483 static void
7484f_len(typval_T *argvars, typval_T *rettv)
7485{
7486 switch (argvars[0].v_type)
7487 {
7488 case VAR_STRING:
7489 case VAR_NUMBER:
7490 rettv->vval.v_number = (varnumber_T)STRLEN(
7491 get_tv_string(&argvars[0]));
7492 break;
7493 case VAR_LIST:
7494 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7495 break;
7496 case VAR_DICT:
7497 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7498 break;
7499 case VAR_UNKNOWN:
7500 case VAR_SPECIAL:
7501 case VAR_FLOAT:
7502 case VAR_FUNC:
7503 case VAR_PARTIAL:
7504 case VAR_JOB:
7505 case VAR_CHANNEL:
7506 EMSG(_("E701: Invalid type for len()"));
7507 break;
7508 }
7509}
7510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007512libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513{
7514#ifdef FEAT_LIBCALL
7515 char_u *string_in;
7516 char_u **string_result;
7517 int nr_result;
7518#endif
7519
7520 rettv->v_type = type;
7521 if (type != VAR_NUMBER)
7522 rettv->vval.v_string = NULL;
7523
7524 if (check_restricted() || check_secure())
7525 return;
7526
7527#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007528 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7530 {
7531 string_in = NULL;
7532 if (argvars[2].v_type == VAR_STRING)
7533 string_in = argvars[2].vval.v_string;
7534 if (type == VAR_NUMBER)
7535 string_result = NULL;
7536 else
7537 string_result = &rettv->vval.v_string;
7538 if (mch_libcall(argvars[0].vval.v_string,
7539 argvars[1].vval.v_string,
7540 string_in,
7541 argvars[2].vval.v_number,
7542 string_result,
7543 &nr_result) == OK
7544 && type == VAR_NUMBER)
7545 rettv->vval.v_number = nr_result;
7546 }
7547#endif
7548}
7549
7550/*
7551 * "libcall()" function
7552 */
7553 static void
7554f_libcall(typval_T *argvars, typval_T *rettv)
7555{
7556 libcall_common(argvars, rettv, VAR_STRING);
7557}
7558
7559/*
7560 * "libcallnr()" function
7561 */
7562 static void
7563f_libcallnr(typval_T *argvars, typval_T *rettv)
7564{
7565 libcall_common(argvars, rettv, VAR_NUMBER);
7566}
7567
7568/*
7569 * "line(string)" function
7570 */
7571 static void
7572f_line(typval_T *argvars, typval_T *rettv)
7573{
7574 linenr_T lnum = 0;
7575 pos_T *fp;
7576 int fnum;
7577
7578 fp = var2fpos(&argvars[0], TRUE, &fnum);
7579 if (fp != NULL)
7580 lnum = fp->lnum;
7581 rettv->vval.v_number = lnum;
7582}
7583
7584/*
7585 * "line2byte(lnum)" function
7586 */
7587 static void
7588f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7589{
7590#ifndef FEAT_BYTEOFF
7591 rettv->vval.v_number = -1;
7592#else
7593 linenr_T lnum;
7594
7595 lnum = get_tv_lnum(argvars);
7596 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7597 rettv->vval.v_number = -1;
7598 else
7599 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7600 if (rettv->vval.v_number >= 0)
7601 ++rettv->vval.v_number;
7602#endif
7603}
7604
7605/*
7606 * "lispindent(lnum)" function
7607 */
7608 static void
7609f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7610{
7611#ifdef FEAT_LISP
7612 pos_T pos;
7613 linenr_T lnum;
7614
7615 pos = curwin->w_cursor;
7616 lnum = get_tv_lnum(argvars);
7617 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7618 {
7619 curwin->w_cursor.lnum = lnum;
7620 rettv->vval.v_number = get_lisp_indent();
7621 curwin->w_cursor = pos;
7622 }
7623 else
7624#endif
7625 rettv->vval.v_number = -1;
7626}
7627
7628/*
7629 * "localtime()" function
7630 */
7631 static void
7632f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7633{
7634 rettv->vval.v_number = (varnumber_T)time(NULL);
7635}
7636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 static void
7638get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7639{
7640 char_u *keys;
7641 char_u *which;
7642 char_u buf[NUMBUFLEN];
7643 char_u *keys_buf = NULL;
7644 char_u *rhs;
7645 int mode;
7646 int abbr = FALSE;
7647 int get_dict = FALSE;
7648 mapblock_T *mp;
7649 int buffer_local;
7650
7651 /* return empty string for failure */
7652 rettv->v_type = VAR_STRING;
7653 rettv->vval.v_string = NULL;
7654
7655 keys = get_tv_string(&argvars[0]);
7656 if (*keys == NUL)
7657 return;
7658
7659 if (argvars[1].v_type != VAR_UNKNOWN)
7660 {
7661 which = get_tv_string_buf_chk(&argvars[1], buf);
7662 if (argvars[2].v_type != VAR_UNKNOWN)
7663 {
7664 abbr = (int)get_tv_number(&argvars[2]);
7665 if (argvars[3].v_type != VAR_UNKNOWN)
7666 get_dict = (int)get_tv_number(&argvars[3]);
7667 }
7668 }
7669 else
7670 which = (char_u *)"";
7671 if (which == NULL)
7672 return;
7673
7674 mode = get_map_mode(&which, 0);
7675
7676 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7677 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7678 vim_free(keys_buf);
7679
7680 if (!get_dict)
7681 {
7682 /* Return a string. */
7683 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007684 {
7685 if (*rhs == NUL)
7686 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7687 else
7688 rettv->vval.v_string = str2special_save(rhs, FALSE);
7689 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690
7691 }
7692 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7693 {
7694 /* Return a dictionary. */
7695 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7696 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7697 dict_T *dict = rettv->vval.v_dict;
7698
Bram Moolenaare0be1672018-07-08 16:50:37 +02007699 dict_add_string(dict, "lhs", lhs);
7700 dict_add_string(dict, "rhs", mp->m_orig_str);
7701 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7702 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7703 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007704 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7705 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007706 dict_add_number(dict, "buffer", (long)buffer_local);
7707 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7708 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709
7710 vim_free(lhs);
7711 vim_free(mapmode);
7712 }
7713}
7714
7715#ifdef FEAT_FLOAT
7716/*
7717 * "log()" function
7718 */
7719 static void
7720f_log(typval_T *argvars, typval_T *rettv)
7721{
7722 float_T f = 0.0;
7723
7724 rettv->v_type = VAR_FLOAT;
7725 if (get_float_arg(argvars, &f) == OK)
7726 rettv->vval.v_float = log(f);
7727 else
7728 rettv->vval.v_float = 0.0;
7729}
7730
7731/*
7732 * "log10()" function
7733 */
7734 static void
7735f_log10(typval_T *argvars, typval_T *rettv)
7736{
7737 float_T f = 0.0;
7738
7739 rettv->v_type = VAR_FLOAT;
7740 if (get_float_arg(argvars, &f) == OK)
7741 rettv->vval.v_float = log10(f);
7742 else
7743 rettv->vval.v_float = 0.0;
7744}
7745#endif
7746
7747#ifdef FEAT_LUA
7748/*
7749 * "luaeval()" function
7750 */
7751 static void
7752f_luaeval(typval_T *argvars, typval_T *rettv)
7753{
7754 char_u *str;
7755 char_u buf[NUMBUFLEN];
7756
7757 str = get_tv_string_buf(&argvars[0], buf);
7758 do_luaeval(str, argvars + 1, rettv);
7759}
7760#endif
7761
7762/*
7763 * "map()" function
7764 */
7765 static void
7766f_map(typval_T *argvars, typval_T *rettv)
7767{
7768 filter_map(argvars, rettv, TRUE);
7769}
7770
7771/*
7772 * "maparg()" function
7773 */
7774 static void
7775f_maparg(typval_T *argvars, typval_T *rettv)
7776{
7777 get_maparg(argvars, rettv, TRUE);
7778}
7779
7780/*
7781 * "mapcheck()" function
7782 */
7783 static void
7784f_mapcheck(typval_T *argvars, typval_T *rettv)
7785{
7786 get_maparg(argvars, rettv, FALSE);
7787}
7788
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007789typedef enum
7790{
7791 MATCH_END, /* matchend() */
7792 MATCH_MATCH, /* match() */
7793 MATCH_STR, /* matchstr() */
7794 MATCH_LIST, /* matchlist() */
7795 MATCH_POS /* matchstrpos() */
7796} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797
7798 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007799find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800{
7801 char_u *str = NULL;
7802 long len = 0;
7803 char_u *expr = NULL;
7804 char_u *pat;
7805 regmatch_T regmatch;
7806 char_u patbuf[NUMBUFLEN];
7807 char_u strbuf[NUMBUFLEN];
7808 char_u *save_cpo;
7809 long start = 0;
7810 long nth = 1;
7811 colnr_T startcol = 0;
7812 int match = 0;
7813 list_T *l = NULL;
7814 listitem_T *li = NULL;
7815 long idx = 0;
7816 char_u *tofree = NULL;
7817
7818 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7819 save_cpo = p_cpo;
7820 p_cpo = (char_u *)"";
7821
7822 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007823 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007825 /* type MATCH_LIST: return empty list when there are no matches.
7826 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 if (rettv_list_alloc(rettv) == FAIL)
7828 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007829 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 && (list_append_string(rettv->vval.v_list,
7831 (char_u *)"", 0) == FAIL
7832 || list_append_number(rettv->vval.v_list,
7833 (varnumber_T)-1) == FAIL
7834 || list_append_number(rettv->vval.v_list,
7835 (varnumber_T)-1) == FAIL
7836 || list_append_number(rettv->vval.v_list,
7837 (varnumber_T)-1) == FAIL))
7838 {
7839 list_free(rettv->vval.v_list);
7840 rettv->vval.v_list = NULL;
7841 goto theend;
7842 }
7843 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007844 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845 {
7846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = NULL;
7848 }
7849
7850 if (argvars[0].v_type == VAR_LIST)
7851 {
7852 if ((l = argvars[0].vval.v_list) == NULL)
7853 goto theend;
7854 li = l->lv_first;
7855 }
7856 else
7857 {
7858 expr = str = get_tv_string(&argvars[0]);
7859 len = (long)STRLEN(str);
7860 }
7861
7862 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7863 if (pat == NULL)
7864 goto theend;
7865
7866 if (argvars[2].v_type != VAR_UNKNOWN)
7867 {
7868 int error = FALSE;
7869
7870 start = (long)get_tv_number_chk(&argvars[2], &error);
7871 if (error)
7872 goto theend;
7873 if (l != NULL)
7874 {
7875 li = list_find(l, start);
7876 if (li == NULL)
7877 goto theend;
7878 idx = l->lv_idx; /* use the cached index */
7879 }
7880 else
7881 {
7882 if (start < 0)
7883 start = 0;
7884 if (start > len)
7885 goto theend;
7886 /* When "count" argument is there ignore matches before "start",
7887 * otherwise skip part of the string. Differs when pattern is "^"
7888 * or "\<". */
7889 if (argvars[3].v_type != VAR_UNKNOWN)
7890 startcol = start;
7891 else
7892 {
7893 str += start;
7894 len -= start;
7895 }
7896 }
7897
7898 if (argvars[3].v_type != VAR_UNKNOWN)
7899 nth = (long)get_tv_number_chk(&argvars[3], &error);
7900 if (error)
7901 goto theend;
7902 }
7903
7904 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7905 if (regmatch.regprog != NULL)
7906 {
7907 regmatch.rm_ic = p_ic;
7908
7909 for (;;)
7910 {
7911 if (l != NULL)
7912 {
7913 if (li == NULL)
7914 {
7915 match = FALSE;
7916 break;
7917 }
7918 vim_free(tofree);
7919 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7920 if (str == NULL)
7921 break;
7922 }
7923
7924 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7925
7926 if (match && --nth <= 0)
7927 break;
7928 if (l == NULL && !match)
7929 break;
7930
7931 /* Advance to just after the match. */
7932 if (l != NULL)
7933 {
7934 li = li->li_next;
7935 ++idx;
7936 }
7937 else
7938 {
7939#ifdef FEAT_MBYTE
7940 startcol = (colnr_T)(regmatch.startp[0]
7941 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7942#else
7943 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7944#endif
7945 if (startcol > (colnr_T)len
7946 || str + startcol <= regmatch.startp[0])
7947 {
7948 match = FALSE;
7949 break;
7950 }
7951 }
7952 }
7953
7954 if (match)
7955 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007956 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 {
7958 listitem_T *li1 = rettv->vval.v_list->lv_first;
7959 listitem_T *li2 = li1->li_next;
7960 listitem_T *li3 = li2->li_next;
7961 listitem_T *li4 = li3->li_next;
7962
7963 vim_free(li1->li_tv.vval.v_string);
7964 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7965 (int)(regmatch.endp[0] - regmatch.startp[0]));
7966 li3->li_tv.vval.v_number =
7967 (varnumber_T)(regmatch.startp[0] - expr);
7968 li4->li_tv.vval.v_number =
7969 (varnumber_T)(regmatch.endp[0] - expr);
7970 if (l != NULL)
7971 li2->li_tv.vval.v_number = (varnumber_T)idx;
7972 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007973 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 {
7975 int i;
7976
7977 /* return list with matched string and submatches */
7978 for (i = 0; i < NSUBEXP; ++i)
7979 {
7980 if (regmatch.endp[i] == NULL)
7981 {
7982 if (list_append_string(rettv->vval.v_list,
7983 (char_u *)"", 0) == FAIL)
7984 break;
7985 }
7986 else if (list_append_string(rettv->vval.v_list,
7987 regmatch.startp[i],
7988 (int)(regmatch.endp[i] - regmatch.startp[i]))
7989 == FAIL)
7990 break;
7991 }
7992 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007993 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 {
7995 /* return matched string */
7996 if (l != NULL)
7997 copy_tv(&li->li_tv, rettv);
7998 else
7999 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8000 (int)(regmatch.endp[0] - regmatch.startp[0]));
8001 }
8002 else if (l != NULL)
8003 rettv->vval.v_number = idx;
8004 else
8005 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008006 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 rettv->vval.v_number =
8008 (varnumber_T)(regmatch.startp[0] - str);
8009 else
8010 rettv->vval.v_number =
8011 (varnumber_T)(regmatch.endp[0] - str);
8012 rettv->vval.v_number += (varnumber_T)(str - expr);
8013 }
8014 }
8015 vim_regfree(regmatch.regprog);
8016 }
8017
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008018theend:
8019 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 /* matchstrpos() without a list: drop the second item. */
8021 listitem_remove(rettv->vval.v_list,
8022 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023 vim_free(tofree);
8024 p_cpo = save_cpo;
8025}
8026
8027/*
8028 * "match()" function
8029 */
8030 static void
8031f_match(typval_T *argvars, typval_T *rettv)
8032{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008033 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034}
8035
Bram Moolenaar95e51472018-07-28 16:55:56 +02008036#ifdef FEAT_SEARCH_EXTRA
8037 static int
8038matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8039{
8040 dictitem_T *di;
8041
8042 if (tv->v_type != VAR_DICT)
8043 {
8044 EMSG(_(e_dictreq));
8045 return FAIL;
8046 }
8047
8048 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
8049 *conceal_char = get_dict_string(tv->vval.v_dict,
8050 (char_u *)"conceal", FALSE);
8051
8052 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8053 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008054 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008055 if (*win == NULL)
8056 {
8057 EMSG(_("E957: Invalid window number"));
8058 return FAIL;
8059 }
8060 }
8061
8062 return OK;
8063}
8064#endif
8065
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066/*
8067 * "matchadd()" function
8068 */
8069 static void
8070f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8071{
8072#ifdef FEAT_SEARCH_EXTRA
8073 char_u buf[NUMBUFLEN];
8074 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8075 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8076 int prio = 10; /* default priority */
8077 int id = -1;
8078 int error = FALSE;
8079 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008080 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081
8082 rettv->vval.v_number = -1;
8083
8084 if (grp == NULL || pat == NULL)
8085 return;
8086 if (argvars[2].v_type != VAR_UNKNOWN)
8087 {
8088 prio = (int)get_tv_number_chk(&argvars[2], &error);
8089 if (argvars[3].v_type != VAR_UNKNOWN)
8090 {
8091 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008092 if (argvars[4].v_type != VAR_UNKNOWN
8093 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8094 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 }
8096 }
8097 if (error == TRUE)
8098 return;
8099 if (id >= 1 && id <= 3)
8100 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008101 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 return;
8103 }
8104
Bram Moolenaar95e51472018-07-28 16:55:56 +02008105 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008106 conceal_char);
8107#endif
8108}
8109
8110/*
8111 * "matchaddpos()" function
8112 */
8113 static void
8114f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8115{
8116#ifdef FEAT_SEARCH_EXTRA
8117 char_u buf[NUMBUFLEN];
8118 char_u *group;
8119 int prio = 10;
8120 int id = -1;
8121 int error = FALSE;
8122 list_T *l;
8123 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008124 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125
8126 rettv->vval.v_number = -1;
8127
8128 group = get_tv_string_buf_chk(&argvars[0], buf);
8129 if (group == NULL)
8130 return;
8131
8132 if (argvars[1].v_type != VAR_LIST)
8133 {
8134 EMSG2(_(e_listarg), "matchaddpos()");
8135 return;
8136 }
8137 l = argvars[1].vval.v_list;
8138 if (l == NULL)
8139 return;
8140
8141 if (argvars[2].v_type != VAR_UNKNOWN)
8142 {
8143 prio = (int)get_tv_number_chk(&argvars[2], &error);
8144 if (argvars[3].v_type != VAR_UNKNOWN)
8145 {
8146 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008147
8148 if (argvars[4].v_type != VAR_UNKNOWN
8149 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8150 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 }
8152 }
8153 if (error == TRUE)
8154 return;
8155
8156 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8157 if (id == 1 || id == 2)
8158 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008159 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 return;
8161 }
8162
Bram Moolenaar95e51472018-07-28 16:55:56 +02008163 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 conceal_char);
8165#endif
8166}
8167
8168/*
8169 * "matcharg()" function
8170 */
8171 static void
8172f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8173{
8174 if (rettv_list_alloc(rettv) == OK)
8175 {
8176#ifdef FEAT_SEARCH_EXTRA
8177 int id = (int)get_tv_number(&argvars[0]);
8178 matchitem_T *m;
8179
8180 if (id >= 1 && id <= 3)
8181 {
8182 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8183 {
8184 list_append_string(rettv->vval.v_list,
8185 syn_id2name(m->hlg_id), -1);
8186 list_append_string(rettv->vval.v_list, m->pattern, -1);
8187 }
8188 else
8189 {
8190 list_append_string(rettv->vval.v_list, NULL, -1);
8191 list_append_string(rettv->vval.v_list, NULL, -1);
8192 }
8193 }
8194#endif
8195 }
8196}
8197
8198/*
8199 * "matchdelete()" function
8200 */
8201 static void
8202f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8203{
8204#ifdef FEAT_SEARCH_EXTRA
8205 rettv->vval.v_number = match_delete(curwin,
8206 (int)get_tv_number(&argvars[0]), TRUE);
8207#endif
8208}
8209
8210/*
8211 * "matchend()" function
8212 */
8213 static void
8214f_matchend(typval_T *argvars, typval_T *rettv)
8215{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008216 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217}
8218
8219/*
8220 * "matchlist()" function
8221 */
8222 static void
8223f_matchlist(typval_T *argvars, typval_T *rettv)
8224{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008225 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226}
8227
8228/*
8229 * "matchstr()" function
8230 */
8231 static void
8232f_matchstr(typval_T *argvars, typval_T *rettv)
8233{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008234 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235}
8236
8237/*
8238 * "matchstrpos()" function
8239 */
8240 static void
8241f_matchstrpos(typval_T *argvars, typval_T *rettv)
8242{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008243 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244}
8245
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 static void
8247max_min(typval_T *argvars, typval_T *rettv, int domax)
8248{
8249 varnumber_T n = 0;
8250 varnumber_T i;
8251 int error = FALSE;
8252
8253 if (argvars[0].v_type == VAR_LIST)
8254 {
8255 list_T *l;
8256 listitem_T *li;
8257
8258 l = argvars[0].vval.v_list;
8259 if (l != NULL)
8260 {
8261 li = l->lv_first;
8262 if (li != NULL)
8263 {
8264 n = get_tv_number_chk(&li->li_tv, &error);
8265 for (;;)
8266 {
8267 li = li->li_next;
8268 if (li == NULL)
8269 break;
8270 i = get_tv_number_chk(&li->li_tv, &error);
8271 if (domax ? i > n : i < n)
8272 n = i;
8273 }
8274 }
8275 }
8276 }
8277 else if (argvars[0].v_type == VAR_DICT)
8278 {
8279 dict_T *d;
8280 int first = TRUE;
8281 hashitem_T *hi;
8282 int todo;
8283
8284 d = argvars[0].vval.v_dict;
8285 if (d != NULL)
8286 {
8287 todo = (int)d->dv_hashtab.ht_used;
8288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8289 {
8290 if (!HASHITEM_EMPTY(hi))
8291 {
8292 --todo;
8293 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8294 if (first)
8295 {
8296 n = i;
8297 first = FALSE;
8298 }
8299 else if (domax ? i > n : i < n)
8300 n = i;
8301 }
8302 }
8303 }
8304 }
8305 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008306 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 rettv->vval.v_number = error ? 0 : n;
8308}
8309
8310/*
8311 * "max()" function
8312 */
8313 static void
8314f_max(typval_T *argvars, typval_T *rettv)
8315{
8316 max_min(argvars, rettv, TRUE);
8317}
8318
8319/*
8320 * "min()" function
8321 */
8322 static void
8323f_min(typval_T *argvars, typval_T *rettv)
8324{
8325 max_min(argvars, rettv, FALSE);
8326}
8327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328/*
8329 * Create the directory in which "dir" is located, and higher levels when
8330 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008331 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 */
8333 static int
8334mkdir_recurse(char_u *dir, int prot)
8335{
8336 char_u *p;
8337 char_u *updir;
8338 int r = FAIL;
8339
8340 /* Get end of directory name in "dir".
8341 * We're done when it's "/" or "c:/". */
8342 p = gettail_sep(dir);
8343 if (p <= get_past_head(dir))
8344 return OK;
8345
8346 /* If the directory exists we're done. Otherwise: create it.*/
8347 updir = vim_strnsave(dir, (int)(p - dir));
8348 if (updir == NULL)
8349 return FAIL;
8350 if (mch_isdir(updir))
8351 r = OK;
8352 else if (mkdir_recurse(updir, prot) == OK)
8353 r = vim_mkdir_emsg(updir, prot);
8354 vim_free(updir);
8355 return r;
8356}
8357
8358#ifdef vim_mkdir
8359/*
8360 * "mkdir()" function
8361 */
8362 static void
8363f_mkdir(typval_T *argvars, typval_T *rettv)
8364{
8365 char_u *dir;
8366 char_u buf[NUMBUFLEN];
8367 int prot = 0755;
8368
8369 rettv->vval.v_number = FAIL;
8370 if (check_restricted() || check_secure())
8371 return;
8372
8373 dir = get_tv_string_buf(&argvars[0], buf);
8374 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008375 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008377 if (*gettail(dir) == NUL)
8378 /* remove trailing slashes */
8379 *gettail_sep(dir) = NUL;
8380
8381 if (argvars[1].v_type != VAR_UNKNOWN)
8382 {
8383 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008385 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8386 if (prot == -1)
8387 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008389 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8390 {
8391 if (mch_isdir(dir))
8392 {
8393 /* With the "p" flag it's OK if the dir already exists. */
8394 rettv->vval.v_number = OK;
8395 return;
8396 }
8397 mkdir_recurse(dir, prot);
8398 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008400 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401}
8402#endif
8403
8404/*
8405 * "mode()" function
8406 */
8407 static void
8408f_mode(typval_T *argvars, typval_T *rettv)
8409{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008410 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411
Bram Moolenaar612cc382018-07-29 15:34:26 +02008412 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413
8414 if (time_for_testing == 93784)
8415 {
8416 /* Testing the two-character code. */
8417 buf[0] = 'x';
8418 buf[1] = '!';
8419 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008420#ifdef FEAT_TERMINAL
8421 else if (term_use_loop())
8422 buf[0] = 't';
8423#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 else if (VIsual_active)
8425 {
8426 if (VIsual_select)
8427 buf[0] = VIsual_mode + 's' - 'v';
8428 else
8429 buf[0] = VIsual_mode;
8430 }
8431 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8432 || State == CONFIRM)
8433 {
8434 buf[0] = 'r';
8435 if (State == ASKMORE)
8436 buf[1] = 'm';
8437 else if (State == CONFIRM)
8438 buf[1] = '?';
8439 }
8440 else if (State == EXTERNCMD)
8441 buf[0] = '!';
8442 else if (State & INSERT)
8443 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 if (State & VREPLACE_FLAG)
8445 {
8446 buf[0] = 'R';
8447 buf[1] = 'v';
8448 }
8449 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008450 {
8451 if (State & REPLACE_FLAG)
8452 buf[0] = 'R';
8453 else
8454 buf[0] = 'i';
8455#ifdef FEAT_INS_EXPAND
8456 if (ins_compl_active())
8457 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008458 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008459 buf[1] = 'x';
8460#endif
8461 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008463 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 {
8465 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008466 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008468 else if (exmode_active == EXMODE_NORMAL)
8469 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 }
8471 else
8472 {
8473 buf[0] = 'n';
8474 if (finish_op)
8475 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008476 else if (restart_edit == 'I' || restart_edit == 'R'
8477 || restart_edit == 'V')
8478 {
8479 buf[1] = 'i';
8480 buf[2] = restart_edit;
8481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 }
8483
8484 /* Clear out the minor mode when the argument is not a non-zero number or
8485 * non-empty string. */
8486 if (!non_zero_arg(&argvars[0]))
8487 buf[1] = NUL;
8488
8489 rettv->vval.v_string = vim_strsave(buf);
8490 rettv->v_type = VAR_STRING;
8491}
8492
8493#if defined(FEAT_MZSCHEME) || defined(PROTO)
8494/*
8495 * "mzeval()" function
8496 */
8497 static void
8498f_mzeval(typval_T *argvars, typval_T *rettv)
8499{
8500 char_u *str;
8501 char_u buf[NUMBUFLEN];
8502
8503 str = get_tv_string_buf(&argvars[0], buf);
8504 do_mzeval(str, rettv);
8505}
8506
8507 void
8508mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8509{
8510 typval_T argvars[3];
8511
8512 argvars[0].v_type = VAR_STRING;
8513 argvars[0].vval.v_string = name;
8514 copy_tv(args, &argvars[1]);
8515 argvars[2].v_type = VAR_UNKNOWN;
8516 f_call(argvars, rettv);
8517 clear_tv(&argvars[1]);
8518}
8519#endif
8520
8521/*
8522 * "nextnonblank()" function
8523 */
8524 static void
8525f_nextnonblank(typval_T *argvars, typval_T *rettv)
8526{
8527 linenr_T lnum;
8528
8529 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8530 {
8531 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8532 {
8533 lnum = 0;
8534 break;
8535 }
8536 if (*skipwhite(ml_get(lnum)) != NUL)
8537 break;
8538 }
8539 rettv->vval.v_number = lnum;
8540}
8541
8542/*
8543 * "nr2char()" function
8544 */
8545 static void
8546f_nr2char(typval_T *argvars, typval_T *rettv)
8547{
8548 char_u buf[NUMBUFLEN];
8549
8550#ifdef FEAT_MBYTE
8551 if (has_mbyte)
8552 {
8553 int utf8 = 0;
8554
8555 if (argvars[1].v_type != VAR_UNKNOWN)
8556 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8557 if (utf8)
8558 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8559 else
8560 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8561 }
8562 else
8563#endif
8564 {
8565 buf[0] = (char_u)get_tv_number(&argvars[0]);
8566 buf[1] = NUL;
8567 }
8568 rettv->v_type = VAR_STRING;
8569 rettv->vval.v_string = vim_strsave(buf);
8570}
8571
8572/*
8573 * "or(expr, expr)" function
8574 */
8575 static void
8576f_or(typval_T *argvars, typval_T *rettv)
8577{
8578 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8579 | get_tv_number_chk(&argvars[1], NULL);
8580}
8581
8582/*
8583 * "pathshorten()" function
8584 */
8585 static void
8586f_pathshorten(typval_T *argvars, typval_T *rettv)
8587{
8588 char_u *p;
8589
8590 rettv->v_type = VAR_STRING;
8591 p = get_tv_string_chk(&argvars[0]);
8592 if (p == NULL)
8593 rettv->vval.v_string = NULL;
8594 else
8595 {
8596 p = vim_strsave(p);
8597 rettv->vval.v_string = p;
8598 if (p != NULL)
8599 shorten_dir(p);
8600 }
8601}
8602
8603#ifdef FEAT_PERL
8604/*
8605 * "perleval()" function
8606 */
8607 static void
8608f_perleval(typval_T *argvars, typval_T *rettv)
8609{
8610 char_u *str;
8611 char_u buf[NUMBUFLEN];
8612
8613 str = get_tv_string_buf(&argvars[0], buf);
8614 do_perleval(str, rettv);
8615}
8616#endif
8617
8618#ifdef FEAT_FLOAT
8619/*
8620 * "pow()" function
8621 */
8622 static void
8623f_pow(typval_T *argvars, typval_T *rettv)
8624{
8625 float_T fx = 0.0, fy = 0.0;
8626
8627 rettv->v_type = VAR_FLOAT;
8628 if (get_float_arg(argvars, &fx) == OK
8629 && get_float_arg(&argvars[1], &fy) == OK)
8630 rettv->vval.v_float = pow(fx, fy);
8631 else
8632 rettv->vval.v_float = 0.0;
8633}
8634#endif
8635
8636/*
8637 * "prevnonblank()" function
8638 */
8639 static void
8640f_prevnonblank(typval_T *argvars, typval_T *rettv)
8641{
8642 linenr_T lnum;
8643
8644 lnum = get_tv_lnum(argvars);
8645 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8646 lnum = 0;
8647 else
8648 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8649 --lnum;
8650 rettv->vval.v_number = lnum;
8651}
8652
8653/* This dummy va_list is here because:
8654 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8655 * - locally in the function results in a "used before set" warning
8656 * - using va_start() to initialize it gives "function with fixed args" error */
8657static va_list ap;
8658
8659/*
8660 * "printf()" function
8661 */
8662 static void
8663f_printf(typval_T *argvars, typval_T *rettv)
8664{
8665 char_u buf[NUMBUFLEN];
8666 int len;
8667 char_u *s;
8668 int saved_did_emsg = did_emsg;
8669 char *fmt;
8670
8671 rettv->v_type = VAR_STRING;
8672 rettv->vval.v_string = NULL;
8673
8674 /* Get the required length, allocate the buffer and do it for real. */
8675 did_emsg = FALSE;
8676 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008677 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678 if (!did_emsg)
8679 {
8680 s = alloc(len + 1);
8681 if (s != NULL)
8682 {
8683 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008684 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8685 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 }
8687 }
8688 did_emsg |= saved_did_emsg;
8689}
8690
Bram Moolenaarf2732452018-06-03 14:47:35 +02008691#ifdef FEAT_JOB_CHANNEL
8692/*
8693 * "prompt_setcallback({buffer}, {callback})" function
8694 */
8695 static void
8696f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8697{
8698 buf_T *buf;
8699 char_u *callback;
8700 partial_T *partial;
8701
8702 if (check_secure())
8703 return;
8704 buf = get_buf_tv(&argvars[0], FALSE);
8705 if (buf == NULL)
8706 return;
8707
8708 callback = get_callback(&argvars[1], &partial);
8709 if (callback == NULL)
8710 return;
8711
8712 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8713 if (partial == NULL)
8714 buf->b_prompt_callback = vim_strsave(callback);
8715 else
8716 /* pointer into the partial */
8717 buf->b_prompt_callback = callback;
8718 buf->b_prompt_partial = partial;
8719}
8720
8721/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008722 * "prompt_setinterrupt({buffer}, {callback})" function
8723 */
8724 static void
8725f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8726{
8727 buf_T *buf;
8728 char_u *callback;
8729 partial_T *partial;
8730
8731 if (check_secure())
8732 return;
8733 buf = get_buf_tv(&argvars[0], FALSE);
8734 if (buf == NULL)
8735 return;
8736
8737 callback = get_callback(&argvars[1], &partial);
8738 if (callback == NULL)
8739 return;
8740
8741 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8742 if (partial == NULL)
8743 buf->b_prompt_interrupt = vim_strsave(callback);
8744 else
8745 /* pointer into the partial */
8746 buf->b_prompt_interrupt = callback;
8747 buf->b_prompt_int_partial = partial;
8748}
8749
8750/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008751 * "prompt_setprompt({buffer}, {text})" function
8752 */
8753 static void
8754f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8755{
8756 buf_T *buf;
8757 char_u *text;
8758
8759 if (check_secure())
8760 return;
8761 buf = get_buf_tv(&argvars[0], FALSE);
8762 if (buf == NULL)
8763 return;
8764
8765 text = get_tv_string(&argvars[1]);
8766 vim_free(buf->b_prompt_text);
8767 buf->b_prompt_text = vim_strsave(text);
8768}
8769#endif
8770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771/*
8772 * "pumvisible()" function
8773 */
8774 static void
8775f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8776{
8777#ifdef FEAT_INS_EXPAND
8778 if (pum_visible())
8779 rettv->vval.v_number = 1;
8780#endif
8781}
8782
8783#ifdef FEAT_PYTHON3
8784/*
8785 * "py3eval()" function
8786 */
8787 static void
8788f_py3eval(typval_T *argvars, typval_T *rettv)
8789{
8790 char_u *str;
8791 char_u buf[NUMBUFLEN];
8792
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008793 if (p_pyx == 0)
8794 p_pyx = 3;
8795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 str = get_tv_string_buf(&argvars[0], buf);
8797 do_py3eval(str, rettv);
8798}
8799#endif
8800
8801#ifdef FEAT_PYTHON
8802/*
8803 * "pyeval()" function
8804 */
8805 static void
8806f_pyeval(typval_T *argvars, typval_T *rettv)
8807{
8808 char_u *str;
8809 char_u buf[NUMBUFLEN];
8810
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008811 if (p_pyx == 0)
8812 p_pyx = 2;
8813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 str = get_tv_string_buf(&argvars[0], buf);
8815 do_pyeval(str, rettv);
8816}
8817#endif
8818
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008819#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8820/*
8821 * "pyxeval()" function
8822 */
8823 static void
8824f_pyxeval(typval_T *argvars, typval_T *rettv)
8825{
8826# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8827 init_pyxversion();
8828 if (p_pyx == 2)
8829 f_pyeval(argvars, rettv);
8830 else
8831 f_py3eval(argvars, rettv);
8832# elif defined(FEAT_PYTHON)
8833 f_pyeval(argvars, rettv);
8834# elif defined(FEAT_PYTHON3)
8835 f_py3eval(argvars, rettv);
8836# endif
8837}
8838#endif
8839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840/*
8841 * "range()" function
8842 */
8843 static void
8844f_range(typval_T *argvars, typval_T *rettv)
8845{
8846 varnumber_T start;
8847 varnumber_T end;
8848 varnumber_T stride = 1;
8849 varnumber_T i;
8850 int error = FALSE;
8851
8852 start = get_tv_number_chk(&argvars[0], &error);
8853 if (argvars[1].v_type == VAR_UNKNOWN)
8854 {
8855 end = start - 1;
8856 start = 0;
8857 }
8858 else
8859 {
8860 end = get_tv_number_chk(&argvars[1], &error);
8861 if (argvars[2].v_type != VAR_UNKNOWN)
8862 stride = get_tv_number_chk(&argvars[2], &error);
8863 }
8864
8865 if (error)
8866 return; /* type error; errmsg already given */
8867 if (stride == 0)
8868 EMSG(_("E726: Stride is zero"));
8869 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8870 EMSG(_("E727: Start past end"));
8871 else
8872 {
8873 if (rettv_list_alloc(rettv) == OK)
8874 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8875 if (list_append_number(rettv->vval.v_list,
8876 (varnumber_T)i) == FAIL)
8877 break;
8878 }
8879}
8880
8881/*
8882 * "readfile()" function
8883 */
8884 static void
8885f_readfile(typval_T *argvars, typval_T *rettv)
8886{
8887 int binary = FALSE;
8888 int failed = FALSE;
8889 char_u *fname;
8890 FILE *fd;
8891 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8892 int io_size = sizeof(buf);
8893 int readlen; /* size of last fread() */
8894 char_u *prev = NULL; /* previously read bytes, if any */
8895 long prevlen = 0; /* length of data in prev */
8896 long prevsize = 0; /* size of prev buffer */
8897 long maxline = MAXLNUM;
8898 long cnt = 0;
8899 char_u *p; /* position in buf */
8900 char_u *start; /* start of current line */
8901
8902 if (argvars[1].v_type != VAR_UNKNOWN)
8903 {
8904 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8905 binary = TRUE;
8906 if (argvars[2].v_type != VAR_UNKNOWN)
8907 maxline = (long)get_tv_number(&argvars[2]);
8908 }
8909
8910 if (rettv_list_alloc(rettv) == FAIL)
8911 return;
8912
8913 /* Always open the file in binary mode, library functions have a mind of
8914 * their own about CR-LF conversion. */
8915 fname = get_tv_string(&argvars[0]);
8916 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8917 {
8918 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8919 return;
8920 }
8921
8922 while (cnt < maxline || maxline < 0)
8923 {
8924 readlen = (int)fread(buf, 1, io_size, fd);
8925
8926 /* This for loop processes what was read, but is also entered at end
8927 * of file so that either:
8928 * - an incomplete line gets written
8929 * - a "binary" file gets an empty line at the end if it ends in a
8930 * newline. */
8931 for (p = buf, start = buf;
8932 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8933 ++p)
8934 {
8935 if (*p == '\n' || readlen <= 0)
8936 {
8937 listitem_T *li;
8938 char_u *s = NULL;
8939 long_u len = p - start;
8940
8941 /* Finished a line. Remove CRs before NL. */
8942 if (readlen > 0 && !binary)
8943 {
8944 while (len > 0 && start[len - 1] == '\r')
8945 --len;
8946 /* removal may cross back to the "prev" string */
8947 if (len == 0)
8948 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8949 --prevlen;
8950 }
8951 if (prevlen == 0)
8952 s = vim_strnsave(start, (int)len);
8953 else
8954 {
8955 /* Change "prev" buffer to be the right size. This way
8956 * the bytes are only copied once, and very long lines are
8957 * allocated only once. */
8958 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8959 {
8960 mch_memmove(s + prevlen, start, len);
8961 s[prevlen + len] = NUL;
8962 prev = NULL; /* the list will own the string */
8963 prevlen = prevsize = 0;
8964 }
8965 }
8966 if (s == NULL)
8967 {
8968 do_outofmem_msg((long_u) prevlen + len + 1);
8969 failed = TRUE;
8970 break;
8971 }
8972
8973 if ((li = listitem_alloc()) == NULL)
8974 {
8975 vim_free(s);
8976 failed = TRUE;
8977 break;
8978 }
8979 li->li_tv.v_type = VAR_STRING;
8980 li->li_tv.v_lock = 0;
8981 li->li_tv.vval.v_string = s;
8982 list_append(rettv->vval.v_list, li);
8983
8984 start = p + 1; /* step over newline */
8985 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8986 break;
8987 }
8988 else if (*p == NUL)
8989 *p = '\n';
8990#ifdef FEAT_MBYTE
8991 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8992 * when finding the BF and check the previous two bytes. */
8993 else if (*p == 0xbf && enc_utf8 && !binary)
8994 {
8995 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8996 * + 1, these may be in the "prev" string. */
8997 char_u back1 = p >= buf + 1 ? p[-1]
8998 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8999 char_u back2 = p >= buf + 2 ? p[-2]
9000 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9001 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9002
9003 if (back2 == 0xef && back1 == 0xbb)
9004 {
9005 char_u *dest = p - 2;
9006
9007 /* Usually a BOM is at the beginning of a file, and so at
9008 * the beginning of a line; then we can just step over it.
9009 */
9010 if (start == dest)
9011 start = p + 1;
9012 else
9013 {
9014 /* have to shuffle buf to close gap */
9015 int adjust_prevlen = 0;
9016
9017 if (dest < buf)
9018 {
9019 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9020 dest = buf;
9021 }
9022 if (readlen > p - buf + 1)
9023 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9024 readlen -= 3 - adjust_prevlen;
9025 prevlen -= adjust_prevlen;
9026 p = dest - 1;
9027 }
9028 }
9029 }
9030#endif
9031 } /* for */
9032
9033 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9034 break;
9035 if (start < p)
9036 {
9037 /* There's part of a line in buf, store it in "prev". */
9038 if (p - start + prevlen >= prevsize)
9039 {
9040 /* need bigger "prev" buffer */
9041 char_u *newprev;
9042
9043 /* A common use case is ordinary text files and "prev" gets a
9044 * fragment of a line, so the first allocation is made
9045 * small, to avoid repeatedly 'allocing' large and
9046 * 'reallocing' small. */
9047 if (prevsize == 0)
9048 prevsize = (long)(p - start);
9049 else
9050 {
9051 long grow50pc = (prevsize * 3) / 2;
9052 long growmin = (long)((p - start) * 2 + prevlen);
9053 prevsize = grow50pc > growmin ? grow50pc : growmin;
9054 }
9055 newprev = prev == NULL ? alloc(prevsize)
9056 : vim_realloc(prev, prevsize);
9057 if (newprev == NULL)
9058 {
9059 do_outofmem_msg((long_u)prevsize);
9060 failed = TRUE;
9061 break;
9062 }
9063 prev = newprev;
9064 }
9065 /* Add the line part to end of "prev". */
9066 mch_memmove(prev + prevlen, start, p - start);
9067 prevlen += (long)(p - start);
9068 }
9069 } /* while */
9070
9071 /*
9072 * For a negative line count use only the lines at the end of the file,
9073 * free the rest.
9074 */
9075 if (!failed && maxline < 0)
9076 while (cnt > -maxline)
9077 {
9078 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9079 --cnt;
9080 }
9081
9082 if (failed)
9083 {
9084 list_free(rettv->vval.v_list);
9085 /* readfile doc says an empty list is returned on error */
9086 rettv->vval.v_list = list_alloc();
9087 }
9088
9089 vim_free(prev);
9090 fclose(fd);
9091}
9092
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009093 static void
9094return_register(int regname, typval_T *rettv)
9095{
9096 char_u buf[2] = {0, 0};
9097
9098 buf[0] = (char_u)regname;
9099 rettv->v_type = VAR_STRING;
9100 rettv->vval.v_string = vim_strsave(buf);
9101}
9102
9103/*
9104 * "reg_executing()" function
9105 */
9106 static void
9107f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9108{
9109 return_register(reg_executing, rettv);
9110}
9111
9112/*
9113 * "reg_recording()" function
9114 */
9115 static void
9116f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9117{
9118 return_register(reg_recording, rettv);
9119}
9120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122/*
9123 * Convert a List to proftime_T.
9124 * Return FAIL when there is something wrong.
9125 */
9126 static int
9127list2proftime(typval_T *arg, proftime_T *tm)
9128{
9129 long n1, n2;
9130 int error = FALSE;
9131
9132 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9133 || arg->vval.v_list->lv_len != 2)
9134 return FAIL;
9135 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9136 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9137# ifdef WIN3264
9138 tm->HighPart = n1;
9139 tm->LowPart = n2;
9140# else
9141 tm->tv_sec = n1;
9142 tm->tv_usec = n2;
9143# endif
9144 return error ? FAIL : OK;
9145}
9146#endif /* FEAT_RELTIME */
9147
9148/*
9149 * "reltime()" function
9150 */
9151 static void
9152f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9153{
9154#ifdef FEAT_RELTIME
9155 proftime_T res;
9156 proftime_T start;
9157
9158 if (argvars[0].v_type == VAR_UNKNOWN)
9159 {
9160 /* No arguments: get current time. */
9161 profile_start(&res);
9162 }
9163 else if (argvars[1].v_type == VAR_UNKNOWN)
9164 {
9165 if (list2proftime(&argvars[0], &res) == FAIL)
9166 return;
9167 profile_end(&res);
9168 }
9169 else
9170 {
9171 /* Two arguments: compute the difference. */
9172 if (list2proftime(&argvars[0], &start) == FAIL
9173 || list2proftime(&argvars[1], &res) == FAIL)
9174 return;
9175 profile_sub(&res, &start);
9176 }
9177
9178 if (rettv_list_alloc(rettv) == OK)
9179 {
9180 long n1, n2;
9181
9182# ifdef WIN3264
9183 n1 = res.HighPart;
9184 n2 = res.LowPart;
9185# else
9186 n1 = res.tv_sec;
9187 n2 = res.tv_usec;
9188# endif
9189 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9190 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9191 }
9192#endif
9193}
9194
9195#ifdef FEAT_FLOAT
9196/*
9197 * "reltimefloat()" function
9198 */
9199 static void
9200f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9201{
9202# ifdef FEAT_RELTIME
9203 proftime_T tm;
9204# endif
9205
9206 rettv->v_type = VAR_FLOAT;
9207 rettv->vval.v_float = 0;
9208# ifdef FEAT_RELTIME
9209 if (list2proftime(&argvars[0], &tm) == OK)
9210 rettv->vval.v_float = profile_float(&tm);
9211# endif
9212}
9213#endif
9214
9215/*
9216 * "reltimestr()" function
9217 */
9218 static void
9219f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9220{
9221#ifdef FEAT_RELTIME
9222 proftime_T tm;
9223#endif
9224
9225 rettv->v_type = VAR_STRING;
9226 rettv->vval.v_string = NULL;
9227#ifdef FEAT_RELTIME
9228 if (list2proftime(&argvars[0], &tm) == OK)
9229 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9230#endif
9231}
9232
9233#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009234 static void
9235make_connection(void)
9236{
9237 if (X_DISPLAY == NULL
9238# ifdef FEAT_GUI
9239 && !gui.in_use
9240# endif
9241 )
9242 {
9243 x_force_connect = TRUE;
9244 setup_term_clip();
9245 x_force_connect = FALSE;
9246 }
9247}
9248
9249 static int
9250check_connection(void)
9251{
9252 make_connection();
9253 if (X_DISPLAY == NULL)
9254 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009255 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 return FAIL;
9257 }
9258 return OK;
9259}
9260#endif
9261
9262#ifdef FEAT_CLIENTSERVER
9263 static void
9264remote_common(typval_T *argvars, typval_T *rettv, int expr)
9265{
9266 char_u *server_name;
9267 char_u *keys;
9268 char_u *r = NULL;
9269 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009270 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271# ifdef WIN32
9272 HWND w;
9273# else
9274 Window w;
9275# endif
9276
9277 if (check_restricted() || check_secure())
9278 return;
9279
9280# ifdef FEAT_X11
9281 if (check_connection() == FAIL)
9282 return;
9283# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009284 if (argvars[2].v_type != VAR_UNKNOWN
9285 && argvars[3].v_type != VAR_UNKNOWN)
9286 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287
9288 server_name = get_tv_string_chk(&argvars[0]);
9289 if (server_name == NULL)
9290 return; /* type error; errmsg already given */
9291 keys = get_tv_string_buf(&argvars[1], buf);
9292# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009293 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009295 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9296 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297# endif
9298 {
9299 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009300 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009302 vim_free(r);
9303 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 else
9305 EMSG2(_("E241: Unable to send to %s"), server_name);
9306 return;
9307 }
9308
9309 rettv->vval.v_string = r;
9310
9311 if (argvars[2].v_type != VAR_UNKNOWN)
9312 {
9313 dictitem_T v;
9314 char_u str[30];
9315 char_u *idvar;
9316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009318 if (idvar != NULL && *idvar != NUL)
9319 {
9320 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9321 v.di_tv.v_type = VAR_STRING;
9322 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009324 vim_free(v.di_tv.vval.v_string);
9325 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326 }
9327}
9328#endif
9329
9330/*
9331 * "remote_expr()" function
9332 */
9333 static void
9334f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9335{
9336 rettv->v_type = VAR_STRING;
9337 rettv->vval.v_string = NULL;
9338#ifdef FEAT_CLIENTSERVER
9339 remote_common(argvars, rettv, TRUE);
9340#endif
9341}
9342
9343/*
9344 * "remote_foreground()" function
9345 */
9346 static void
9347f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9348{
9349#ifdef FEAT_CLIENTSERVER
9350# ifdef WIN32
9351 /* On Win32 it's done in this application. */
9352 {
9353 char_u *server_name = get_tv_string_chk(&argvars[0]);
9354
9355 if (server_name != NULL)
9356 serverForeground(server_name);
9357 }
9358# else
9359 /* Send a foreground() expression to the server. */
9360 argvars[1].v_type = VAR_STRING;
9361 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9362 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009363 rettv->v_type = VAR_STRING;
9364 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 remote_common(argvars, rettv, TRUE);
9366 vim_free(argvars[1].vval.v_string);
9367# endif
9368#endif
9369}
9370
9371 static void
9372f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9373{
9374#ifdef FEAT_CLIENTSERVER
9375 dictitem_T v;
9376 char_u *s = NULL;
9377# ifdef WIN32
9378 long_u n = 0;
9379# endif
9380 char_u *serverid;
9381
9382 if (check_restricted() || check_secure())
9383 {
9384 rettv->vval.v_number = -1;
9385 return;
9386 }
9387 serverid = get_tv_string_chk(&argvars[0]);
9388 if (serverid == NULL)
9389 {
9390 rettv->vval.v_number = -1;
9391 return; /* type error; errmsg already given */
9392 }
9393# ifdef WIN32
9394 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9395 if (n == 0)
9396 rettv->vval.v_number = -1;
9397 else
9398 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009399 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 rettv->vval.v_number = (s != NULL);
9401 }
9402# else
9403 if (check_connection() == FAIL)
9404 return;
9405
9406 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9407 serverStrToWin(serverid), &s);
9408# endif
9409
9410 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9411 {
9412 char_u *retvar;
9413
9414 v.di_tv.v_type = VAR_STRING;
9415 v.di_tv.vval.v_string = vim_strsave(s);
9416 retvar = get_tv_string_chk(&argvars[1]);
9417 if (retvar != NULL)
9418 set_var(retvar, &v.di_tv, FALSE);
9419 vim_free(v.di_tv.vval.v_string);
9420 }
9421#else
9422 rettv->vval.v_number = -1;
9423#endif
9424}
9425
9426 static void
9427f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9428{
9429 char_u *r = NULL;
9430
9431#ifdef FEAT_CLIENTSERVER
9432 char_u *serverid = get_tv_string_chk(&argvars[0]);
9433
9434 if (serverid != NULL && !check_restricted() && !check_secure())
9435 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009436 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009437# ifdef WIN32
9438 /* The server's HWND is encoded in the 'id' parameter */
9439 long_u n = 0;
9440# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009441
9442 if (argvars[1].v_type != VAR_UNKNOWN)
9443 timeout = get_tv_number(&argvars[1]);
9444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9447 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009448 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 if (r == NULL)
9450# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009451 if (check_connection() == FAIL
9452 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9453 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454# endif
9455 EMSG(_("E277: Unable to read a server reply"));
9456 }
9457#endif
9458 rettv->v_type = VAR_STRING;
9459 rettv->vval.v_string = r;
9460}
9461
9462/*
9463 * "remote_send()" function
9464 */
9465 static void
9466f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9467{
9468 rettv->v_type = VAR_STRING;
9469 rettv->vval.v_string = NULL;
9470#ifdef FEAT_CLIENTSERVER
9471 remote_common(argvars, rettv, FALSE);
9472#endif
9473}
9474
9475/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009476 * "remote_startserver()" function
9477 */
9478 static void
9479f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9480{
9481#ifdef FEAT_CLIENTSERVER
9482 char_u *server = get_tv_string_chk(&argvars[0]);
9483
9484 if (server == NULL)
9485 return; /* type error; errmsg already given */
9486 if (serverName != NULL)
9487 EMSG(_("E941: already started a server"));
9488 else
9489 {
9490# ifdef FEAT_X11
9491 if (check_connection() == OK)
9492 serverRegisterName(X_DISPLAY, server);
9493# else
9494 serverSetName(server);
9495# endif
9496 }
9497#else
9498 EMSG(_("E942: +clientserver feature not available"));
9499#endif
9500}
9501
9502/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503 * "remove()" function
9504 */
9505 static void
9506f_remove(typval_T *argvars, typval_T *rettv)
9507{
9508 list_T *l;
9509 listitem_T *item, *item2;
9510 listitem_T *li;
9511 long idx;
9512 long end;
9513 char_u *key;
9514 dict_T *d;
9515 dictitem_T *di;
9516 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9517
9518 if (argvars[0].v_type == VAR_DICT)
9519 {
9520 if (argvars[2].v_type != VAR_UNKNOWN)
9521 EMSG2(_(e_toomanyarg), "remove()");
9522 else if ((d = argvars[0].vval.v_dict) != NULL
9523 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9524 {
9525 key = get_tv_string_chk(&argvars[1]);
9526 if (key != NULL)
9527 {
9528 di = dict_find(d, key, -1);
9529 if (di == NULL)
9530 EMSG2(_(e_dictkey), key);
9531 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9532 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9533 {
9534 *rettv = di->di_tv;
9535 init_tv(&di->di_tv);
9536 dictitem_remove(d, di);
9537 }
9538 }
9539 }
9540 }
9541 else if (argvars[0].v_type != VAR_LIST)
9542 EMSG2(_(e_listdictarg), "remove()");
9543 else if ((l = argvars[0].vval.v_list) != NULL
9544 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9545 {
9546 int error = FALSE;
9547
9548 idx = (long)get_tv_number_chk(&argvars[1], &error);
9549 if (error)
9550 ; /* type error: do nothing, errmsg already given */
9551 else if ((item = list_find(l, idx)) == NULL)
9552 EMSGN(_(e_listidx), idx);
9553 else
9554 {
9555 if (argvars[2].v_type == VAR_UNKNOWN)
9556 {
9557 /* Remove one item, return its value. */
9558 vimlist_remove(l, item, item);
9559 *rettv = item->li_tv;
9560 vim_free(item);
9561 }
9562 else
9563 {
9564 /* Remove range of items, return list with values. */
9565 end = (long)get_tv_number_chk(&argvars[2], &error);
9566 if (error)
9567 ; /* type error: do nothing */
9568 else if ((item2 = list_find(l, end)) == NULL)
9569 EMSGN(_(e_listidx), end);
9570 else
9571 {
9572 int cnt = 0;
9573
9574 for (li = item; li != NULL; li = li->li_next)
9575 {
9576 ++cnt;
9577 if (li == item2)
9578 break;
9579 }
9580 if (li == NULL) /* didn't find "item2" after "item" */
9581 EMSG(_(e_invrange));
9582 else
9583 {
9584 vimlist_remove(l, item, item2);
9585 if (rettv_list_alloc(rettv) == OK)
9586 {
9587 l = rettv->vval.v_list;
9588 l->lv_first = item;
9589 l->lv_last = item2;
9590 item->li_prev = NULL;
9591 item2->li_next = NULL;
9592 l->lv_len = cnt;
9593 }
9594 }
9595 }
9596 }
9597 }
9598 }
9599}
9600
9601/*
9602 * "rename({from}, {to})" function
9603 */
9604 static void
9605f_rename(typval_T *argvars, typval_T *rettv)
9606{
9607 char_u buf[NUMBUFLEN];
9608
9609 if (check_restricted() || check_secure())
9610 rettv->vval.v_number = -1;
9611 else
9612 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9613 get_tv_string_buf(&argvars[1], buf));
9614}
9615
9616/*
9617 * "repeat()" function
9618 */
9619 static void
9620f_repeat(typval_T *argvars, typval_T *rettv)
9621{
9622 char_u *p;
9623 int n;
9624 int slen;
9625 int len;
9626 char_u *r;
9627 int i;
9628
9629 n = (int)get_tv_number(&argvars[1]);
9630 if (argvars[0].v_type == VAR_LIST)
9631 {
9632 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9633 while (n-- > 0)
9634 if (list_extend(rettv->vval.v_list,
9635 argvars[0].vval.v_list, NULL) == FAIL)
9636 break;
9637 }
9638 else
9639 {
9640 p = get_tv_string(&argvars[0]);
9641 rettv->v_type = VAR_STRING;
9642 rettv->vval.v_string = NULL;
9643
9644 slen = (int)STRLEN(p);
9645 len = slen * n;
9646 if (len <= 0)
9647 return;
9648
9649 r = alloc(len + 1);
9650 if (r != NULL)
9651 {
9652 for (i = 0; i < n; i++)
9653 mch_memmove(r + i * slen, p, (size_t)slen);
9654 r[len] = NUL;
9655 }
9656
9657 rettv->vval.v_string = r;
9658 }
9659}
9660
9661/*
9662 * "resolve()" function
9663 */
9664 static void
9665f_resolve(typval_T *argvars, typval_T *rettv)
9666{
9667 char_u *p;
9668#ifdef HAVE_READLINK
9669 char_u *buf = NULL;
9670#endif
9671
9672 p = get_tv_string(&argvars[0]);
9673#ifdef FEAT_SHORTCUT
9674 {
9675 char_u *v = NULL;
9676
9677 v = mch_resolve_shortcut(p);
9678 if (v != NULL)
9679 rettv->vval.v_string = v;
9680 else
9681 rettv->vval.v_string = vim_strsave(p);
9682 }
9683#else
9684# ifdef HAVE_READLINK
9685 {
9686 char_u *cpy;
9687 int len;
9688 char_u *remain = NULL;
9689 char_u *q;
9690 int is_relative_to_current = FALSE;
9691 int has_trailing_pathsep = FALSE;
9692 int limit = 100;
9693
9694 p = vim_strsave(p);
9695
9696 if (p[0] == '.' && (vim_ispathsep(p[1])
9697 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9698 is_relative_to_current = TRUE;
9699
9700 len = STRLEN(p);
9701 if (len > 0 && after_pathsep(p, p + len))
9702 {
9703 has_trailing_pathsep = TRUE;
9704 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9705 }
9706
9707 q = getnextcomp(p);
9708 if (*q != NUL)
9709 {
9710 /* Separate the first path component in "p", and keep the
9711 * remainder (beginning with the path separator). */
9712 remain = vim_strsave(q - 1);
9713 q[-1] = NUL;
9714 }
9715
9716 buf = alloc(MAXPATHL + 1);
9717 if (buf == NULL)
9718 goto fail;
9719
9720 for (;;)
9721 {
9722 for (;;)
9723 {
9724 len = readlink((char *)p, (char *)buf, MAXPATHL);
9725 if (len <= 0)
9726 break;
9727 buf[len] = NUL;
9728
9729 if (limit-- == 0)
9730 {
9731 vim_free(p);
9732 vim_free(remain);
9733 EMSG(_("E655: Too many symbolic links (cycle?)"));
9734 rettv->vval.v_string = NULL;
9735 goto fail;
9736 }
9737
9738 /* Ensure that the result will have a trailing path separator
9739 * if the argument has one. */
9740 if (remain == NULL && has_trailing_pathsep)
9741 add_pathsep(buf);
9742
9743 /* Separate the first path component in the link value and
9744 * concatenate the remainders. */
9745 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9746 if (*q != NUL)
9747 {
9748 if (remain == NULL)
9749 remain = vim_strsave(q - 1);
9750 else
9751 {
9752 cpy = concat_str(q - 1, remain);
9753 if (cpy != NULL)
9754 {
9755 vim_free(remain);
9756 remain = cpy;
9757 }
9758 }
9759 q[-1] = NUL;
9760 }
9761
9762 q = gettail(p);
9763 if (q > p && *q == NUL)
9764 {
9765 /* Ignore trailing path separator. */
9766 q[-1] = NUL;
9767 q = gettail(p);
9768 }
9769 if (q > p && !mch_isFullName(buf))
9770 {
9771 /* symlink is relative to directory of argument */
9772 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9773 if (cpy != NULL)
9774 {
9775 STRCPY(cpy, p);
9776 STRCPY(gettail(cpy), buf);
9777 vim_free(p);
9778 p = cpy;
9779 }
9780 }
9781 else
9782 {
9783 vim_free(p);
9784 p = vim_strsave(buf);
9785 }
9786 }
9787
9788 if (remain == NULL)
9789 break;
9790
9791 /* Append the first path component of "remain" to "p". */
9792 q = getnextcomp(remain + 1);
9793 len = q - remain - (*q != NUL);
9794 cpy = vim_strnsave(p, STRLEN(p) + len);
9795 if (cpy != NULL)
9796 {
9797 STRNCAT(cpy, remain, len);
9798 vim_free(p);
9799 p = cpy;
9800 }
9801 /* Shorten "remain". */
9802 if (*q != NUL)
9803 STRMOVE(remain, q - 1);
9804 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009805 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 }
9807
9808 /* If the result is a relative path name, make it explicitly relative to
9809 * the current directory if and only if the argument had this form. */
9810 if (!vim_ispathsep(*p))
9811 {
9812 if (is_relative_to_current
9813 && *p != NUL
9814 && !(p[0] == '.'
9815 && (p[1] == NUL
9816 || vim_ispathsep(p[1])
9817 || (p[1] == '.'
9818 && (p[2] == NUL
9819 || vim_ispathsep(p[2]))))))
9820 {
9821 /* Prepend "./". */
9822 cpy = concat_str((char_u *)"./", p);
9823 if (cpy != NULL)
9824 {
9825 vim_free(p);
9826 p = cpy;
9827 }
9828 }
9829 else if (!is_relative_to_current)
9830 {
9831 /* Strip leading "./". */
9832 q = p;
9833 while (q[0] == '.' && vim_ispathsep(q[1]))
9834 q += 2;
9835 if (q > p)
9836 STRMOVE(p, p + 2);
9837 }
9838 }
9839
9840 /* Ensure that the result will have no trailing path separator
9841 * if the argument had none. But keep "/" or "//". */
9842 if (!has_trailing_pathsep)
9843 {
9844 q = p + STRLEN(p);
9845 if (after_pathsep(p, q))
9846 *gettail_sep(p) = NUL;
9847 }
9848
9849 rettv->vval.v_string = p;
9850 }
9851# else
9852 rettv->vval.v_string = vim_strsave(p);
9853# endif
9854#endif
9855
9856 simplify_filename(rettv->vval.v_string);
9857
9858#ifdef HAVE_READLINK
9859fail:
9860 vim_free(buf);
9861#endif
9862 rettv->v_type = VAR_STRING;
9863}
9864
9865/*
9866 * "reverse({list})" function
9867 */
9868 static void
9869f_reverse(typval_T *argvars, typval_T *rettv)
9870{
9871 list_T *l;
9872 listitem_T *li, *ni;
9873
9874 if (argvars[0].v_type != VAR_LIST)
9875 EMSG2(_(e_listarg), "reverse()");
9876 else if ((l = argvars[0].vval.v_list) != NULL
9877 && !tv_check_lock(l->lv_lock,
9878 (char_u *)N_("reverse() argument"), TRUE))
9879 {
9880 li = l->lv_last;
9881 l->lv_first = l->lv_last = NULL;
9882 l->lv_len = 0;
9883 while (li != NULL)
9884 {
9885 ni = li->li_prev;
9886 list_append(l, li);
9887 li = ni;
9888 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009889 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 l->lv_idx = l->lv_len - l->lv_idx - 1;
9891 }
9892}
9893
9894#define SP_NOMOVE 0x01 /* don't move cursor */
9895#define SP_REPEAT 0x02 /* repeat to find outer pair */
9896#define SP_RETCOUNT 0x04 /* return matchcount */
9897#define SP_SETPCMARK 0x08 /* set previous context mark */
9898#define SP_START 0x10 /* accept match at start position */
9899#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9900#define SP_END 0x40 /* leave cursor at end of match */
9901#define SP_COLUMN 0x80 /* start at cursor column */
9902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903/*
9904 * Get flags for a search function.
9905 * Possibly sets "p_ws".
9906 * Returns BACKWARD, FORWARD or zero (for an error).
9907 */
9908 static int
9909get_search_arg(typval_T *varp, int *flagsp)
9910{
9911 int dir = FORWARD;
9912 char_u *flags;
9913 char_u nbuf[NUMBUFLEN];
9914 int mask;
9915
9916 if (varp->v_type != VAR_UNKNOWN)
9917 {
9918 flags = get_tv_string_buf_chk(varp, nbuf);
9919 if (flags == NULL)
9920 return 0; /* type error; errmsg already given */
9921 while (*flags != NUL)
9922 {
9923 switch (*flags)
9924 {
9925 case 'b': dir = BACKWARD; break;
9926 case 'w': p_ws = TRUE; break;
9927 case 'W': p_ws = FALSE; break;
9928 default: mask = 0;
9929 if (flagsp != NULL)
9930 switch (*flags)
9931 {
9932 case 'c': mask = SP_START; break;
9933 case 'e': mask = SP_END; break;
9934 case 'm': mask = SP_RETCOUNT; break;
9935 case 'n': mask = SP_NOMOVE; break;
9936 case 'p': mask = SP_SUBPAT; break;
9937 case 'r': mask = SP_REPEAT; break;
9938 case 's': mask = SP_SETPCMARK; break;
9939 case 'z': mask = SP_COLUMN; break;
9940 }
9941 if (mask == 0)
9942 {
9943 EMSG2(_(e_invarg2), flags);
9944 dir = 0;
9945 }
9946 else
9947 *flagsp |= mask;
9948 }
9949 if (dir == 0)
9950 break;
9951 ++flags;
9952 }
9953 }
9954 return dir;
9955}
9956
9957/*
9958 * Shared by search() and searchpos() functions.
9959 */
9960 static int
9961search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9962{
9963 int flags;
9964 char_u *pat;
9965 pos_T pos;
9966 pos_T save_cursor;
9967 int save_p_ws = p_ws;
9968 int dir;
9969 int retval = 0; /* default: FAIL */
9970 long lnum_stop = 0;
9971 proftime_T tm;
9972#ifdef FEAT_RELTIME
9973 long time_limit = 0;
9974#endif
9975 int options = SEARCH_KEEP;
9976 int subpatnum;
9977
9978 pat = get_tv_string(&argvars[0]);
9979 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9980 if (dir == 0)
9981 goto theend;
9982 flags = *flagsp;
9983 if (flags & SP_START)
9984 options |= SEARCH_START;
9985 if (flags & SP_END)
9986 options |= SEARCH_END;
9987 if (flags & SP_COLUMN)
9988 options |= SEARCH_COL;
9989
9990 /* Optional arguments: line number to stop searching and timeout. */
9991 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9992 {
9993 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9994 if (lnum_stop < 0)
9995 goto theend;
9996#ifdef FEAT_RELTIME
9997 if (argvars[3].v_type != VAR_UNKNOWN)
9998 {
9999 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
10000 if (time_limit < 0)
10001 goto theend;
10002 }
10003#endif
10004 }
10005
10006#ifdef FEAT_RELTIME
10007 /* Set the time limit, if there is one. */
10008 profile_setlimit(time_limit, &tm);
10009#endif
10010
10011 /*
10012 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10013 * Check to make sure only those flags are set.
10014 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10015 * flags cannot be set. Check for that condition also.
10016 */
10017 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10018 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10019 {
10020 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
10021 goto theend;
10022 }
10023
10024 pos = save_cursor = curwin->w_cursor;
10025 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010026 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 if (subpatnum != FAIL)
10028 {
10029 if (flags & SP_SUBPAT)
10030 retval = subpatnum;
10031 else
10032 retval = pos.lnum;
10033 if (flags & SP_SETPCMARK)
10034 setpcmark();
10035 curwin->w_cursor = pos;
10036 if (match_pos != NULL)
10037 {
10038 /* Store the match cursor position */
10039 match_pos->lnum = pos.lnum;
10040 match_pos->col = pos.col + 1;
10041 }
10042 /* "/$" will put the cursor after the end of the line, may need to
10043 * correct that here */
10044 check_cursor();
10045 }
10046
10047 /* If 'n' flag is used: restore cursor position. */
10048 if (flags & SP_NOMOVE)
10049 curwin->w_cursor = save_cursor;
10050 else
10051 curwin->w_set_curswant = TRUE;
10052theend:
10053 p_ws = save_p_ws;
10054
10055 return retval;
10056}
10057
10058#ifdef FEAT_FLOAT
10059
10060/*
10061 * round() is not in C90, use ceil() or floor() instead.
10062 */
10063 float_T
10064vim_round(float_T f)
10065{
10066 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10067}
10068
10069/*
10070 * "round({float})" function
10071 */
10072 static void
10073f_round(typval_T *argvars, typval_T *rettv)
10074{
10075 float_T f = 0.0;
10076
10077 rettv->v_type = VAR_FLOAT;
10078 if (get_float_arg(argvars, &f) == OK)
10079 rettv->vval.v_float = vim_round(f);
10080 else
10081 rettv->vval.v_float = 0.0;
10082}
10083#endif
10084
10085/*
10086 * "screenattr()" function
10087 */
10088 static void
10089f_screenattr(typval_T *argvars, typval_T *rettv)
10090{
10091 int row;
10092 int col;
10093 int c;
10094
10095 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10096 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10097 if (row < 0 || row >= screen_Rows
10098 || col < 0 || col >= screen_Columns)
10099 c = -1;
10100 else
10101 c = ScreenAttrs[LineOffset[row] + col];
10102 rettv->vval.v_number = c;
10103}
10104
10105/*
10106 * "screenchar()" function
10107 */
10108 static void
10109f_screenchar(typval_T *argvars, typval_T *rettv)
10110{
10111 int row;
10112 int col;
10113 int off;
10114 int c;
10115
10116 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10117 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10118 if (row < 0 || row >= screen_Rows
10119 || col < 0 || col >= screen_Columns)
10120 c = -1;
10121 else
10122 {
10123 off = LineOffset[row] + col;
10124#ifdef FEAT_MBYTE
10125 if (enc_utf8 && ScreenLinesUC[off] != 0)
10126 c = ScreenLinesUC[off];
10127 else
10128#endif
10129 c = ScreenLines[off];
10130 }
10131 rettv->vval.v_number = c;
10132}
10133
10134/*
10135 * "screencol()" function
10136 *
10137 * First column is 1 to be consistent with virtcol().
10138 */
10139 static void
10140f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10141{
10142 rettv->vval.v_number = screen_screencol() + 1;
10143}
10144
10145/*
10146 * "screenrow()" function
10147 */
10148 static void
10149f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10150{
10151 rettv->vval.v_number = screen_screenrow() + 1;
10152}
10153
10154/*
10155 * "search()" function
10156 */
10157 static void
10158f_search(typval_T *argvars, typval_T *rettv)
10159{
10160 int flags = 0;
10161
10162 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10163}
10164
10165/*
10166 * "searchdecl()" function
10167 */
10168 static void
10169f_searchdecl(typval_T *argvars, typval_T *rettv)
10170{
10171 int locally = 1;
10172 int thisblock = 0;
10173 int error = FALSE;
10174 char_u *name;
10175
10176 rettv->vval.v_number = 1; /* default: FAIL */
10177
10178 name = get_tv_string_chk(&argvars[0]);
10179 if (argvars[1].v_type != VAR_UNKNOWN)
10180 {
10181 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10182 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10183 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10184 }
10185 if (!error && name != NULL)
10186 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10187 locally, thisblock, SEARCH_KEEP) == FAIL;
10188}
10189
10190/*
10191 * Used by searchpair() and searchpairpos()
10192 */
10193 static int
10194searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10195{
10196 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010197 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 int save_p_ws = p_ws;
10199 int dir;
10200 int flags = 0;
10201 char_u nbuf1[NUMBUFLEN];
10202 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203 int retval = 0; /* default: FAIL */
10204 long lnum_stop = 0;
10205 long time_limit = 0;
10206
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010207 /* Get the three pattern arguments: start, middle, end. Will result in an
10208 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 spat = get_tv_string_chk(&argvars[0]);
10210 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10211 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10212 if (spat == NULL || mpat == NULL || epat == NULL)
10213 goto theend; /* type error */
10214
10215 /* Handle the optional fourth argument: flags */
10216 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10217 if (dir == 0)
10218 goto theend;
10219
10220 /* Don't accept SP_END or SP_SUBPAT.
10221 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10222 */
10223 if ((flags & (SP_END | SP_SUBPAT)) != 0
10224 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10225 {
10226 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10227 goto theend;
10228 }
10229
10230 /* Using 'r' implies 'W', otherwise it doesn't work. */
10231 if (flags & SP_REPEAT)
10232 p_ws = FALSE;
10233
10234 /* Optional fifth argument: skip expression */
10235 if (argvars[3].v_type == VAR_UNKNOWN
10236 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010237 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 else
10239 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010240 skip = &argvars[4];
10241 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10242 && skip->v_type != VAR_STRING)
10243 {
10244 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010245 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010246 goto theend;
10247 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010248 if (argvars[5].v_type != VAR_UNKNOWN)
10249 {
10250 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10251 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010252 {
10253 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010255 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256#ifdef FEAT_RELTIME
10257 if (argvars[6].v_type != VAR_UNKNOWN)
10258 {
10259 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10260 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010261 {
10262 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010264 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 }
10266#endif
10267 }
10268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269
10270 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10271 match_pos, lnum_stop, time_limit);
10272
10273theend:
10274 p_ws = save_p_ws;
10275
10276 return retval;
10277}
10278
10279/*
10280 * "searchpair()" function
10281 */
10282 static void
10283f_searchpair(typval_T *argvars, typval_T *rettv)
10284{
10285 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10286}
10287
10288/*
10289 * "searchpairpos()" function
10290 */
10291 static void
10292f_searchpairpos(typval_T *argvars, typval_T *rettv)
10293{
10294 pos_T match_pos;
10295 int lnum = 0;
10296 int col = 0;
10297
10298 if (rettv_list_alloc(rettv) == FAIL)
10299 return;
10300
10301 if (searchpair_cmn(argvars, &match_pos) > 0)
10302 {
10303 lnum = match_pos.lnum;
10304 col = match_pos.col;
10305 }
10306
10307 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10308 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10309}
10310
10311/*
10312 * Search for a start/middle/end thing.
10313 * Used by searchpair(), see its documentation for the details.
10314 * Returns 0 or -1 for no match,
10315 */
10316 long
10317do_searchpair(
10318 char_u *spat, /* start pattern */
10319 char_u *mpat, /* middle pattern */
10320 char_u *epat, /* end pattern */
10321 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010322 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 int flags, /* SP_SETPCMARK and other SP_ values */
10324 pos_T *match_pos,
10325 linenr_T lnum_stop, /* stop at this line if not zero */
10326 long time_limit UNUSED) /* stop after this many msec */
10327{
10328 char_u *save_cpo;
10329 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10330 long retval = 0;
10331 pos_T pos;
10332 pos_T firstpos;
10333 pos_T foundpos;
10334 pos_T save_cursor;
10335 pos_T save_pos;
10336 int n;
10337 int r;
10338 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010339 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010340 int err;
10341 int options = SEARCH_KEEP;
10342 proftime_T tm;
10343
10344 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10345 save_cpo = p_cpo;
10346 p_cpo = empty_option;
10347
10348#ifdef FEAT_RELTIME
10349 /* Set the time limit, if there is one. */
10350 profile_setlimit(time_limit, &tm);
10351#endif
10352
10353 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10354 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010355 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10356 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 if (pat2 == NULL || pat3 == NULL)
10358 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010359 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 if (*mpat == NUL)
10361 STRCPY(pat3, pat2);
10362 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010363 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010364 spat, epat, mpat);
10365 if (flags & SP_START)
10366 options |= SEARCH_START;
10367
Bram Moolenaar48570482017-10-30 21:48:41 +010010368 if (skip != NULL)
10369 {
10370 /* Empty string means to not use the skip expression. */
10371 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10372 use_skip = skip->vval.v_string != NULL
10373 && *skip->vval.v_string != NUL;
10374 }
10375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 save_cursor = curwin->w_cursor;
10377 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010378 CLEAR_POS(&firstpos);
10379 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010380 pat = pat3;
10381 for (;;)
10382 {
10383 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010384 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010385 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010386 /* didn't find it or found the first match again: FAIL */
10387 break;
10388
10389 if (firstpos.lnum == 0)
10390 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010391 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 {
10393 /* Found the same position again. Can happen with a pattern that
10394 * has "\zs" at the end and searching backwards. Advance one
10395 * character and try again. */
10396 if (dir == BACKWARD)
10397 decl(&pos);
10398 else
10399 incl(&pos);
10400 }
10401 foundpos = pos;
10402
10403 /* clear the start flag to avoid getting stuck here */
10404 options &= ~SEARCH_START;
10405
10406 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010407 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 {
10409 save_pos = curwin->w_cursor;
10410 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010411 err = FALSE;
10412 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413 curwin->w_cursor = save_pos;
10414 if (err)
10415 {
10416 /* Evaluating {skip} caused an error, break here. */
10417 curwin->w_cursor = save_cursor;
10418 retval = -1;
10419 break;
10420 }
10421 if (r)
10422 continue;
10423 }
10424
10425 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10426 {
10427 /* Found end when searching backwards or start when searching
10428 * forward: nested pair. */
10429 ++nest;
10430 pat = pat2; /* nested, don't search for middle */
10431 }
10432 else
10433 {
10434 /* Found end when searching forward or start when searching
10435 * backward: end of (nested) pair; or found middle in outer pair. */
10436 if (--nest == 1)
10437 pat = pat3; /* outer level, search for middle */
10438 }
10439
10440 if (nest == 0)
10441 {
10442 /* Found the match: return matchcount or line number. */
10443 if (flags & SP_RETCOUNT)
10444 ++retval;
10445 else
10446 retval = pos.lnum;
10447 if (flags & SP_SETPCMARK)
10448 setpcmark();
10449 curwin->w_cursor = pos;
10450 if (!(flags & SP_REPEAT))
10451 break;
10452 nest = 1; /* search for next unmatched */
10453 }
10454 }
10455
10456 if (match_pos != NULL)
10457 {
10458 /* Store the match cursor position */
10459 match_pos->lnum = curwin->w_cursor.lnum;
10460 match_pos->col = curwin->w_cursor.col + 1;
10461 }
10462
10463 /* If 'n' flag is used or search failed: restore cursor position. */
10464 if ((flags & SP_NOMOVE) || retval == 0)
10465 curwin->w_cursor = save_cursor;
10466
10467theend:
10468 vim_free(pat2);
10469 vim_free(pat3);
10470 if (p_cpo == empty_option)
10471 p_cpo = save_cpo;
10472 else
10473 /* Darn, evaluating the {skip} expression changed the value. */
10474 free_string_option(save_cpo);
10475
10476 return retval;
10477}
10478
10479/*
10480 * "searchpos()" function
10481 */
10482 static void
10483f_searchpos(typval_T *argvars, typval_T *rettv)
10484{
10485 pos_T match_pos;
10486 int lnum = 0;
10487 int col = 0;
10488 int n;
10489 int flags = 0;
10490
10491 if (rettv_list_alloc(rettv) == FAIL)
10492 return;
10493
10494 n = search_cmn(argvars, &match_pos, &flags);
10495 if (n > 0)
10496 {
10497 lnum = match_pos.lnum;
10498 col = match_pos.col;
10499 }
10500
10501 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10502 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10503 if (flags & SP_SUBPAT)
10504 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10505}
10506
10507 static void
10508f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10509{
10510#ifdef FEAT_CLIENTSERVER
10511 char_u buf[NUMBUFLEN];
10512 char_u *server = get_tv_string_chk(&argvars[0]);
10513 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10514
10515 rettv->vval.v_number = -1;
10516 if (server == NULL || reply == NULL)
10517 return;
10518 if (check_restricted() || check_secure())
10519 return;
10520# ifdef FEAT_X11
10521 if (check_connection() == FAIL)
10522 return;
10523# endif
10524
10525 if (serverSendReply(server, reply) < 0)
10526 {
10527 EMSG(_("E258: Unable to send to client"));
10528 return;
10529 }
10530 rettv->vval.v_number = 0;
10531#else
10532 rettv->vval.v_number = -1;
10533#endif
10534}
10535
10536 static void
10537f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10538{
10539 char_u *r = NULL;
10540
10541#ifdef FEAT_CLIENTSERVER
10542# ifdef WIN32
10543 r = serverGetVimNames();
10544# else
10545 make_connection();
10546 if (X_DISPLAY != NULL)
10547 r = serverGetVimNames(X_DISPLAY);
10548# endif
10549#endif
10550 rettv->v_type = VAR_STRING;
10551 rettv->vval.v_string = r;
10552}
10553
10554/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010555 * "setbufline()" function
10556 */
10557 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010558f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010559{
10560 linenr_T lnum;
10561 buf_T *buf;
10562
10563 buf = get_buf_tv(&argvars[0], FALSE);
10564 if (buf == NULL)
10565 rettv->vval.v_number = 1; /* FAIL */
10566 else
10567 {
10568 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010569 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010570 }
10571}
10572
10573/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574 * "setbufvar()" function
10575 */
10576 static void
10577f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10578{
10579 buf_T *buf;
10580 char_u *varname, *bufvarname;
10581 typval_T *varp;
10582 char_u nbuf[NUMBUFLEN];
10583
10584 if (check_restricted() || check_secure())
10585 return;
10586 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10587 varname = get_tv_string_chk(&argvars[1]);
10588 buf = get_buf_tv(&argvars[0], FALSE);
10589 varp = &argvars[2];
10590
10591 if (buf != NULL && varname != NULL && varp != NULL)
10592 {
10593 if (*varname == '&')
10594 {
10595 long numval;
10596 char_u *strval;
10597 int error = FALSE;
10598 aco_save_T aco;
10599
10600 /* set curbuf to be our buf, temporarily */
10601 aucmd_prepbuf(&aco, buf);
10602
10603 ++varname;
10604 numval = (long)get_tv_number_chk(varp, &error);
10605 strval = get_tv_string_buf_chk(varp, nbuf);
10606 if (!error && strval != NULL)
10607 set_option_value(varname, numval, strval, OPT_LOCAL);
10608
10609 /* reset notion of buffer */
10610 aucmd_restbuf(&aco);
10611 }
10612 else
10613 {
10614 buf_T *save_curbuf = curbuf;
10615
10616 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10617 if (bufvarname != NULL)
10618 {
10619 curbuf = buf;
10620 STRCPY(bufvarname, "b:");
10621 STRCPY(bufvarname + 2, varname);
10622 set_var(bufvarname, varp, TRUE);
10623 vim_free(bufvarname);
10624 curbuf = save_curbuf;
10625 }
10626 }
10627 }
10628}
10629
10630 static void
10631f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10632{
10633 dict_T *d;
10634 dictitem_T *di;
10635 char_u *csearch;
10636
10637 if (argvars[0].v_type != VAR_DICT)
10638 {
10639 EMSG(_(e_dictreq));
10640 return;
10641 }
10642
10643 if ((d = argvars[0].vval.v_dict) != NULL)
10644 {
10645 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10646 if (csearch != NULL)
10647 {
10648#ifdef FEAT_MBYTE
10649 if (enc_utf8)
10650 {
10651 int pcc[MAX_MCO];
10652 int c = utfc_ptr2char(csearch, pcc);
10653
10654 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10655 }
10656 else
10657#endif
10658 set_last_csearch(PTR2CHAR(csearch),
10659 csearch, MB_PTR2LEN(csearch));
10660 }
10661
10662 di = dict_find(d, (char_u *)"forward", -1);
10663 if (di != NULL)
10664 set_csearch_direction((int)get_tv_number(&di->di_tv)
10665 ? FORWARD : BACKWARD);
10666
10667 di = dict_find(d, (char_u *)"until", -1);
10668 if (di != NULL)
10669 set_csearch_until(!!get_tv_number(&di->di_tv));
10670 }
10671}
10672
10673/*
10674 * "setcmdpos()" function
10675 */
10676 static void
10677f_setcmdpos(typval_T *argvars, typval_T *rettv)
10678{
10679 int pos = (int)get_tv_number(&argvars[0]) - 1;
10680
10681 if (pos >= 0)
10682 rettv->vval.v_number = set_cmdline_pos(pos);
10683}
10684
10685/*
10686 * "setfperm({fname}, {mode})" function
10687 */
10688 static void
10689f_setfperm(typval_T *argvars, typval_T *rettv)
10690{
10691 char_u *fname;
10692 char_u modebuf[NUMBUFLEN];
10693 char_u *mode_str;
10694 int i;
10695 int mask;
10696 int mode = 0;
10697
10698 rettv->vval.v_number = 0;
10699 fname = get_tv_string_chk(&argvars[0]);
10700 if (fname == NULL)
10701 return;
10702 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10703 if (mode_str == NULL)
10704 return;
10705 if (STRLEN(mode_str) != 9)
10706 {
10707 EMSG2(_(e_invarg2), mode_str);
10708 return;
10709 }
10710
10711 mask = 1;
10712 for (i = 8; i >= 0; --i)
10713 {
10714 if (mode_str[i] != '-')
10715 mode |= mask;
10716 mask = mask << 1;
10717 }
10718 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10719}
10720
10721/*
10722 * "setline()" function
10723 */
10724 static void
10725f_setline(typval_T *argvars, typval_T *rettv)
10726{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010727 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010728
Bram Moolenaarca851592018-06-06 21:04:07 +020010729 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730}
10731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732/*
10733 * Used by "setqflist()" and "setloclist()" functions
10734 */
10735 static void
10736set_qf_ll_list(
10737 win_T *wp UNUSED,
10738 typval_T *list_arg UNUSED,
10739 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010740 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 typval_T *rettv)
10742{
10743#ifdef FEAT_QUICKFIX
10744 static char *e_invact = N_("E927: Invalid action: '%s'");
10745 char_u *act;
10746 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010747 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748#endif
10749
10750 rettv->vval.v_number = -1;
10751
10752#ifdef FEAT_QUICKFIX
10753 if (list_arg->v_type != VAR_LIST)
10754 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010755 else if (recursive != 0)
10756 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 else
10758 {
10759 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010760 dict_T *d = NULL;
10761 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762
10763 if (action_arg->v_type == VAR_STRING)
10764 {
10765 act = get_tv_string_chk(action_arg);
10766 if (act == NULL)
10767 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010768 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10769 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 action = *act;
10771 else
10772 EMSG2(_(e_invact), act);
10773 }
10774 else if (action_arg->v_type == VAR_UNKNOWN)
10775 action = ' ';
10776 else
10777 EMSG(_(e_stringreq));
10778
Bram Moolenaard823fa92016-08-12 16:29:27 +020010779 if (action_arg->v_type != VAR_UNKNOWN
10780 && what_arg->v_type != VAR_UNKNOWN)
10781 {
10782 if (what_arg->v_type == VAR_DICT)
10783 d = what_arg->vval.v_dict;
10784 else
10785 {
10786 EMSG(_(e_dictreq));
10787 valid_dict = FALSE;
10788 }
10789 }
10790
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010791 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010792 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010793 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10794 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010796 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 }
10798#endif
10799}
10800
10801/*
10802 * "setloclist()" function
10803 */
10804 static void
10805f_setloclist(typval_T *argvars, typval_T *rettv)
10806{
10807 win_T *win;
10808
10809 rettv->vval.v_number = -1;
10810
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010811 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010813 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814}
10815
10816/*
10817 * "setmatches()" function
10818 */
10819 static void
10820f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10821{
10822#ifdef FEAT_SEARCH_EXTRA
10823 list_T *l;
10824 listitem_T *li;
10825 dict_T *d;
10826 list_T *s = NULL;
10827
10828 rettv->vval.v_number = -1;
10829 if (argvars[0].v_type != VAR_LIST)
10830 {
10831 EMSG(_(e_listreq));
10832 return;
10833 }
10834 if ((l = argvars[0].vval.v_list) != NULL)
10835 {
10836
10837 /* To some extent make sure that we are dealing with a list from
10838 * "getmatches()". */
10839 li = l->lv_first;
10840 while (li != NULL)
10841 {
10842 if (li->li_tv.v_type != VAR_DICT
10843 || (d = li->li_tv.vval.v_dict) == NULL)
10844 {
10845 EMSG(_(e_invarg));
10846 return;
10847 }
10848 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10849 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10850 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10851 && dict_find(d, (char_u *)"priority", -1) != NULL
10852 && dict_find(d, (char_u *)"id", -1) != NULL))
10853 {
10854 EMSG(_(e_invarg));
10855 return;
10856 }
10857 li = li->li_next;
10858 }
10859
10860 clear_matches(curwin);
10861 li = l->lv_first;
10862 while (li != NULL)
10863 {
10864 int i = 0;
10865 char_u buf[5];
10866 dictitem_T *di;
10867 char_u *group;
10868 int priority;
10869 int id;
10870 char_u *conceal;
10871
10872 d = li->li_tv.vval.v_dict;
10873 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10874 {
10875 if (s == NULL)
10876 {
10877 s = list_alloc();
10878 if (s == NULL)
10879 return;
10880 }
10881
10882 /* match from matchaddpos() */
10883 for (i = 1; i < 9; i++)
10884 {
10885 sprintf((char *)buf, (char *)"pos%d", i);
10886 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10887 {
10888 if (di->di_tv.v_type != VAR_LIST)
10889 return;
10890
10891 list_append_tv(s, &di->di_tv);
10892 s->lv_refcount++;
10893 }
10894 else
10895 break;
10896 }
10897 }
10898
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010899 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 priority = (int)get_dict_number(d, (char_u *)"priority");
10901 id = (int)get_dict_number(d, (char_u *)"id");
10902 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010903 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 : NULL;
10905 if (i == 0)
10906 {
10907 match_add(curwin, group,
10908 get_dict_string(d, (char_u *)"pattern", FALSE),
10909 priority, id, NULL, conceal);
10910 }
10911 else
10912 {
10913 match_add(curwin, group, NULL, priority, id, s, conceal);
10914 list_unref(s);
10915 s = NULL;
10916 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010917 vim_free(group);
10918 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919
10920 li = li->li_next;
10921 }
10922 rettv->vval.v_number = 0;
10923 }
10924#endif
10925}
10926
10927/*
10928 * "setpos()" function
10929 */
10930 static void
10931f_setpos(typval_T *argvars, typval_T *rettv)
10932{
10933 pos_T pos;
10934 int fnum;
10935 char_u *name;
10936 colnr_T curswant = -1;
10937
10938 rettv->vval.v_number = -1;
10939 name = get_tv_string_chk(argvars);
10940 if (name != NULL)
10941 {
10942 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10943 {
10944 if (--pos.col < 0)
10945 pos.col = 0;
10946 if (name[0] == '.' && name[1] == NUL)
10947 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010948 /* set cursor; "fnum" is ignored */
10949 curwin->w_cursor = pos;
10950 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010951 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010952 curwin->w_curswant = curswant - 1;
10953 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010954 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010955 check_cursor();
10956 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957 }
10958 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10959 {
10960 /* set mark */
10961 if (setmark_pos(name[1], &pos, fnum) == OK)
10962 rettv->vval.v_number = 0;
10963 }
10964 else
10965 EMSG(_(e_invarg));
10966 }
10967 }
10968}
10969
10970/*
10971 * "setqflist()" function
10972 */
10973 static void
10974f_setqflist(typval_T *argvars, typval_T *rettv)
10975{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010976 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977}
10978
10979/*
10980 * "setreg()" function
10981 */
10982 static void
10983f_setreg(typval_T *argvars, typval_T *rettv)
10984{
10985 int regname;
10986 char_u *strregname;
10987 char_u *stropt;
10988 char_u *strval;
10989 int append;
10990 char_u yank_type;
10991 long block_len;
10992
10993 block_len = -1;
10994 yank_type = MAUTO;
10995 append = FALSE;
10996
10997 strregname = get_tv_string_chk(argvars);
10998 rettv->vval.v_number = 1; /* FAIL is default */
10999
11000 if (strregname == NULL)
11001 return; /* type error; errmsg already given */
11002 regname = *strregname;
11003 if (regname == 0 || regname == '@')
11004 regname = '"';
11005
11006 if (argvars[2].v_type != VAR_UNKNOWN)
11007 {
11008 stropt = get_tv_string_chk(&argvars[2]);
11009 if (stropt == NULL)
11010 return; /* type error */
11011 for (; *stropt != NUL; ++stropt)
11012 switch (*stropt)
11013 {
11014 case 'a': case 'A': /* append */
11015 append = TRUE;
11016 break;
11017 case 'v': case 'c': /* character-wise selection */
11018 yank_type = MCHAR;
11019 break;
11020 case 'V': case 'l': /* line-wise selection */
11021 yank_type = MLINE;
11022 break;
11023 case 'b': case Ctrl_V: /* block-wise selection */
11024 yank_type = MBLOCK;
11025 if (VIM_ISDIGIT(stropt[1]))
11026 {
11027 ++stropt;
11028 block_len = getdigits(&stropt) - 1;
11029 --stropt;
11030 }
11031 break;
11032 }
11033 }
11034
11035 if (argvars[1].v_type == VAR_LIST)
11036 {
11037 char_u **lstval;
11038 char_u **allocval;
11039 char_u buf[NUMBUFLEN];
11040 char_u **curval;
11041 char_u **curallocval;
11042 list_T *ll = argvars[1].vval.v_list;
11043 listitem_T *li;
11044 int len;
11045
11046 /* If the list is NULL handle like an empty list. */
11047 len = ll == NULL ? 0 : ll->lv_len;
11048
11049 /* First half: use for pointers to result lines; second half: use for
11050 * pointers to allocated copies. */
11051 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11052 if (lstval == NULL)
11053 return;
11054 curval = lstval;
11055 allocval = lstval + len + 2;
11056 curallocval = allocval;
11057
11058 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11059 li = li->li_next)
11060 {
11061 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11062 if (strval == NULL)
11063 goto free_lstval;
11064 if (strval == buf)
11065 {
11066 /* Need to make a copy, next get_tv_string_buf_chk() will
11067 * overwrite the string. */
11068 strval = vim_strsave(buf);
11069 if (strval == NULL)
11070 goto free_lstval;
11071 *curallocval++ = strval;
11072 }
11073 *curval++ = strval;
11074 }
11075 *curval++ = NULL;
11076
11077 write_reg_contents_lst(regname, lstval, -1,
11078 append, yank_type, block_len);
11079free_lstval:
11080 while (curallocval > allocval)
11081 vim_free(*--curallocval);
11082 vim_free(lstval);
11083 }
11084 else
11085 {
11086 strval = get_tv_string_chk(&argvars[1]);
11087 if (strval == NULL)
11088 return;
11089 write_reg_contents_ex(regname, strval, -1,
11090 append, yank_type, block_len);
11091 }
11092 rettv->vval.v_number = 0;
11093}
11094
11095/*
11096 * "settabvar()" function
11097 */
11098 static void
11099f_settabvar(typval_T *argvars, typval_T *rettv)
11100{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 tabpage_T *save_curtab;
11102 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011103 char_u *varname, *tabvarname;
11104 typval_T *varp;
11105
11106 rettv->vval.v_number = 0;
11107
11108 if (check_restricted() || check_secure())
11109 return;
11110
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112 varname = get_tv_string_chk(&argvars[1]);
11113 varp = &argvars[2];
11114
Bram Moolenaar4033c552017-09-16 20:54:51 +020011115 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011117 save_curtab = curtab;
11118 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119
11120 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11121 if (tabvarname != NULL)
11122 {
11123 STRCPY(tabvarname, "t:");
11124 STRCPY(tabvarname + 2, varname);
11125 set_var(tabvarname, varp, TRUE);
11126 vim_free(tabvarname);
11127 }
11128
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 /* Restore current tabpage */
11130 if (valid_tabpage(save_curtab))
11131 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132 }
11133}
11134
11135/*
11136 * "settabwinvar()" function
11137 */
11138 static void
11139f_settabwinvar(typval_T *argvars, typval_T *rettv)
11140{
11141 setwinvar(argvars, rettv, 1);
11142}
11143
11144/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011145 * "settagstack()" function
11146 */
11147 static void
11148f_settagstack(typval_T *argvars, typval_T *rettv)
11149{
11150 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11151 win_T *wp;
11152 dict_T *d;
11153 int action = 'r';
11154
11155 rettv->vval.v_number = -1;
11156
11157 // first argument: window number or id
11158 wp = find_win_by_nr_or_id(&argvars[0]);
11159 if (wp == NULL)
11160 return;
11161
11162 // second argument: dict with items to set in the tag stack
11163 if (argvars[1].v_type != VAR_DICT)
11164 {
11165 EMSG(_(e_dictreq));
11166 return;
11167 }
11168 d = argvars[1].vval.v_dict;
11169 if (d == NULL)
11170 return;
11171
11172 // third argument: action - 'a' for append and 'r' for replace.
11173 // default is to replace the stack.
11174 if (argvars[2].v_type == VAR_UNKNOWN)
11175 action = 'r';
11176 else if (argvars[2].v_type == VAR_STRING)
11177 {
11178 char_u *actstr;
11179 actstr = get_tv_string_chk(&argvars[2]);
11180 if (actstr == NULL)
11181 return;
11182 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11183 action = *actstr;
11184 else
11185 {
11186 EMSG2(_(e_invact2), actstr);
11187 return;
11188 }
11189 }
11190 else
11191 {
11192 EMSG(_(e_stringreq));
11193 return;
11194 }
11195
11196 if (set_tagstack(wp, d, action) == OK)
11197 rettv->vval.v_number = 0;
11198}
11199
11200/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011201 * "setwinvar()" function
11202 */
11203 static void
11204f_setwinvar(typval_T *argvars, typval_T *rettv)
11205{
11206 setwinvar(argvars, rettv, 0);
11207}
11208
11209#ifdef FEAT_CRYPT
11210/*
11211 * "sha256({string})" function
11212 */
11213 static void
11214f_sha256(typval_T *argvars, typval_T *rettv)
11215{
11216 char_u *p;
11217
11218 p = get_tv_string(&argvars[0]);
11219 rettv->vval.v_string = vim_strsave(
11220 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11221 rettv->v_type = VAR_STRING;
11222}
11223#endif /* FEAT_CRYPT */
11224
11225/*
11226 * "shellescape({string})" function
11227 */
11228 static void
11229f_shellescape(typval_T *argvars, typval_T *rettv)
11230{
Bram Moolenaar20615522017-06-05 18:46:26 +020011231 int do_special = non_zero_arg(&argvars[1]);
11232
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011233 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011234 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 rettv->v_type = VAR_STRING;
11236}
11237
11238/*
11239 * shiftwidth() function
11240 */
11241 static void
11242f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11243{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011244 rettv->vval.v_number = 0;
11245
11246 if (argvars[0].v_type != VAR_UNKNOWN)
11247 {
11248 long col;
11249
11250 col = (long)get_tv_number_chk(argvars, NULL);
11251 if (col < 0)
11252 return; // type error; errmsg already given
11253#ifdef FEAT_VARTABS
11254 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11255 return;
11256#endif
11257 }
11258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011259 rettv->vval.v_number = get_sw_value(curbuf);
11260}
11261
11262/*
11263 * "simplify()" function
11264 */
11265 static void
11266f_simplify(typval_T *argvars, typval_T *rettv)
11267{
11268 char_u *p;
11269
11270 p = get_tv_string(&argvars[0]);
11271 rettv->vval.v_string = vim_strsave(p);
11272 simplify_filename(rettv->vval.v_string); /* simplify in place */
11273 rettv->v_type = VAR_STRING;
11274}
11275
11276#ifdef FEAT_FLOAT
11277/*
11278 * "sin()" function
11279 */
11280 static void
11281f_sin(typval_T *argvars, typval_T *rettv)
11282{
11283 float_T f = 0.0;
11284
11285 rettv->v_type = VAR_FLOAT;
11286 if (get_float_arg(argvars, &f) == OK)
11287 rettv->vval.v_float = sin(f);
11288 else
11289 rettv->vval.v_float = 0.0;
11290}
11291
11292/*
11293 * "sinh()" function
11294 */
11295 static void
11296f_sinh(typval_T *argvars, typval_T *rettv)
11297{
11298 float_T f = 0.0;
11299
11300 rettv->v_type = VAR_FLOAT;
11301 if (get_float_arg(argvars, &f) == OK)
11302 rettv->vval.v_float = sinh(f);
11303 else
11304 rettv->vval.v_float = 0.0;
11305}
11306#endif
11307
11308static int
11309#ifdef __BORLANDC__
11310 _RTLENTRYF
11311#endif
11312 item_compare(const void *s1, const void *s2);
11313static int
11314#ifdef __BORLANDC__
11315 _RTLENTRYF
11316#endif
11317 item_compare2(const void *s1, const void *s2);
11318
11319/* struct used in the array that's given to qsort() */
11320typedef struct
11321{
11322 listitem_T *item;
11323 int idx;
11324} sortItem_T;
11325
11326/* struct storing information about current sort */
11327typedef struct
11328{
11329 int item_compare_ic;
11330 int item_compare_numeric;
11331 int item_compare_numbers;
11332#ifdef FEAT_FLOAT
11333 int item_compare_float;
11334#endif
11335 char_u *item_compare_func;
11336 partial_T *item_compare_partial;
11337 dict_T *item_compare_selfdict;
11338 int item_compare_func_err;
11339 int item_compare_keep_zero;
11340} sortinfo_T;
11341static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011342#define ITEM_COMPARE_FAIL 999
11343
11344/*
11345 * Compare functions for f_sort() and f_uniq() below.
11346 */
11347 static int
11348#ifdef __BORLANDC__
11349_RTLENTRYF
11350#endif
11351item_compare(const void *s1, const void *s2)
11352{
11353 sortItem_T *si1, *si2;
11354 typval_T *tv1, *tv2;
11355 char_u *p1, *p2;
11356 char_u *tofree1 = NULL, *tofree2 = NULL;
11357 int res;
11358 char_u numbuf1[NUMBUFLEN];
11359 char_u numbuf2[NUMBUFLEN];
11360
11361 si1 = (sortItem_T *)s1;
11362 si2 = (sortItem_T *)s2;
11363 tv1 = &si1->item->li_tv;
11364 tv2 = &si2->item->li_tv;
11365
11366 if (sortinfo->item_compare_numbers)
11367 {
11368 varnumber_T v1 = get_tv_number(tv1);
11369 varnumber_T v2 = get_tv_number(tv2);
11370
11371 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11372 }
11373
11374#ifdef FEAT_FLOAT
11375 if (sortinfo->item_compare_float)
11376 {
11377 float_T v1 = get_tv_float(tv1);
11378 float_T v2 = get_tv_float(tv2);
11379
11380 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11381 }
11382#endif
11383
11384 /* tv2string() puts quotes around a string and allocates memory. Don't do
11385 * that for string variables. Use a single quote when comparing with a
11386 * non-string to do what the docs promise. */
11387 if (tv1->v_type == VAR_STRING)
11388 {
11389 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11390 p1 = (char_u *)"'";
11391 else
11392 p1 = tv1->vval.v_string;
11393 }
11394 else
11395 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11396 if (tv2->v_type == VAR_STRING)
11397 {
11398 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11399 p2 = (char_u *)"'";
11400 else
11401 p2 = tv2->vval.v_string;
11402 }
11403 else
11404 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11405 if (p1 == NULL)
11406 p1 = (char_u *)"";
11407 if (p2 == NULL)
11408 p2 = (char_u *)"";
11409 if (!sortinfo->item_compare_numeric)
11410 {
11411 if (sortinfo->item_compare_ic)
11412 res = STRICMP(p1, p2);
11413 else
11414 res = STRCMP(p1, p2);
11415 }
11416 else
11417 {
11418 double n1, n2;
11419 n1 = strtod((char *)p1, (char **)&p1);
11420 n2 = strtod((char *)p2, (char **)&p2);
11421 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11422 }
11423
11424 /* When the result would be zero, compare the item indexes. Makes the
11425 * sort stable. */
11426 if (res == 0 && !sortinfo->item_compare_keep_zero)
11427 res = si1->idx > si2->idx ? 1 : -1;
11428
11429 vim_free(tofree1);
11430 vim_free(tofree2);
11431 return res;
11432}
11433
11434 static int
11435#ifdef __BORLANDC__
11436_RTLENTRYF
11437#endif
11438item_compare2(const void *s1, const void *s2)
11439{
11440 sortItem_T *si1, *si2;
11441 int res;
11442 typval_T rettv;
11443 typval_T argv[3];
11444 int dummy;
11445 char_u *func_name;
11446 partial_T *partial = sortinfo->item_compare_partial;
11447
11448 /* shortcut after failure in previous call; compare all items equal */
11449 if (sortinfo->item_compare_func_err)
11450 return 0;
11451
11452 si1 = (sortItem_T *)s1;
11453 si2 = (sortItem_T *)s2;
11454
11455 if (partial == NULL)
11456 func_name = sortinfo->item_compare_func;
11457 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011458 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011459
11460 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11461 * in the copy without changing the original list items. */
11462 copy_tv(&si1->item->li_tv, &argv[0]);
11463 copy_tv(&si2->item->li_tv, &argv[1]);
11464
11465 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11466 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011467 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 partial, sortinfo->item_compare_selfdict);
11469 clear_tv(&argv[0]);
11470 clear_tv(&argv[1]);
11471
11472 if (res == FAIL)
11473 res = ITEM_COMPARE_FAIL;
11474 else
11475 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11476 if (sortinfo->item_compare_func_err)
11477 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11478 clear_tv(&rettv);
11479
11480 /* When the result would be zero, compare the pointers themselves. Makes
11481 * the sort stable. */
11482 if (res == 0 && !sortinfo->item_compare_keep_zero)
11483 res = si1->idx > si2->idx ? 1 : -1;
11484
11485 return res;
11486}
11487
11488/*
11489 * "sort({list})" function
11490 */
11491 static void
11492do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11493{
11494 list_T *l;
11495 listitem_T *li;
11496 sortItem_T *ptrs;
11497 sortinfo_T *old_sortinfo;
11498 sortinfo_T info;
11499 long len;
11500 long i;
11501
11502 /* Pointer to current info struct used in compare function. Save and
11503 * restore the current one for nested calls. */
11504 old_sortinfo = sortinfo;
11505 sortinfo = &info;
11506
11507 if (argvars[0].v_type != VAR_LIST)
11508 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11509 else
11510 {
11511 l = argvars[0].vval.v_list;
11512 if (l == NULL || tv_check_lock(l->lv_lock,
11513 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11514 TRUE))
11515 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011516 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011517
11518 len = list_len(l);
11519 if (len <= 1)
11520 goto theend; /* short list sorts pretty quickly */
11521
11522 info.item_compare_ic = FALSE;
11523 info.item_compare_numeric = FALSE;
11524 info.item_compare_numbers = FALSE;
11525#ifdef FEAT_FLOAT
11526 info.item_compare_float = FALSE;
11527#endif
11528 info.item_compare_func = NULL;
11529 info.item_compare_partial = NULL;
11530 info.item_compare_selfdict = NULL;
11531 if (argvars[1].v_type != VAR_UNKNOWN)
11532 {
11533 /* optional second argument: {func} */
11534 if (argvars[1].v_type == VAR_FUNC)
11535 info.item_compare_func = argvars[1].vval.v_string;
11536 else if (argvars[1].v_type == VAR_PARTIAL)
11537 info.item_compare_partial = argvars[1].vval.v_partial;
11538 else
11539 {
11540 int error = FALSE;
11541
11542 i = (long)get_tv_number_chk(&argvars[1], &error);
11543 if (error)
11544 goto theend; /* type error; errmsg already given */
11545 if (i == 1)
11546 info.item_compare_ic = TRUE;
11547 else if (argvars[1].v_type != VAR_NUMBER)
11548 info.item_compare_func = get_tv_string(&argvars[1]);
11549 else if (i != 0)
11550 {
11551 EMSG(_(e_invarg));
11552 goto theend;
11553 }
11554 if (info.item_compare_func != NULL)
11555 {
11556 if (*info.item_compare_func == NUL)
11557 {
11558 /* empty string means default sort */
11559 info.item_compare_func = NULL;
11560 }
11561 else if (STRCMP(info.item_compare_func, "n") == 0)
11562 {
11563 info.item_compare_func = NULL;
11564 info.item_compare_numeric = TRUE;
11565 }
11566 else if (STRCMP(info.item_compare_func, "N") == 0)
11567 {
11568 info.item_compare_func = NULL;
11569 info.item_compare_numbers = TRUE;
11570 }
11571#ifdef FEAT_FLOAT
11572 else if (STRCMP(info.item_compare_func, "f") == 0)
11573 {
11574 info.item_compare_func = NULL;
11575 info.item_compare_float = TRUE;
11576 }
11577#endif
11578 else if (STRCMP(info.item_compare_func, "i") == 0)
11579 {
11580 info.item_compare_func = NULL;
11581 info.item_compare_ic = TRUE;
11582 }
11583 }
11584 }
11585
11586 if (argvars[2].v_type != VAR_UNKNOWN)
11587 {
11588 /* optional third argument: {dict} */
11589 if (argvars[2].v_type != VAR_DICT)
11590 {
11591 EMSG(_(e_dictreq));
11592 goto theend;
11593 }
11594 info.item_compare_selfdict = argvars[2].vval.v_dict;
11595 }
11596 }
11597
11598 /* Make an array with each entry pointing to an item in the List. */
11599 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11600 if (ptrs == NULL)
11601 goto theend;
11602
11603 i = 0;
11604 if (sort)
11605 {
11606 /* sort(): ptrs will be the list to sort */
11607 for (li = l->lv_first; li != NULL; li = li->li_next)
11608 {
11609 ptrs[i].item = li;
11610 ptrs[i].idx = i;
11611 ++i;
11612 }
11613
11614 info.item_compare_func_err = FALSE;
11615 info.item_compare_keep_zero = FALSE;
11616 /* test the compare function */
11617 if ((info.item_compare_func != NULL
11618 || info.item_compare_partial != NULL)
11619 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11620 == ITEM_COMPARE_FAIL)
11621 EMSG(_("E702: Sort compare function failed"));
11622 else
11623 {
11624 /* Sort the array with item pointers. */
11625 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11626 info.item_compare_func == NULL
11627 && info.item_compare_partial == NULL
11628 ? item_compare : item_compare2);
11629
11630 if (!info.item_compare_func_err)
11631 {
11632 /* Clear the List and append the items in sorted order. */
11633 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11634 l->lv_len = 0;
11635 for (i = 0; i < len; ++i)
11636 list_append(l, ptrs[i].item);
11637 }
11638 }
11639 }
11640 else
11641 {
11642 int (*item_compare_func_ptr)(const void *, const void *);
11643
11644 /* f_uniq(): ptrs will be a stack of items to remove */
11645 info.item_compare_func_err = FALSE;
11646 info.item_compare_keep_zero = TRUE;
11647 item_compare_func_ptr = info.item_compare_func != NULL
11648 || info.item_compare_partial != NULL
11649 ? item_compare2 : item_compare;
11650
11651 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11652 li = li->li_next)
11653 {
11654 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11655 == 0)
11656 ptrs[i++].item = li;
11657 if (info.item_compare_func_err)
11658 {
11659 EMSG(_("E882: Uniq compare function failed"));
11660 break;
11661 }
11662 }
11663
11664 if (!info.item_compare_func_err)
11665 {
11666 while (--i >= 0)
11667 {
11668 li = ptrs[i].item->li_next;
11669 ptrs[i].item->li_next = li->li_next;
11670 if (li->li_next != NULL)
11671 li->li_next->li_prev = ptrs[i].item;
11672 else
11673 l->lv_last = ptrs[i].item;
11674 list_fix_watch(l, li);
11675 listitem_free(li);
11676 l->lv_len--;
11677 }
11678 }
11679 }
11680
11681 vim_free(ptrs);
11682 }
11683theend:
11684 sortinfo = old_sortinfo;
11685}
11686
11687/*
11688 * "sort({list})" function
11689 */
11690 static void
11691f_sort(typval_T *argvars, typval_T *rettv)
11692{
11693 do_sort_uniq(argvars, rettv, TRUE);
11694}
11695
11696/*
11697 * "uniq({list})" function
11698 */
11699 static void
11700f_uniq(typval_T *argvars, typval_T *rettv)
11701{
11702 do_sort_uniq(argvars, rettv, FALSE);
11703}
11704
11705/*
11706 * "soundfold({word})" function
11707 */
11708 static void
11709f_soundfold(typval_T *argvars, typval_T *rettv)
11710{
11711 char_u *s;
11712
11713 rettv->v_type = VAR_STRING;
11714 s = get_tv_string(&argvars[0]);
11715#ifdef FEAT_SPELL
11716 rettv->vval.v_string = eval_soundfold(s);
11717#else
11718 rettv->vval.v_string = vim_strsave(s);
11719#endif
11720}
11721
11722/*
11723 * "spellbadword()" function
11724 */
11725 static void
11726f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11727{
11728 char_u *word = (char_u *)"";
11729 hlf_T attr = HLF_COUNT;
11730 int len = 0;
11731
11732 if (rettv_list_alloc(rettv) == FAIL)
11733 return;
11734
11735#ifdef FEAT_SPELL
11736 if (argvars[0].v_type == VAR_UNKNOWN)
11737 {
11738 /* Find the start and length of the badly spelled word. */
11739 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11740 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011742 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011743 curwin->w_set_curswant = TRUE;
11744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011745 }
11746 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11747 {
11748 char_u *str = get_tv_string_chk(&argvars[0]);
11749 int capcol = -1;
11750
11751 if (str != NULL)
11752 {
11753 /* Check the argument for spelling. */
11754 while (*str != NUL)
11755 {
11756 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11757 if (attr != HLF_COUNT)
11758 {
11759 word = str;
11760 break;
11761 }
11762 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011763 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011764 }
11765 }
11766 }
11767#endif
11768
11769 list_append_string(rettv->vval.v_list, word, len);
11770 list_append_string(rettv->vval.v_list, (char_u *)(
11771 attr == HLF_SPB ? "bad" :
11772 attr == HLF_SPR ? "rare" :
11773 attr == HLF_SPL ? "local" :
11774 attr == HLF_SPC ? "caps" :
11775 ""), -1);
11776}
11777
11778/*
11779 * "spellsuggest()" function
11780 */
11781 static void
11782f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11783{
11784#ifdef FEAT_SPELL
11785 char_u *str;
11786 int typeerr = FALSE;
11787 int maxcount;
11788 garray_T ga;
11789 int i;
11790 listitem_T *li;
11791 int need_capital = FALSE;
11792#endif
11793
11794 if (rettv_list_alloc(rettv) == FAIL)
11795 return;
11796
11797#ifdef FEAT_SPELL
11798 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11799 {
11800 str = get_tv_string(&argvars[0]);
11801 if (argvars[1].v_type != VAR_UNKNOWN)
11802 {
11803 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11804 if (maxcount <= 0)
11805 return;
11806 if (argvars[2].v_type != VAR_UNKNOWN)
11807 {
11808 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11809 if (typeerr)
11810 return;
11811 }
11812 }
11813 else
11814 maxcount = 25;
11815
11816 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11817
11818 for (i = 0; i < ga.ga_len; ++i)
11819 {
11820 str = ((char_u **)ga.ga_data)[i];
11821
11822 li = listitem_alloc();
11823 if (li == NULL)
11824 vim_free(str);
11825 else
11826 {
11827 li->li_tv.v_type = VAR_STRING;
11828 li->li_tv.v_lock = 0;
11829 li->li_tv.vval.v_string = str;
11830 list_append(rettv->vval.v_list, li);
11831 }
11832 }
11833 ga_clear(&ga);
11834 }
11835#endif
11836}
11837
11838 static void
11839f_split(typval_T *argvars, typval_T *rettv)
11840{
11841 char_u *str;
11842 char_u *end;
11843 char_u *pat = NULL;
11844 regmatch_T regmatch;
11845 char_u patbuf[NUMBUFLEN];
11846 char_u *save_cpo;
11847 int match;
11848 colnr_T col = 0;
11849 int keepempty = FALSE;
11850 int typeerr = FALSE;
11851
11852 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11853 save_cpo = p_cpo;
11854 p_cpo = (char_u *)"";
11855
11856 str = get_tv_string(&argvars[0]);
11857 if (argvars[1].v_type != VAR_UNKNOWN)
11858 {
11859 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11860 if (pat == NULL)
11861 typeerr = TRUE;
11862 if (argvars[2].v_type != VAR_UNKNOWN)
11863 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11864 }
11865 if (pat == NULL || *pat == NUL)
11866 pat = (char_u *)"[\\x01- ]\\+";
11867
11868 if (rettv_list_alloc(rettv) == FAIL)
11869 return;
11870 if (typeerr)
11871 return;
11872
11873 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11874 if (regmatch.regprog != NULL)
11875 {
11876 regmatch.rm_ic = FALSE;
11877 while (*str != NUL || keepempty)
11878 {
11879 if (*str == NUL)
11880 match = FALSE; /* empty item at the end */
11881 else
11882 match = vim_regexec_nl(&regmatch, str, col);
11883 if (match)
11884 end = regmatch.startp[0];
11885 else
11886 end = str + STRLEN(str);
11887 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11888 && *str != NUL && match && end < regmatch.endp[0]))
11889 {
11890 if (list_append_string(rettv->vval.v_list, str,
11891 (int)(end - str)) == FAIL)
11892 break;
11893 }
11894 if (!match)
11895 break;
11896 /* Advance to just after the match. */
11897 if (regmatch.endp[0] > str)
11898 col = 0;
11899 else
11900 {
11901 /* Don't get stuck at the same match. */
11902#ifdef FEAT_MBYTE
11903 col = (*mb_ptr2len)(regmatch.endp[0]);
11904#else
11905 col = 1;
11906#endif
11907 }
11908 str = regmatch.endp[0];
11909 }
11910
11911 vim_regfree(regmatch.regprog);
11912 }
11913
11914 p_cpo = save_cpo;
11915}
11916
11917#ifdef FEAT_FLOAT
11918/*
11919 * "sqrt()" function
11920 */
11921 static void
11922f_sqrt(typval_T *argvars, typval_T *rettv)
11923{
11924 float_T f = 0.0;
11925
11926 rettv->v_type = VAR_FLOAT;
11927 if (get_float_arg(argvars, &f) == OK)
11928 rettv->vval.v_float = sqrt(f);
11929 else
11930 rettv->vval.v_float = 0.0;
11931}
11932
11933/*
11934 * "str2float()" function
11935 */
11936 static void
11937f_str2float(typval_T *argvars, typval_T *rettv)
11938{
11939 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011940 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011941
Bram Moolenaar08243d22017-01-10 16:12:29 +010011942 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011943 p = skipwhite(p + 1);
11944 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011945 if (isneg)
11946 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011947 rettv->v_type = VAR_FLOAT;
11948}
11949#endif
11950
11951/*
11952 * "str2nr()" function
11953 */
11954 static void
11955f_str2nr(typval_T *argvars, typval_T *rettv)
11956{
11957 int base = 10;
11958 char_u *p;
11959 varnumber_T n;
11960 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011961 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011962
11963 if (argvars[1].v_type != VAR_UNKNOWN)
11964 {
11965 base = (int)get_tv_number(&argvars[1]);
11966 if (base != 2 && base != 8 && base != 10 && base != 16)
11967 {
11968 EMSG(_(e_invarg));
11969 return;
11970 }
11971 }
11972
11973 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011974 isneg = (*p == '-');
11975 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011976 p = skipwhite(p + 1);
11977 switch (base)
11978 {
11979 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11980 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11981 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11982 default: what = 0;
11983 }
11984 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011985 if (isneg)
11986 rettv->vval.v_number = -n;
11987 else
11988 rettv->vval.v_number = n;
11989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011990}
11991
11992#ifdef HAVE_STRFTIME
11993/*
11994 * "strftime({format}[, {time}])" function
11995 */
11996 static void
11997f_strftime(typval_T *argvars, typval_T *rettv)
11998{
11999 char_u result_buf[256];
12000 struct tm *curtime;
12001 time_t seconds;
12002 char_u *p;
12003
12004 rettv->v_type = VAR_STRING;
12005
12006 p = get_tv_string(&argvars[0]);
12007 if (argvars[1].v_type == VAR_UNKNOWN)
12008 seconds = time(NULL);
12009 else
12010 seconds = (time_t)get_tv_number(&argvars[1]);
12011 curtime = localtime(&seconds);
12012 /* MSVC returns NULL for an invalid value of seconds. */
12013 if (curtime == NULL)
12014 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12015 else
12016 {
12017# ifdef FEAT_MBYTE
12018 vimconv_T conv;
12019 char_u *enc;
12020
12021 conv.vc_type = CONV_NONE;
12022 enc = enc_locale();
12023 convert_setup(&conv, p_enc, enc);
12024 if (conv.vc_type != CONV_NONE)
12025 p = string_convert(&conv, p, NULL);
12026# endif
12027 if (p != NULL)
12028 (void)strftime((char *)result_buf, sizeof(result_buf),
12029 (char *)p, curtime);
12030 else
12031 result_buf[0] = NUL;
12032
12033# ifdef FEAT_MBYTE
12034 if (conv.vc_type != CONV_NONE)
12035 vim_free(p);
12036 convert_setup(&conv, enc, p_enc);
12037 if (conv.vc_type != CONV_NONE)
12038 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12039 else
12040# endif
12041 rettv->vval.v_string = vim_strsave(result_buf);
12042
12043# ifdef FEAT_MBYTE
12044 /* Release conversion descriptors */
12045 convert_setup(&conv, NULL, NULL);
12046 vim_free(enc);
12047# endif
12048 }
12049}
12050#endif
12051
12052/*
12053 * "strgetchar()" function
12054 */
12055 static void
12056f_strgetchar(typval_T *argvars, typval_T *rettv)
12057{
12058 char_u *str;
12059 int len;
12060 int error = FALSE;
12061 int charidx;
12062
12063 rettv->vval.v_number = -1;
12064 str = get_tv_string_chk(&argvars[0]);
12065 if (str == NULL)
12066 return;
12067 len = (int)STRLEN(str);
12068 charidx = (int)get_tv_number_chk(&argvars[1], &error);
12069 if (error)
12070 return;
12071#ifdef FEAT_MBYTE
12072 {
12073 int byteidx = 0;
12074
12075 while (charidx >= 0 && byteidx < len)
12076 {
12077 if (charidx == 0)
12078 {
12079 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12080 break;
12081 }
12082 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012083 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012084 }
12085 }
12086#else
12087 if (charidx < len)
12088 rettv->vval.v_number = str[charidx];
12089#endif
12090}
12091
12092/*
12093 * "stridx()" function
12094 */
12095 static void
12096f_stridx(typval_T *argvars, typval_T *rettv)
12097{
12098 char_u buf[NUMBUFLEN];
12099 char_u *needle;
12100 char_u *haystack;
12101 char_u *save_haystack;
12102 char_u *pos;
12103 int start_idx;
12104
12105 needle = get_tv_string_chk(&argvars[1]);
12106 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
12107 rettv->vval.v_number = -1;
12108 if (needle == NULL || haystack == NULL)
12109 return; /* type error; errmsg already given */
12110
12111 if (argvars[2].v_type != VAR_UNKNOWN)
12112 {
12113 int error = FALSE;
12114
12115 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
12116 if (error || start_idx >= (int)STRLEN(haystack))
12117 return;
12118 if (start_idx >= 0)
12119 haystack += start_idx;
12120 }
12121
12122 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12123 if (pos != NULL)
12124 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12125}
12126
12127/*
12128 * "string()" function
12129 */
12130 static void
12131f_string(typval_T *argvars, typval_T *rettv)
12132{
12133 char_u *tofree;
12134 char_u numbuf[NUMBUFLEN];
12135
12136 rettv->v_type = VAR_STRING;
12137 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12138 get_copyID());
12139 /* Make a copy if we have a value but it's not in allocated memory. */
12140 if (rettv->vval.v_string != NULL && tofree == NULL)
12141 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12142}
12143
12144/*
12145 * "strlen()" function
12146 */
12147 static void
12148f_strlen(typval_T *argvars, typval_T *rettv)
12149{
12150 rettv->vval.v_number = (varnumber_T)(STRLEN(
12151 get_tv_string(&argvars[0])));
12152}
12153
12154/*
12155 * "strchars()" function
12156 */
12157 static void
12158f_strchars(typval_T *argvars, typval_T *rettv)
12159{
12160 char_u *s = get_tv_string(&argvars[0]);
12161 int skipcc = 0;
12162#ifdef FEAT_MBYTE
12163 varnumber_T len = 0;
12164 int (*func_mb_ptr2char_adv)(char_u **pp);
12165#endif
12166
12167 if (argvars[1].v_type != VAR_UNKNOWN)
12168 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12169 if (skipcc < 0 || skipcc > 1)
12170 EMSG(_(e_invarg));
12171 else
12172 {
12173#ifdef FEAT_MBYTE
12174 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12175 while (*s != NUL)
12176 {
12177 func_mb_ptr2char_adv(&s);
12178 ++len;
12179 }
12180 rettv->vval.v_number = len;
12181#else
12182 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12183#endif
12184 }
12185}
12186
12187/*
12188 * "strdisplaywidth()" function
12189 */
12190 static void
12191f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12192{
12193 char_u *s = get_tv_string(&argvars[0]);
12194 int col = 0;
12195
12196 if (argvars[1].v_type != VAR_UNKNOWN)
12197 col = (int)get_tv_number(&argvars[1]);
12198
12199 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12200}
12201
12202/*
12203 * "strwidth()" function
12204 */
12205 static void
12206f_strwidth(typval_T *argvars, typval_T *rettv)
12207{
12208 char_u *s = get_tv_string(&argvars[0]);
12209
12210 rettv->vval.v_number = (varnumber_T)(
12211#ifdef FEAT_MBYTE
12212 mb_string2cells(s, -1)
12213#else
12214 STRLEN(s)
12215#endif
12216 );
12217}
12218
12219/*
12220 * "strcharpart()" function
12221 */
12222 static void
12223f_strcharpart(typval_T *argvars, typval_T *rettv)
12224{
12225#ifdef FEAT_MBYTE
12226 char_u *p;
12227 int nchar;
12228 int nbyte = 0;
12229 int charlen;
12230 int len = 0;
12231 int slen;
12232 int error = FALSE;
12233
12234 p = get_tv_string(&argvars[0]);
12235 slen = (int)STRLEN(p);
12236
12237 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12238 if (!error)
12239 {
12240 if (nchar > 0)
12241 while (nchar > 0 && nbyte < slen)
12242 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012243 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012244 --nchar;
12245 }
12246 else
12247 nbyte = nchar;
12248 if (argvars[2].v_type != VAR_UNKNOWN)
12249 {
12250 charlen = (int)get_tv_number(&argvars[2]);
12251 while (charlen > 0 && nbyte + len < slen)
12252 {
12253 int off = nbyte + len;
12254
12255 if (off < 0)
12256 len += 1;
12257 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012258 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012259 --charlen;
12260 }
12261 }
12262 else
12263 len = slen - nbyte; /* default: all bytes that are available. */
12264 }
12265
12266 /*
12267 * Only return the overlap between the specified part and the actual
12268 * string.
12269 */
12270 if (nbyte < 0)
12271 {
12272 len += nbyte;
12273 nbyte = 0;
12274 }
12275 else if (nbyte > slen)
12276 nbyte = slen;
12277 if (len < 0)
12278 len = 0;
12279 else if (nbyte + len > slen)
12280 len = slen - nbyte;
12281
12282 rettv->v_type = VAR_STRING;
12283 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12284#else
12285 f_strpart(argvars, rettv);
12286#endif
12287}
12288
12289/*
12290 * "strpart()" function
12291 */
12292 static void
12293f_strpart(typval_T *argvars, typval_T *rettv)
12294{
12295 char_u *p;
12296 int n;
12297 int len;
12298 int slen;
12299 int error = FALSE;
12300
12301 p = get_tv_string(&argvars[0]);
12302 slen = (int)STRLEN(p);
12303
12304 n = (int)get_tv_number_chk(&argvars[1], &error);
12305 if (error)
12306 len = 0;
12307 else if (argvars[2].v_type != VAR_UNKNOWN)
12308 len = (int)get_tv_number(&argvars[2]);
12309 else
12310 len = slen - n; /* default len: all bytes that are available. */
12311
12312 /*
12313 * Only return the overlap between the specified part and the actual
12314 * string.
12315 */
12316 if (n < 0)
12317 {
12318 len += n;
12319 n = 0;
12320 }
12321 else if (n > slen)
12322 n = slen;
12323 if (len < 0)
12324 len = 0;
12325 else if (n + len > slen)
12326 len = slen - n;
12327
12328 rettv->v_type = VAR_STRING;
12329 rettv->vval.v_string = vim_strnsave(p + n, len);
12330}
12331
12332/*
12333 * "strridx()" function
12334 */
12335 static void
12336f_strridx(typval_T *argvars, typval_T *rettv)
12337{
12338 char_u buf[NUMBUFLEN];
12339 char_u *needle;
12340 char_u *haystack;
12341 char_u *rest;
12342 char_u *lastmatch = NULL;
12343 int haystack_len, end_idx;
12344
12345 needle = get_tv_string_chk(&argvars[1]);
12346 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12347
12348 rettv->vval.v_number = -1;
12349 if (needle == NULL || haystack == NULL)
12350 return; /* type error; errmsg already given */
12351
12352 haystack_len = (int)STRLEN(haystack);
12353 if (argvars[2].v_type != VAR_UNKNOWN)
12354 {
12355 /* Third argument: upper limit for index */
12356 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12357 if (end_idx < 0)
12358 return; /* can never find a match */
12359 }
12360 else
12361 end_idx = haystack_len;
12362
12363 if (*needle == NUL)
12364 {
12365 /* Empty string matches past the end. */
12366 lastmatch = haystack + end_idx;
12367 }
12368 else
12369 {
12370 for (rest = haystack; *rest != '\0'; ++rest)
12371 {
12372 rest = (char_u *)strstr((char *)rest, (char *)needle);
12373 if (rest == NULL || rest > haystack + end_idx)
12374 break;
12375 lastmatch = rest;
12376 }
12377 }
12378
12379 if (lastmatch == NULL)
12380 rettv->vval.v_number = -1;
12381 else
12382 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12383}
12384
12385/*
12386 * "strtrans()" function
12387 */
12388 static void
12389f_strtrans(typval_T *argvars, typval_T *rettv)
12390{
12391 rettv->v_type = VAR_STRING;
12392 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12393}
12394
12395/*
12396 * "submatch()" function
12397 */
12398 static void
12399f_submatch(typval_T *argvars, typval_T *rettv)
12400{
12401 int error = FALSE;
12402 int no;
12403 int retList = 0;
12404
12405 no = (int)get_tv_number_chk(&argvars[0], &error);
12406 if (error)
12407 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012408 if (no < 0 || no >= NSUBEXP)
12409 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012410 EMSGN(_("E935: invalid submatch number: %d"), no);
12411 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012412 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413 if (argvars[1].v_type != VAR_UNKNOWN)
12414 retList = (int)get_tv_number_chk(&argvars[1], &error);
12415 if (error)
12416 return;
12417
12418 if (retList == 0)
12419 {
12420 rettv->v_type = VAR_STRING;
12421 rettv->vval.v_string = reg_submatch(no);
12422 }
12423 else
12424 {
12425 rettv->v_type = VAR_LIST;
12426 rettv->vval.v_list = reg_submatch_list(no);
12427 }
12428}
12429
12430/*
12431 * "substitute()" function
12432 */
12433 static void
12434f_substitute(typval_T *argvars, typval_T *rettv)
12435{
12436 char_u patbuf[NUMBUFLEN];
12437 char_u subbuf[NUMBUFLEN];
12438 char_u flagsbuf[NUMBUFLEN];
12439
12440 char_u *str = get_tv_string_chk(&argvars[0]);
12441 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012442 char_u *sub = NULL;
12443 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12445
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012446 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12447 expr = &argvars[2];
12448 else
12449 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012452 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12453 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 rettv->vval.v_string = NULL;
12455 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012456 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012457}
12458
12459/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012460 * "swapinfo(swap_filename)" function
12461 */
12462 static void
12463f_swapinfo(typval_T *argvars, typval_T *rettv)
12464{
12465 if (rettv_dict_alloc(rettv) == OK)
12466 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12467}
12468
12469/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012470 * "swapname(expr)" function
12471 */
12472 static void
12473f_swapname(typval_T *argvars, typval_T *rettv)
12474{
12475 buf_T *buf;
12476
12477 rettv->v_type = VAR_STRING;
12478 buf = get_buf_tv(&argvars[0], FALSE);
12479 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12480 || buf->b_ml.ml_mfp->mf_fname == NULL)
12481 rettv->vval.v_string = NULL;
12482 else
12483 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12484}
12485
12486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487 * "synID(lnum, col, trans)" function
12488 */
12489 static void
12490f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12491{
12492 int id = 0;
12493#ifdef FEAT_SYN_HL
12494 linenr_T lnum;
12495 colnr_T col;
12496 int trans;
12497 int transerr = FALSE;
12498
12499 lnum = get_tv_lnum(argvars); /* -1 on type error */
12500 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12501 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12502
12503 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12504 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12505 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12506#endif
12507
12508 rettv->vval.v_number = id;
12509}
12510
12511/*
12512 * "synIDattr(id, what [, mode])" function
12513 */
12514 static void
12515f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12516{
12517 char_u *p = NULL;
12518#ifdef FEAT_SYN_HL
12519 int id;
12520 char_u *what;
12521 char_u *mode;
12522 char_u modebuf[NUMBUFLEN];
12523 int modec;
12524
12525 id = (int)get_tv_number(&argvars[0]);
12526 what = get_tv_string(&argvars[1]);
12527 if (argvars[2].v_type != VAR_UNKNOWN)
12528 {
12529 mode = get_tv_string_buf(&argvars[2], modebuf);
12530 modec = TOLOWER_ASC(mode[0]);
12531 if (modec != 't' && modec != 'c' && modec != 'g')
12532 modec = 0; /* replace invalid with current */
12533 }
12534 else
12535 {
12536#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12537 if (USE_24BIT)
12538 modec = 'g';
12539 else
12540#endif
12541 if (t_colors > 1)
12542 modec = 'c';
12543 else
12544 modec = 't';
12545 }
12546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547 switch (TOLOWER_ASC(what[0]))
12548 {
12549 case 'b':
12550 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12551 p = highlight_color(id, what, modec);
12552 else /* bold */
12553 p = highlight_has_attr(id, HL_BOLD, modec);
12554 break;
12555
12556 case 'f': /* fg[#] or font */
12557 p = highlight_color(id, what, modec);
12558 break;
12559
12560 case 'i':
12561 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12562 p = highlight_has_attr(id, HL_INVERSE, modec);
12563 else /* italic */
12564 p = highlight_has_attr(id, HL_ITALIC, modec);
12565 break;
12566
12567 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012568 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 break;
12570
12571 case 'r': /* reverse */
12572 p = highlight_has_attr(id, HL_INVERSE, modec);
12573 break;
12574
12575 case 's':
12576 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12577 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012578 /* strikeout */
12579 else if (TOLOWER_ASC(what[1]) == 't' &&
12580 TOLOWER_ASC(what[2]) == 'r')
12581 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582 else /* standout */
12583 p = highlight_has_attr(id, HL_STANDOUT, modec);
12584 break;
12585
12586 case 'u':
12587 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12588 /* underline */
12589 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12590 else
12591 /* undercurl */
12592 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12593 break;
12594 }
12595
12596 if (p != NULL)
12597 p = vim_strsave(p);
12598#endif
12599 rettv->v_type = VAR_STRING;
12600 rettv->vval.v_string = p;
12601}
12602
12603/*
12604 * "synIDtrans(id)" function
12605 */
12606 static void
12607f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12608{
12609 int id;
12610
12611#ifdef FEAT_SYN_HL
12612 id = (int)get_tv_number(&argvars[0]);
12613
12614 if (id > 0)
12615 id = syn_get_final_id(id);
12616 else
12617#endif
12618 id = 0;
12619
12620 rettv->vval.v_number = id;
12621}
12622
12623/*
12624 * "synconcealed(lnum, col)" function
12625 */
12626 static void
12627f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12628{
12629#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12630 linenr_T lnum;
12631 colnr_T col;
12632 int syntax_flags = 0;
12633 int cchar;
12634 int matchid = 0;
12635 char_u str[NUMBUFLEN];
12636#endif
12637
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012638 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639
12640#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12641 lnum = get_tv_lnum(argvars); /* -1 on type error */
12642 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12643
12644 vim_memset(str, NUL, sizeof(str));
12645
12646 if (rettv_list_alloc(rettv) != FAIL)
12647 {
12648 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12649 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12650 && curwin->w_p_cole > 0)
12651 {
12652 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12653 syntax_flags = get_syntax_info(&matchid);
12654
12655 /* get the conceal character */
12656 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12657 {
12658 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012659 if (cchar == NUL && curwin->w_p_cole == 1)
12660 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 if (cchar != NUL)
12662 {
12663# ifdef FEAT_MBYTE
12664 if (has_mbyte)
12665 (*mb_char2bytes)(cchar, str);
12666 else
12667# endif
12668 str[0] = cchar;
12669 }
12670 }
12671 }
12672
12673 list_append_number(rettv->vval.v_list,
12674 (syntax_flags & HL_CONCEAL) != 0);
12675 /* -1 to auto-determine strlen */
12676 list_append_string(rettv->vval.v_list, str, -1);
12677 list_append_number(rettv->vval.v_list, matchid);
12678 }
12679#endif
12680}
12681
12682/*
12683 * "synstack(lnum, col)" function
12684 */
12685 static void
12686f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12687{
12688#ifdef FEAT_SYN_HL
12689 linenr_T lnum;
12690 colnr_T col;
12691 int i;
12692 int id;
12693#endif
12694
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012695 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696
12697#ifdef FEAT_SYN_HL
12698 lnum = get_tv_lnum(argvars); /* -1 on type error */
12699 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12700
12701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12702 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12703 && rettv_list_alloc(rettv) != FAIL)
12704 {
12705 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12706 for (i = 0; ; ++i)
12707 {
12708 id = syn_get_stack_item(i);
12709 if (id < 0)
12710 break;
12711 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12712 break;
12713 }
12714 }
12715#endif
12716}
12717
12718 static void
12719get_cmd_output_as_rettv(
12720 typval_T *argvars,
12721 typval_T *rettv,
12722 int retlist)
12723{
12724 char_u *res = NULL;
12725 char_u *p;
12726 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012727 int err = FALSE;
12728 FILE *fd;
12729 list_T *list = NULL;
12730 int flags = SHELL_SILENT;
12731
12732 rettv->v_type = VAR_STRING;
12733 rettv->vval.v_string = NULL;
12734 if (check_restricted() || check_secure())
12735 goto errret;
12736
12737 if (argvars[1].v_type != VAR_UNKNOWN)
12738 {
12739 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012740 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 * command.
12742 */
12743 if ((infile = vim_tempname('i', TRUE)) == NULL)
12744 {
12745 EMSG(_(e_notmp));
12746 goto errret;
12747 }
12748
12749 fd = mch_fopen((char *)infile, WRITEBIN);
12750 if (fd == NULL)
12751 {
12752 EMSG2(_(e_notopen), infile);
12753 goto errret;
12754 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012755 if (argvars[1].v_type == VAR_NUMBER)
12756 {
12757 linenr_T lnum;
12758 buf_T *buf;
12759
12760 buf = buflist_findnr(argvars[1].vval.v_number);
12761 if (buf == NULL)
12762 {
12763 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012764 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012765 goto errret;
12766 }
12767
12768 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12769 {
12770 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12771 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12772 {
12773 err = TRUE;
12774 break;
12775 }
12776 if (putc(NL, fd) == EOF)
12777 {
12778 err = TRUE;
12779 break;
12780 }
12781 }
12782 }
12783 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784 {
12785 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12786 err = TRUE;
12787 }
12788 else
12789 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012790 size_t len;
12791 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792
12793 p = get_tv_string_buf_chk(&argvars[1], buf);
12794 if (p == NULL)
12795 {
12796 fclose(fd);
12797 goto errret; /* type error; errmsg already given */
12798 }
12799 len = STRLEN(p);
12800 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12801 err = TRUE;
12802 }
12803 if (fclose(fd) != 0)
12804 err = TRUE;
12805 if (err)
12806 {
12807 EMSG(_("E677: Error writing temp file"));
12808 goto errret;
12809 }
12810 }
12811
12812 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12813 * echoes typeahead, that messes up the display. */
12814 if (!msg_silent)
12815 flags += SHELL_COOKED;
12816
12817 if (retlist)
12818 {
12819 int len;
12820 listitem_T *li;
12821 char_u *s = NULL;
12822 char_u *start;
12823 char_u *end;
12824 int i;
12825
12826 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12827 if (res == NULL)
12828 goto errret;
12829
12830 list = list_alloc();
12831 if (list == NULL)
12832 goto errret;
12833
12834 for (i = 0; i < len; ++i)
12835 {
12836 start = res + i;
12837 while (i < len && res[i] != NL)
12838 ++i;
12839 end = res + i;
12840
12841 s = alloc((unsigned)(end - start + 1));
12842 if (s == NULL)
12843 goto errret;
12844
12845 for (p = s; start < end; ++p, ++start)
12846 *p = *start == NUL ? NL : *start;
12847 *p = NUL;
12848
12849 li = listitem_alloc();
12850 if (li == NULL)
12851 {
12852 vim_free(s);
12853 goto errret;
12854 }
12855 li->li_tv.v_type = VAR_STRING;
12856 li->li_tv.v_lock = 0;
12857 li->li_tv.vval.v_string = s;
12858 list_append(list, li);
12859 }
12860
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012861 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862 list = NULL;
12863 }
12864 else
12865 {
12866 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12867#ifdef USE_CR
12868 /* translate <CR> into <NL> */
12869 if (res != NULL)
12870 {
12871 char_u *s;
12872
12873 for (s = res; *s; ++s)
12874 {
12875 if (*s == CAR)
12876 *s = NL;
12877 }
12878 }
12879#else
12880# ifdef USE_CRNL
12881 /* translate <CR><NL> into <NL> */
12882 if (res != NULL)
12883 {
12884 char_u *s, *d;
12885
12886 d = res;
12887 for (s = res; *s; ++s)
12888 {
12889 if (s[0] == CAR && s[1] == NL)
12890 ++s;
12891 *d++ = *s;
12892 }
12893 *d = NUL;
12894 }
12895# endif
12896#endif
12897 rettv->vval.v_string = res;
12898 res = NULL;
12899 }
12900
12901errret:
12902 if (infile != NULL)
12903 {
12904 mch_remove(infile);
12905 vim_free(infile);
12906 }
12907 if (res != NULL)
12908 vim_free(res);
12909 if (list != NULL)
12910 list_free(list);
12911}
12912
12913/*
12914 * "system()" function
12915 */
12916 static void
12917f_system(typval_T *argvars, typval_T *rettv)
12918{
12919 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12920}
12921
12922/*
12923 * "systemlist()" function
12924 */
12925 static void
12926f_systemlist(typval_T *argvars, typval_T *rettv)
12927{
12928 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12929}
12930
12931/*
12932 * "tabpagebuflist()" function
12933 */
12934 static void
12935f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12936{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 tabpage_T *tp;
12938 win_T *wp = NULL;
12939
12940 if (argvars[0].v_type == VAR_UNKNOWN)
12941 wp = firstwin;
12942 else
12943 {
12944 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12945 if (tp != NULL)
12946 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12947 }
12948 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12949 {
12950 for (; wp != NULL; wp = wp->w_next)
12951 if (list_append_number(rettv->vval.v_list,
12952 wp->w_buffer->b_fnum) == FAIL)
12953 break;
12954 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012955}
12956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012957/*
12958 * "tabpagenr()" function
12959 */
12960 static void
12961f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12962{
12963 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964 char_u *arg;
12965
12966 if (argvars[0].v_type != VAR_UNKNOWN)
12967 {
12968 arg = get_tv_string_chk(&argvars[0]);
12969 nr = 0;
12970 if (arg != NULL)
12971 {
12972 if (STRCMP(arg, "$") == 0)
12973 nr = tabpage_index(NULL) - 1;
12974 else
12975 EMSG2(_(e_invexpr2), arg);
12976 }
12977 }
12978 else
12979 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 rettv->vval.v_number = nr;
12981}
12982
12983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012984/*
12985 * Common code for tabpagewinnr() and winnr().
12986 */
12987 static int
12988get_winnr(tabpage_T *tp, typval_T *argvar)
12989{
12990 win_T *twin;
12991 int nr = 1;
12992 win_T *wp;
12993 char_u *arg;
12994
12995 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12996 if (argvar->v_type != VAR_UNKNOWN)
12997 {
12998 arg = get_tv_string_chk(argvar);
12999 if (arg == NULL)
13000 nr = 0; /* type error; errmsg already given */
13001 else if (STRCMP(arg, "$") == 0)
13002 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13003 else if (STRCMP(arg, "#") == 0)
13004 {
13005 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13006 if (twin == NULL)
13007 nr = 0;
13008 }
13009 else
13010 {
13011 EMSG2(_(e_invexpr2), arg);
13012 nr = 0;
13013 }
13014 }
13015
13016 if (nr > 0)
13017 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13018 wp != twin; wp = wp->w_next)
13019 {
13020 if (wp == NULL)
13021 {
13022 /* didn't find it in this tabpage */
13023 nr = 0;
13024 break;
13025 }
13026 ++nr;
13027 }
13028 return nr;
13029}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013030
13031/*
13032 * "tabpagewinnr()" function
13033 */
13034 static void
13035f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13036{
13037 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013038 tabpage_T *tp;
13039
13040 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13041 if (tp == NULL)
13042 nr = 0;
13043 else
13044 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045 rettv->vval.v_number = nr;
13046}
13047
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013048/*
13049 * "tagfiles()" function
13050 */
13051 static void
13052f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13053{
13054 char_u *fname;
13055 tagname_T tn;
13056 int first;
13057
13058 if (rettv_list_alloc(rettv) == FAIL)
13059 return;
13060 fname = alloc(MAXPATHL);
13061 if (fname == NULL)
13062 return;
13063
13064 for (first = TRUE; ; first = FALSE)
13065 if (get_tagfname(&tn, first, fname) == FAIL
13066 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13067 break;
13068 tagname_free(&tn);
13069 vim_free(fname);
13070}
13071
13072/*
13073 * "taglist()" function
13074 */
13075 static void
13076f_taglist(typval_T *argvars, typval_T *rettv)
13077{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013078 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079 char_u *tag_pattern;
13080
13081 tag_pattern = get_tv_string(&argvars[0]);
13082
13083 rettv->vval.v_number = FALSE;
13084 if (*tag_pattern == NUL)
13085 return;
13086
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013087 if (argvars[1].v_type != VAR_UNKNOWN)
13088 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013090 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091}
13092
13093/*
13094 * "tempname()" function
13095 */
13096 static void
13097f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13098{
13099 static int x = 'A';
13100
13101 rettv->v_type = VAR_STRING;
13102 rettv->vval.v_string = vim_tempname(x, FALSE);
13103
13104 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13105 * names. Skip 'I' and 'O', they are used for shell redirection. */
13106 do
13107 {
13108 if (x == 'Z')
13109 x = '0';
13110 else if (x == '9')
13111 x = 'A';
13112 else
13113 {
13114#ifdef EBCDIC
13115 if (x == 'I')
13116 x = 'J';
13117 else if (x == 'R')
13118 x = 'S';
13119 else
13120#endif
13121 ++x;
13122 }
13123 } while (x == 'I' || x == 'O');
13124}
13125
13126#ifdef FEAT_FLOAT
13127/*
13128 * "tan()" function
13129 */
13130 static void
13131f_tan(typval_T *argvars, typval_T *rettv)
13132{
13133 float_T f = 0.0;
13134
13135 rettv->v_type = VAR_FLOAT;
13136 if (get_float_arg(argvars, &f) == OK)
13137 rettv->vval.v_float = tan(f);
13138 else
13139 rettv->vval.v_float = 0.0;
13140}
13141
13142/*
13143 * "tanh()" function
13144 */
13145 static void
13146f_tanh(typval_T *argvars, typval_T *rettv)
13147{
13148 float_T f = 0.0;
13149
13150 rettv->v_type = VAR_FLOAT;
13151 if (get_float_arg(argvars, &f) == OK)
13152 rettv->vval.v_float = tanh(f);
13153 else
13154 rettv->vval.v_float = 0.0;
13155}
13156#endif
13157
13158/*
13159 * "test_alloc_fail(id, countdown, repeat)" function
13160 */
13161 static void
13162f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13163{
13164 if (argvars[0].v_type != VAR_NUMBER
13165 || argvars[0].vval.v_number <= 0
13166 || argvars[1].v_type != VAR_NUMBER
13167 || argvars[1].vval.v_number < 0
13168 || argvars[2].v_type != VAR_NUMBER)
13169 EMSG(_(e_invarg));
13170 else
13171 {
13172 alloc_fail_id = argvars[0].vval.v_number;
13173 if (alloc_fail_id >= aid_last)
13174 EMSG(_(e_invarg));
13175 alloc_fail_countdown = argvars[1].vval.v_number;
13176 alloc_fail_repeat = argvars[2].vval.v_number;
13177 did_outofmem_msg = FALSE;
13178 }
13179}
13180
13181/*
13182 * "test_autochdir()"
13183 */
13184 static void
13185f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13186{
13187#if defined(FEAT_AUTOCHDIR)
13188 test_autochdir = TRUE;
13189#endif
13190}
13191
13192/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013193 * "test_feedinput()"
13194 */
13195 static void
13196f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13197{
13198#ifdef USE_INPUT_BUF
13199 char_u *val = get_tv_string_chk(&argvars[0]);
13200
13201 if (val != NULL)
13202 {
13203 trash_input_buf();
13204 add_to_input_buf_csi(val, (int)STRLEN(val));
13205 }
13206#endif
13207}
13208
13209/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013210 * "test_option_not_set({name})" function
13211 */
13212 static void
13213f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13214{
13215 char_u *name = (char_u *)"";
13216
13217 if (argvars[0].v_type != VAR_STRING)
13218 EMSG(_(e_invarg));
13219 else
13220 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013221 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013222 if (reset_option_was_set(name) == FAIL)
13223 EMSG2(_(e_invarg2), name);
13224 }
13225}
13226
13227/*
13228 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013229 */
13230 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013231f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013233 char_u *name = (char_u *)"";
13234 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013235 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013236
13237 if (argvars[0].v_type != VAR_STRING
13238 || (argvars[1].v_type) != VAR_NUMBER)
13239 EMSG(_(e_invarg));
13240 else
13241 {
13242 name = get_tv_string_chk(&argvars[0]);
13243 val = (int)get_tv_number(&argvars[1]);
13244
13245 if (STRCMP(name, (char_u *)"redraw") == 0)
13246 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013247 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13248 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013249 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13250 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013251 else if (STRCMP(name, (char_u *)"starting") == 0)
13252 {
13253 if (val)
13254 {
13255 if (save_starting < 0)
13256 save_starting = starting;
13257 starting = 0;
13258 }
13259 else
13260 {
13261 starting = save_starting;
13262 save_starting = -1;
13263 }
13264 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013265 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13266 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013267 else if (STRCMP(name, (char_u *)"ALL") == 0)
13268 {
13269 disable_char_avail_for_testing = FALSE;
13270 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013271 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013272 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013273 if (save_starting >= 0)
13274 {
13275 starting = save_starting;
13276 save_starting = -1;
13277 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013278 }
13279 else
13280 EMSG2(_(e_invarg2), name);
13281 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013282}
13283
13284/*
13285 * "test_garbagecollect_now()" function
13286 */
13287 static void
13288f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13289{
13290 /* This is dangerous, any Lists and Dicts used internally may be freed
13291 * while still in use. */
13292 garbage_collect(TRUE);
13293}
13294
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013295/*
13296 * "test_ignore_error()" function
13297 */
13298 static void
13299f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13300{
13301 ignore_error_for_testing(get_tv_string(&argvars[0]));
13302}
13303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304#ifdef FEAT_JOB_CHANNEL
13305 static void
13306f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13307{
13308 rettv->v_type = VAR_CHANNEL;
13309 rettv->vval.v_channel = NULL;
13310}
13311#endif
13312
13313 static void
13314f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13315{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013316 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317}
13318
13319#ifdef FEAT_JOB_CHANNEL
13320 static void
13321f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13322{
13323 rettv->v_type = VAR_JOB;
13324 rettv->vval.v_job = NULL;
13325}
13326#endif
13327
13328 static void
13329f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13330{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013331 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332}
13333
13334 static void
13335f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13336{
13337 rettv->v_type = VAR_PARTIAL;
13338 rettv->vval.v_partial = NULL;
13339}
13340
13341 static void
13342f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13343{
13344 rettv->v_type = VAR_STRING;
13345 rettv->vval.v_string = NULL;
13346}
13347
Bram Moolenaarab186732018-09-14 21:27:06 +020013348#ifdef FEAT_GUI
13349 static void
13350f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13351{
13352 char_u *which;
13353 long value;
13354 int dragging;
13355 scrollbar_T *sb = NULL;
13356
13357 if (argvars[0].v_type != VAR_STRING
13358 || (argvars[1].v_type) != VAR_NUMBER
13359 || (argvars[2].v_type) != VAR_NUMBER)
13360 {
13361 EMSG(_(e_invarg));
13362 return;
13363 }
13364 which = get_tv_string(&argvars[0]);
13365 value = get_tv_number(&argvars[1]);
13366 dragging = get_tv_number(&argvars[2]);
13367
13368 if (STRCMP(which, "left") == 0)
13369 sb = &curwin->w_scrollbars[SBAR_LEFT];
13370 else if (STRCMP(which, "right") == 0)
13371 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13372 else if (STRCMP(which, "hor") == 0)
13373 sb = &gui.bottom_sbar;
13374 if (sb == NULL)
13375 {
13376 EMSG2(_(e_invarg2), which);
13377 return;
13378 }
13379 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013380# ifndef USE_ON_FLY_SCROLL
13381 // need to loop through normal_cmd() to handle the scroll events
13382 exec_normal(FALSE, TRUE, FALSE);
13383# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013384}
13385#endif
13386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387 static void
13388f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13389{
13390 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13391}
13392
13393#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13394/*
13395 * Get a callback from "arg". It can be a Funcref or a function name.
13396 * When "arg" is zero return an empty string.
13397 * Return NULL for an invalid argument.
13398 */
13399 char_u *
13400get_callback(typval_T *arg, partial_T **pp)
13401{
13402 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13403 {
13404 *pp = arg->vval.v_partial;
13405 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013406 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013407 }
13408 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013409 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 {
13411 func_ref(arg->vval.v_string);
13412 return arg->vval.v_string;
13413 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13415 return (char_u *)"";
13416 EMSG(_("E921: Invalid callback argument"));
13417 return NULL;
13418}
13419
13420/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013421 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013422 */
13423 void
13424free_callback(char_u *callback, partial_T *partial)
13425{
13426 if (partial != NULL)
13427 partial_unref(partial);
13428 else if (callback != NULL)
13429 {
13430 func_unref(callback);
13431 vim_free(callback);
13432 }
13433}
13434#endif
13435
13436#ifdef FEAT_TIMERS
13437/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013438 * "timer_info([timer])" function
13439 */
13440 static void
13441f_timer_info(typval_T *argvars, typval_T *rettv)
13442{
13443 timer_T *timer = NULL;
13444
13445 if (rettv_list_alloc(rettv) != OK)
13446 return;
13447 if (argvars[0].v_type != VAR_UNKNOWN)
13448 {
13449 if (argvars[0].v_type != VAR_NUMBER)
13450 EMSG(_(e_number_exp));
13451 else
13452 {
13453 timer = find_timer((int)get_tv_number(&argvars[0]));
13454 if (timer != NULL)
13455 add_timer_info(rettv, timer);
13456 }
13457 }
13458 else
13459 add_timer_info_all(rettv);
13460}
13461
13462/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013463 * "timer_pause(timer, paused)" function
13464 */
13465 static void
13466f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13467{
13468 timer_T *timer = NULL;
13469 int paused = (int)get_tv_number(&argvars[1]);
13470
13471 if (argvars[0].v_type != VAR_NUMBER)
13472 EMSG(_(e_number_exp));
13473 else
13474 {
13475 timer = find_timer((int)get_tv_number(&argvars[0]));
13476 if (timer != NULL)
13477 timer->tr_paused = paused;
13478 }
13479}
13480
13481/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482 * "timer_start(time, callback [, options])" function
13483 */
13484 static void
13485f_timer_start(typval_T *argvars, typval_T *rettv)
13486{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013487 long msec = (long)get_tv_number(&argvars[0]);
13488 timer_T *timer;
13489 int repeat = 0;
13490 char_u *callback;
13491 dict_T *dict;
13492 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013493
Bram Moolenaar75537a92016-09-05 22:45:28 +020013494 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013495 if (check_secure())
13496 return;
13497 if (argvars[2].v_type != VAR_UNKNOWN)
13498 {
13499 if (argvars[2].v_type != VAR_DICT
13500 || (dict = argvars[2].vval.v_dict) == NULL)
13501 {
13502 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13503 return;
13504 }
13505 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13506 repeat = get_dict_number(dict, (char_u *)"repeat");
13507 }
13508
Bram Moolenaar75537a92016-09-05 22:45:28 +020013509 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013511 return;
13512
13513 timer = create_timer(msec, repeat);
13514 if (timer == NULL)
13515 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 else
13517 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013518 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013519 timer->tr_callback = vim_strsave(callback);
13520 else
13521 /* pointer into the partial */
13522 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013523 timer->tr_partial = partial;
13524 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013525 }
13526}
13527
13528/*
13529 * "timer_stop(timer)" function
13530 */
13531 static void
13532f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13533{
13534 timer_T *timer;
13535
13536 if (argvars[0].v_type != VAR_NUMBER)
13537 {
13538 EMSG(_(e_number_exp));
13539 return;
13540 }
13541 timer = find_timer((int)get_tv_number(&argvars[0]));
13542 if (timer != NULL)
13543 stop_timer(timer);
13544}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013545
13546/*
13547 * "timer_stopall()" function
13548 */
13549 static void
13550f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13551{
13552 stop_all_timers();
13553}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013554#endif
13555
13556/*
13557 * "tolower(string)" function
13558 */
13559 static void
13560f_tolower(typval_T *argvars, typval_T *rettv)
13561{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013563 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013564}
13565
13566/*
13567 * "toupper(string)" function
13568 */
13569 static void
13570f_toupper(typval_T *argvars, typval_T *rettv)
13571{
13572 rettv->v_type = VAR_STRING;
13573 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13574}
13575
13576/*
13577 * "tr(string, fromstr, tostr)" function
13578 */
13579 static void
13580f_tr(typval_T *argvars, typval_T *rettv)
13581{
13582 char_u *in_str;
13583 char_u *fromstr;
13584 char_u *tostr;
13585 char_u *p;
13586#ifdef FEAT_MBYTE
13587 int inlen;
13588 int fromlen;
13589 int tolen;
13590 int idx;
13591 char_u *cpstr;
13592 int cplen;
13593 int first = TRUE;
13594#endif
13595 char_u buf[NUMBUFLEN];
13596 char_u buf2[NUMBUFLEN];
13597 garray_T ga;
13598
13599 in_str = get_tv_string(&argvars[0]);
13600 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13601 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13602
13603 /* Default return value: empty string. */
13604 rettv->v_type = VAR_STRING;
13605 rettv->vval.v_string = NULL;
13606 if (fromstr == NULL || tostr == NULL)
13607 return; /* type error; errmsg already given */
13608 ga_init2(&ga, (int)sizeof(char), 80);
13609
13610#ifdef FEAT_MBYTE
13611 if (!has_mbyte)
13612#endif
13613 /* not multi-byte: fromstr and tostr must be the same length */
13614 if (STRLEN(fromstr) != STRLEN(tostr))
13615 {
13616#ifdef FEAT_MBYTE
13617error:
13618#endif
13619 EMSG2(_(e_invarg2), fromstr);
13620 ga_clear(&ga);
13621 return;
13622 }
13623
13624 /* fromstr and tostr have to contain the same number of chars */
13625 while (*in_str != NUL)
13626 {
13627#ifdef FEAT_MBYTE
13628 if (has_mbyte)
13629 {
13630 inlen = (*mb_ptr2len)(in_str);
13631 cpstr = in_str;
13632 cplen = inlen;
13633 idx = 0;
13634 for (p = fromstr; *p != NUL; p += fromlen)
13635 {
13636 fromlen = (*mb_ptr2len)(p);
13637 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13638 {
13639 for (p = tostr; *p != NUL; p += tolen)
13640 {
13641 tolen = (*mb_ptr2len)(p);
13642 if (idx-- == 0)
13643 {
13644 cplen = tolen;
13645 cpstr = p;
13646 break;
13647 }
13648 }
13649 if (*p == NUL) /* tostr is shorter than fromstr */
13650 goto error;
13651 break;
13652 }
13653 ++idx;
13654 }
13655
13656 if (first && cpstr == in_str)
13657 {
13658 /* Check that fromstr and tostr have the same number of
13659 * (multi-byte) characters. Done only once when a character
13660 * of in_str doesn't appear in fromstr. */
13661 first = FALSE;
13662 for (p = tostr; *p != NUL; p += tolen)
13663 {
13664 tolen = (*mb_ptr2len)(p);
13665 --idx;
13666 }
13667 if (idx != 0)
13668 goto error;
13669 }
13670
13671 (void)ga_grow(&ga, cplen);
13672 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13673 ga.ga_len += cplen;
13674
13675 in_str += inlen;
13676 }
13677 else
13678#endif
13679 {
13680 /* When not using multi-byte chars we can do it faster. */
13681 p = vim_strchr(fromstr, *in_str);
13682 if (p != NULL)
13683 ga_append(&ga, tostr[p - fromstr]);
13684 else
13685 ga_append(&ga, *in_str);
13686 ++in_str;
13687 }
13688 }
13689
13690 /* add a terminating NUL */
13691 (void)ga_grow(&ga, 1);
13692 ga_append(&ga, NUL);
13693
13694 rettv->vval.v_string = ga.ga_data;
13695}
13696
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013697/*
13698 * "trim({expr})" function
13699 */
13700 static void
13701f_trim(typval_T *argvars, typval_T *rettv)
13702{
13703 char_u buf1[NUMBUFLEN];
13704 char_u buf2[NUMBUFLEN];
13705 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13706 char_u *mask = NULL;
13707 char_u *tail;
13708 char_u *prev;
13709 char_u *p;
13710 int c1;
13711
13712 rettv->v_type = VAR_STRING;
13713 if (head == NULL)
13714 {
13715 rettv->vval.v_string = NULL;
13716 return;
13717 }
13718
13719 if (argvars[1].v_type == VAR_STRING)
13720 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13721
13722 while (*head != NUL)
13723 {
13724 c1 = PTR2CHAR(head);
13725 if (mask == NULL)
13726 {
13727 if (c1 > ' ' && c1 != 0xa0)
13728 break;
13729 }
13730 else
13731 {
13732 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13733 if (c1 == PTR2CHAR(p))
13734 break;
13735 if (*p == NUL)
13736 break;
13737 }
13738 MB_PTR_ADV(head);
13739 }
13740
13741 for (tail = head + STRLEN(head); tail > head; tail = prev)
13742 {
13743 prev = tail;
13744 MB_PTR_BACK(head, prev);
13745 c1 = PTR2CHAR(prev);
13746 if (mask == NULL)
13747 {
13748 if (c1 > ' ' && c1 != 0xa0)
13749 break;
13750 }
13751 else
13752 {
13753 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13754 if (c1 == PTR2CHAR(p))
13755 break;
13756 if (*p == NUL)
13757 break;
13758 }
13759 }
13760 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13761}
13762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013763#ifdef FEAT_FLOAT
13764/*
13765 * "trunc({float})" function
13766 */
13767 static void
13768f_trunc(typval_T *argvars, typval_T *rettv)
13769{
13770 float_T f = 0.0;
13771
13772 rettv->v_type = VAR_FLOAT;
13773 if (get_float_arg(argvars, &f) == OK)
13774 /* trunc() is not in C90, use floor() or ceil() instead. */
13775 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13776 else
13777 rettv->vval.v_float = 0.0;
13778}
13779#endif
13780
13781/*
13782 * "type(expr)" function
13783 */
13784 static void
13785f_type(typval_T *argvars, typval_T *rettv)
13786{
13787 int n = -1;
13788
13789 switch (argvars[0].v_type)
13790 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013791 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13792 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013793 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013794 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13795 case VAR_LIST: n = VAR_TYPE_LIST; break;
13796 case VAR_DICT: n = VAR_TYPE_DICT; break;
13797 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013798 case VAR_SPECIAL:
13799 if (argvars[0].vval.v_number == VVAL_FALSE
13800 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013801 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013802 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013803 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013804 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013805 case VAR_JOB: n = VAR_TYPE_JOB; break;
13806 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013807 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013808 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809 n = -1;
13810 break;
13811 }
13812 rettv->vval.v_number = n;
13813}
13814
13815/*
13816 * "undofile(name)" function
13817 */
13818 static void
13819f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13820{
13821 rettv->v_type = VAR_STRING;
13822#ifdef FEAT_PERSISTENT_UNDO
13823 {
13824 char_u *fname = get_tv_string(&argvars[0]);
13825
13826 if (*fname == NUL)
13827 {
13828 /* If there is no file name there will be no undo file. */
13829 rettv->vval.v_string = NULL;
13830 }
13831 else
13832 {
13833 char_u *ffname = FullName_save(fname, FALSE);
13834
13835 if (ffname != NULL)
13836 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13837 vim_free(ffname);
13838 }
13839 }
13840#else
13841 rettv->vval.v_string = NULL;
13842#endif
13843}
13844
13845/*
13846 * "undotree()" function
13847 */
13848 static void
13849f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13850{
13851 if (rettv_dict_alloc(rettv) == OK)
13852 {
13853 dict_T *dict = rettv->vval.v_dict;
13854 list_T *list;
13855
Bram Moolenaare0be1672018-07-08 16:50:37 +020013856 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13857 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13858 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13859 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13860 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13861 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862
13863 list = list_alloc();
13864 if (list != NULL)
13865 {
13866 u_eval_tree(curbuf->b_u_oldhead, list);
13867 dict_add_list(dict, "entries", list);
13868 }
13869 }
13870}
13871
13872/*
13873 * "values(dict)" function
13874 */
13875 static void
13876f_values(typval_T *argvars, typval_T *rettv)
13877{
13878 dict_list(argvars, rettv, 1);
13879}
13880
13881/*
13882 * "virtcol(string)" function
13883 */
13884 static void
13885f_virtcol(typval_T *argvars, typval_T *rettv)
13886{
13887 colnr_T vcol = 0;
13888 pos_T *fp;
13889 int fnum = curbuf->b_fnum;
13890
13891 fp = var2fpos(&argvars[0], FALSE, &fnum);
13892 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13893 && fnum == curbuf->b_fnum)
13894 {
13895 getvvcol(curwin, fp, NULL, NULL, &vcol);
13896 ++vcol;
13897 }
13898
13899 rettv->vval.v_number = vcol;
13900}
13901
13902/*
13903 * "visualmode()" function
13904 */
13905 static void
13906f_visualmode(typval_T *argvars, typval_T *rettv)
13907{
13908 char_u str[2];
13909
13910 rettv->v_type = VAR_STRING;
13911 str[0] = curbuf->b_visual_mode_eval;
13912 str[1] = NUL;
13913 rettv->vval.v_string = vim_strsave(str);
13914
13915 /* A non-zero number or non-empty string argument: reset mode. */
13916 if (non_zero_arg(&argvars[0]))
13917 curbuf->b_visual_mode_eval = NUL;
13918}
13919
13920/*
13921 * "wildmenumode()" function
13922 */
13923 static void
13924f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13925{
13926#ifdef FEAT_WILDMENU
13927 if (wild_menu_showing)
13928 rettv->vval.v_number = 1;
13929#endif
13930}
13931
13932/*
13933 * "winbufnr(nr)" function
13934 */
13935 static void
13936f_winbufnr(typval_T *argvars, typval_T *rettv)
13937{
13938 win_T *wp;
13939
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013940 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941 if (wp == NULL)
13942 rettv->vval.v_number = -1;
13943 else
13944 rettv->vval.v_number = wp->w_buffer->b_fnum;
13945}
13946
13947/*
13948 * "wincol()" function
13949 */
13950 static void
13951f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13952{
13953 validate_cursor();
13954 rettv->vval.v_number = curwin->w_wcol + 1;
13955}
13956
13957/*
13958 * "winheight(nr)" function
13959 */
13960 static void
13961f_winheight(typval_T *argvars, typval_T *rettv)
13962{
13963 win_T *wp;
13964
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013965 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966 if (wp == NULL)
13967 rettv->vval.v_number = -1;
13968 else
13969 rettv->vval.v_number = wp->w_height;
13970}
13971
13972/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013973 * "winlayout()" function
13974 */
13975 static void
13976f_winlayout(typval_T *argvars, typval_T *rettv)
13977{
13978 tabpage_T *tp;
13979
13980 if (rettv_list_alloc(rettv) != OK)
13981 return;
13982
13983 if (argvars[0].v_type == VAR_UNKNOWN)
13984 tp = curtab;
13985 else
13986 {
13987 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13988 if (tp == NULL)
13989 return;
13990 }
13991
13992 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13993}
13994
13995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 * "winline()" function
13997 */
13998 static void
13999f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14000{
14001 validate_cursor();
14002 rettv->vval.v_number = curwin->w_wrow + 1;
14003}
14004
14005/*
14006 * "winnr()" function
14007 */
14008 static void
14009f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14010{
14011 int nr = 1;
14012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014013 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014014 rettv->vval.v_number = nr;
14015}
14016
14017/*
14018 * "winrestcmd()" function
14019 */
14020 static void
14021f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14022{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014023 win_T *wp;
14024 int winnr = 1;
14025 garray_T ga;
14026 char_u buf[50];
14027
14028 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014029 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014030 {
14031 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14032 ga_concat(&ga, buf);
14033 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14034 ga_concat(&ga, buf);
14035 ++winnr;
14036 }
14037 ga_append(&ga, NUL);
14038
14039 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014040 rettv->v_type = VAR_STRING;
14041}
14042
14043/*
14044 * "winrestview()" function
14045 */
14046 static void
14047f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14048{
14049 dict_T *dict;
14050
14051 if (argvars[0].v_type != VAR_DICT
14052 || (dict = argvars[0].vval.v_dict) == NULL)
14053 EMSG(_(e_invarg));
14054 else
14055 {
14056 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
14057 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
14058 if (dict_find(dict, (char_u *)"col", -1) != NULL)
14059 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
14060#ifdef FEAT_VIRTUALEDIT
14061 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
14062 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
14063#endif
14064 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14065 {
14066 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
14067 curwin->w_set_curswant = FALSE;
14068 }
14069
14070 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
14071 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
14072#ifdef FEAT_DIFF
14073 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
14074 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
14075#endif
14076 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
14077 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
14078 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
14079 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
14080
14081 check_cursor();
14082 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014083 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 changed_window_setting();
14085
14086 if (curwin->w_topline <= 0)
14087 curwin->w_topline = 1;
14088 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14089 curwin->w_topline = curbuf->b_ml.ml_line_count;
14090#ifdef FEAT_DIFF
14091 check_topfill(curwin, TRUE);
14092#endif
14093 }
14094}
14095
14096/*
14097 * "winsaveview()" function
14098 */
14099 static void
14100f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14101{
14102 dict_T *dict;
14103
14104 if (rettv_dict_alloc(rettv) == FAIL)
14105 return;
14106 dict = rettv->vval.v_dict;
14107
Bram Moolenaare0be1672018-07-08 16:50:37 +020014108 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14109 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014110#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014111 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014112#endif
14113 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014114 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115
Bram Moolenaare0be1672018-07-08 16:50:37 +020014116 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014118 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014120 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14121 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014122}
14123
14124/*
14125 * "winwidth(nr)" function
14126 */
14127 static void
14128f_winwidth(typval_T *argvars, typval_T *rettv)
14129{
14130 win_T *wp;
14131
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014132 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133 if (wp == NULL)
14134 rettv->vval.v_number = -1;
14135 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137}
14138
14139/*
14140 * "wordcount()" function
14141 */
14142 static void
14143f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14144{
14145 if (rettv_dict_alloc(rettv) == FAIL)
14146 return;
14147 cursor_pos_info(rettv->vval.v_dict);
14148}
14149
14150/*
14151 * "writefile()" function
14152 */
14153 static void
14154f_writefile(typval_T *argvars, typval_T *rettv)
14155{
14156 int binary = FALSE;
14157 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014158#ifdef HAVE_FSYNC
14159 int do_fsync = p_fs;
14160#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 char_u *fname;
14162 FILE *fd;
14163 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014164 listitem_T *li;
14165 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014167 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168 if (check_restricted() || check_secure())
14169 return;
14170
14171 if (argvars[0].v_type != VAR_LIST)
14172 {
14173 EMSG2(_(e_listarg), "writefile()");
14174 return;
14175 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014176 list = argvars[0].vval.v_list;
14177 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014178 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014179 for (li = list->lv_first; li != NULL; li = li->li_next)
14180 if (get_tv_string_chk(&li->li_tv) == NULL)
14181 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182
14183 if (argvars[2].v_type != VAR_UNKNOWN)
14184 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014185 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14186
14187 if (arg2 == NULL)
14188 return;
14189 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014190 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014191 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014193#ifdef HAVE_FSYNC
14194 if (vim_strchr(arg2, 's') != NULL)
14195 do_fsync = TRUE;
14196 else if (vim_strchr(arg2, 'S') != NULL)
14197 do_fsync = FALSE;
14198#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014199 }
14200
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014201 fname = get_tv_string_chk(&argvars[1]);
14202 if (fname == NULL)
14203 return;
14204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014205 /* Always open the file in binary mode, library functions have a mind of
14206 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014207 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14208 append ? APPENDBIN : WRITEBIN)) == NULL)
14209 {
14210 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14211 ret = -1;
14212 }
14213 else
14214 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014215 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014217#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014218 else if (do_fsync)
14219 /* Ignore the error, the user wouldn't know what to do about it.
14220 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014221 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014222#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223 fclose(fd);
14224 }
14225
14226 rettv->vval.v_number = ret;
14227}
14228
14229/*
14230 * "xor(expr, expr)" function
14231 */
14232 static void
14233f_xor(typval_T *argvars, typval_T *rettv)
14234{
14235 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14236 ^ get_tv_number_chk(&argvars[1], NULL);
14237}
14238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014239#endif /* FEAT_EVAL */